1

Could someone explain how to understand this notation:

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

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

customcommander
  • 17,580
  • 5
  • 58
  • 84
Maciej Miklas
  • 3,305
  • 4
  • 27
  • 52

2 Answers2

6
((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.

customcommander
  • 17,580
  • 5
  • 58
  • 84
  • Very nice, concise answer! I'd love to have [one more line](http://fourwindssoft.com/link/16) that reintroduced the parentheses. – Scott Sauyet Mar 18 '21 at 13:11
  • @customcommander that was the best explanation I've ever seen. Could you please tell me how this signature should be treated ((acc, x) → (acc, y)) → acc → [x] → (acc, [y]). It takes three arguments and returns a tuple. I have trouble understanding the left side: ((acc, x) → (acc, y)) - it takes a function with two arguments that returns what? Another function? – EA0906 Jul 15 '22 at 08:57
3

I believe its Hindley-Milner notation: https://drboolean.gitbooks.io/mostly-adequate-guide-old/content/ch7.html

Redark
  • 173
  • 1
  • 11