Delete the 1st pair in a map

Also thanks to this thread TIL that in Elixir small maps (<= 32 elements) automatically sort the keys by Erlang term order, while large maps (> 32 elements) do not!

In Elixir, the order of the keys is persisted if You have less than about 135 keys. I do not remember the exact number
 nor where I learned that :slight_smile:

:face_with_monocle:

1 Like

Oh, thanks :slight_smile:

intereseting fact, but 


citation needed.

Hmm :sweat_smile: I only “learned” by playing around in the repl. High probability that I’m wrong :slightly_smiling_face:

1 Like

I checked, as I do remember it was my first question on the Elixir forum.

32 is the number I had in mind at that time, I guess I inflated the number a little bit :slight_smile:

No, it’s me :slight_smile:

2 Likes

Final and conclusive solution; always deleting the 1st pair:

new_map = 
  %{“name” => “foo”, “address”=> “bar”}
  |> Enum.into(%{}, fn
    {“name”, v} -> {“1st”, v}
    other -> other
  end)
  |> Map.drop([“1st”])

ps. try this at home but never at work.

2 Likes

At the moment of writing it’s true but one should not rely on it. So we may as well forget it as it’s an implementation detail that might change.

Still a fun Trivia fact to know though
for as long as it lasts :slight_smile:

3 Likes

If it wasn’t piping a list into a Map function this would be the definitive way to drop the 1st pair of a map :joy:

If performance is a concern and we don’t want to create a full map from scratch, maybe we could do something like (disclaimer: I didn’t benchmark):

{first_key, _, _} = map |> :maps.iterator() |> :maps.next()
Map.delete(map, first_key)

(“first_key” in the sense of whatever keys comes out first :slightly_smiling_face:)

1 Like

If you want the internet to respont, make an error - Elvis Presley :sweat_smile:

ps. quoting the brilliant and definitive answer, rewriting it using Enum.map/2 so you can say it’s wrong
 it is Lousy. Jea Lousy. How would you feel if you liked a post and the author edited it and blamed you for something you did not?

Tag: satire

4 Likes

*respond

3 Likes

Haha!
Perhaps Elvis only left the building 'cos it had rubbish internet?

1 Like

The proof was in the statement indeed. Suprised it took 2 hours :joy:

2 Likes

@sodapopcan
They are duplicated entries from database so they’re pretty much the same except some properties are different so I can delete them based on logic
If some “that” properties are the same then

I wanted to extract the first out, then delete the rest. I should have meant that in the beginning, sorry for the confusion guys. I wished I could have edited the post

1 Like

Aha! This makes so much sense. Perhaps in your SQL query you could limit: 1 ?

1 Like

-duplicate suggestion-

Limiting in the database is much better as there is no need to send all records over the wire just to flush all but 1 of them.

Ecto returns lists of maps or structs so the take first element does not seem to be an issue. hd/1 would do.

Now I think I get the whole picture. The ’take the first’ is less of an issue; the ‘how to create the correct query’ is.

1 Like

As the application was progressing, it allowed users sign up with different methods as well as they can also be added manually through Phoenix UI database management app created by template
Now that’s changed to signing up just by one method which is by domestic register only and people want to put a unique constraint on it

1 Like

With every answer we get spoon fed another bite. Sooo curious about what the final question and answer will be. Stay tuned! :slight_smile: