Functional Programming - Nazek42/arcyou GitHub Wiki
Arcyóu includes many powerful functional programming constructs. It has the standard map, filter, reduce,
%
(map)
Definition: F (func list)
Applies func
to every element of list
in turn, and returns the resulting list. Suppose you wanted to add 3 to every element of a list:
(% (F(x)(+ x 3)) (' 1 2 3 4 5))
The %
function will return [4, 5, 6, 7, 8]
.
/
(filter)
Definition: F (func list)
Returns list
with the elements for which (func element)
does not return a falsy value. Suppose you wanted to remove all 1
s from a list:
(/ [ (' 1 5 7 20 1 1 4))
The /
function will return [5, 7, 20, 4]
.
r
(reduce aka left fold)
Definition: F (func list)
Applies func
to the first two elements of list
, then applies it to the result of that and the third element of list
, and so on. Suppose you wanted to sum up everything in a list:
(r + (' 1 2 3 4 5))
The r
function turns this into:
(+ (+ (+ (+ 1 2) 3) 4) 5)
Or, in more familiar infix notation:
(((1 + 2) + 3) + 4) + 5
This is the sum of the list, and will evaluate to 15
.
%%
(stackmap)
Definition: F (list func1 func2 func3 ...)
or F (list funclist)
This is a function designed specifically for Arcyóu. It maps each function given it, whether through a list or as individual arguments, onto the list in order. This can effectively be considered as just nested map functions. Suppose you wanted to add 1 to everything in a list before printing each value:
(%% (' 1 2 3 4) ] p)
This will print:
2
3
4
5
//
(stackfilter)
Definition: F (list func1 func2 func3 ...)
or F (list funclist)
Filters the list using every function given it. This can be considered as nested filter functions. Suppose you wanted to find all the prime divisors of 340:
(// (_ 340) p? (F(x) (d? 340 x)))
This first filters for primality, then for if 340 is divisible by it.