fn.js icon indicating copy to clipboard operation
fn.js copied to clipboard

Function modifiers to take function as last argument

Open CrowdHailer opened this issue 9 years ago • 8 comments

By default function should be passed as last argument. Work out how to setup changelog

CrowdHailer avatar Jan 09 '16 19:01 CrowdHailer

Switch argument order of throttle arguments

CrowdHailer avatar Jan 10 '16 11:01 CrowdHailer

There are further functions such as apply that also break this rule

CrowdHailer avatar Jan 10 '16 11:01 CrowdHailer

I'm confused as to why you would want to switch all methods to have function be last argument. Making methods take their data as the last argument is what makes methods composable, and is the very reason why underscore/lodash is unsuitable for functional programming. Maybe I am mistaken on the intention of this issue?

eliperelman avatar Jan 10 '16 12:01 eliperelman

I think it depends how you are composing, I think. If all functions are curried then having a function as last args means you can do the following.

var throttleToMyLimit = throttle(100);

var logIt = throttleToMyLimit(console.log);

CrowdHailer avatar Jan 10 '16 12:01 CrowdHailer

Although I think it might make sense to have a function with the arguments reversed so throttle and throttleTo could exist like delay and delayFor

CrowdHailer avatar Jan 10 '16 12:01 CrowdHailer

This is very different from the direction we took with Ramda, and I'm trying to see the advantage.

To me the order is straightforward: from the parameter least likely to change to the one most likely to change. A simple example is map. This seems a much more likely requirement:

var squareAll = map(square) ;
squareAll([1, 2, 3]): //=> [1, 4, 9]
squareAll([4, 5, 6]); //=> [16, 25, 36]

than does this:

var map123 = map ([1, 2, 3]);
map123(square) ; //=> [1, 4, 9]
map123(add(1)); //=> [2, 3, 4]

So the transformation function should be the first parameter and the list of values second. And most every function works this way.

CrossEye avatar Jan 10 '16 15:01 CrossEye

@CrossEye I agree with what you are saying there.

I think the same logic applies(although the terms might get more confusing) with functions on functions.

If you have a library of higer-order functions who's purpose is to modify other raw functions then the raw function to be modified should be supplied as the last parameter.

So in the example of throttle it make sense to me to be throttle(delay, rawFunction) And use of the curried form acts as a factory to produce preconfigued throttling functions

var throttle100 = throttle(100);
throttle100(rawFunction);

CrowdHailer avatar Jan 10 '16 16:01 CrowdHailer

I think I was reacting more to the title than to your example. throttle seems either ambiguous to me or perhaps slightly biased toward function-last. That's reasonable. From the title, I thought you meant to change to function-last everywhere.

Carry on. Don't mind me. :smile:

CrossEye avatar Jan 10 '16 20:01 CrossEye