Hi!
Before I put any actual effort into this, would there be interest in a pipe macro that works like |> but ignores nil values? Think Groovy's ?. [1] or Clojure's some-> macro [2].
Here's a quick implementation with the proposed operator (?>):
defmacro left ?> right do
[{h, _}|t] = Macro.unpipe({:|>, [], [left, right]})
case h do
nil -> nil
_ -> :lists.foldl fn {x, pos}, acc -> Macro.pipe(acc, x, pos) end, h, t
end
end
Example:
iex(21)> inc = &(&1+1)
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(22)> nil |> inc.()
** (ArithmeticError) bad argument in arithmetic expression
:erlang.+(nil, 1)
iex(22)> nil ?> inc.()
nil
Cheers,
Michael
References: