Search code examples
functional-programmingramda.jshindley-milner

How to interpret this Ramda signature?


Could someone explain how to understand this notation:

((a, b) → a) → a → [b] → a

See: https://ramdajs.com/docs/#reduce


Solution

  • ((a, b) → a) → a → [b] → a
    ^^^^^^^^^^^^   ^   ^^^   ^
    1              2   3     4
    

    This is a function that takes three arguments (1) (2) (3) and returns a value of type a (4):

    1. The 1st arg is a function that takes two arguments (maybe of the same type) and returns a value of the same type as the first argument.
    2. The 2nd argument is a value of type a
    3. The 3rd argument is a list of values of type b
    reduce( (acc, x) => acc + x.length,    0,   ["foo", "bar", "baz"]); //=> 9
    //       ^^^  ^     ^^^^^^^^^^^^^^     ^    ^^^^^^^^^^^^^^^^^^^^^        ^
    //       a    b     a                  a    [b]                          a
    //     ((a -> b) -> a             ) -> a -> [b]                       -> a
    

    In this case a stands for the number type and b stands for the string type.