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

[scala-language] Seq[Set].flatten => error: could not find implicit value for parameter asTraversable ?

3 replies
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.

Hello,

I just found that I'm not able to do a flatten on a Seq of Set, and that
seems really strange as I'm able to flatten a Seq of Iterables.

Is it related to http://lampsvn.epfl.ch/trac/scala/ticket/3813 ?
If not, could someone explain me what is the reason for that ?

8<---------- REPL session ----------------

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java
1.6.0_23).
Type in expressions to have them evaluated.
Type :help for more information.

scala> Seq( Set("a"), Set(), Set("b", "a") ).flatten
:6: error: could not find implicit value for parameter
asTraversable: (scala.collection.immutable.Set[_ <: java.lang.String])
=> Traversable[B]
Seq( Set("a"), Set(), Set("b", "a") ).flatten
^

scala> Seq( Seq("a"), Seq(), Seq("b", "a") ).flatten
res1: Seq[java.lang.String] = List(a, b, a)

scala> Set( Seq("a"), Seq(), Seq("b", "a") ).flatten
res2: scala.collection.immutable.Set[java.lang.String] = Set(a, b)

scala> Seq( Seq("a"), Seq(), Seq("b", "a") ).flatMap( _.toSeq )
res3: Seq[java.lang.String] = List(a, b, a)

Seq( Iterable("a"), Iterable("a", "b") ).flatten
res4: Seq[java.lang.String] = List(a, a, b)

Seq( Set("a"):Iterable[String], Set():Iterable[String], Set("b",
"a"):Iterable[String] ).flatten
res5: Seq[String] = List(a, b, a)

8<---------- /REPL session ----------------

Thanks,

Derek Williams
Joined: 2009-06-13,
User offline. Last seen 42 years 45 weeks ago.
[scala-language] Re: Seq[Set].flatten => error: could not find i
I believe this is due to Set being invariant (or whatever it is called, I'm bad with jargon). This works:
Seq( Set("a"), Set.empty[String], Set("b", "a") ).flatten
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: [scala-language] Re: Seq[Set].flatten => error: could not fi

On 28/01/2011 16:55, Derek Williams wrote:
> I believe this is due to Set being invariant (or whatever it is called,
> I'm bad with jargon). This works:
>
> Seq( Set("a"), Set.empty[String], Set("b", "a") ).flatten

Ah, quite intersting. Thanks !

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: [scala-language] Re: Seq[Set].flatten => error: could not fi
Oh, that's wise. It is invariant now. 
2011/1/28 Derek Williams <derek@nebvin.ca>
I believe this is due to Set being invariant (or whatever it is called, I'm bad with jargon). This works:
Seq( Set("a"), Set.empty[String], Set("b", "a") ).flatten



--
Thanks,
-Vlad

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