diff --git a/src/fold b/src/fold index 0a6a225..53bc23d 100755 --- a/src/fold +++ b/src/fold @@ -11,11 +11,33 @@ fold () { return 1 } else { typeset f="\$($1 \$acc \$1)"; shift - foldl "$f" "$@" + foldlp "$f" "$@" } } foldl () { + if (($#<2)) { + { + print -- 'Warning, l is not for left! Its for lambda style expression!' + print -- 'Though this is left fold still :)' + } >&2 + return 1 + } else { + local body=$1 + local acc=$2 + shift 2 + for x; acc=$(folde_ $x $acc $body) + print -- $acc + return 0 + } +} + +folda () { + typeset f="\$[ $1 ]"; shift + foldlp "$f" "$@" +} + +foldlp () { if (($#<2)) { { print -- 'Warning, l is not for left! Its for lambda style expression!' @@ -32,13 +54,13 @@ foldl () { } } -folda () { - typeset f="\$[ $1 ]"; shift - foldl "$f" "$@" -} - fold_ () { local acc=$2 local body=$3 print "${(e)body}" } +folde_ () { + local acc=$2 + local body=$3 + eval "${(e)body}" +} diff --git a/src/map b/src/map index be2b720..40fa7bb 100755 --- a/src/map +++ b/src/map @@ -31,9 +31,13 @@ mapl () { typeset f="$1"; shift typeset x typeset result=0 - for x; map_ "$x" "$f" || result=$? + for x; mapl_ "$x" "$f" || result=$? return $result } +mapl_ () { + eval "${(e)2}" +} + mapa () { (($#<1)) && { @@ -48,10 +52,18 @@ mapa () { return 1 } typeset f="\$[ $1 ]"; shift - mapl "$f" "$@" + mapa__ "$f" "$@" } -map_ () { +mapa__ () { + (($#<1)) && return 1 + typeset f="$1"; shift + typeset x + typeset result=0 + for x; mapa_ "$x" "$f" || result=$? + return $result +} + +mapa_ () { print -- "${(e)2}" } - diff --git a/test/test b/test/test index 7874002..ccabdb4 100755 --- a/test/test +++ b/test/test @@ -37,15 +37,15 @@ addition () { echo $(($1 + $2)) } ignore_acc () { echo $2 } TEST "map can (+1) numbers " "map plus_one {0..5} " "1 2 3 4 5 6" -TEST "mapl echo append " "mapl '\$1 day' good bad " "good day bad day" +TEST "mapl echo append " "mapl 'echo \$1 day' good bad " "good day bad day" TEST "mapa can (+1) " "mapa '\$1 + 5' {1..3} " "6 7 8" TEST "filter can remove odd numbers " "filter divisible_by_two {0..4} " "0 2 4" TEST "filterl can grep out words " "filterl 'echo \$1 | grep a --silent' ab bc ac " "ab ac" TEST "filtera can remove odd numbers " "filtera '\$1%2 == 0' {0..4} " "0 2 4" TEST "fold can sum numbers " "fold addition 0 {1..5} " "15" TEST "fold is not commutative " "fold ignore_acc a b c d " "d" -TEST "foldl palin " "foldl '\$1\$acc\$1' MIDDLE = + = = " "==+=MIDDLE=+==" -TEST "foldl palin2 " "foldl '\$1\$2\$1' MIDDLE = + = = " "==+=MIDDLE=+==" +TEST "foldl palin " "foldl 'echo \$1\$acc\$1' MIDDLE - O o . " ".oO-MIDDLE-Oo." +TEST "foldl palin2 " "foldl 'echo \$1\$2\$1' MIDDLE - O o . " ".oO-MIDDLE-Oo." TEST "folda can sum numbers " "folda '\$1+\$2' 0 {1..5} " "15" TEST "each can only append " "each 'echo young' boy girl " "young boy young girl" TEST "eachl can prepend " "eachl 'echo \$1 day' good bad " "good day bad day"