Fl4m3Ph03n1x

Fl4m3Ph03n1x

Can an elixir comprehensions return something other than a List?

Background

For the longest time I have been estranged to the magic lands of comprehensions in FP languages.
Now I am trying to pick it up.

Comprehensions use the for expression in Elixir:

How Scala does it

Most FP languages these days have comprehensions in one way or another. For the purposes of this discussion I am picking Scala because I think it looks somewhat familiar (syntax is fairly similar to that of elixir).

See the following comprehension (Scala):

for {
  a <- List(1, 2)
  b <- Set(2, 1)
} yield a * b

> List(2, 1, 4, 2)

This comprehension is the equivalent of (Elixir):

for a <- [1, 2], 
    b <- MapSet.new([2, 1]) do 
  a * b 
end

> [1, 2, 2, 4]

The Scala example returns a List because in Scala, the comprehension’s return type will be the type of the first enumerator (in this case a <- [1, 2]).

The Elixir example returns also a List. I have no idea why. Maybe they work in reverse? Let’s find out:

for {
  a <- Set(1, 2)
  b <- List(2, 1)
} yield a * b

> Set(2, 1, 4)

In this Scala example I switched things around. Lets see what Elixir returns:

for a <- MapSet.new([1, 2]), 
    b <- [2, 1] do 
  a * b 
end

> [2, 1, 4, 2]

It also returns a List

Questions

So, I am rather confused here. So here is my question:

  • In an Elixir comprehension with enumerators of different types, how do I know what the resulting type of the expression will be?

Marked As Solved

gregvaughn

gregvaughn

I mean the do block.

The types of the generator sources don’t come into play. They all must be Enumerable and the comprehension … enumerates them.

If it helps, you can envision a comprehension with a default into: [] if you leave it unspecified.

Also Liked

gregvaughn

gregvaughn

It’s always a list unless …
you give it an :into which can be any Collectable
you give it a :reduce in which case it is whatever type your block returns on last iteration

dimitarvp

dimitarvp

Sure, the :reduce option:

for i <- 1..4, reduce: 0 do
  acc -> acc + i
end

(returns 10)

LostKobrakai

LostKobrakai

for, Stream and Enum are all based on the Enumerable protocol, which disregards the actual input datatype in favor of an unbounded enumeration of values. The enumerable implementations essentially turn the raw data into something users of the protocol can reduce over.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

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
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement