OK, sorry for the duplicated proposal. My fault! 😬 > On 22 Sep 2017, at 11:58, Kelvin Raffael Stinghen <[email protected]> > wrote: > > I really liked the lib https://github.com/meyercm/shorter_maps. It makes the > maps creation and matching a lot more DRY. > > Some examples for those who don't want to open the repo: > > iex> import ShorterMaps > ...> name = "Chris" > ...> id = 6 > ...> ~M{name, id} > %{name: "Chris", id: 6} > > # It's ok to mix in other expressions: > ...> ~M{name, id: id + 200} > %{name: "Chris", id: 206} > > # or even nest the sigil (note the change in delimiters to paren): > ...> ~M{name, id, extra_copy: ~M(name, id)} > %{name: "Chris", id: 6, extra_copy: %{name: "Chris", id: 6}} > > # We can use String keys: > ...> ~m{name, id} > %{"name" => "Chris", "id" => 6} > > # And we can update existing maps: > ...> map_1 = %{name: "Bob", id: 9} > ...> ~M{map_1|name} > %{name: "Chris", id: 9} > > # Struct syntax is a little funky: > ...> defmodule MyStruct do > ...> defstruct [id: nil, name: :default] > ...> end > ...> ~M{%MyStruct id} > %MyStruct{id: 6, name: :default} > > # Structs can be updated too: > ...> initial_struct = %MyStruct{name: "Chris", id: :unknown} > ...> ~M{%MyStruct initial_struct|id} > %MyStruct{name: "Chris", id: 6} > > # Because the expansion happens at compile time, they can be used > __anywhere__: > > # in function heads: > ...> defmodule MyModule do > ...> def my_func(~M{name, _id}), do: {:id_present, name} > ...> def my_func(~M{name}), do: {:no_id, name} > ...> end > > # in pattern matches: > ...> ~M{age, model} = %{age: -30, model: "Delorean", manufacturer: "AMC"} > ...> age > -30 > > What do you think about incorporating it to the language? I would even > suggest to implement it without the use of sigils. Something like: > > ...> %{name, id} > %{name: "Chris", id: 6} > ...> %MyStruct{id} > %MyStruct{id: 6, name: :default} > > > What do you think? > > > -- > 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 [email protected] > <mailto:[email protected]>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/7d18ef9a-5ae9-42df-8003-58ad8f3b2abb%40googlegroups.com > > <https://groups.google.com/d/msgid/elixir-lang-core/7d18ef9a-5ae9-42df-8003-58ad8f3b2abb%40googlegroups.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>.
-- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/BC59A247-973A-4F93-ACD3-EBB941E98791%40gmail.com. For more options, visit https://groups.google.com/d/optout.
