### Quick Start Bash Script Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates how to source the bash-fun library from GitHub and use a basic pipeline with the 'sum' function. ```bash #!/bin/bash . <(test -e fun.sh || curl -Ls https://raw.githubusercontent.com/ssledz/bash-fun/master/src/fun.sh > fun.sh; cat fun.sh) seq 1 4 | sum ``` -------------------------------- ### Curry Function for Partial Application Source: https://github.com/ssledz/bash-fun/blob/master/README.md Illustrates the 'curry' function, which allows for partial application of functions. An example 'add2' function is defined and then curried. ```bash add2() { echo $(($1 + $2)) } $ curry inc add2 1 $ inc 2 3 $ seq 1 3 | map λ a . 'inc $a' 2 3 4 ``` -------------------------------- ### Sequence Manipulation: Take, Drop, Lhead, Ltail, Last Source: https://github.com/ssledz/bash-fun/blob/master/README.md Illustrates functions for extracting sub-sequences from a list: 'drop' removes elements from the beginning, 'take' selects elements from the beginning, 'lhead' gets the first element, 'ltail' gets all but the first, and 'last' gets the final element. ```bash $ list 1 2 3 4 | drop 2 3 4 $ list 1 2 3 4 5 | lhead 1 $ list 1 2 3 4 | ltail 2 3 4 $ list 1 2 3 4 5 | last 5 $ list 1 2 3 4 5 | take 2 1 2 ``` -------------------------------- ### Bash Functional String Processing Example Source: https://github.com/ssledz/bash-fun/blob/master/README.md An example of functional programming in Bash using `list`, `filter`, `map`, and `foldl` to process a list of names. It filters names longer than one character and converts them to uppercase. ```bash processNames() { uppercase() { local str=$1 echo $(tr 'a-z' 'A-Z' <<< ${str:0:1})${str:1} } list $@ \ | filter λ name . '[[ ${#name} -gt 1 ]] && ret true || ret false' \ | map λ name . 'uppercase $name' \ | foldl λ acc el . 'echo $acc,$el' } processNames adam monika s slawek d daniel Bartek j k ``` ```bash Adam,Monika,Slawek,Daniel,Bartek ``` -------------------------------- ### Fold Left and Fold Right Operations Source: https://github.com/ssledz/bash-fun/blob/master/README.md Shows 'foldl' (left fold) and 'foldr' (right fold) for accumulating values from a list using a binary function. Examples include string concatenation and arithmetic summation. ```bash $ list a b c d | foldl λ acc el . 'echo -n $acc-$el' a-b-c-d $ list '' a b c d | foldr λ acc el . 'if [[ ! -z $acc ]]; then echo -n $acc-$el; else echo -n $el; fi' d-c-b-a $ seq 1 4 | foldl λ acc el . 'echo $(($acc + $el))' 10 $ seq 1 4 | foldl λ acc el . 'echo $(mul $(($acc + 1)) $el)' 64 # 1 + (1 + 1) * 2 + (4 + 1) * 3 + (15 + 1) * 4 = 64 $ seq 1 4 | foldr λ acc el . 'echo $(mul $(($acc + 1)) $el)' 56 # 1 + (1 + 1) * 4 + (8 + 1) * 3 + (27 + 1) * 2 = 56 ``` -------------------------------- ### Bash Test Runner Execution Source: https://github.com/ssledz/bash-fun/blob/master/README.md Shows a simple command to navigate into a 'test' directory and execute a test runner script. ```bash cd test ./test_runner ``` -------------------------------- ### List and Unlist Functions Source: https://github.com/ssledz/bash-fun/blob/master/README.md Shows how to create a list of items using 'list' and how to flatten a list into a single line with 'unlist'. ```bash $ list 1 2 3 1 2 3 $ list 1 2 3 4 5 | unlist 1 2 3 4 5 ``` -------------------------------- ### Bash Error Handling with try/catch Source: https://github.com/ssledz/bash-fun/blob/master/README.md Illustrates how to use `try` and `catch` constructs in Bash for robust error handling. `try` executes a lambda on success or failure, while `catch` specifically handles errors, capturing command status and values. ```bash $ echo 'expr 2 / 0' | try λ _ . 'echo 0' 0 $ echo 'expr 2 / 0' | try λ status . 'echo $status' 2 $ echo 'expr 2 / 2' | try λ _ . 'echo 0' 1 ``` ```bash try λ _ . 'echo some errors during pull; exit 1' < <(echo git pull) ``` ```bash $ echo 'expr 2 / 0' \ | LANG=en catch λ cmd status val . 'echo cmd=$cmd,status=$status,val=$val' cmd=expr 2 / 0,status=2,val=(expr:,division,by,zero) $ echo 'expr 2 / 2' | catch λ _ _ val . 'tupl $val' 1 ``` -------------------------------- ### Peek Function for Debugging Pipelines Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates the 'peek' function, which allows inserting debugging or logging steps into a pipeline without altering the data flow. It takes a lambda function to process each element. ```bash $ list 1 2 3 \ | peek lambda a . echo 'dbg a : $a' \ | map lambda a . 'mul $a 2' \ | peek lambda a . echo 'dbg b : $a' \ | sum dbg a : 1 dbg a : 2 dbg a : 3 dbg b : 2 dbg b : 4 dbg b : 6 12 $ a=$(seq 1 4 | peek lambda a . echo 'dbg: $a' | sum) dbg: 1 dbg: 2 dbg: 3 dbg: 4 $ echo $a 10 ``` -------------------------------- ### Tuple Creation and Extraction: Tup, Tupl, Tupr, Tupx Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates creating tuples with 'tup', extracting the first element with 'tupl', the last element with 'tupr', and arbitrary elements/ranges with 'tupx'. Handles simple and complex data within tuples. ```bash $ tup a 1 (a,1) $ tup 'foo bar' 1 'one' 2 (foo bar,1,one,2) $ tup , 1 3 (u002c,1,3) $ tupl $(tup a 1) a $ tupr $(tup a 1) 1 $ tup , 1 3 | tupl , $ tup 'foo bar' 1 'one' 2 | tupl foo bar $ tup 'foo bar' 1 'one' 2 | tupr 2 $ tup 'foo bar' 1 'one' 2 | tupx 2 1 $ tup 'foo bar' 1 'one' 2 | tupx 1,3 foo bar one $ tup 'foo bar' 1 'one' 2 | tupx 2-4 1 one 2 ``` -------------------------------- ### Map Function for Element Transformation Source: https://github.com/ssledz/bash-fun/blob/master/README.md Shows how to apply a transformation function to each element of a list using 'map'. Supports inline lambda expressions and simple commands. ```bash $ seq 1 5 | map λ a . 'echo $((a + 5))' 6 7 8 9 10 $ list a b s d e | map λ a . 'echo $a$(echo $a | tr a-z A-Z)' aA bB sS dD eE $ list 1 2 3 | map echo 1 2 3 $ list 1 2 3 | map 'echo $ is a number' 1 is a number 2 is a number 3 is a number $ list 1 2 3 4 | map 'echo \($,$\) is a point' (1,1) is a point (2,2) is a point (3,3) is a point (4,4) is a point ``` -------------------------------- ### Lzip Function for Zipping Lists Source: https://github.com/ssledz/bash-fun/blob/master/README.md Shows how 'lzip' pairs elements from multiple lists together, creating tuples. It stops when the shortest list is exhausted. ```bash $ list a b c d e f | lzip $(seq 1 10) (a,1) (b,2) (c,3) (d,4) (e,5) (f,6) $ list a b c d e f | lzip $(seq 1 10) | last | tupr 6 ``` -------------------------------- ### Flat Map for Nested Transformations Source: https://github.com/ssledz/bash-fun/blob/master/README.md Illustrates 'flat map' functionality, where each element is transformed into a list, and all resulting lists are concatenated. ```bash $ seq 2 3 | map λ a . 'seq 1 $a' | join , [ ] [1,2,1,2,3] $ list a b c | map λ a . 'echo $a; echo $a | tr a-z A-z' | join , [ ] [a,A,b,B,c,C] ``` -------------------------------- ### Bash File Predicate Utilities Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates various Bash functions for checking file properties like existence, readability, writability, and directory status. Uses `list`, `filter`, and lambda expressions for conditional checks on file paths. ```bash $ touch /tmp/foo $ isfile /tmp/foo true $ not iswritable / true $ files="/etc/passwd /etc/sudoers /tmp /tmp/foo /no_such_file" $ list $files | filter λ a . 'isfile $a' /etc/passwd /etc/sudoers /tmp/foo $ list $files | filter λ a . 'isdir $a' /tmp $ list $files | filter λ a . 'isreadable $a' /etc/passwd /tmp /tmp/foo $ list $files | filter λ a . 'iswritable $a' /tmp /tmp/foo $ list $files | filter λ a . 'isnonzerofile $a' /etc/passwd /etc/sudoers /tmp $ list $files | filter λ a . 'not isfile $a' /tmp /no_such_file ``` -------------------------------- ### Bash Scanl for Cumulative Operations Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates the `scanl` function in Bash, which applies a lambda cumulatively to elements of a list, producing a list of intermediate results. Useful for calculating running totals or similar aggregations. ```bash $ seq 1 5 | scanl lambda acc el . 'echo $(($acc + $el))' 1 3 6 10 15 ``` ```bash $ seq 1 5 | scanl lambda a b . 'echo $(($a + $b))' | last 15 ``` -------------------------------- ### Join Function for List Concatenation Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates the 'join' function to concatenate list elements with a specified delimiter, optionally enclosing the result in brackets. ```bash $ list 1 2 3 4 5 | join , 1,2,3,4,5 $ list 1 2 3 4 5 | join , [ ] [1,2,3,4,5] ``` -------------------------------- ### Maybe, Maybemap, Maybevalue for Optional Values Source: https://github.com/ssledz/bash-fun/blob/master/README.md Introduces functions for handling potentially absent values. 'maybe' wraps a value in a 'Just' or 'Nothing' constructor. 'maybemap' applies a function to a 'Just' value, and 'maybevalue' extracts the value or provides a default. ```bash $ list Hello | maybe (Just,Hello) $ list " " | maybe (Nothing) $ list Hello | maybe | maybemap λ a . 'tr oH Oh <<<$a' (Just,hellO) $ list " " | maybe | maybemap λ a . 'tr oH Oh <<<$a' (Nothing) $ echo bash-fun rocks | maybe | maybevalue DEFAULT bash-fun rocks $ echo | maybe | maybevalue DEFAULT DEFAULT ``` -------------------------------- ### Buffer Function for Grouped Operations Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates the 'buff' function, which groups elements from a list and applies a function to each group. The function receives group elements as arguments. ```bash $ seq 1 10 | buff λ a b . 'echo $(($a + $b))' 3 7 11 15 19 $ seq 1 10 | buff λ a b c d e . 'echo $(($a + $b + $c + $d + $e))' 15 40 ``` -------------------------------- ### Encoded Tuple Operations: Ntup, Ntupl, Ntupr, Ntupx Source: https://github.com/ssledz/bash-fun/blob/master/README.md Introduces 'ntup' for creating base64 encoded tuples and 'ntupl', 'ntupr', 'ntupx' for extracting elements from these encoded tuples. Useful for handling complex or binary data within Bash. ```bash $ ntup tuples that $(ntup safely nest) (dHVwbGVzCg==,dGhhdAo=,KGMyRm1aV3g1Q2c9PSxibVZ6ZEFvPSkK) echo '(dHVwbGVzCg==,dGhhdAo=,KGMyRm1aV3g1Q2c9PSxibVZ6ZEFvPSkK)' | ntupx 3 | ntupr nest $ ntup 'foo,bar' 1 one 1 (Zm9vLGJhcgo=,MQo=,b25lCg==,MQo=) $ echo '(Zm9vLGJhcgo=,MQo=,b25lCg==,MQo=)' | ntupx 1 foo,bar $ ntupl $(ntup 'foo bar' 1 one 2) foo bar $ ntupr $(ntup 'foo bar' 1 one 2) 2 ``` -------------------------------- ### Logical and Type Check Functions: Not, Isint, Isempty Source: https://github.com/ssledz/bash-fun/blob/master/README.md Provides utility functions for logical negation ('not'), integer checking ('isint'), and emptiness checking ('isempty'). These are often used with 'filter' or conditional logic. ```bash $ isint 42 true $ list blah | isint false $ not true false $ not isint 777 false $ list 1 2 "" c d 6 | filter λ a . 'isint $a' 1 2 6 $ list 1 2 "" c d 6 | filter λ a . 'not isempty $a' 1 2 c d 6 ``` -------------------------------- ### Filter Function for Element Selection Source: https://github.com/ssledz/bash-fun/blob/master/README.md Demonstrates the 'filter' function, which selects elements from a list based on a predicate function. The predicate must return 'true' or 'false'. ```bash $ seq 1 10 | filter λ a . '[[ $(mod $a 2) -eq 0 ]] && ret true || ret false' 2 4 6 8 10 ``` -------------------------------- ### Bash Trampoline Recursion with with_trampoline/res/call Source: https://github.com/ssledz/bash-fun/blob/master/README.md Implements a factorial function using trampolining for tail-call optimization in Bash. It utilizes `with_trampoline`, `res`, and `call` to manage recursive calls without stack overflow, demonstrated with large numbers. ```bash factorial() { fact_iter() { local product=$1 local counter=$2 local max_count=$3 if [[ $counter -gt $max_count ]]; then res $product else call fact_iter $(echo $counter*$product | bc) $(($counter + 1)) $max_count fi } with_trampoline fact_iter 1 1 $1 } ``` ```bash $ time factorial 30 | fold -w 70 265252859812191058636308480000000 real 0m1.854s user 0m0.072s sys 0m0.368s ``` ```bash time factorial 60 | fold -w 70 8320987112741390144276341183223364380754172606361245952449277696409600 000000000000 real 0m3.635s user 0m0.148s sys 0m0.692s ``` ```bash $ time factorial 90 | fold -w 70 1485715964481761497309522733620825737885569961284688766942216863704985 393094065876545992131370884059645617234469978112000000000000000000000 real 0m4.371s user 0m0.108s sys 0m0.436s ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.