Fork me on GitHub
#other-languages
<
2022-10-20
>
grav19:10:35

What the devil is up with Elixir's tuples? They seem to be a bit like vectors in Clojure, but then again there's this magic syntax around 2-ples, like

iex> [name: "Dave", city: "Dallas", likes: "Programming"]
[{:name, "Dave"}, {:city, "Dallas"}, {:likes, "Programming"}]
(from Programming Elixir) Why not just a map? Is ordering somehow more important in idiomatic Elixir?

grav20:10:50

Later in Programming Elixir, it states: > In general, use keyword lists for things such as command line parameters and passing around options, and use maps when you want an associative array. Those use-cases seem very specific to warrent special syntax, and I'd argue that maps would be at least as suitable for passing around options. So I'm wondering if something's missing on the list?

russmatney20:10:07

my memory of elixir (it's been a few years) is that maps vs keyword lists have a lot of overlap, as you're sensing - for me it comes down to the ergonomics of the api/implementation you're writing. for example, sometimes it makes sense to pop/destructure some kwarg off the list and pass the rest of the list to downstream functions. tho you could achieve that with a map as well, it might not be as minimal to implement. one detail is that i think keyword lists can have duplicate keys - i.e. multiple entries for "paths" if you're handling some --path "/a" --path "/b" input args

👍 1
mauricio.szabo19:10:05

I also remember that Erlang didn't have maps before, so this "Keyword List" is the historical way of doing things where maps are the more recent version that almost nobody uses, or something :thinking_face:

💡 1
Cora (she/her)03:10:56

keyword lists are super cool, though

Cora (she/her)03:10:15

and yes, maps are quite recent

Cora (she/her)03:10:10

parsing query strings is another place that keyword lists shine

Cora (she/her)03:10:50

as in, a value can appear more than once in a query string and keyword lists preserve that

didibus03:10:13

I think it's an Erlangism, since Erlang has property lists, some old Lisps also have those, like Emacs and I think Common Lisp have property lists as well.

grav09:11:03

@U02N27RK69K So eg ?foo=42&foo=43 right? I guess it makes sense, but I think I'd still prefer to work with something like {:foo [42 43]} ...

grav09:11:10

In the https://podcast.thinkingelixir.com/ podcast, in one of the "10 years of Elixir", the author of Elixir, José Valim, also mentions that Beam (the Erlang VM) originally didn't have hashmaps, so Elixir didn't either. That also seems to indicate that the use of property lists is a legacy thing. I don't remember the specific episode, but in general I think those five "10 years" episodes give a nice intro to Elixir. Can recommend!

Cora (she/her)09:11:27

yes, beam didn't have maps to begin with (I used it both before and after that)