What about `Map.update_if` and `Keyword.update_if`? Benefits:

- It's scoped to the data structure that will in fact receive the update 
(or not)
- Closer to the original proposal
- Similar to existing functions
- Not misleading to broader concepts
- May be implemented on other modules by demand in the future

On Monday, June 8, 2020 at 10:19:44 AM UTC-4, José Valim wrote:
>
> Unfortunately a run_if that has both success and failure branches offers 
> no benefit to a "if" - so I would suggest to use that instead.
>
> Furthermore, I wouldn't expect a function called "run_if" to receive (or 
> return) the first argument if the condition evaluates to false. For 
> something named run or apply, I would simply expect to return "nil".
>
> That's why I proposed something along the lines of "update_if", because it 
> should be clear that the intent is to update/modify the first argument 
> somehow.
>
> On Mon, Jun 8, 2020 at 4:13 PM Marcus <[email protected] <javascript:>> 
> wrote:
>
>> I like the Map.put_if from the first post. I am not sure if 
>> run_if/update_if is a good idea because it adds to much "magic", and it 
>> could lead to the writing of unreadable code.
>> If we go with run_if, I think we should have a function for each branch 
>> (true/false). Something like:
>>
>> @spec run_if(data, cond, do_fun, else_fun \\ &identity/1) 
>>   when data: term(), 
>>        cond: boolean() | (term() -> boolean()), 
>>        do_fun: (term() -> term()), 
>>        else_fun: (term() -> term())
>> def run_if ...
>>
>> x = nil
>> y = 11
>> z = nil
>>
>> map 
>> |> run_if(x != nil, fn m -> Map.put(m, :x, x) end)
>> |> run_if(y != nil, fn m -> Map.put(m, :y, y) end)
>> |> run_if(fn m -> Map.has_key(m, :a) end, fn m -> Map.put(m, :b) end)
>> |> run_if(
>>      z != nil, 
>>      fn m -> Map.put(m, :z, z) end, 
>>      fn m -> Map.update(m, :missing, :z, fn x -> [:z|x] end)
>>    )
>>
>> -- 
>> 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 [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/elixir-lang-core/f1e65671-48c9-47cb-9d9f-65a695e41e8co%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/elixir-lang-core/f1e65671-48c9-47cb-9d9f-65a695e41e8co%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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/02bfeb72-8e11-4374-8608-ed8e2436a95eo%40googlegroups.com.

Reply via email to