CrowdHailer / fn.js

A JavaScript library built to encourage a functional programming style & strategy. - http://eliperelman.com/fn.js
MIT License
399 stars 30 forks source link

Function modifiers to take function as last argument #25

Open CrowdHailer opened 8 years ago

CrowdHailer commented 8 years ago

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

CrowdHailer commented 8 years ago

Switch argument order of throttle arguments

CrowdHailer commented 8 years ago

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

eliperelman commented 8 years ago

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?

CrowdHailer commented 8 years ago

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 commented 8 years ago

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

CrossEye commented 8 years ago

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.

CrowdHailer commented 8 years ago

@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);
CrossEye commented 8 years ago

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: