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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/f1e65671-48c9-47cb-9d9f-65a695e41e8co%40googlegroups.com.

Reply via email to