Skip to content

Instantly share code, notes, and snippets.

@dentarg
Forked from xevz/zsh_map_function.sh
Created May 24, 2010 23:31
Show Gist options
  • Save dentarg/412561 to your computer and use it in GitHub Desktop.
Save dentarg/412561 to your computer and use it in GitHub Desktop.
# From http://www.reddit.com/r/linux/comments/akt3j/a_functional_programming_style_map_function_for/
# Modified to work in zsh.
function map() {
local command i rep
if [ $# -lt 2 ] || [[ ! "$@" =~ :[[:space:]] ]];then
echo "Invalid syntax." >&2; return 1
fi
until [[ $1 =~ : ]]; do
command="$command $1"; shift
done
command="$command ${1%:}"; shift
for i in "$@"; do
if [[ $command =~ \\{\\} ]];then
rep="${command//\{\}/\"$i\"}"
eval "${rep//\\/\\\\}"
else
eval "${command//\\/\\\\} \"${i//\\/\\\\}\""
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment