Thank you Zach. When I wrote the proposal I felt it was missing something still and I think you nailed it.
Passing two anonymous functions would help with the pipeline but it feels it would be detrimental to other cases. *José Valimhttps://dashbit.co/ <https://dashbit.co/>* On Fri, Dec 6, 2024 at 17:41 Zach Daniel <zachary.s.dan...@gmail.com> wrote: > Despite typically being a "put it in the standard library" guy, I don't > think that `then_if` actually composes as well as it looks like it does on > the tin due to the fact that `then` is often used in pipelines, where some > transformation has happened and you want to check a condition *on that > result*. For example: > > ```elixir > changeset > |> do_some_checking() > |> then_if(<is_valid>, &do_more/1) > ``` > > I think that `then` is kind of "already" the composition tool that we need > for expressive pipes. > > ```elixir > changeset > |> do_some_checking() > |> then(fn changeset -> > If changeset.valid do > do_more(changeset) > else > changeset > end > end) > ``` > > I can see an argument that it is very verbose, but its also about as > flexible as it can get. My suggestion would be to, if added, have `then_if` > take a function as its first argument. > > ```elixir > changeset > |> do_some_checking() > |> then_if(&(&1.valid?), &do_more/1) > ``` > > > On Dec 6, 2024, at 10:30 AM, Ben Wilson <benwilson...@gmail.com> wrote: > > Exploring what that looks concretely in this case: > > ``` > map > |> other_stuff > |> then_if(opts[:foo], &Map.put(&1, :key, value)) > ``` > > I like it! Conditional map insert helper functions are definitely > something we've written over and over again in our code bases and while > it's easy to do, I think in some cases this is cleaner looking than a > proliferation of `maybe_put_foo` functions. > > - Ben > > On Friday, December 6, 2024 at 9:59:40 AM UTC-5 José Valim wrote: > >> Hi Juan! >> >> My initial gut feeling is that this approach does not scale. What if you >> want to delete a key conditionally? Should we have delete_if? >> >> It feels a more general approach would be to introduce `then_if`: >> >> then_if(subject, condition?, function) >> >> Or similar. :) >> >> *José Valimhttps://dashbit.co/ <https://dashbit.co/>* >> >> >> On Fri, Dec 6, 2024 at 3:27 PM Juan Manuel Azambuja <ju...@mimiquate.com> >> wrote: >> >>> Hello, >>> >>> After working with Elixir for some time I have found myself repeating >>> some patterns when dealing with maps. >>> >>> One pattern I see repeated constantly in different apps developed by >>> myself or others is adding values to a map conditionally or returning the >>> map unchanged. This comes in different flavors: >>> >>> [image: Screenshot 2024-12-06 at 11.13.23 AM.png] >>> or >>> [image: Screenshot 2024-12-06 at 11.14.32 AM.png] >>> >>> When this pattern gets used enough in an app, it's normal to see it >>> abstracted in a MapUtils module that updates the map conditionally if a >>> condition is met or returns the map unchanged otherwise. >>> >>> My proposal is to include Map.put_if/4 which would abstract the >>> condition check and return the map unchanged if the condition is not met: >>> >>> [image: Screenshot 2024-12-06 at 11.17.21 AM.png] >>> >>> Enhancing the API by doing this will result in less code and more >>> readable solutions. >>> >>> Thanks for reading! >>> >>> -- >>> 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/ed7da716-b9f5-4f64-a77d-d32696326b9en%40googlegroups.com >>> <https://groups.google.com/d/msgid/elixir-lang-core/ed7da716-b9f5-4f64-a77d-d32696326b9en%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/e9e799a2-ad69-4791-bd9a-22bca327652fn%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/e9e799a2-ad69-4791-bd9a-22bca327652fn%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/61753088-63E3-4DA0-8CEF-925149D789C6%40gmail.com > <https://groups.google.com/d/msgid/elixir-lang-core/61753088-63E3-4DA0-8CEF-925149D789C6%40gmail.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/CAGnRm4JSE7vhfHukf2EZ6bmi4%3DNrfX28q3%2BKpQGZMgFoCM%3D%2BWg%40mail.gmail.com.