├── LICENSE ├── README.md └── functions ├── __functional_foldl.fish ├── __functional_foldr.fish ├── __functional_impl.fish ├── each.fish ├── filter.fish ├── foldl.fish ├── foldr.fish ├── map.fish ├── scanl.fish └── scanr.fish /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # functional - Higher order functions for fish shell 2 | The aim of this plugin is to provide some higher order functions for fish shell. Inspired by https://github.com/Tarrasch/zsh-functional. 3 | 4 | ## Examples 5 | ```fish 6 | $ map '$_1$_1' a b c # => aa \n bb \n cc \n 7 | $ printf "%d\n" 1 2 3 | map '(math "$_1 ^ 2")' # => 1 \n 4 \n 9 \n 8 | $ filter '[ $_1 -gt 10 ]' 12 1 21 # => 12 \n 21 \n 9 | $ seq 1 100 | foldl 0 '(math $_1 + $_acc)' # => 5050 10 | $ foldr '$_acc$_1' "" a b c # => cba 11 | $ scanl 0 '(math $_1 + $_acc)' (seq 1 5) # => 0 \n 1 \n 3 \n 6 \n 10 \n 15 \n 12 | $ scanr '(math $_1 + $_acc)' 0 (seq 1 5) # => 0 \n 5 \n 9 \n 12 \n 14 \n 15 \n 13 | $ each 'curl $_1 > /dev/null' "https://github.com" "http://fishshell.com" 14 | ``` 15 | 16 | ## Explanation 17 | Every command (except `each`) contains implicit echo. Thus, `each 'echo $_1'` is equivalent to `map '$_1'`. 18 | ### Limitations 19 | Right variants of fold and scan (`foldl`, `scanl`) do not support piping. 20 | 21 | ##Feedback, contribution 22 | Your feedback is welcomed! In case of bug and/or feature improvements please leave me an issue. 23 | -------------------------------------------------------------------------------- /functions/__functional_foldl.fish: -------------------------------------------------------------------------------- 1 | function __functional_foldl --argument init lambda iter 2 | set -e argv[1..3] 3 | set -l _acc $init 4 | if [ (count $argv) -eq 0 ] 5 | while read -l _1 6 | eval "$iter" 7 | set _acc (eval "echo $lambda") 8 | end 9 | else 10 | for _1 in $argv 11 | eval "$iter" 12 | set _acc (eval "echo $lambda") 13 | end 14 | end 15 | echo $_acc 16 | end 17 | -------------------------------------------------------------------------------- /functions/__functional_foldr.fish: -------------------------------------------------------------------------------- 1 | function __functional_foldr --argument lambda init iter 2 | set -l _acc $init 3 | set -l __count (count $argv) 4 | while [ $__count -ge 4 ] 5 | eval "$iter" 6 | set _1 $argv[$__count] 7 | set _acc (eval "echo $lambda") 8 | set __count (math "$__count - 1") 9 | end 10 | echo $_acc 11 | end -------------------------------------------------------------------------------- /functions/__functional_impl.fish: -------------------------------------------------------------------------------- 1 | function __functional_impl --argument to_eval 2 | if [ (count $argv) -eq 1 ] 3 | while read -l _1 4 | eval "$to_eval" 5 | end 6 | else 7 | for _1 in $argv[2..-1] 8 | eval "$to_eval" 9 | end 10 | end 11 | end 12 | 13 | -------------------------------------------------------------------------------- /functions/each.fish: -------------------------------------------------------------------------------- 1 | function each --argument lambda 2 | set -e argv[1] 3 | __functional_impl $lambda $argv 4 | end 5 | -------------------------------------------------------------------------------- /functions/filter.fish: -------------------------------------------------------------------------------- 1 | function filter --argument lambda 2 | set -e argv[1] 3 | __functional_impl "eval $lambda; and echo \$_1" $argv 4 | end 5 | -------------------------------------------------------------------------------- /functions/foldl.fish: -------------------------------------------------------------------------------- 1 | function foldl --argument init lambda 2 | set -e argv[1..2] 3 | __functional_foldl "$init" "$lambda" "" $argv 4 | end 5 | -------------------------------------------------------------------------------- /functions/foldr.fish: -------------------------------------------------------------------------------- 1 | function foldr --argument lambda init 2 | set -e argv[1..2] 3 | __functional_foldr "$lambda" "$init" "" $argv 4 | end 5 | -------------------------------------------------------------------------------- /functions/map.fish: -------------------------------------------------------------------------------- 1 | function map --argument lambda 2 | set -e argv[1] 3 | __functional_impl "echo $lambda" $argv 4 | end 5 | -------------------------------------------------------------------------------- /functions/scanl.fish: -------------------------------------------------------------------------------- 1 | function scanl --argument init lambda 2 | set -e argv[1..2] 3 | __functional_foldl "$init" "$lambda" 'echo $_acc' $argv 4 | end 5 | -------------------------------------------------------------------------------- /functions/scanr.fish: -------------------------------------------------------------------------------- 1 | function scanr --argument lambda init 2 | set -e argv[1..2] 3 | __functional_foldr "$lambda" "$init" 'echo $_acc' $argv 4 | end 5 | --------------------------------------------------------------------------------