These are chat archives for ramda/ramda

1st
Feb 2015
mac10688
@mac10688
Feb 01 2015 23:25
Can someone explains how arity works?
The documentation seems confusing to me
David Chambers
@davidchambers
Feb 01 2015 23:32
It takes a function of arbitrary arity and returns a function of the desired arity.
Say, for example, you have a function with one required argument and one optional argument.
In order to safely use it with Array.prototype.map, you'd need to wrap the function like so:
[1, 2, 3, 4, 5].map(function(n) { return f(n); })
With R.arity you could write this instead:
[1, 2, 3, 4, 5].map(R.arity(1, f))
One could even use the shorthand, R.unary:
[1, 2, 3, 4, 5].map(R.unary(f))
Athan Clark
@athanclark
Feb 01 2015 23:36
@davidchambers You may find this interesting: http://www.deconfigured.com/blog/atlc (shameless self-promo)
David Chambers
@davidchambers
Feb 01 2015 23:36
Actually, scrap that, I was thinking of R.nAry!
R.arity is useful in circumstances where a function's length property is misleading, as is often the case with partially applied functions. Ramda looks at a function's length property to know whether sufficient arguments have been provided.
mac10688
@mac10688
Feb 01 2015 23:39
thanks david, i'm reading over what you just typed
ok so my question is the last three lines in the documentation
takesOneArg.length; //=> 1
// All arguments are passed through to the wrapped function
takesOneArg(1, 2); //=> [1, 2]
it seems the last line should be
takesOneArg(2); //=> [1,2]
I'm not a functional guru and i'm hoping to use this library to learn functional programming concepts, so thanks for taking the time to answer my question
Athan Clark
@athanclark
Feb 01 2015 23:42
(Also, that's why I'm here - I come from Haskell, happy to add other ideas)
mac10688
@mac10688
Feb 01 2015 23:42
athan thanks for joining
Athan Clark
@athanclark
Feb 01 2015 23:42
no worries, Pure FP isn't obvious :)
David Chambers
@davidchambers
Feb 01 2015 23:44
@mac10688, R.arity and R.nAry are quite similar. The way to think of R.arity is as a function which takes a function and a desired length and returns an equivalent function with the desired length.
Take this silly example:
function add() { return arguments[0] + arguments[1]; }
This would confuse R.curry as it appears to be a nullary function (one that takes no arguments). We could wrap add in a function in order to report the arity correctly:
function add2(a, b) {
  return add(a, b);
}
This is exactly what R.arity does, in fact, so the following is equivalent:
var add2 = R.arity(2, add);
In my experience it's rarely necessary to set a function's length in this way.
mac10688
@mac10688
Feb 01 2015 23:49
oh!
David Chambers
@davidchambers
Feb 01 2015 23:49
@athanclark, that's a interesting post, though most of it is over my head at this stage.
mac10688
@mac10688
Feb 01 2015 23:49
I'm not sure if I ever knew or forgot that you could pass parameters to a function without declaring them
That example you gave me makes sense david, the documentation is still confusing.
'''
var takesOneArg = R.arity(1, takesTwoArgs);
takesOneArg.length; //=> 1
// All arguments are passed through to the wrapped function
takesOneArg(1, 2); //=> [1, 2]
crap
i don't know how to get that nice looking code format you do
'''
var takesOneArg = R.arity(1, takesTwoArgs);
takesOneArg.length; //=> 1
// All arguments are passed through to the wrapped function
takesOneArg(1, 2); //=> [1, 2]
'''
ok i'm done, i'm just spamming
I'll just let this sink in for a few days
Athan Clark
@athanclark
Feb 01 2015 23:54
@davidchambers Thanks :) It was actually inspired by javascript, I feel like this poly-arity type resolution is similar to this project (but I'm also a noob)
@mac10688 It will take a while, in functional programming, you "slip" around parameter passing by using arity
it's really nice, the use of "currying" and . (composition) are huge in pure FP:
David Chambers
@davidchambers
Feb 01 2015 23:56
@mac10688, if you use backticks rather than quote marks the code blocks will be formatted nicely. :)
Athan Clark
@athanclark
Feb 01 2015 23:56
(f . g) x = f (g x)
f x y = g x y can also be rewritten as f = g
@mac10688 ^