Hello, I work with Riccardo so I frequently experience the same issue. I really like José and Piotr solutions, however I'd like to point out that they might introduce some clutter and in real world code they might cause long lines that are broken from the formatter, which can be ugly when using pipes. For this reason I still think that Map.update_if might be a good idea, anyway run_if already fixes the copy&paste helper function issue, which is already better than nothing.
Regards, Davide Bettio. Il giorno venerdì 5 giugno 2020 15:27:51 UTC+2, José Valim ha scritto: > This is an interesting proposal! Unfortunately if the value you want to > compute is expensive, then you need to fallback to the usual `if` approach. > This is also specific to maps. It may be worth considering a more general > purpose mechanism, for example: > > map > |> update_if(foo != nil, &Map.put(&1, :foo, foo)) > |> update_if(bar != nil, &Map.put(&1, :bar, normalize_bar(bar))) > > Being in Kernel also allows us to write it as a macro, which can be > further optimized. Not sure if update_if is the best name though. > Suggestions and comparisons to other languages are welcome. > > -- 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 on the web visit https://groups.google.com/d/msgid/elixir-lang-core/4ab01515-f870-493c-8544-a540badd63c8o%40googlegroups.com.