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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 11 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
for,StreamandEnumare all based on theEnumerableprotocol, 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
I mean the
doblock.The types of the generator sources don’t come into play. They all must be
Enumerableand the comprehension … enumerates them.If it helps, you can envision a comprehension with a default
into: []if you leave it unspecified.Fl4m3Ph03n1x
By block, do you mean the
doblock, or the last generator of the expression?gregvaughn
It’s always a list unless …
you give it an
:intowhich can be anyCollectableyou give it a
:reducein which case it is whatever type your block returns on last iterationdimitarvp
Yep, sorry, didn’t mean to pollute the thread.
D4no0
As a sidenote I tried to search for
forsource code and it is a part of Kernel.SpecialForms and is implemented in bootstap module, kind of strange, maybe they did some optimizations.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:
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
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 (
:intoand:reduce).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
I am fairly aware I can force the return of the expression to be
MapSetby using:into.However my objective is not to force a specific return type, is to understand how the returns type is picked.