I am now doing it like this in a route:
query = %{ type: params["type"] }
query = if !!params["language"], do: Map.put(query, :language, params["language"]), else: query
query = if !!params["id"], do: Map.put(query, :language, params["id"]), else: query
I wonder, could we afford a function to decide whether to put the key?(I do mention this as a proposal if there is no best practice.)
RIP, Joe.
--
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/11ef9a1a-1bbd-4ce3-93ff-3ad16466cb0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4Lgi29wSQ0SXEoADsnEO%2BzSdojY9X9ZjJY7SMsRd9AjHA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/4052719e-1bda-4d53-96c9-26f0e385a599%40Spark.
For more options, visit https://groups.google.com/d/optout.
query = %MyRequest{
language: params["language"] || "english",
id: params["id"] || session.id
}
shape = case params["shape"] do "square" ->
%Square{position: params["position"], width: params["width"], height: params["height"]} "circle" ->
%Circle{position: params["position"], radius: params["radius"]}
_ -> raise ArgumentError, "unknown kind of shape provided"
end
def normalize_keys(into \\ %{},
input, accepted_keys) do
string_keys = MapSet.new(accepted_keys, &to_string/1)
Enum.reduce(input, into, fn {string_key, val}, acc ->
if string_key in string_keys do
put_in(acc, [:"#{string_key}"], val)
else
acc
end
end)
end
iex> normalize_keys(%{"language" => "english", "id" => 33, "disregard_me" => 10}, [:id, :language])
%{id: 33, language: "english"}
In a case like this, I usually prefer to flip the task around and reduce over the params:Enum.reduce(params, %{type: nil}, fn{"language", language}, acc -> Map.put(acc, :language, language){"id", id}, acc -> Map.put(acc, :id, id){"type", type}, acc -> Map.put(acc, :type, type)_, acc -> accend)It works nice and doesn't require any conditionals. I saw the double negation - if that's important for the logic, it could be achieved by adding in `when language not in [nil, false]`, which could be further reduced to a nice `when truthy(language)` defguard macro.
Michał.
On 22 Apr 2019, 10:26 +0200, 胡永浩 <christo...@gmail.com>, wrote:
--I am now doing it like this in a route:
query = %{ type: params["type"] } query = if !!params["language"], do: Map.put(query, :language, params["language"]), else: query query = if !!params["id"], do: Map.put(query, :language, params["id"]), else: query
I wonder, could we afford a function to decide whether to put the key?(I do mention this as a proposal if there is no best practice.)
RIP, Joe.
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.