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 11 to 2

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.

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.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

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

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

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

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.

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:

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

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?

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.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews