In our codebase, we have a helper called `pipe_if` which evaluates the 
block if the condition is met, so you can do `params |> pipe_if(foo == 
"bar", &Map.put(&1, :key, value))` otherwise it just passes through. 

W dniu poniedziałek, 8 czerwca 2020 16:19:44 UTC+2 użytkownik José Valim 
napisał:
>
> 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/1f884728-d69d-4a2d-9993-ca7da7a021eeo%40googlegroups.com.

Reply via email to