- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
flatMap Confusion
Sun, 2011-11-27, 15:41
Dear all,
Reading Chapter 23 of PinS, I feel I gained some nice introspection into how for expressions are managed in Scala. Yet, I also ended up wrestling with how the yield thing works. Consider the following snippet from S16.7/362:
"The flatMap operator is similar to map, but it takes a function returning a list of elements as its right operand. It applies the function to each list element and returns the concatenation of all function results."
I seem to find that contradictory with the following formal definition (taken from S23.6/529):
def flatMap[A, B](xs: List[A], f: A => List[B]): List[B] = for (x <- xs; y <- f(x)) yield y
Correct me if I'm wrong. But, I think the definition of map given right above that of flatMap above (in the same page of PinS) can be rewritten as:
def map[A, B](xs: List[A], f: A => B): List[B] = for (x <- xs; y <- f(x)) yield y
Then, the only difference I can spot across the above two definitions is that y in flatMap is an instance of List[B] whilst it's of type B in map. Now, I wonder how it is that in the case of flatMap, the same yield expression also performs the concatenation whilst the map one doesn't! Any ideas?
TIA,
--Hossein
--------------------------------------------------------------------------------------------------------------
Seyed H. HAERI (Hossein)
Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany
ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Reading Chapter 23 of PinS, I feel I gained some nice introspection into how for expressions are managed in Scala. Yet, I also ended up wrestling with how the yield thing works. Consider the following snippet from S16.7/362:
"The flatMap operator is similar to map, but it takes a function returning a list of elements as its right operand. It applies the function to each list element and returns the concatenation of all function results."
I seem to find that contradictory with the following formal definition (taken from S23.6/529):
def flatMap[A, B](xs: List[A], f: A => List[B]): List[B] = for (x <- xs; y <- f(x)) yield y
Correct me if I'm wrong. But, I think the definition of map given right above that of flatMap above (in the same page of PinS) can be rewritten as:
def map[A, B](xs: List[A], f: A => B): List[B] = for (x <- xs; y <- f(x)) yield y
Then, the only difference I can spot across the above two definitions is that y in flatMap is an instance of List[B] whilst it's of type B in map. Now, I wonder how it is that in the case of flatMap, the same yield expression also performs the concatenation whilst the map one doesn't! Any ideas?
TIA,
--Hossein
--------------------------------------------------------------------------------------------------------------
Seyed H. HAERI (Hossein)
Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany
ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Sun, 2011-11-27, 16:07
#2
Re: flatMap Confusion
On Sun, Nov 27, 2011 at 9:41 AM, Seyed H. HAERI (Hossein) <hossein.haeri@gmail.com> wrote:
Dear all,
Reading Chapter 23 of PinS, I feel I gained some nice introspection into how for expressions are managed in Scala. Yet, I also ended up wrestling with how the yield thing works. Consider the following snippet from S16.7/362:
"The flatMap operator is similar to map, but it takes a function returning a list of elements as its right operand. It applies the function to each list element and returns the concatenation of all function results."
I seem to find that contradictory with the following formal definition (taken from S23.6/529):
def flatMap[A, B](xs: List[A], f: A => List[B]): List[B] = for (x <- xs; y <- f(x)) yield y
Correct me if I'm wrong. But, I think the definition of map given right above that of flatMap above (in the same page of PinS) can be rewritten as:
def map[A, B](xs: List[A], f: A => B): List[B] = for (x <- xs; y <- f(x)) yield y
This is not the case. You can't run a generator on something that is not a "collection-ish" i.e. you mean:
for(x <- xs) yield f(x) // A Map operation.
*or*
for { x <- xs y = f(x)} yield y // Still a map.
What may boggle the mind is that a for-expression *is implemented using* the map and flatMap methods.
Then, the only difference I can spot across the above two definitions is that y in flatMap is an instance of List[B] whilst it's of type B in map. Now, I wonder how it is that in the case of flatMap, the same yield expression also performs the concatenation whilst the map one doesn't! Any ideas?
TIA,
--Hossein
--------------------------------------------------------------------------------------------------------------
Seyed H. HAERI (Hossein)
Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany
ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Sun, 2011-11-27, 16:27
#3
Re: flatMap Confusion
Hi Josh,
> This is not the case. You can't run a generator on something that is not a "collection-ish" i.e. you mean:
> for(x <- xs) yield f(x) // A Map operation.
> *or*
> for {
> x <- xs
> y = f(x)
> } yield y // Still a map.
Bingo! That's the difference: y = f(x) vs y <- f(x).
Thank a lot,
--Hossein
--------------------------------------------------------------------------------------------------------------
Seyed H. HAERI (Hossein)
Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany
ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
but the difference between map and flatMap
is that, for flatMap, somehow, "sequencing"
of two things is involved, while for map only one,
well, mapping is involved
On Sun, Nov 27, 2011 at 3:41 PM, Seyed H. HAERI (Hossein) <hossein.haeri@gmail.com> wrote:
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination