Even the "map_reduce: ..." option I proposed a couple times, and it was not accepted, can be implemented today, although it is quite more verbose than simply piping the result:
# double each element and compute the sum at once {mapped, reduce} = for x <- collection, reduce: {[], 0} do {list, acc} -> {[x * 2 | list], acc + x} end {Enum.reverse(mapped), reduce} *José Valimhttps://dashbit.co/ <https://dashbit.co/>* On Sat, Nov 16, 2024 at 11:28 AM José Valim <jose.va...@gmail.com> wrote: > It does conflict in the same way. What does this mean: "into: %{}, > reverse: true"? Or "into: <<>>, reverse: true"? Do you reverse bytes or > graphemes? "reduce: ..., reverse: true" also only works if you reduce into > a list, which is rarely the case. > > The key aspect to keep in mind is that "into: ..." already gives options > to do all of this. You could have a "ReverseList" collectable, a "Joiner" > collectable, or "ReverseBytes". > > And "for" is already complex enough, I don't think we should add options > such as `then: ...` which is syntax sugar to an already existing construct. > > As far as I know, the constructs you cannot do today using either into or > reduce is zipping, or anything that aborts early (reduce_while or > take/drop), which would be a break in other programming languages. > > > *José Valimhttps://dashbit.co/ <https://dashbit.co/>* > > > On Sat, Nov 16, 2024 at 10:59 AM Daniel Kukula <daniel.k...@gmail.com> > wrote: > >> I have also one proposal to for loops: >> reverse: true >> for applies already the reverse and when I don't care about the order, or >> want to reverse the result anyway then it has to be reversed twice. >> It would be a nice addition that could add a small bit of performance in >> some cases. Also it should not conflict with existing features?. >> >> On Friday, November 15, 2024 at 3:16:04 PM UTC+1 cbla...@gmail.com wrote: >> >>> What I'm proposing, is only syntaxic sugar. >>> >>> I don't like piping comprehension's result. It looks less readable than >>> piping with regular Enum functions. >>> Le 15 nov. 2024 à 12:42 +0100, José Valim <jose....@gmail.com>, a écrit >>> : >>> >>> But then why not simply pipe to the result afterwards? This is all >>> possible today already: >>> >>> for(...) |> Enum.join(", ") >>> for(...) |> then(&Enum.join(&1, ", ")) >>> >>> for do >>> ... >>> end |> then(&Enum.join(&1, ", ")) >>> >>> >>> *José Valim* >>> https://dashbit.co/ >>> >>> >>> On Fri, Nov 15, 2024 at 11:13 AM Christian Blavier <cbla...@gmail.com> >>> wrote: >>> >>>> My point wasn’t about adding a join but a then option (like >>>> Kernel.then), which seems much more versatile. >>>> >>>> Whatever I was expecting a no :) >>>> Le 15 nov. 2024 à 11:10 +0100, José Valim <jose....@gmail.com>, a >>>> écrit : >>>> >>>> It is the same answer as before, I am afraid. The path to adding :join >>>> is also the path to adding :intersperse, and :sum, and :product, etc. We >>>> should have a general mechanism for expressing those, instead of individual >>>> options. >>>> >>>> And, as before, a joiner does not really work well with into and reduce. >>>> >>>> >>>> *José Valim* >>>> https://dashbit.co/ >>>> >>>> >>>> On Fri, Nov 15, 2024 at 11:06 AM Christian Blavier <cbla...@gmail.com> >>>> wrote: >>>> >>>>> Hey there, >>>>> >>>>> I'm still thinking about extending comprehension. What about a `then` >>>>> option? >>>>> >>>>> for i <- 1..3, do: i, then: &Enum.sum/1 >>>>> >>>>> for s <- ~w(a b c), do: s, then: &Enum.join(&1, ", ") >>>>> >>>>> (I know this can be achieved more efficiently with a reduce, but >>>>> sometimes concise syntax is a better choice) >>>>> >>>>> Le mardi 21 juin 2022 à 18:40:46 UTC+2, José Valim a écrit : >>>>> >>>>>> It should probably be done with using `into: joiner(",")`, where the >>>>>> joiner implements the collectable protocol. join itself probably doesn't >>>>>> make much sense because it doesn't play well with other options such as >>>>>> :into itself, or :reduce. >>>>>> >>>>>> On Tue, Jun 21, 2022 at 6:15 PM Christian Blavier <cbla...@gmail.com> >>>>>> wrote: >>>>>> >>>>>>> Do you think a join option would be a good idea for comprehensions? >>>>>>> Something like Enum.map_join/2 that could map and join lists in a >>>>>>> single pass. >>>>>>> book_ids = for book <- books, join: ",", do: book.id >>>>>>> >>>>>>> If it looks like a good idea, I will open a PR. >>>>>>> >>>>>>> Best, >>>>>>> >>>>>>> Christian >>>>>>> >>>>>>> -- >>>>>>> You received this message because you are subscribed to the Google >>>>>>> Groups "elixir-lang-core" group. >>>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>>> send an email to elixir-lang-co...@googlegroups.com. >>>>>>> To view this discussion on the web visit >>>>>>> https://groups.google.com/d/msgid/elixir-lang-core/f8b94dec-35a1-4b78-a14c-6913c7206fe3n%40googlegroups.com >>>>>>> <https://groups.google.com/d/msgid/elixir-lang-core/f8b94dec-35a1-4b78-a14c-6913c7206fe3n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>>>> . >>>>>>> >>>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "elixir-lang-core" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to elixir-lang-co...@googlegroups.com. >>>>> To view this discussion visit >>>>> https://groups.google.com/d/msgid/elixir-lang-core/736e38fd-e0dd-4489-b079-73c116b5ecb7n%40googlegroups.com >>>>> <https://groups.google.com/d/msgid/elixir-lang-core/736e38fd-e0dd-4489-b079-73c116b5ecb7n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>> . >>>>> >>>> -- >>>> You received this message because you are subscribed to a topic in the >>>> Google Groups "elixir-lang-core" group. >>>> To unsubscribe from this topic, visit >>>> https://groups.google.com/d/topic/elixir-lang-core/tF2Xw8nq-O0/unsubscribe >>>> . >>>> To unsubscribe from this group and all its topics, send an email to >>>> elixir-lang-co...@googlegroups.com. >>>> To view this discussion visit >>>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JEM%3DUD70e%2BpNXyKQqNxMB_gZnMbj%2By8ZH7tTr6rO5ahQ%40mail.gmail.com >>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JEM%3DUD70e%2BpNXyKQqNxMB_gZnMbj%2By8ZH7tTr6rO5ahQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "elixir-lang-core" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to elixir-lang-co...@googlegroups.com. >>>> To view this discussion visit >>>> https://groups.google.com/d/msgid/elixir-lang-core/96f52834-4980-4679-a2ac-b22afacf12cd%40Spark >>>> <https://groups.google.com/d/msgid/elixir-lang-core/96f52834-4980-4679-a2ac-b22afacf12cd%40Spark?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "elixir-lang-core" group. >>> To unsubscribe from this topic, visit >>> https://groups.google.com/d/topic/elixir-lang-core/tF2Xw8nq-O0/unsubscribe >>> . >>> To unsubscribe from this group and all its topics, send an email to >>> elixir-lang-co...@googlegroups.com. >>> >>> To view this discussion visit >>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4Jjq9rUfhDieAEXQMwyZ2en7D6KcEGtywjAzJ5CD5mJkA%40mail.gmail.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4Jjq9rUfhDieAEXQMwyZ2en7D6KcEGtywjAzJ5CD5mJkA%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "elixir-lang-core" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to elixir-lang-core+unsubscr...@googlegroups.com. >> To view this discussion visit >> https://groups.google.com/d/msgid/elixir-lang-core/793ca043-3d4c-42ea-abf2-e245f0bf4566n%40googlegroups.com >> <https://groups.google.com/d/msgid/elixir-lang-core/793ca043-3d4c-42ea-abf2-e245f0bf4566n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K1XbsrGNjwQPXLmBMTcABDE34EsgQx%2BKYWch9E5oWd4g%40mail.gmail.com.