Tagged Questions
1
vote
0answers
42 views
Node.js Empty function, [on hold]
Suppose I have a function defined in the following way.
function MyFunction(param, CallBack) {
// Doing stuff
CallBack();
}
I want to call the same function with three different parameters but ...
1
vote
2answers
34 views
In JavaScript is it possible to have a series of key value functions that contain arguments?
I have an function in javascript that contains a set of variables:
var mainFunction = function(){
this.var1 = null;
this.var2 = null;
}
is there any way to set up the main function to ...
1
vote
1answer
47 views
Coffeescript closures and arrays of random numbers
Running the following coffeescript consistently results in an array of all false.
My question is "Why?"
class A
probability: 0.5
gen: (f) -> f() for x in [0...10]
init: -> alert ...
0
votes
2answers
33 views
Using document.getElementById as a first-class function
As a functional programming language, JavaScript allows statements like
var obj = {
key: function () {return true;}
};
var fn = obj.key;
Simple enough, but why does this not work?
$ = ...
-1
votes
1answer
49 views
How to transform this recursive function into an iterative one?
F = function(node){
return typeof(node)!="object" ?
node
: transformable([F(node[0]),F(node[1])]) ?
F(transform(F(node[0]),F(node[1])))
: ...
0
votes
2answers
35 views
Combining Classes and Event Listeners: `this` keyword
I'm having a hard time answering my question, and I think it's simply because "class" and "this" and other such terms are too generic for effective Googling.
Consider the following code:
function ...
0
votes
4answers
132 views
How do convert this code and my thinking to a functional mindset (Clojure)?
How do I convert this JavaScript code to Clojure?
I am trying to draw a (x,y) world where the cells are on or off according to the fill property. In the example below I am trying to print the rows ...
3
votes
5answers
138 views
What is a pure functional approach to indexing functions?
For example, a function that receives a list of trades and returns a list of value sums, indexed by time:
trades = [{time:1,value:8}, {time:1.1,value:8},... {time:1.2,value:7}, time:2.1,value:8} ...]
...
0
votes
3answers
65 views
Write pure and functional code in Python using the concept higher-order functions like in JavaScript
How would one use higher-order functions (functions returning other functions) in Python?
This is my JavaScript example, whose programming concept I would like to use in Python as well.
Let's say, ...
-1
votes
2answers
30 views
Mapping over Array of JSON
How can I re-factor the below code to not change i or initObj?
// input: [{name: "Kevin"}, {name: "Bob"}]
// output: {"Kevin" : 0, "Bob" : 1}
var map = function(arr, property) {
var i = 0;
...
-1
votes
4answers
129 views
JavaScript “map” Function
Looking at the map function in JavaScript, what am I doing wrong here?
// input: [{name: "Kevin"}, {name: "Bob"}];
// output: [{"Kevin" : 0}, {"Bob" : 1}];
var map = function(arr, property) {
...
0
votes
2answers
60 views
Issue in calling javascript method
I need to call a function like function share() in javascript but the code is in jquery, how to call under this part of code in order to achieve pop up message...
javascript button function call ...
0
votes
1answer
17 views
How can I elegantly make an object from names to indices from a javascript array?
I want to transform
myArr = ["Abel","Bella","Corey"]
into
myIndex = {"Abel": 1, "Bella":2, "Corey":3}
(I know I can do .indexOf but I want to make an efficient lookup dict)
Naively I'd do it ...
1
vote
2answers
208 views
Javascript vs Python with respect to Python 'map()' function
In Python there is a function called map that allows you to go: map(someFunction, [x,y,z]) and go on down that list applying the function. Is there a javascript equivalent to this function?
I am ...
1
vote
2answers
38 views
Error in Functional Javascript book example
I'm reading through Michael Fogus' book Functional JavaScript, and one of the examples in the book isn't working. Here's the code:
function existy(x) {
return x != null;
};
function truthy(x) {
...
5
votes
2answers
72 views
Is there a set of restrictions I can use to simulate functional programming in JavaScript?
I was watching a talk on clean code by Misko Hevery, and he mentioned trying to write a program with no if statements in them (well, as few as humanly possible) in order to simulate working on... ...
1
vote
1answer
62 views
JS best practice for member functions
I'm writing a little mobile games library, and I'm not sure the best practice for declaring member functions of instantiated function objects.
For instance, I might create a simple object with one ...
1
vote
3answers
102 views
Why explicitly define variable as undefined?
EDIT: In his own words: https://twitter.com/fogus/status/393058344413302784
I am reading Michael Fogus's "Functional JavaScript" and came across this listing at the beginning of chapter 5:
function ...
0
votes
2answers
337 views
underscore.js: get first element of a collection that matches iterator function
I would like to achieve something like _.first with _.filter, that is, having a collection of elements, I'd like to get the first one (if exists) that matches a truth test (iterator).
For example, ...
0
votes
2answers
46 views
'Functionnaly' produce a list with only a few elements
This is purely an exercice of though, but given this code :
var someCondition = (....);
var res = [];
if (someCondition) {
res.push("A");
}
res.push("B");
if (someCondition) {
res.push("C")
}
...
1
vote
1answer
42 views
Pass variable to closure on event trigger
I have a loop where I define a closure for an event:
for (var i = 0; i < 3; i++) {
obj.onload = function(e) {
me.myFunction(e, i);
};
}
I need to pass that counter variable to ...
0
votes
1answer
102 views
How should I operate on “private” members of another object with the same prototype?
In a class-based language like Java, I sometimes take advantage of the fact that a private member of a class is accessible by other objects of the same class. For example,
class BitSet {
private ...
3
votes
2answers
51 views
Is there a name for a javascript function that returns a function?
Javascript allows us to write functions that use their parameters to build and return another (often anonymous) function with specific behaviour. Currying is an example of this.
To illustrate, this ...
0
votes
1answer
101 views
Control flow in bacon.js, how to do something at a given time
So I'm fairly new to the functional programming paradigm and especially new to Bacon.js and FRP.
I need some advice on how to conceptualize control flow in FRP. I have a timer in an event stream that ...
1
vote
1answer
74 views
Is it possible to throttle the bacon action `repeatedly` somehow?
I am using bacon.js and have some situation where signal is emitted from two sources:
sourceA :: EventStream a
sourceB :: EventStream Bool
When sourceA is fired, it should trigger some action that ...
6
votes
4answers
173 views
How do you curry any javascript function of arbitrary arity?
Let's say I have some function:
function g(a,b,c){ return a + b + c }
And I'd like to turn it into its "curried" form (in quotations since it's not exactly curried per se):
function h(a,b,c){
...
1
vote
3answers
117 views
binding a specific context to an event handler: Understanding functional context, return statements and closures?
I'm reading John Resig's "Secrets of the Javascript Ninja" and in it he talks about changing the context of an event handler. In it he has the following code. I have no idea WHY it works. Very lost on ...
2
votes
2answers
254 views
D3 onClick handler seems to have wrong scope when executed
I have the following javascript code for D3, which behaves strange in my opinion. As I'm new to D3 I might be missing something, but I have no idea at all. The problem is marked by the alert calls:
I ...
1
vote
1answer
128 views
combineWith vs onValues in Bacon.js
I have 2 Bacon.jQuery Models (but the problem can be illustrated with any Bacon Observable).
I have 3 combo boxes: foo, bar and quux. bar depends on foo and quux depends on a combination of bar and ...
3
votes
1answer
67 views
Writing Truly Private Functions in Javascript
I want to write a function that gives me control over what API's it can execute.
For example, how would I write an anonymous function that privately executes without jQuery after jQuery has been ...
2
votes
1answer
97 views
is this a spot for functional lenses in javascript?
Playing around with point-free style javascript for fun.
Say I am coding the video game Diablo, and I am modeling enemies using complex nested types like this but deeper and more complicated:
{ ...
4
votes
2answers
72 views
Different functions - are they the same?
I'm a JavaScript slightly-more-than-beginner.
While reading the source for EventEmitter, I stumbled upon this interesting and, to me, elegant function:
// alias a method while keeping the correct ...
2
votes
1answer
58 views
Newb: Why does this variable persist instead of getting reset?
I suppose this is a newbie question, but I can't seem to figure it out. I have this code, from eloquent javascript, about the reduce function:
function forEach ( info, func ) {
for ( i = 0; i ...
0
votes
1answer
40 views
Why is this jQuery code written in functional style doesn't work?
I have this minimal example, and it works fine:
var node = $('div');
var fun1 = function(filter) { return node.find(filter) };
console.log(fun1('span'));
DOM:
...
1
vote
3answers
71 views
Trying to find a nice looking way to transform this array using functional programming style
Given an array of objects like this:
[
{ x: 'x1', y: 'y1' },
{ y: 'y2', z: 'z1' },
{ z: 'z2', x: 'x2' }
]
I want to produce an object like this:
{
x: [ 'x1', 'x2' ],
y: [ ...
4
votes
0answers
82 views
Are functions the only JavaScript objects with a “prototype” property? [duplicate]
[First this question may be taking about same concept as How does JavaScript .prototype work?, but is has different context.]
I came across this blog which states:
In JavaScript, each object has ...
0
votes
2answers
852 views
Functional Programming in JavaScript [closed]
Im not sure it this is allowed here, if it is not please tell me where I can ask this.
Although I have herd JavaScript is easy language to learn, i find it very difficult to learn. I am reading about ...
2
votes
3answers
46 views
Functional JavaScript Queston
Okay I understand that in the forEach function the action parameter is acting as the print function and being called on each element in the array for the following Code Below:
function ...
2
votes
1answer
49 views
merge object and objects inside array?
The original data are like this
var d = {A1:{name:"A1",value:10}, A2:{name:"A2",value:8}}
var l = [{name: "A1", 1min: 1, 2min:10}, {name: "A2", 1min:5, 2min:40}}
What I need to do is merge d and l ...
2
votes
1answer
104 views
Handling double clicks AND single clicks gracefully with FRP and Bacon.js
I'm trying to find the most graceful way to distinguish between single and double clicks in Bacon.js, but I feel like I'm not completely grasping how this works. The following works for detecting a ...
4
votes
1answer
193 views
Is this an implementation of a fixpoint combinator?
I presumed this couldn't be called "fixed point recursion" because it was too straightforward. However, I recently realized it actually might be.
Have I effectively implemented fixed point ...
3
votes
1answer
182 views
Where exactly does the performance advantage of LazyEvalutaion emerge from?
In the past few days, I have researched LazyEvaluation, in performance aspect mostly, wondering from where the performance advantage of LazyEvalutaion emerges.
It's been so unclear to me reading ...
4
votes
3answers
124 views
In FP how do you set a reciprocal relationship?
In FP where there is no mutable state and every operation returns a new state of the world. Given: I have a contact list and an individual contact.
I add Dirk to my address book. Dirk is a child of ...
0
votes
2answers
71 views
Fulfilling all function arguments in order before invoking
I'm trying to understand how the following zip function (esp. the invoke function) can be made more functional. The issue I've got is that the invoke method has to wait for both the left and right ...
4
votes
1answer
173 views
What's a Good Name for this extended `compose` function?
I have a function in search of a name.
I've been building a new functional programming library in Javascript, and I recently added a new function that looks useful to me. I named it useWith but I'm ...
3
votes
2answers
158 views
Writing an inverse function in javascript?
I ran into a situation at work today where I needed to write the inverse of a function that I had already written, but I found it inefficient to write the inverse manually because it seems like I ...
2
votes
1answer
65 views
Partial application of JavaScript methods: how to retain th meaning of “this”
In JavaScript, it’s pretty easy to define a partial application method for functions:
Function.prototype.partial = function partial() {
var fn = this
var args = ...
1
vote
5answers
64 views
pass built-in methods into variables/arguments
I'm learning javascript right now, seems like beautiful functional language to me, it is wonderful move from PHP, I should have done this earlier. Although, I cannot figure this one out:
var v1 = ...
0
votes
1answer
324 views
Functional Thinking in javascript/nodejs
I am a newbie reading about functional thinking in javascript these days. My background is mostly OOP in Java/Ruby.
Suppose I want to get user input in node, I could do it as :
...
2
votes
5answers
168 views
What is a functional-style replacement for this loop?
nums = [2 5 3 7]
result = []
result.push {x:nums[0]}
for n in nums.slice(1)
result.push {n:n + result[-1].x}
log result
# [{x:2} {x:7} {x:10} {x:17}]
This is hard to express functionally using ...