Tag: functional programming

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 double-click, but I’m a bit stumped as to how I would specify different behavior (i.e., a different callback function) elegantly for […]

PHP vs JavaScript: Altering the value of an array passed in gives different results

It was very difficult to come up with an informed title for this post. Though PHP and JS are totally different languages, I am very surprised to find that altering an array passed into a function as an argument gives different results. PHP <?php function thing($arr) { $arr[2] = “WOOF”; } $hello = array(1,2,3,4); thing($hello); […]

How to understand composition of functions with multiple arguments?

Such as: var foo = function (a, b) { return a + b; }; var bar = function (a, b) { return a * b; }; var fn = _.compose([foo, bar]); How to understand the fn?

Reducing a 'raw' javascript array into a more compact array (of objects)

In javascript, I’m looking for a tidy way of processing an array such that the short, repeating sequences (tuples) in a collection become reduced into a more compact collection. I want to convert an array of length 3n to a collection of length n. Here’s an imaginary example: // A flat (1-dimensional) array of numbers […]

Is there something similar to lodash _.toArray for ramda.js?

I want to stop using lodash.js and switch to ramda.js but I don’t see any function like _.toArray() for objects, is there something like this in ramda that I should compose or should I continue using lodash for these functions (and possibly more cases that I have not run into yet.) For example In lodash […]

How does this function modify the other? (Higher Order Functions)

What is this code doing? How is it executing? What is x equal to? Another useful type of higher-order function modifies the function value it is given: function negate(func) { return function(x) { return !func(x); }; } var isNotNaN = negate(isNaN); show(isNotNaN(NaN));

How to combine functions in another FP?

I am learning function programming in JavaScript, currently I have the following code. I would like to combine this two functions in another function which alllow an user to pass a value in and having the result equal to the value passed plus 10 multiply 3 using the following functions. Pseudo code example: const myFormulat= […]

Performance of OOJS over functional JavaScript programming

This question relates to a previous question here: Reducing number of calls to the methods of a JavaScript object When profiling these two code snippets with Firebug: function ie6PNGFixLoader(scriptURL) { if(arguments.length > 0) { for (var i = 0; i < arguments.length; i++) { $.ajax({// load PNG fix scripts url: arguments[i], cache: true, dataType: ‘script’ […]

dynamically generated methods

found this example in Pro javascript Technique book function User( name, age ) { var year = (new Date()).getFullYear()-age; this.getYearBorn = function(){ return year; }; } var user = new User( “Bob”, 44 ); alert(user.getYearBorn()); //system date is 2010 ,alerts 1967 I changed system date to 2012 alert(user.getYearBorn()); //alerts 1968. based on above logic i […]

Object methods assigned to variables or function arguments fail when invoked

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 = (/[abc]/).test; v1(‘a’); says test method called on incompatible undefined, I’m trying to store the test method of that regex into variable […]