<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1561" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2>Instead of passing the function name, pass a fun:</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2>Log_compute = wrap_fun(fun() -> compute(AllArgs) end,
logger).</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2>wrap_fun(Fun, Pid) -></FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2> Res = Fun(),</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2> Pid ! Res.</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff size=2>The
fun acts as a "closure" and inherits the state from its
surroundings.</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff size=2>It can
fully encapsulate the data needed, so there is no need for any
</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2>arguments.</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff size=2>(The
example now feels a bit contrived, but perhaps you get the point
anyway?)</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff
size=2>BR,</FONT></SPAN></DIV>
<DIV><SPAN class=312101916-17112006><FONT face=Arial color=#0000ff size=2>Ulf
W</FONT></SPAN></DIV><BR>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> erlang-questions-bounces@erlang.org
[mailto:erlang-questions-bounces@erlang.org] <B>On Behalf Of </B>Ludovic
Coquelle<BR><B>Sent:</B> den 17 november 2006 16:47<BR><B>To:</B>
erlang-questions@erlang.org<BR><B>Subject:</B> [erlang-questions] How to wrap
a function of any arity<BR></FONT><BR></DIV>
<DIV></DIV>I would like to wrap a function into another which send the the
result of the inner function to some external process.<BR>Let say I have a
function called "compute" and a log process "logger": I want to create a new
function with something like: <BR><BR><SPAN
style="FONT-FAMILY: courier new,monospace">Log_compute = wrap_fun(compute,
logger).</SPAN><BR style="FONT-FAMILY: courier new,monospace"><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">wrap_fun(Fun, Pid) -></SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace"> fun(AllArgs)
-></SPAN><BR style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
Res = Fun(AllArgs),</SPAN><BR style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
Pid ! Res</SPAN><BR style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
end.</SPAN><BR><BR>But I don't know how to do that with a function of any
arity (the code above work only for function of arity 1).<BR>Is this do-able?
if not, how should I do?<BR><BR></BLOCKQUOTE></BODY></HTML>