list comprehension with match

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

list comprehension with match

Frans Schneider
Hi list,

I have a list of items that I want to extract data from _and_ check to
make sure they are all of the same type.
When I use

     Member_keys = [Key || #member{key = Key} <- Members].

all members that are not #member records are dropped silently, but I
want it to crash ! The match works as a filter.
Using:

     Member_keys = lists:foldl(fun(#member{key = Key}, Keys) ->
                       [Key | Keys]
                   end, [], Members),

or

     getkeys([#member{key = Key} | Tail]) ->
         [Key | getkeys(Tail)];
     getkeys([]) -> [].

does exactly what I am looking for.

According to [1], a list comprehension [Expr(E) || E <- List] is
basically translated to:

     'lc^0'([E|Tail], Expr) ->
         [Expr(E)|'lc^0'(Tail, Expr)];
     'lc^0'([], _Expr) -> [].

but that doesn't explain this behaviour.

Could somebody explain to me what is going on?


[1] http://erlang.org/doc/efficiency_guide/listHandling.html#id68285
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: list comprehension with match

Kostis Sagonas-2
On 02/10/2017 05:19 PM, Schneider wrote:

> Hi list,
>
> I have a list of items that I want to extract data from _and_ check to
> make sure they are all of the same type.
> When I use
>
>     Member_keys = [Key || #member{key = Key} <- Members].
>
> all members that are not #member records are dropped silently, but I
> want it to crash ! The match works as a filter.

Yes, that's how list comprehensions like the one you wrote work.

Most likely you want the following one:

   Member_keys = [fun(#member{key = Key}=M) -> Key end || M <- Members].

which _will_ crash, most likely not due to the fact that it is not tested ;)

Kostis
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: list comprehension with match

Kostis Sagonas-2
On 02/10/2017 05:26 PM, Kostis Sagonas wrote:
>
>
> Most likely you want the following one:
>
>   Member_keys = [fun(#member{key = Key}=M) -> Key end || M <- Members].
>
> which _will_ crash, most likely not due to the fact that it is not
> tested ;)

Wrote it too quicky...  Of course I meant the following one;

   Member_keys = [(fun(#member{key = Key}) -> Key end)(M) || M <- Members].

Kostis
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: list comprehension with match

Frans Schneider
yes, works and is rather nice if you looked at the explanation of what
list comprehensions are translated to beforehand,  but the simple form
would be so much more expressive.


On 02/10/2017 05:59 PM, Kostis Sagonas wrote:
> Member_keys = [(fun(#member{key = Key}) -> Key end)(M) || M <- Members].

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions