vrod

vrod

Joining list and tuple?

I think I do not understand something. Can someone explain why this works?

iex> [] ++ {:ok, "好"}
{:ok, "好"}

Is a tuple a list?

Most Liked

srowley

srowley

That works for any term:

iex> [] ++ 3
3

iex> [] ++ "foo"
"foo"

The return value is an improper list. As you can read in the documentation, when the right-hand side of this function (++/2) is not a list, the return value is an improper list. (Point being, a tuple is not a list).

srowley

srowley

There was one evening where I was confused because I was building improper lists and they were causing problems because I had no idea what an improper list was or why the cons operator seemed to be appearing randomly in my lists. Once I knew what they were and how to avoid inadvertently creating them it was not a big deal.

This article and the follow-up to it give some good insight into why iolists and the fact that improper lists work as iolists is very helpful at times. Phoenix make liberal use of this as the posts explain. I have a library that generates SVG and I can attest to how incredibly convenient this feature of the language is for building up a big sequence of strings.

mindok

mindok

Ha - I was going to reference that article for the same reasons! So I’ll reference this one instead…

Yes - improper lists make it super easy & efficient to build output from deeply nested structures, and then make it super efficient to write that output to a file or network socket. That’s why Phoenix has response times measured in microseconds rather than milliseconds. It’s all about maximising the work the runtime doesn’t have to do. It may be old-fashioned to think like that now you can rent infinite computing resources, but I wouldn’t say it’s legacy.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement