Higher Order Function Question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Alex Peake
Can someone help me with this (no doubt simple) problem.

I try:

1> attr:isPk(Attr).
false

2> not attr:isPk(Attr).
true

3> not(attr:isPk(Attr)).
true

but cannot find a permutation/combination of:

4> apply({attr, isPk}, [A1]).
false

that can include "not" and work.

Actually, I am trying to lists:filter( ...not isPk... SomeList).

The simple is to define notIsPk(Attr), or perhaps lists:filter( fun(Attr) ->
.. end  but they are not an abstraction of "complement".

Perhaps I should define a function complement(Fn) that returns a function
that is the complement of the function passed?

Perhaps there is already an Erlang way to do this (I hope).

Alex




Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Raimo Niskanen-3
I cannot come up with anything better than:

        lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)

or

        complement(Fun) -> fun (Attr) -> not Fun(Attr) end.

        lists:filter(complement({attr, isPk}), SomeList)
        % or
        lists:filter(complement(fun isPk/1), SomeList) % within module 'attr'
        % or
        lists:filter(complement(fun(Attr) -> attr:isPk(Attr) end, SomeList) %
Very pointless with the complement() function

/ Raimo Niskanen, Erlang/OTP, Ericsson AB




Alex Peake wrote:

>
> Can someone help me with this (no doubt simple) problem.
>
> I try:
>
> 1> attr:isPk(Attr).
> false
>
> 2> not attr:isPk(Attr).
> true
>
> 3> not(attr:isPk(Attr)).
> true
>
> but cannot find a permutation/combination of:
>
> 4> apply({attr, isPk}, [A1]).
> false
>
> that can include "not" and work.
>
> Actually, I am trying to lists:filter( ...not isPk... SomeList).
>
> The simple is to define notIsPk(Attr), or perhaps lists:filter( fun(Attr) ->
> .. end  but they are not an abstraction of "complement".
>
> Perhaps I should define a function complement(Fn) that returns a function
> that is the complement of the function passed?
>
> Perhaps there is already an Erlang way to do this (I hope).
>
> Alex


Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Richard Carlsson-4
On Fri, 24 May 2002, Raimo Niskanen wrote:

> I cannot come up with anything better than:
>
> lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)
>
> or
>
> complement(Fun) -> fun (Attr) -> not Fun(Attr) end.
>
> lists:filter(complement({attr, isPk}), SomeList)
> % or
> lists:filter(complement(fun isPk/1), SomeList) % within module 'attr'
> % or
> lists:filter(complement(fun(Attr) -> attr:isPk(Attr) end, SomeList) %
> Very pointless with the complement() function
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
>
>
>
>
> Alex Peake wrote:
> >
> > Can someone help me with this (no doubt simple) problem.
> >
> > I try:
> >
> > 1> attr:isPk(Attr).
> > false
> >
> > 2> not attr:isPk(Attr).
> > true
> >
> > 3> not(attr:isPk(Attr)).
> > true
> >
> > but cannot find a permutation/combination of:
> >
> > 4> apply({attr, isPk}, [A1]).
> > false
> >
> > that can include "not" and work.
> >
> > Actually, I am trying to lists:filter( ...not isPk... SomeList).
> >
> > The simple is to define notIsPk(Attr), or perhaps lists:filter( fun(Attr) ->
> > .. end  but they are not an abstraction of "complement".
> >
> > Perhaps I should define a function complement(Fn) that returns a function
> > that is the complement of the function passed?
> >
> > Perhaps there is already an Erlang way to do this (I hope).
> >
> > Alex
>

Richard Carlsson (richardc)   (This space intentionally left blank.)
E-mail: Richard.Carlsson WWW: http://www.csd.uu.se/~richardc/
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham



Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Richard Carlsson-4
In reply to this post by Raimo Niskanen-3

(Sorry for the previous one - I hit "send" by mistake.)

A list comprehension is a bit easier to read, and executes faster
(becomes a local recursive function):

        [X || X <- SomeList, not attr:isPk(X)]

> lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)

Yes, this is the way to do it if you prefer to use lists:filter.
And there is not much of a point in defining a "complement" function
unless you really need it often in your program.

> > 4> apply({attr, isPk}, [A1]).

Please forget that this form of function call ever existed. Use

        F(...)

where F = fun ..., or

        M:F(...)

where M and F are atoms.

Only use apply(M, F, [...]) when you really, really cannot find an
alternative.

        /Richard



On Fri, 24 May 2002, Raimo Niskanen wrote:

> I cannot come up with anything better than:
>
> lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)
>
> or
>
> complement(Fun) -> fun (Attr) -> not Fun(Attr) end.
>
> lists:filter(complement({attr, isPk}), SomeList)
> % or
> lists:filter(complement(fun isPk/1), SomeList) % within module 'attr'
> % or
> lists:filter(complement(fun(Attr) -> attr:isPk(Attr) end, SomeList) %
> Very pointless with the complement() function
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
>
>
>
>
> Alex Peake wrote:
> >
> > Can someone help me with this (no doubt simple) problem.
> >
> > I try:
> >
> > 1> attr:isPk(Attr).
> > false
> >
> > 2> not attr:isPk(Attr).
> > true
> >
> > 3> not(attr:isPk(Attr)).
> > true
> >
> > but cannot find a permutation/combination of:
> >
> > 4> apply({attr, isPk}, [A1]).
> > false
> >
> > that can include "not" and work.
> >
> > Actually, I am trying to lists:filter( ...not isPk... SomeList).
> >
> > The simple is to define notIsPk(Attr), or perhaps lists:filter( fun(Attr) ->
> > .. end  but they are not an abstraction of "complement".
> >
> > Perhaps I should define a function complement(Fn) that returns a function
> > that is the complement of the function passed?
> >
> > Perhaps there is already an Erlang way to do this (I hope).
> >
> > Alex
>

Richard Carlsson (richardc)   (This space intentionally left blank.)
E-mail: Richard.Carlsson WWW: http://www.csd.uu.se/~richardc/
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham



Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Alex Peake
In reply to this post by Raimo Niskanen-3
Richard,

Thanks you for the list comprehension suggestion. I had not thought of that
(it does not exist in the Lisp world where I come from).

I do use complement a lot, so I think, for me, it is worth the definition --
but perhaps if I re-examine my approach, list comprehensions may obviate the
need.

As for
4> apply({attr, isPk}, [A1]).

as I mentioned, I was just testing for eventual use in filter, and as far as
I know, in filter (and map, etc.) you *must* use the form {attr, isPk}. I
always get errors when I try attr:isPk within map, filter etc.

150> lists:map(attr:varName, rel:attrList(rel:findNamed(ng,"Member"))).
** exited: {{badexpr,':'},[{erl_eval,expr,3}]} **


Thanks,

Alex



> -----Original Message-----
> From: owner-erlang-questions
> [mailto:owner-erlang-questions]On Behalf Of Raimo Niskanen
> Sent: Friday, May 24, 2002 12:18 AM
> To: erlang-questions
> Subject: Re: Higher Order Function Question
>
>
> I cannot come up with anything better than:
>
> lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)
>
> or
>
> complement(Fun) -> fun (Attr) -> not Fun(Attr) end.
>
> lists:filter(complement({attr, isPk}), SomeList)
> % or
> lists:filter(complement(fun isPk/1), SomeList) % within
> module 'attr'
> % or
> lists:filter(complement(fun(Attr) -> attr:isPk(Attr) end,
> SomeList) %
> Very pointless with the complement() function
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
>
>
>
>
> Alex Peake wrote:
> >
> > Can someone help me with this (no doubt simple) problem.
> >
> > I try:
> >
> > 1> attr:isPk(Attr).
> > false
> >
> > 2> not attr:isPk(Attr).
> > true
> >
> > 3> not(attr:isPk(Attr)).
> > true
> >
> > but cannot find a permutation/combination of:
> >
> > 4> apply({attr, isPk}, [A1]).
> > false
> >
> > that can include "not" and work.
> >
> > Actually, I am trying to lists:filter( ...not isPk... SomeList).
> >
> > The simple is to define notIsPk(Attr), or perhaps lists:filter(
> fun(Attr) ->
> > .. end  but they are not an abstraction of "complement".
> >
> > Perhaps I should define a function complement(Fn) that returns
> a function
> > that is the complement of the function passed?
> >
> > Perhaps there is already an Erlang way to do this (I hope).
> >
> > Alex
>




Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Alex Peake
In reply to this post by Richard Carlsson-4
Richard,

Thanks you for the list comprehension suggestion. I had not thought of that
(it does not exist in the Lisp world where I come from).

I do use complement a lot, so I think, for me, it is worth the definition --
but perhaps if I re-examine my approach, list comprehensions may obviate the
need.

As for
4> apply({attr, isPk}, [A1]).

as I mentioned, I was just testing for eventual use in filter, and as far as
I know, in filter (and map, etc.) you *must* use the form {attr, isPk}. I
always get errors when I try attr:isPk within map, filter etc.

150> lists:map(attr:varName, rel:attrList(rel:findNamed(ng,"Member"))).
** exited: {{badexpr,':'},[{erl_eval,expr,3}]} **


Thanks,

Alex



> -----Original Message-----
> From: owner-erlang-questions
> [mailto:owner-erlang-questions]On Behalf Of Richard Carlsson
> Sent: Friday, May 24, 2002 2:23 AM
> To: raimo.niskanen
> Cc: erlang-questions
> Subject: Re: Higher Order Function Question
>
>
>
> (Sorry for the previous one - I hit "send" by mistake.)
>
> A list comprehension is a bit easier to read, and executes faster
> (becomes a local recursive function):
>
> [X || X <- SomeList, not attr:isPk(X)]
>
> > lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)
>
> Yes, this is the way to do it if you prefer to use lists:filter.
> And there is not much of a point in defining a "complement" function
> unless you really need it often in your program.
>
> > > 4> apply({attr, isPk}, [A1]).
>
> Please forget that this form of function call ever existed. Use
>
> F(...)
>
> where F = fun ..., or
>
> M:F(...)
>
> where M and F are atoms.
>
> Only use apply(M, F, [...]) when you really, really cannot find an
> alternative.
>
> /Richard
>
>
>
> On Fri, 24 May 2002, Raimo Niskanen wrote:
>
> > I cannot come up with anything better than:
> >
> > lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)
> >
> > or
> >
> > complement(Fun) -> fun (Attr) -> not Fun(Attr) end.
> >
> > lists:filter(complement({attr, isPk}), SomeList)
> > % or
> > lists:filter(complement(fun isPk/1), SomeList) % within
> module 'attr'
> > % or
> > lists:filter(complement(fun(Attr) -> attr:isPk(Attr) end,
> SomeList) %
> > Very pointless with the complement() function
> >
> > / Raimo Niskanen, Erlang/OTP, Ericsson AB
> >
> >
> >
> >
> > Alex Peake wrote:
> > >
> > > Can someone help me with this (no doubt simple) problem.
> > >
> > > I try:
> > >
> > > 1> attr:isPk(Attr).
> > > false
> > >
> > > 2> not attr:isPk(Attr).
> > > true
> > >
> > > 3> not(attr:isPk(Attr)).
> > > true
> > >
> > > but cannot find a permutation/combination of:
> > >
> > > 4> apply({attr, isPk}, [A1]).
> > > false
> > >
> > > that can include "not" and work.
> > >
> > > Actually, I am trying to lists:filter( ...not isPk... SomeList).
> > >
> > > The simple is to define notIsPk(Attr), or perhaps
> lists:filter( fun(Attr) ->
> > > .. end  but they are not an abstraction of "complement".
> > >
> > > Perhaps I should define a function complement(Fn) that
> returns a function
> > > that is the complement of the function passed?
> > >
> > > Perhaps there is already an Erlang way to do this (I hope).
> > >
> > > Alex
> >
>
> Richard Carlsson (richardc)   (This space intentionally
> left blank.)
> E-mail: Richard.Carlsson WWW: http://www.csd.uu.se/~richardc/
>  "Having users is like optimization: the wise course is to delay it."
>    -- Paul Graham
>
>




Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Fredrik Linder-2
In reply to this post by Alex Peake
> As for
> 4> apply({attr, isPk}, [A1]).
>
> as I mentioned, I was just testing for eventual use in filter, and as far
as
> I know, in filter (and map, etc.) you *must* use the form {attr, isPk}. I
> always get errors when I try attr:isPk within map, filter etc.

You *must* supply a fun, but there are three alternatives to define one, the
one you chose I guess is considered to be the least elegant.

Fun = {M, F}
Fun = fun M:F/A, where A = arity (requires that M:F/A is a named existing
function)
Fun = fun(Arg) -> M:F(Arg) end

The apply call above can even be written:

{attr, isPk}(A1)     %% !!!
fun(X) -> attr:isPk(X) end(A1)

or simply

attr:isPk(A1)  ;-)

/Fredrik