-
Notifications
You must be signed in to change notification settings - Fork 0
Functionals
João Pedro Neto edited this page Nov 19, 2020
·
3 revisions
Functionals are high-order functions, i.e., functions with functions as parameters.
Yöctọ includes the following functionals:
-
Map maps an unary function over a list of elements, so
map f [x1,...xn]
results in[f(x1),...,f(xn)]
Use: list id Ṁ
where id
is the function identifier (at which line is the function)
The next program outputs [2,4,6,8,10]
2*
5…0Ṁ
-
Filter filters a list of elements satisfying an unary predicate, so
filter isPair [1,2,3,4,5]
results in[2,4]
Use: list id Ḟ
where id
is the function identifier (at which line is the function)
The next program filters [1,2,3,4,5]
by pairs and outputs [2,4]
2%¬
5┅0Ḟ
-
Reduce accumulates a binary function over a list of elements, so
reduce + 0 [1,2,3,4,5]
results in15
. The zero is the neutral element of the applied function (it is the default value if the list is empty).
Use: list default id Ṙ
The next program computes the previous example, and outputs 15
+
5┅0 0Ṙ
The next example computes the factorial of a given input
*
┅1 0Ṙ