- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why does Option not extend the Iterable trait?
Fri, 2010-04-09, 17:03
I asked this on StackOverflow but there have been no answers. Option in 2.8 even declares an iterator method but it does not implement Iterable, instead relying on an implicit conversion. Is this for reasons of conceptual purity, or is there some practical issue with it?
http://stackoverflow.com/questions/2607115/why-does-option-not-implement...
Chris
Get a free e-mail account with Hotmail. Sign-up now.
http://stackoverflow.com/questions/2607115/why-does-option-not-implement...
Chris
Get a free e-mail account with Hotmail. Sign-up now.
Fri, 2010-04-09, 19:17
#2
Re: Why does Option not extend the Iterable trait?
On 04/09/10 14:04, Paul Phillips wrote:
> On Fri, Apr 09, 2010 at 04:00:06PM +0000, christopher marshall wrote:
>> I asked this on StackOverflow but there have been no answers. Option
>> in 2.8 even declares an iterator method but it does not implement
>> Iterable, instead relying on an implicit conversion. Is this for
>> reasons of conceptual purity, or is there some practical issue with
>> it?
>
> Option is not a sufficiently general container to fully interoperate
> with the other collections.
>
> scala> Seq(1) flatMap (_ => Iterable(1, 2))
> res0: Seq[Int] = List(1, 2)
>
> scala> Some(1) flatMap (_ => Iterable(1, 2))
>
= res0 :) // same blocks passed
Seriously though,
Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
None : Option[T] flatMap ~> Seq[T] = List()
But as we've learned before with matz & POLS, the S is both subjective and
usually refers to the powers that be :-)
-Martin
Fri, 2010-04-09, 19:37
#3
Re: Why does Option not extend the Iterable trait?
On Fri, Apr 09, 2010 at 02:14:01PM -0400, Martin S. Weber wrote:
> Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
> None : Option[T] flatMap ~> Seq[T] = List()
Now Option has ceased to be a monad.
> But as we've learned before with matz & POLS, the S is both subjective
> and usually refers to the powers that be :-)
Yes, I would find it rather surprising if Option's monadic methods
starting returning Lists. POLS may be subjective but programming isn't
all about avoiding surprises: there are more important things, like
consistency of theoretical underpinnings.
Fri, 2010-04-09, 19:57
#4
Re: Why does Option not extend the Iterable trait?
This in itself I don't see the problem with. For instance:
scala> Map(1 -> 2, 3 -> 4) flatMap ( _ => Map('a -> 'b))
res44: scala.collection.immutable.Map[Symbol,Symbol] = Map(('a,'b)) scala> Map(1 -> 2, 3 -> 4) flatMap ( _ => Iterable('a, 'b'))
res45: scala.collection.immutable.Iterable[Any] = List('a, b, 'a, b)
The problem I do see is that CanBuildFrom can't help here. It would require overload, but one can't overload based a passed function's type parameters. I take your point, though.
On Fri, Apr 9, 2010 at 3:04 PM, Paul Phillips <paulp@improving.org> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
res44: scala.collection.immutable.Map[Symbol,Symbol] = Map(('a,'b)) scala> Map(1 -> 2, 3 -> 4) flatMap ( _ => Iterable('a, 'b'))
res45: scala.collection.immutable.Iterable[Any] = List('a, b, 'a, b)
The problem I do see is that CanBuildFrom can't help here. It would require overload, but one can't overload based a passed function's type parameters. I take your point, though.
On Fri, Apr 9, 2010 at 3:04 PM, Paul Phillips <paulp@improving.org> wrote:
On Fri, Apr 09, 2010 at 04:00:06PM +0000, christopher marshall wrote:
> I asked this on StackOverflow but there have been no answers. Option
> in 2.8 even declares an iterator method but it does not implement
> Iterable, instead relying on an implicit conversion. Is this for
> reasons of conceptual purity, or is there some practical issue with
> it?
Option is not a sufficiently general container to fully interoperate
with the other collections.
scala> Seq(1) flatMap (_ => Iterable(1, 2))
res0: Seq[Int] = List(1, 2)
scala> Some(1) flatMap (_ => Iterable(1, 2))
<your answer here>
--
Paul Phillips | Atheists dig the best foxholes.
In Theory |
Empiricist |
up hill, pi pals! |----------* http://www.improving.org/paulp/ *----------
--
Daniel C. Sobral
I travel to the future all the time.
Sat, 2010-04-10, 14:17
#5
RE: Why does Option not extend the Iterable trait?
Thanks - great answer. It seems to re-inforce Tony Morris' point about subclassing
> Date: Fri, 9 Apr 2010 11:18:32 -0700
> From: paulp@improving.org
> Subject: Re: [scala-user] Why does Option not extend the Iterable trait?
>
> On Fri, Apr 09, 2010 at 02:14:01PM -0400, Martin S. Weber wrote:
> > Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
> > None : Option[T] flatMap ~> Seq[T] = List()
>
> Now Option has ceased to be a monad.
Get a free e-mail account with Hotmail. Sign-up now.
> Date: Fri, 9 Apr 2010 11:18:32 -0700
> From: paulp@improving.org
> Subject: Re: [scala-user] Why does Option not extend the Iterable trait?
>
> On Fri, Apr 09, 2010 at 02:14:01PM -0400, Martin S. Weber wrote:
> > Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
> > None : Option[T] flatMap ~> Seq[T] = List()
>
> Now Option has ceased to be a monad.
Get a free e-mail account with Hotmail. Sign-up now.
On Fri, Apr 09, 2010 at 04:00:06PM +0000, christopher marshall wrote:
> I asked this on StackOverflow but there have been no answers. Option
> in 2.8 even declares an iterator method but it does not implement
> Iterable, instead relying on an implicit conversion. Is this for
> reasons of conceptual purity, or is there some practical issue with
> it?
Option is not a sufficiently general container to fully interoperate
with the other collections.
scala> Seq(1) flatMap (_ => Iterable(1, 2))
res0: Seq[Int] = List(1, 2)
scala> Some(1) flatMap (_ => Iterable(1, 2))