Skip to content

Instantly share code, notes, and snippets.

@masotime
Created March 2, 2017 18:49
Show Gist options
  • Save masotime/3f90b1215595edd960386a8969f79f55 to your computer and use it in GitHub Desktop.
Save masotime/3f90b1215595edd960386a8969f79f55 to your computer and use it in GitHub Desktop.
Using bash to implement recursion-based reduce
#!/bin/bash
function testRecurse {
ONE=${1:0}
TWO=("${@:1}")
if [[ $# -eq 1 ]]; then
echo Final answer $ONE
else
echo \|${TWO[1]}\|
let ACC=ONE+TWO[1]
ARR=("${TWO[@]:2}")
echo recursion $ACC - $ARR
testRecurse $ACC ${ARR[@]}
fi
}
testRecurse
testRecurse 10
testRecurse 10 1 2 3 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment