From c1ae19f27020e722b9171a442f739019480e9eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20=C5=9Aled=C5=BA?= Date: Sat, 31 Aug 2019 01:52:16 +0200 Subject: [PATCH] #1 : Shorthand notations for lambdas --- src/fun.sh | 20 ++++++++++++++++---- test/map_test.sh | 5 +++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/fun.sh b/src/fun.sh index d0b3af8..ba7b33b 100755 --- a/src/fun.sh +++ b/src/fun.sh @@ -66,10 +66,22 @@ lambda() { } map() { - local x - while read x; do - echo "$x" | "$@" - done + if [[ $1 != "λ" ]] && [[ $1 != "lambda" ]]; then + + local has_dollar=$(list $@ | grep '\$' | wc -l) + + if [[ $has_dollar -ne 0 ]]; then + args=$(echo $@ | sed -e 's/\$/\$a/g') + map λ a . $args + else + map λ a . "$@"' $a' + fi + else + local x + while read x; do + echo "$x" | "$@" + done + fi } foldl() { diff --git a/test/map_test.sh b/test/map_test.sh index b34217c..5371349 100755 --- a/test/map_test.sh +++ b/test/map_test.sh @@ -30,4 +30,9 @@ testFlatMap() { assertEquals "d e h l l l o o r w" "$(list hello world | map lambda x . 'command fold -w 1 <<< $x' | sort | unlist)" } +testMapNoLambdaSyntax() { + assertEquals "1 2 3" "$(list 1 2 3 | map echo | unlist)" + assertEquals "1 is a number 2 is a number 3 is a number" "$(list 1 2 3 | map 'echo $ is a number' | unlist)" +} + . ./shunit2-init.sh \ No newline at end of file