Skip to content

Instantly share code, notes, and snippets.

@lenary
Created November 12, 2014 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lenary/506c1afbce0c65ada8f4 to your computer and use it in GitHub Desktop.
Save lenary/506c1afbce0c65ada8f4 to your computer and use it in GitHub Desktop.
%% So you have an aribtrary function, like so
f(A,B,C) ->
A + B + C.
%% We want something that can be more helpful for sectioning/partial application.
%% What about transforming that thing above to the following:
f(A) ->
fun (B) ->
fun (C) ->
A + B + C
end
end.
%% *and* a definition
f(A, B) ->
fun (C) ->
A + B + C
end.
%% In short, every function fun <name>/<args> gets transformed into fun <name>/1..<args>
%% of course you have to prevent collisions. it's common to have fun <name>/<args> and fun <name>/<args+1>,
%% so you only actually do this transformation on the function with the losest arity for that name.
%% This gets very complex for folds. Maybe we need an annotation like -section(<final_arity>) which,
%% say, does the following:
-section(2).
f(A,B,C) -> A + B + C.
%% Becomes
f(A) ->
fun (B,C) ->
A + B + C
end.
%% of course, usually when i've been saying "becomes", what i really mean is
%% "the function below is added alongside the original function", so actually the created function can be:
f(A) ->
fun (B,C) ->
f(A,B,C)
end.
@seancribbs
Copy link

I disagree. I want something less about transforming the called function and more about transforming the callsite.

f(A,B,C) -> A + B + C. % unchanged

`f(A). % like line 11

`f(A,B). % like line 20

`f(_, _, C). % like erlando's cut syntax, perhaps for folds

@seancribbs
Copy link

The fundamental problem, however, is that we don't know the arity of the partially-applied function (unless perhaps it's in the same module) and we'd need deep compiler tricks to know that.

Maybe the final option with cuts is best because then the arity of the target function is explicit.

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