Hi,
I'd like to propose a syntax sugar for maps, specially for pattern matching 
and construction.

# Map construction
language = :elixir
%{ language }
=> %{language: :elixir}


# Map pattern match
map = %{feature: :pattern_match}
%{ feature } = map
=> %{feature: :pattern_match}
feature
=> :pattern_match

case %{ key: "some value" } do
%{key} ->
  key
  _ ->
  nil
end
=> "some value"


# Map in function args
defmodule Foo do
def bar(%{ key }),
  do: key
end

Foo.bar(%{ key: "some value" })
=> "some value"


Of course this would be just a syntax sugar. It won't compromise program's 
performance.

The advantage of that is mainly when pattern matching function, but also 
*case* and *with* statements, on maps with some considerable amount of keys.


with %{ key1 } <- %{key1: "value1"},
{:ok, %{key2, key3, key4, key5}} <-
  {:ok, %{key2: "value2", key3: "value3", key4: "value4", key5: "value5"}} 
do
{key1, key2, key3, key4, key5}
end
=> {"value1", "value2", "value3", "value4", "value5"}


I'd like to know if more people would be interested on that too.

-- 
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-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/7efbf751-56d7-401d-8192-5796eff26048n%40googlegroups.com.

Reply via email to