Skip to content

Instantly share code, notes, and snippets.

@abesto
Last active February 28, 2024 18:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abesto/4286574 to your computer and use it in GitHub Desktop.
Save abesto/4286574 to your computer and use it in GitHub Desktop.
Partial application in Bash
function curry() {
exportfun=$1; shift
fun=$1; shift
params=$*
cmd=$"function $exportfun() {
more_params=\$*;
$fun $params \$more_params;
}"
eval $cmd
}
function add() {
a=$1; shift
b=$1; shift
expr $a + $b
}
curry add3 add 3
$add3 9
# Guess the output :)
@tgerder
Copy link

tgerder commented Mar 13, 2015

really nice. it remembers me of aliases. am i right they are kind of currying with special syntax?

curry in .bashrc, then console:

$ function hello(){ echo "hello, $1"; }
$ alias hello2='hello roger2'
$ curry hello3 'hello roger,roger'
$ hello2
hello, roger2
$ hello3
hello, roger,roger

@nadapez
Copy link

nadapez commented Feb 28, 2024

What is the differencce with alias?

@abesto
Copy link
Author

abesto commented Feb 28, 2024

Mostly some nuances about how aliases are handled by Bash. Check out https://gist.github.com/abesto/97a249f0d0b8406f6fc5e700d3efac93. I think functions and aliases also have different rules about sharing to subshells.

https://www.gnu.org/software/bash/manual/html_node/Aliases.html has this nugget:

For almost every purpose, shell functions are preferred over aliases.

So I guess you could argue that's another reason for this to exist.

But really this is just some educational fun, if you ever find yourself thinking you want this, you probably want to use a real programming language instead :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment