Anonymous user
Partial function application: Difference between revisions
no edit summary
(Add Lua solution) |
No edit summary |
||
Line 251:
(dolist (seq '((0 1 2 3) (2 4 6 8)))
(format t "~%seq: ~A~% fsf1 seq: ~A~% fsf2 seq: ~A"
Output: <pre>seq: (0 1 2 3)
Line 274:
pfunc partial(intfunc fin)
{
}
int square(int a)
{
}
int dbl(int a)
{
}
int main()
{
}</lang>output<lang>partial square:
1
Line 542:
{4 8 12 16}
{4 16 36 64}
</lang>
=={{header|Erlang}}==
Partial applications can be built using the Erlang Meta Interpreter.
<lang erlang>
-module(partial_application).
-export([run/0]).
partial(F, Args) ->
{arity, InitialArity} = erlang:fun_info(F, arity),
case length(Args) of
L when L < InitialArity ->
MissingArgs = [{var, 1, N} || N <- lists:seq(1, InitialArity - L)],
ArgList = [case is_function(A) of
false -> erl_parse:abstract(A);
true -> {var, 1, erlang:fun_to_list(A)}
end || A <- Args] ++ MissingArgs,
Parsed = [{'fun', 1,
{clauses, [{clause, 1, MissingArgs, [],
[{call, 1, {var, 1, 'F'}, ArgList}]}]}}],
{value, R, _} = erl_eval:exprs(Parsed, [{'F', F}] ++
[{erlang:fun_to_list(A), A} ||
A <- Args, is_function(A)]),
R
end.
run() ->
F = partial(fun lists:map/2, [fun(X) -> X + 1 end]),
F([0, 1, 2, 3, 4]).
</lang>
'''Output:'''
<lang erlang>
> partial_application:run().
[1,2,3,4,5]
</lang>
Line 604 ⟶ 641:
// fs applies fn to each argument returning all results.
func (f fn) fs(s ...int) (r []int) {
}
Line 618 ⟶ 655:
// addn returns a function that adds n to a sequence of numbers
func addn(n int) func(...int) []int {
}
}
func main() {
}</lang>
{{out}}
Line 806 ⟶ 843:
public class PartialApplication {
}
}
}
}
}</lang>
Line 1,266 ⟶ 1,303:
Works with SWI-Prolog.
<lang Prolog>fs(P, S, S1) :-
f1(X, Y) :-
f2(X, Y) :-
create_partial(P, fs(P)).
Line 1,279 ⟶ 1,316:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fs :-
</lang>
Output :
Line 1,322 ⟶ 1,359:
Explicitly spelling out the partial function without hiding behind a library:<lang Python>def partial(f, g):
def fs(f, *x): return [ f(a) for a in x]
Line 1,470 ⟶ 1,507:
variable ctr
coroutine __curry[incr ctr] apply {{f1 f2} {
}} $f1 $f2
}</lang>
Line 1,479 ⟶ 1,516:
set r {}
foreach n $s {
}
return $r
|