[erlang-questions] list comprehension with match

Kostis Sagonas kostis@REDACTED
Fri Feb 10 17:26:44 CET 2017


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



More information about the erlang-questions mailing list