A small Bash library for functional programming. Right now it contains map, filter, reduce, forEach, some and split.
The callback is always the first argument and you can either use normal
functions or a string with the function body inside of ()
as first argument.
These two code blocks are equal:
reduce '( echo $(( $1 + $2 )) )' 0 1 2 3 4 5
# 15
add() { echo $(($1 + $2)); }
reduce add 0 1 2 3 4 5
# 15
As data input you can either use positional arguments or the function
reads lines from the standard input - like the native mapfile
command does -
and takes those as arguments. The following example demostrates the two
alternatives:
map '( echo $(( $1 + 10 )) )' 1 2 3 4 5
map '( echo $(( $1 + 10 )) )' < <(printf "%s\n" 1 2 3 4 5)
# 11
# 12
# 13
# 14
# 15
You can of course chain different commands together:
reduce '( echo $(( $1 + $2 )) )' < <(filter '( (( $1 % 2 )) )' < <(map '( echo $(( $1 + 10 )) )' 1 2 3 4 5))
# 39
map '( echo $(( $1 + 10 )) )' < <(printf "%s\n" 1 2 3 4 5)
# 11
# 12
# 13
# 14
# 15
filter '( (( $1 % 2 )) )' < <(printf "%s\n" 1 2 3 4 5)
# 1
# 3
# 5
The second argument, in this case the 10
, is the starting value of the
accumulator.
reduce '( echo $(( $1 + $2 )) )' 10 < <(printf "%s\n" 1 2 3 4 5)
# 25
forEach '( notify-send $1 )' < <(printf "%s\n" 1 2 3 4 5)
The some
function tests whether at least one element passes the test
implemented by the provided function.
some '((( $1 % 2 )))' < <(printf "%s\n" 1 2 3 4 5)
# echo $?
# 0
The API for this function is quite different. The first argument is a string and the second argument takes a seperator.
split 'Hello,World,Good,Evening' ','
# Hello
# World
# Good
# Evening