Skip to content

Instantly share code, notes, and snippets.

@abesto

abesto/test.sh Secret

Created February 28, 2024 18:29
Show Gist options
  • Save abesto/97a249f0d0b8406f6fc5e700d3efac93 to your computer and use it in GitHub Desktop.
Save abesto/97a249f0d0b8406f6fc5e700d3efac93 to your computer and use it in GitHub Desktop.
function curry() {
exportfun=$1; shift
fun=$1; shift
params=$*
cmd=$"function $exportfun() {
more_params=\$*;
$fun $params \$more_params;
}"
eval $cmd
}
function arrays_sum() {
# Calculate the sum of all items in all input arrays
sum=0
for array in "$@"; do
for x in "${array[@]}"; do
sum=$((sum + x))
done
done
echo "$sum"
}
array1=(1 2 3)
array2=(3 4 5)
array3=(6 7 8)
echo raw
arrays_sum "${array1[@]}"
arrays_sum "${array1[@]}" "${array2[@]}"
arrays_sum "${array1[@]}" "${array2[@]}" "${array3[@]}"
echo
echo curry
curry arrays_sum1 arrays_sum "${array1[@]}"
arrays_sum1
arrays_sum1 "${array2[@]}"
arrays_sum1 "${array2[@]}" "${array3[@]}"
echo
shopt -s expand_aliases
echo alias expanded at use time
alias arrays_sum2='arrays_sum "${array1[@]}"'
arrays_sum2
echo
echo changed array1
array1=(0)
arrays_sum1
arrays_sum2
echo
echo alias expanded at definition time
alias arrays_sum3="arrays_sum \"\${array1[@]}\""
arrays_sum3
echo
echo changed again
array1=(10 11 12)
arrays_sum3
arrays_sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment