def some_fun(s) do
MyStruct{
foo: foo,
bar: bar,
baz: baz
} = s
map = %{a: "hello", b: 42}
map =
if foo do
Map.put(map, :foo, foo)
else
map
end
map =
if bar do
Map.put(map, :bar, bar)
else
map
end
if bar do
Map.put(map, :baz, baz)
else
map
end
end
def some_fun(s) do
MyStruct{
foo: foo,
bar: bar,
baz: baz
} = s
%{a: "hello", b: 42}
|> Map.put(:foo, foo)
|> Map.put(:bar, bar)
|> Map.put(:baz, baz)
|> Enum.filter(fn {_k, v} -> v end)
|> Enum.into(%{})
end
def some_fun(s) do
MyStruct{
foo: foo,
bar: bar,
baz: baz
} = s
%{a: "hello", b: 42}
|> maybe_put(:foo, foo)
|> maybe_put(:bar, bar)
|> maybe_put(:baz, baz)
end
defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)
def some_fun(s) do
MyStruct{
foo: foo,
bar: bar,
baz: baz
} = s
%{a: "hello", b: 42}
|> Map.put_if(:foo, foo, foo != nil)
|>
Map.put_if
(:bar, bar, bar
!= nil
)
|>
Map.put_if
(:baz, baz, baz
!= nil
)
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/f513b901-fd93-4f28-88a2-f496a3c84a21o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K_QVpD-eRG6JUOMYV5pVTLchpTgbO0fqN6pPCmuSL4NA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CANmTJ6thcegKJjb7aKPhtQoqfo5%3DCgJo2-KnORRg8B2UcEB%3D-A%40mail.gmail.com.
We have something similar in our code base we called the Maybe.Pipe: ~>
On 5 Jun 2020, at 14:41, Piotr Szmielew <p.sz...@ava.waw.pl> wrote:
maybe it should be called run_if? so, something like this:defmacro run_if(piped_value, condition, fun) do
quote do
if unquote(condition) do
unquote(fun).(unquote(piped_value))
else
unquote(piped_value)
end
end
endand then used as:%{a: 1, b: 2}
|> X.run_if(1 == 2, &Map.put(&1, :foo, "foo"))
|> X.run_if(1 == 1, &Map.put(&1, :foo, "bar"))# => %{a: 1, b: 2, foo: "bar"}
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/f513b901-fd93-4f28-88a2-f496a3c84a21o%40googlegroups.com.
--
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-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K_QVpD-eRG6JUOMYV5pVTLchpTgbO0fqN6pPCmuSL4NA%40mail.gmail.com.
--
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-l...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/45a9ec20-f997-4fdf-a4c0-ea88388a8fabo%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/4F9BCFD1-91CB-4A45-B6C2-BC1E534889DF%40channelviewestates.co.uk.
def update_nil_values(map, new_values, into \\ %{}) do
Enum.reduce(map, into
, fn
{key, nil}, acc -> Map.put(acc, key, Map.get(new_values, key))
{key, value}, acc -> Map.put(acc, key, value)
end)
end
%MyStruct{a: nil, b: 10, c: nil}
|> update_nil_values(%{a: 20}, %MyStruct{})
|> other_stuff()
|> update_nil_values(%{c: 50}, %MyStruct{})
Enum.reduce() does not compose as well when you have conditional Map.put spread over multiple private functions.
On Fri, Jun 5, 2020 at 5:20 PM Adam Lancaster <ad...@channelviewestates.co.uk> wrote:
Thinking about it, can you solve the original problem with reduce?```elixirnew_values = %{foo: 10,bar: 20}Enum.reduce(map, %{}, fn{key, nil}, acc -> Map.put(acc, key, Map.get(new_values, key)){key, value}, acc -> Map.put(acc, key, value)end)```Best
Adam
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/45a9ec20-f997-4fdf-a4c0-ea88388a8fabo%40googlegroups.com.
--
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-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/4F9BCFD1-91CB-4A45-B6C2-BC1E534889DF%40channelviewestates.co.uk.
--
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-co...@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.
|> run_if(x == 1, some_fun)
|> maybe_run(x == 1, some_fun)
--
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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/12633886-f5ce-4b9f-b27c-b0871b472554o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAFXvW-6MeEPPFKGG6%2B%3DuJryEESik3oWNBEGZ8E%2BYwjkUvhtVkw%40mail.gmail.com.
--
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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/f1e65671-48c9-47cb-9d9f-65a695e41e8co%40googlegroups.com.
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 <esurk...@gmail.com> 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 = nily = 11z = nilmap|> 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 elixir-l...@googlegroups.com.
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 <esurk...@gmail.com> 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 = nily = 11z = nilmap|> 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 elixir-l...@googlegroups.com.
--
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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/c9edf4de-ed6d-4d77-b7a2-1e67a113e4f1n%40googlegroups.com.
```
transform_if(x, condition, fun)
```
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAAWU5L5r3x9MULDDFdHKOaNDeu0n5Ly5dCS4hbHXEwa9ozh3MQ%40mail.gmail.com.