patbonecrusher / fun.sh

Functional /bin/sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#fbash.sh: Functional /bin/bash. Functional bash scripting. Forked from https://github.com/mikeplus64/fun.sh

Functions:

lambda (var.., function)

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))'

fold (function, list)

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:

6

list(var..)

The list function prints each element to a new line in order.

eg.,

list '1' '2' '3'

result:

1
2
3

rlist(var..)

The reverse list function prints each element to a new line in reverse order.

eg.,

rlist '1' '2' '3'

result:

3
2
1

strcmp(stringA, stringB)

The strcmp function compares two strings, returning 1 for a match and 0 for no match.

eg.,

strcmp 'hello' 'world'

result:

0

eg.,

strcmp 'hello' 'hello'

result:

1

filter(function, list)

The 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:

hello

match(function, list)

The 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:

world

position(function, list)

The 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:

0

partial(result_function, function, args...)

Partially apply function to args, store the resulting function in result_function.

add() {
    expr $1 + $2
}
partial adder3 add 3
adder3 6

result:

9

compose(result_function, outer_function, inner_function)

Compose 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 3

rersult:

25

About

Functional /bin/sh


Languages

Language:Shell 100.0%