Ted

Ted

'mix format' and grouping maps by an atom

I’m trying to figure out the idiomatic way to group a list of maps by an atom because mix format gives me an interesting result, depending on what I try.

Here’s my input and I’d like to group by :species.

iex(7)> characters = [                                   
...(7)>   %{species: :human, name: "Calvin"},
...(7)>   %{species: :human, name: "Rosalyn"},       
...(7)>   %{species: :feline, name: "Hobbes"}       
...(7)> ]

My first approach was to be explicit by using Map.get/3:

iex(8)> characters |> Enum.group_by(&Map.get(&1, :species))
%{
  feline: [%{name: "Hobbes", species: :feline}],
  human: [
    %{name: "Calvin", species: :human},
    %{name: "Rosalyn", species: :human}
  ]
}

That looks pretty good.

And then I tried it with a combination of capturing and map.key syntax*, getting the same result. :sunglasses:

iex(9)> characters |> Enum.group_by(&(&1.species))

Now for the interesting part: when the latter example is subjected to mix format the result is:

characters |> Enum.group_by(& &1.species)

To my novice eyes, that (& &1.species) part looks really weird and now I’m thinking I’m barking up the wrong tree. :thinking:

I’d appreciate any thoughts. Thanks!


Extra stuff:

I found this (really cool) reply by Jose, but in this case the capture involves a tuple and mix format doesn’t change it:

(* I understand that map.key will raise when Map.get/3 will nil, and therefore they’re not exactly the same.)

Most Liked

peerreynders

peerreynders

It’s not. The macro beside the source link is misleading - the source it links to is a macro but that isn’t what helps to implement the described behaviour:

defmacro unquote(:&)(expr), do: error!([expr])

That would generate an error if it evaluated during compilation.

Kernel.SpecialForms:

Special forms are the basic building blocks of Elixir, and therefore cannot be overridden by the developer.

So implementing Kernel.SpecialForms.&/1 is built into the compiler.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Well, fundamentally it’s syntatictally a macro, which is what’s relevant here. It follows the same syntax calling rules as other macros in that the formatter can choose to elide parens by default, as it does with if, def, and other macros.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Enum.group_by(&(&1.species)) is exactly equivalent to Enum.group_by(& &1.species). The parens for & are always optional. The formatter removes them because they’re unnecessary. It doesn’t remove the {} in Jose’s example because those are part of the actual datastructure definition.

Ted

Ted

Thanks for clarifying, Ben.

(And I’m really looking forward to digging into your book once I get more Elixir fundamentals down. I’ve been writing a fair number of RESTful APIs in Java lately and, well… seeing something new will be very cool. :wink:)

I also took a look at the doc for Kernel.&/1 and that helped solidify the fact that it’s just another macro and nothing magical is going on with the invocation.

So, what I gather is that Enum.group_by(& &1.species) won’t look so weird to me as I continue to develop my Elixir eyes and this is indeed idiomatic.

Ted

Ted

Thanks for clarifying that it is indeed a special form; I was a little confused by the source that I saw.

Where Next?

Popular in Chat/Questions Top

pietrofxq
I’ve bought the following books: Programming Elixir 1.6 Programming Phoenix 1.4 Programming Ecto Functional Web Development with Elixir...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
LegitStack
I’m not a hugely experienced programmer, just a few years. So I’m looking for resources to learn about a topic but I can’t seem to find m...
New
mchean
I was wondering if anyone is using some learning tools that they can recommend? I’ve been looking at two specifically so would also be ...
New
RKC07
I’m new to elixir. I did some coding in python and C. I want to learn elixir for starting my career in web development. I need suggestion...
New
ggwc82
Looking to get started with FP and Elixir coming mainly from an OOP Rails and PHP background. My first question is, whats the best cours...
New
zervis
Hello, I’m about to dive into web development. I was thinking about Laravel or Ruby on Rails, but then I found Phoenix. Do you recommen...
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
SavagePixie
I was wondering if there are any beginner-friendly, exercise-based resources for learning Elixir out there. I’m looking for something lik...
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement