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?

Sure, the :reduce option:

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

(returns 10)

2 Likes

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.

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?

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).

2 Likes

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:

1 Like

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.

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

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

4 Likes

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

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.

1 Like

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.

2 Likes