Created
October 28, 2016 20:12
-
-
Save nikarh/88504100d44c769fdf7c1b997a7f564a to your computer and use it in GitHub Desktop.
Filter map and reduc in pure Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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