#fbash.sh: Functional /bin/bash. Functional bash scripting. Forked from https://github.com/mikeplus64/fun.sh
The lambda function represents an anonymous function. The lambda function is used as a predicate for other functions.
eg.,
lambda a b : 'echo $(($a - $b))'The fold function reduces a list (or other structure) to a single value by "folding" a binary operator (function) between successive elements of the list.
eg.,
list {1..3} | fold lambda a b : 'echo $(($a + $b))'result:
6The list function prints each element to a new line in order.
eg.,
list '1' '2' '3'result:
1
2
3The reverse list function prints each element to a new line in reverse order.
eg.,
rlist '1' '2' '3'result:
3
2
1The strcmp function compares two strings, returning 1 for a match and 0 for no match.
eg.,
strcmp 'hello' 'world'result:
0eg.,
strcmp 'hello' 'hello'result:
1The filter function compares elements in a list using a given function, in ascending order. A new list is returned containing only list elements that satify the function.
eg.,
list 'hello' 'world' | filter lambda a : 'echo $(strcomp "$a" "hello")'result:
helloThe match function compares elements in a list using a given function, in ascending order. The first matching element is returned and if no match then nothing is returned.
eg.,
list 'hello' 'world' | match lambda a : 'echo $(strcomp "$a" "world")'result:
worldThe position function compares elements in a list using a given function, in ascending order. The position of the first matching element is returned. Initial position is 0. Nothing is returned for no match.
eg.,
list 'hello' 'world' | position lambda a : 'echo $(strcomp "$a" "hello")'result:
0Partially apply function to args, store the resulting function in result_function.
add() {
expr $1 + $2
}
partial adder3 add 3
adder3 6result:
9Compose functions. Remember: (outer o inner)(x) = outer(inner(x))
add() {
expr $1 + $2
}
square() {
expr $1 \* $1
}
compose square_of_sum square add
square_of_sum 2 3rersult:
25