Skip to content

Instantly share code, notes, and snippets.

@nikarh
Created October 28, 2016 20:12
Show Gist options
  • Save nikarh/88504100d44c769fdf7c1b997a7f564a to your computer and use it in GitHub Desktop.
Save nikarh/88504100d44c769fdf7c1b997a7f564a to your computer and use it in GitHub Desktop.
Filter map and reduc in pure Bash
#!/usr/bin/env bash
function filter {
while read line; do
if [ "$($1 $line)" -ne 0 ]; then
echo $line
fi
done
}
function map {
while read line; do
echo "$($1 $line)"
done
}
function reduce {
local acc=$2
while read line; do
acc="$($1 $acc $line)"
done
echo $acc
}
function odd {
echo $(($1 % 2 == 1))
}
function double {
echo $(($1 * 2))
}
function sum {
echo $(($1 + $2))
}
seq 1 5 \
| filter odd \
| map double \
| reduce sum 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment