This page is no longer maintained — Please continue to the home page at www.scala-lang.org

"Collapsing" options of same type?

5 replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
This should give a brief idea of what I'm trying to do:
class Branch { def leaf: Option[Leaf] }
val parent = Option[Branch]
val result = parent map (_.leaf)
The result will either be None, or Option(None), or Some(Some(aLeaf)). But of course what I really want is for Some(None) to map to None and for Some(Some(leaf)) to map to Some(leaf). I'd prefer not to do this explicitly, but Option doesn't seem to have a built-in way of managing this (which is fairly common, I think, when you're invoking statements like (parent map (_.leaf)) recursively as part of going over a tree structure--which is what I'm doing.)
Of course, I could easily be missing something obvious here, so please feel free to enlighten me.
Thanks,Ken
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: "Collapsing" options of same type?
flatMap

Date: Wed, 25 May 2011 09:58:41 -0700
From: ykkenmcd@gmail.com
To: scala-user@googlegroups.com
Subject: [scala-user] "Collapsing" options of same type?

This should give a brief idea of what I'm trying to do:
class Branch { def leaf: Option[Leaf] }
val parent = Option[Branch]
val result = parent map (_.leaf)
The result will either be None, or Option(None), or Some(Some(aLeaf)). But of course what I really want is for Some(None) to map to None and for Some(Some(leaf)) to map to Some(leaf). I'd prefer not to do this explicitly, but Option doesn't seem to have a built-in way of managing this (which is fairly common, I think, when you're invoking statements like (parent map (_.leaf)) recursively as part of going over a tree structure--which is what I'm doing.)
Of course, I could easily be missing something obvious here, so please feel free to enlighten me.
Thanks,Ken
vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: "Collapsing" options of same type?
parent flatMap?


Thanks,
-Vlad


On Wed, May 25, 2011 at 9:58 AM, Ken McDonald <ykkenmcd@gmail.com> wrote:
This should give a brief idea of what I'm trying to do:
class Branch { def leaf: Option[Leaf] }
val parent = Option[Branch]
val result = parent map (_.leaf)
The result will either be None, or Option(None), or Some(Some(aLeaf)). But of course what I really want is for Some(None) to map to None and for Some(Some(leaf)) to map to Some(leaf). I'd prefer not to do this explicitly, but Option doesn't seem to have a built-in way of managing this (which is fairly common, I think, when you're invoking statements like (parent map (_.leaf)) recursively as part of going over a tree structure--which is what I'm doing.)
Of course, I could easily be missing something obvious here, so please feel free to enlighten me.
Thanks,Ken

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: "Collapsing" options of same type?
Oh yeah, since flatMap pulls items from inner lists and puts them into outer lists (so to speak). Please forgive me, my mind just doesn't find list method names natural with Options, even though they are in some sense one-element lists. It took me a surprising amount of time to realize I should be using "map" instead of instead of trying to make "collect" fill the bill.
Thanks!Ken
vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: "Collapsing" options of same type?
They are not list methods, they are monadic operations. The one we are talking about is multiplication combined with mapping. First map, then "flatten", aka "multiply": Option[Option[T]] -> Option[T].

Thanks,
-Vlad


On Wed, May 25, 2011 at 10:44 AM, Ken McDonald <ykkenmcd@gmail.com> wrote:
Oh yeah, since flatMap pulls items from inner lists and puts them into outer lists (so to speak). Please forgive me, my mind just doesn't find list method names natural with Options, even though they are in some sense one-element lists. It took me a surprising amount of time to realize I should be using "map" instead of instead of trying to make "collect" fill the bill.
Thanks!Ken

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: "Collapsing" options of same type?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 26/05/11 03:44, Ken McDonald wrote:
> Oh yeah, since flatMap pulls items from inner lists and puts them
> into outer lists (so to speak). Please forgive me, my mind just
> doesn't find list method names natural with Options, even though
> they are in some sense one-element lists. It took me a surprising
> amount of time to realize I should be using "map" instead of
> instead of trying to make "collect" fill the bill.
>
> Thanks! Ken
>
For future, "map then collapse" is equivalent to flatMap inside any
monad (not just Option).

Syntactically: (x map f).flatten == x flatMap f

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland