fireproofsocks

fireproofsocks

I’m sure this has been asked elsewhere (but forgive me I could not find something that addressed this specifically)…

Why do Elixir functions usually take keyword lists as options? Why not maps? Keyword lists are problematic especially if you’d like to pattern match on a map key to route to different functions, for example. Is there a historical or performance-based reason why keywords are used over maps?

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

Maybe not an exhaustive list, but some reasons:

  • Erlang historically uses proplists. Not 100% the same but similar.
  • The keyword syntax does have some nice sugar
  • Elixir used to have HashDict instead of maps, so keyword lists might have been supported before maps were
hauleth

hauleth

Elixir keyword lists are strict subset of proplists.

This could be done for maps as well TBH.


My reasons:

  • Maps are way younger than property lists/keyword lists
  • Keyword lists allow for duplicated keys which can be useful sometimes
christhekeele

christhekeele

This is worth highlighting, it gives you a CLI-repeated-flag-like interface for options.

10
Post #3
dimitarvp

dimitarvp

Joining the last two. I’d prefer maps any day for config but prioritising the last given config key is impossible with maps.

I do however do my best to only use maps for config in my internal projects.

Phillipp

Phillipp

I find using keyword lists with Keyword.get/3 very handy because you can easily define a default value. Might be somehow possible with Maps too, I guess.

NobbZ

NobbZ

Map.get/3

Though as others already have said, the big plus of KW is that it enables for having a key multiple times, and they are also ordered…

Try ecto with maps instead of KW… You won’t have much joy…

lucaong

lucaong

Yep, I think it’s a mix of historical and practical reasons (the latter including possibility of duplicated keys, and explicit ordering).

You are right that keyword lists are more inconvenient when using pattern matching. But then again, I think they make a good outside interface, and usually I validate and parse options into another data structure (like a map or struct) as soon as they get within the boundary of my module or library.

gregvaughn

gregvaughn

Keyword lists are very core to the Elixir syntax itself. For example

def the_answer do
  42
end

is syntax sugar for

def the_answer, do: 42

or even

def(the_answer, [do: 42])

which is basically 2 parameters passed to the def macro. The last one is a keyword list with a :do key in it.

This is why all your functions can take a keyword list without square braces as the last parameter – because the core language syntax uses that.

19
Post #8
al2o3cr

al2o3cr

Maps weren’t introduced until OTP 17.0 in 2014, and were experimental until at least 2015.

DaAnalyst

DaAnalyst

Personally, I like KW lists because of a) order, b) syntactic sugar.

foo( bar, a: 1, b: 2, c: 3) 

is so much nicer than:

foo( bar, %{ a: 1, b: 2, c: 2})

When I need accessing the options in a map-like fashion, I’m willing to pay the price of having the KW list converted to a map in my function.

Besides, maybe what you sometimes need is precisely a list-like pattern matching e.g.:

def foo( bar, [ a: 1 | [ b: 2 | _]] = opts)

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews