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.

dimitarvp

dimitarvp

Ah, I see you need an explanation of sorts. Oh well, I am just telling you what I’m seeing in the docs. You either return a list or a data structure you desire (:into and :reduce).

Fl4m3Ph03n1x

Fl4m3Ph03n1x

One could, for example, based on the concept of familiarity, also expect that the return type of the Elixir comprehension is also the type of the first enumerator.

So for example:

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

> Would return a MapSet here

This is not the case. I am trying to understand why.

I appreciate the enthusiasm, but before posting this question I also checked the documentation (I posted a link to some of it in my question as well, to invite readers to take a peek).

I personally believe that everyone’s time here is valuable, so its up to me to do proper homework before asking for help :smiley:

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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 45766 226
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement