Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As of <a href="https://github.com/scala/scala/commit/73f7001d963a80950751b4e13b95b05d2526edc8">this commit</a> it's a lot easier to "enrich" Scala collections than it was when Rex gave his excellent answer. For simple cases it might look like this,</p> <pre><code>import scala.collection.generic.{ CanBuildFrom, FromRepr, HasElem } import language.implicitConversions class FilterMapImpl[A, Repr](val r : Repr)(implicit hasElem : HasElem[Repr, A]) { def filterMap[B, That](f : A =&gt; Option[B]) (implicit cbf : CanBuildFrom[Repr, B, That]) : That = r.flatMap(f(_).toSeq) } implicit def filterMap[Repr : FromRepr](r : Repr) = new FilterMapImpl(r) </code></pre> <p>which adds a "same result type" respecting <code>filterMap</code> operation to all <code>GenTraversableLike</code>s,</p> <pre><code>scala&gt; val l = List(1, 2, 3, 4, 5) l: List[Int] = List(1, 2, 3, 4, 5) scala&gt; l.filterMap(i =&gt; if(i % 2 == 0) Some(i) else None) res0: List[Int] = List(2, 4) scala&gt; val a = Array(1, 2, 3, 4, 5) a: Array[Int] = Array(1, 2, 3, 4, 5) scala&gt; a.filterMap(i =&gt; if(i % 2 == 0) Some(i) else None) res1: Array[Int] = Array(2, 4) scala&gt; val s = "Hello World" s: String = Hello World scala&gt; s.filterMap(c =&gt; if(c &gt;= 'A' &amp;&amp; c &lt;= 'Z') Some(c) else None) res2: String = HW </code></pre> <p>And for the example from the question, the solution now looks like,</p> <pre><code>class GroupIdenticalImpl[A, Repr : FromRepr](val r: Repr) (implicit hasElem : HasElem[Repr, A]) { def groupIdentical[That](implicit cbf: CanBuildFrom[Repr,Repr,That]): That = { val builder = cbf(r) def group(r: Repr) : Unit = { val first = r.head val (same, rest) = r.span(_ == first) builder += same if(!rest.isEmpty) group(rest) } if(!r.isEmpty) group(r) builder.result } } implicit def groupIdentical[Repr : FromRepr](r: Repr) = new GroupIdenticalImpl(r) </code></pre> <p>Sample REPL session,</p> <pre><code>scala&gt; val l = List(1, 1, 2, 2, 3, 3, 1, 1) l: List[Int] = List(1, 1, 2, 2, 3, 3, 1, 1) scala&gt; l.groupIdentical res0: List[List[Int]] = List(List(1, 1),List(2, 2),List(3, 3),List(1, 1)) scala&gt; val a = Array(1, 1, 2, 2, 3, 3, 1, 1) a: Array[Int] = Array(1, 1, 2, 2, 3, 3, 1, 1) scala&gt; a.groupIdentical res1: Array[Array[Int]] = Array(Array(1, 1),Array(2, 2),Array(3, 3),Array(1, 1)) scala&gt; val s = "11223311" s: String = 11223311 scala&gt; s.groupIdentical res2: scala.collection.immutable.IndexedSeq[String] = Vector(11, 22, 33, 11) </code></pre> <p>Again, note that the same result type principle has been observed in exactly the same way that it would have been had <code>groupIdentical</code> been directly defined on <code>GenTraversableLike</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload