Skip to content

Commit

Permalink
add functions/each: apply a function on each of the arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpoelen committed Mar 12, 2022
1 parent 8ed1984 commit 98862c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions functions/each
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env zsh

# Apply a function on each of the arguments
#
# $ each cmd arg1 arg2
# equivalent to
# $ cmd arg1
# $ cmd arg2
#
# -- is used to mark end of options
#
# $ each cnd opt -- arg1 arg2
# equivalent to
# $ cmd opt arg1
# $ cmd opt arg2
#
# For further usage, please consider zargs command
#
# Add the following line for auto-completion:
# _comps[each]=_precommand

local -i iargs=${@[(ie)--]} ecmd=1
local args=
if (( $iargs > $# )); then
iargs=1
else
ecmd=iargs-1
if (( $iargs > 2 )); then
args="${(q)${@[2,$ecmd]}}"
fi
fi

for f in $@[$iargs+1,-1]; do
eval "$1 $args ${(q)f}"
done
6 changes: 6 additions & 0 deletions test/functions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ check_cmd()
}


if test_function each ; then
test echo a b; check_s $'a\nb'
test echo a -- b c; check_s $'a b\na c'
test echo -- a -- b; check_s $'a\n--\nb'
fi

if test_function jln-glob ; then
eval "pre_$(functions xxx)"
xxx() { pre_xxx $=1 ; shift ; jln-glob-pop $~@ }
Expand Down

0 comments on commit 98862c9

Please sign in to comment.