thousandsofthem
Writing idiomatic Elixir code
Trying to write good elixir code.
Input:
data = %{"a" => 0, "b" => 2, "c" => 0, "evilkey" => 666}
for specific keys i need to write some specific output if condition is true
Ruby version:
out = []
out << "text1" if data["a"] > 0
out << "text2" if data["b"] > 0
out << "text3" if data["c"] > 0
out
Ugly elixir version:
out = []
out = if data["a"] > 0 do
out ++ ["text1"]
else
out
end
...
...
out
Good elixir version: ???
Any thoughts here?
Marked As Solved
thousandsofthem
Thanks!
The solution with filters and map look about right.
Re: IO.iodata_to_binary thing - i can’t just put things together this way, but overall it looks good, just need to add |> Enum.filter(fn(x) -> !is_nil(x) end), i.e.
[
(if data["a"] > 0, do: "text1"),
(if data["b"] > 0, do: "text2"),
(if data["c"] > 0, do: "text3")
] |> Enum.filter(fn(x) -> !is_nil(x) end)
Also Liked
michalmuskala
You can do that by using if as an expression and assembling list of values finally concatenating them together:
IO.iodata_to_binary([
if(data["a"] > 0, do: "text1", else: ""),
if(data["b"] > 0, do: "text2", else: ""),
if(data["c"] > 0, do: "text3", else: "")
])
You could even extract the if to a separate function, if the pattern if very repeating:
defp if_positive(value, text), do: if(value > 0, do: text, else: "")
IO.iodata_to_binary([
if_positive(data["a"], "text1"),
if_positive(data["b"], "text2"),
if_positive(data["c"], "text3")
])
benwilson512
data
|> Enum.filter(&match?({_, v} when v > 0, &1))
|> Enum.map(fn
{"a", _} -> "text1"
{"b", _} -> "text2"
{"c", _} -> "text3"
end)
taiansu
A more functional apporach
output_texts = %{
"a" => "text1",
"b" => "text2",
"c" => "text3",
}
out = data
|> Enum.filter(fn {_, v} -> v > 0 end)
|> Enum.filter(fn {k, _} -> Enum.member(Map.keys(output_texts), k) end)
|> Enum.map(fn {k, _} -> output_texts[k] end)
|> Enum.join()
Last Post!
rvirding
Yes, for many structures you definitely need to know whether you are folding left or right. That is one reason why calling them foldl and foldr is better than just reduce. The :maps module just has fold as there is no defined ordering.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









