I like where this discussion is going! I didn't mention this in my first message, but one way I have solved this is by implementing a macro that looks very similar to Chris' first option. Personally, I like modifying `if` with `else:` (Chris' second option).
I will give it a spin, but I think it addresses the problem nicely and it doesn't look too weird; I know it does look a bit strange but I think it's just because it's something new and not because it's not well thought. El viernes, 6 de diciembre de 2024 a las 22:52:17 UTC-3, christ...@gmail.com escribió: > Created a branch here > <https://github.com/elixir-lang/elixir/compare/main...christhekeele:elixir:if-else-do-end> > > if folk wanna play with this form and see if it improves readability around > conditional re-assignment in their codebases: > https://github.com/elixir-lang/elixir/compare/main...christhekeele:elixir:if-else-do-end > > map = %{} > condition = true > map = if condition, else: map do > Map.put(map, :key, :value) > end > #=> %{key: :value} > > On Friday, December 6, 2024 at 7:02:48 PM UTC-6 sabi...@gmail.com wrote: > >> For reference, there has been a previous discussion about then_if: >> >> https://groups.google.com/g/elixir-lang-core/c/uM_M-DWh42A/m/6pNKZcLyAQAJ?pli=1 >> >> >> Le ven. 6 déc. 2024 à 23:59, José Valim <jose....@gmail.com> a écrit : >> >>> 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-co...@googlegroups.com. >>> >> To view this discussion visit >>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BLFJvKvdZX%3DnZEJj77sjmkXNm19KQz_vJr%2BAmKrecXPQ%40mail.gmail.com >>> >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BLFJvKvdZX%3DnZEJj77sjmkXNm19KQz_vJr%2BAmKrecXPQ%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/8124ef6b-724d-404b-808d-d21b250187cfn%40googlegroups.com.