Skip to content

Instantly share code, notes, and snippets.

@SamRothCA
Created January 24, 2016 03:42
Show Gist options
  • Save SamRothCA/dc0df2f3d9961e2cebec to your computer and use it in GitHub Desktop.
Save SamRothCA/dc0df2f3d9961e2cebec to your computer and use it in GitHub Desktop.
#!/bin/bash
echo $'infix operator .. {}\n'
declare min=2; max=4
for count in `eval echo {$min..$max}`;
do
declare -a originalArgSets=() providedArgSets=() remainingArgSets=() closureArgSets=() invocationArgSets=() parenthesesSets=()
args=(`eval echo A{0..$count}`)
genericArgs="${args[*]}"
genericArgs="${genericArgs// /, },"
for ((w=0; w < (max - count); w++)); do genericArgs="$genericArgs "; done
for ((i=0; i < (2 ** count); i++));
do
originalArgs=""
parentheses=""
for ((b=0; b<count; b++));
do
if (( (1 << b) & i ))
then
originalArgs="${originalArgs}${args[$b]}, "
parentheses="${parentheses} 0"
else
originalArgs="${originalArgs}${args[$b]})->("
parentheses="${parentheses} 1"
fi;
done
originalArgs="${originalArgs}${args[$count]}"
for ((w=0; w < (max - count); w++)); do originalArgs="$originalArgs "; done
originalArgSets=("${originalArgSets[@]}" "$originalArgs")
parenthesesSets=("${parenthesesSets[@]}" "$parentheses")
done
for ((i=0; i < (2 ** (count+1) ); i++));
do
declare providedArgs="" remainingArgs="" closureArgs="" invocationArgs=""
for ((b=0; b < (count + 1); b++));
do
if ((b < count))
then comma=', '; space=' '
else comma='' ; space=''; fi
if (( (1 << b) & i )); then
providedArgs="${providedArgs}${args[$b]}${comma}"
remainingArgs="${remainingArgs} ${space}"
closureArgs="${closureArgs} ${space}"
invocationArgs="${invocationArgs}p.$b${comma}"
else
providedArgs="${providedArgs}()${comma}"
remainingArgs="${remainingArgs}${args[$b]}${comma}"
closureArgs="${closureArgs}a$b${comma}"
invocationArgs="${invocationArgs} a$b${comma}"
fi;
done
for ((w=0; w < (max - count); w++)); do providedArgs="$providedArgs "; done
for ((w=0; w < (max - count); w++)); do remainingArgs="$remainingArgs "; done
for ((w=0; w < (max - count); w++)); do closureArgs="$closureArgs "; done
for ((w=0; w < (max - count); w++)); do invocationArgs="$invocationArgs "; done
providedArgSets=("${providedArgSets[@]}" "$providedArgs")
remainingArgSets=("${remainingArgSets[@]}" "${remainingArgs}")
closureArgSets=("${closureArgSets[@]}" "${closureArgs}")
invocationArgSets=("${invocationArgSets[@]}" "${invocationArgs}")
done
for ((f=0; f < ${#originalArgSets[@]}; f++ ));
do
originalArgs="${originalArgSets[$f]}"
originalParentheses=(${parenthesesSets[$f]})
(( ${#originalArgs} )) || continue
for ((p=0; p < ${#providedArgSets[@]}; p++ ))
do
providedArgs="${providedArgSets[$p]}"
remainingArgs="${remainingArgSets[$p]}"
closureArgs="${closureArgSets[$p]}"
invocationArgs="${invocationArgSets[$p]}"
if [[ "$closureArgs" && $closureArgs ]]; then
closureArgs="$closureArgs in"
else
closureArgs=""
fi
for index in {0..3}; do if (( ${originalParentheses[$index]} )); then
let offset=$[ (index * 5) + 3 ]
invocationArgs="${invocationArgs:0:offset})(${invocationArgs:offset+2}"
fi
done
if (( ${#providedArgs} )); then
finalOutput="public func .. <$genericArgs R> (f: ($originalArgs) -> R, p: ($providedArgs)) -> ($remainingArgs) -> R { return { ${closureArgs} f($invocationArgs) } }"
fi
while [[ "$finalOutput" =~ (,[[:space:]]+(\)|[[:space:]]in[[:space:]]f\()) ]] && match="${BASH_REMATCH[1]}"; do
fixed=" ${match:1}"
finalOutput="${finalOutput/$match/$fixed}"
done
while [[ "$finalOutput" =~ \{[[:space:]]+([[:space:]]in[[:space:]]f\() ]] && match="${BASH_REMATCH[1]}"; do
fixed=" ${match:3}"
finalOutput="${finalOutput/$match/$fixed}"
done
echo "$finalOutput"
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment