[erlang-questions] List comprehension question

Darach Ennis darach@REDACTED
Thu Jan 16 18:16:13 CET 2014


Similarly, bit comprehensions can benefit also, but not as useful as
flexibly as the list comprehension case:

[ X + Y || << X:8, Y:8 >>  <= << 1:8, 2:8, 3:8, 4:8, 0:1 >> ]. %% discards
final byte
[ X + Y || << X:8, Y:8, Z:8 >>  <= << 1:8, 2:8, 0:8, 3:8, 4:8, 0:8, 1:8 >>
]. %% discards every 3rd byte and trailing
[ X + Y || << 1:8, Y:8, Z:8 >>  <= << 1:8, 2:8, 0:8, 3:8, 4:8, 0:8, 1:8 >>
]. %% discards every 3rd byte and trailing
[ X + Y || << X:8, Y:8, Z:8 >>  <= << 1:8, 2:8, 0:8, 3:8, 4:8, 0:8, 1:8 >>,
X =:= 1 ]. %% discards pair except matching structure with 1st byte =:= 1

Cheers,

Darach.


On Thu, Jan 16, 2014 at 4:58 PM, <amindfv@REDACTED> wrote:

> Or! You can write:
>
> [ A || A = {a, _} <- Foos() ]
>
> Both of these examples work in Haskell also.
>
> Tom
>
>
> El Jan 16, 2014, a las 11:55, amindfv@REDACTED escribió:
>
> > This is intentional. I use a pattern like this a lot:
> >
> > -type foo :: {'a', string()} | {'b', string()}.
> >
> > Foos() -> [{a, "hello"}, {b, "goodbye"}].
> >
> > As() -> [ S || {a, S} <- Foos() ]. % ["hello"]
> >
> > Tom
> >
> >
> > El Jan 16, 2014, a las 11:39, Rich Neswold <rich.neswold@REDACTED>
> escribió:
> >
> >> Hello,
> >>
> >> This morning I became aware of a (powerful) feature of list
> >> comprehensions. Take the following example:
> >>
> >>   [ X + Y || {X, Y} <- L].
> >>
> >> If we set L to [{1,2}, ok, {1,3}], we get the result [3,4] instead of
> >> a pattern match exception (as I was expecting.) This means that list
> >> comprehensions give you a "free" lists:filter/2 in the generator
> >> expressions!
> >>
> >> I've looked through the OTP documentation, quite a few Stack Overflow
> >> questions, and several list comprehension tutorials and none of them
> >> explicitly state that generator expressions filter elements that don't
> >> match their pattern. The web pages emphasize generators and guards are
> >> like combinations of lists:map and lists:filter, which isn't exactly
> >> correct. For instance, the above example is not the same as:
> >>
> >>   lists:map(fun ({X, Y}) -> X + Y end, L).
> >>
> >> but is more equivalent to:
> >>
> >>   lists:foldr(fun ({X, Y}, Acc) -> [X + Y | Acc];
> >>                   (_, Acc) -> Acc
> >>               end, [], L).
> >>
> >> Is this an expected, but undocumented, feature of list comprehensions?
> >> Or am I in "undefined behavior" territory?
> >>
> >> --
> >> Rich
> >> _______________________________________________
> >> erlang-questions mailing list
> >> erlang-questions@REDACTED
> >> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140116/2dfca855/attachment.htm>


More information about the erlang-questions mailing list