Fl4m3Ph03n1x

Fl4m3Ph03n1x

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?

Showing Posts 1 to 10

dimitarvp

dimitarvp

Sure, the :reduce option:

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

(returns 10)

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I am fairly aware I can force the return of the expression to be MapSet by using :into.
However my objective is not to force a specific return type, is to understand how the returns type is picked.

dimitarvp

dimitarvp

But I am not completely sure what kind of expected output you’re after. Doing a combination of two or more collections can easily yield elements that don’t adhere to the rule of a MapSet (namely no repetitions). But maybe that’s exactly what you want? A collection with the products of two other collections where duplicates are eliminated?

Can you show us your ideal return value for the scenario in your first post?

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 OP

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:

D4no0

D4no0

As a sidenote I tried to search for for source code and it is a part of Kernel.SpecialForms and is implemented in bootstap module, kind of strange, maybe they did some optimizations.

dimitarvp

dimitarvp

Yep, sorry, didn’t mean to pollute the thread.

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

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

By block, do you mean the do block, or the last generator of the expression?

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.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews