Re: [elixir-talk:13544] Re: Dealing with complicated nesting in API JSON requests in a functional way

68 views
Skip to first unread message

Peter Hamilton

unread,
Jul 27, 2016, 1:20:22 AM7/27/16
to elixir-lang-talk, si...@kindynow.com

This is a fairly straightforward (though a bit long) for comprehension:

for {room_id, room} <- data,
      {date, obj} <- room,
      stuff = obj["important_stuff"],
      {child_id, child} <- stuff,
      child_id != "id" do
  %{
      room_id: room_id,
      date: date,
      ...
   }
end


On Tue, Jul 26, 2016, 9:45 PM Sam Hains <sdha...@gmail.com> wrote:
That should read:  triple nested Enum.each ****

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/44849237-d460-4760-ad3d-4ced90c9f96c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pierre de Lacroix

unread,
Jul 29, 2016, 9:33:14 AM7/29/16
to elixir-l...@googlegroups.com
In a comprehension like this, what's the difference between pattern
matching with "<-" and "=" ?
> > <https://groups.google.com/d/msgid/elixir-lang-talk/44849237-d460-4760-ad3d-4ced90c9f96c%40googlegroups.com?utm_medium=email&utm_source=footer>
> > .

José Valim

unread,
Jul 29, 2016, 9:40:07 AM7/29/16
to elixir-l...@googlegroups.com
<- will traverse the collection on the right side matching each element with the pattern on the left side. = is the = we all know and love.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/20160729153309.7e557ad9%40ArchPierre2.

For more options, visit https://groups.google.com/d/optout.


--


José Valim
Skype: jv.ptec
Founder and Director of R&D

Pierre de Lacroix

unread,
Jul 30, 2016, 5:04:01 AM7/30/16
to elixir-l...@googlegroups.com
OK, because it's in a for. I've seen that both could be used in a with construct too, do they have the same meaning in this case ?
Envoyé de mon appareil Android avec K-9 Mail. Veuillez excuser ma brièveté.

José Valim

unread,
Jul 30, 2016, 5:26:10 AM7/30/16
to elixir-l...@googlegroups.com
In with <- matches the value on the right to the pattern on the left, only continuing if they match. The difference is that "for" traverses the collection on the right while "with" takes the value on the right at "face value". = has the same meaning on both expressions which is the same as in any other Elixir code. In other words, <- slightly changes its meaning based on the wrapping construct.



José Valim
Skype: jv.ptec
Founder and Director of R&D

Reply all
Reply to author
Forward
0 new messages