Higher Order Function Question

Håkan Stenholm hakan.stenholm@REDACTED
Sat May 25 15:18:11 CEST 2002


 >Thank you Haken,
 >
 >but the issue is not "apply" - I was only using that to experiment
 >-- the issue is "map" or "filter" which, apparently, will only
 >accept {attr, isPk} and not attr:isPk (which I am told is "more
 >elegant" and certainly more efficient).
 >
I think there has been slight missunderstanding here, apply(M,F,A) is 
the old classic version of how to call a fun, modern erlang supports 
funs to be called as F1(A) or M:F2(A) where  F1 is a fun as defined 
previously:
 > Fun = fun(Arg1, Arg2 ....) -> ... end  % anynomouse fun
 > Fun = fun FunctionName/Arity           % local fun in current file
 > Fun = {Module, FunctionName}           % any exported function can be 
used
 >
but it is also possible for M and F2 to be arbitrary atoms i.e. this can 
also be done:

% define this function
my_apply(M,F2,A) -> M:F2(A).
% this can be called as
my_apply(lists,append,[[1,2],[3,4]])
% and result in [1,2,3,4]

But "Fun = lists:append," (or similar construct) is not a legal way to 
define a fun. The F1(A) and M:F2(A) syntax is prefered to the 
apply(M,F,A) syntax because it's more efficent in most cases (see the 
"performance hints" link on the R8B documentation index.html page).

 >map and filter will accept:
 >
 >lists:map(fun(Arg) -> attr:varName(Arg) end,
 >rel:attrList(rel:findNamed(ng,"Member"))).
 >
 >but this is quite ugly looking code (albeit probably more efficient 
 >than {attr, isPk}...
 >
 >>Alex
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1482 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20020525/ad5fd375/attachment.bin>


More information about the erlang-questions mailing list