1

Could someone explain how to understand this notation:

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

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

customcommander
  • 14,568
  • 4
  • 48
  • 68
Maciej Miklas
  • 3,276
  • 4
  • 25
  • 50

2 Answers2

4
((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
  • 14,568
  • 4
  • 48
  • 68
  • 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
3

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

Redark
  • 163
  • 1
  • 9