Type signatures in Ramda and Arrow functions

node v0.12.18
version: 3.0.0
endpointsharetweet
In the Ramda docs and gitter chat you'll often see weird glyphs that are rather unintelligible. For example: (acc → x → (acc, y)) → acc → [x] → (acc, [y]) I'd like to quickly explain how to interpret this alien language. I should note that I am not expert and this is just my way of reading it. To get an authoritave definition: https://github.com/ramda/ramda/wiki/Type-Signatures First let's implement reduce:
var reduce = fn => acc => xs => xs.reduce(fn, acc)
reduce( (acc, y) => acc + y)(0)([1,2,3])
Look familiar? Because in Ramda every function is curried, it makes sense to think of every argument returning a new function. The simplest way to express that in es6 is to use arrow functions. So when you see a → substitute it for a `=>` Anything wrapped in `()` is a function that will be received as an argument. And the names, and types with in those parens define the type that visitor function should adhere to. So ` (acc, y)` is saying the visitor function should accept two arguments: `acc` and `y`. In our usage code we follow this convention:
(acc, y) => acc + y The last thing to observe is that the argument names are not arbitrary. When the signature mentions `x` and later on it mentions `[x]` this should be read as: `[x]` will return a list of the same type as the original `x` specified. When the signature mentions acc and y with different names it is stating that x,y and acc could all have different types to eachother. If you see the same name in a signature twice, it means it must be the same type. This is a really simple way to annotate generics.
Loading…

no comments

    sign in to comment