Skip to content

Functionals

João Pedro Neto edited this page Nov 20, 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 in 15. 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Ṙ

  • Scan returns a list of all partial results applied to a reduce, so scan + 0 [1,2,3,4,5] returns [0, 1, 3, 6, 10, 15].

Use: list default id Ṡ

The next program computes the previous list

+
5┅ 0 0Ṡ

  • Clear returns the original list, but places an empty string over those elements not satisfying a predicate

Use: list id Ċ

The next program checks which elements are less than 3 returning [1, 2, '', '', '']

3<
5┅ 0Ċ

  • Zip With applies a binary function over the elements of two lists, elementwise.

Use: list1 list2 id Ż

The next program subtracts the values of lists [1,2,3,4,5] and [0,1,2] returning [1,1,1] (as in the typical zip function, it only computes until the smaller list runs out of elements)

-
5┅ 3… 0Ż

Clone this wiki locally