Skip to content

Commit f536859

Browse files
committed
Adding the extension method to any IterableOnce instead to just Iterators
1 parent ecf7fcc commit f536859

File tree

3 files changed

+91
-34
lines changed

3 files changed

+91
-34
lines changed

src/main/scala/scala/collection/next/IteratorExtensions.scala renamed to src/main/scala/scala/collection/next/IterableOnceExtensions.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
package scala.collection
1414
package next
1515

16-
object IteratorExtensions {
17-
implicit class NextIteratorExtensions[A](private val iter: Iterator[A]) extends AnyVal {
16+
object IterableOnceExtensions {
17+
implicit class NextIterableOnceOpsExtensions[A](private val iter: IterableOnceOps[A, Any, Any]) extends AnyVal {
1818
/**
19-
* Partitions this Iterator into a map according to a discriminator function `key`. All the values that
19+
* Partitions this IterableOnce into a map according to a discriminator function `key`. All the values that
2020
* have the same discriminator are then transformed by the `value` function and then reduced into a
2121
* single value with the `reduce` function.
2222
*
2323
* {{{
24-
* def occurrences[A](as: Iterator[A]): Map[A, Int] =
25-
* as.groupMapReduce(identity)(_ => 1)(_ + _)
24+
* def occurrences[A](as: IterableOnce[A]): Map[A, Int] =
25+
* as.iterator.groupMapReduce(identity)(_ => 1)(_ + _)
2626
* }}}
2727
*
2828
* @note This will force the evaluation of the Iterator.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.collection.next
14+
15+
import org.junit.Assert._
16+
import org.junit.Test
17+
import scala.collection.IterableOnceOps
18+
import scala.collection.generic.IsIterableOnce
19+
20+
import IterableOnceExtensions._
21+
22+
final class TestIterableOnceExtensions {
23+
import TestIterableOnceExtensions.LowerCaseString
24+
25+
@Test
26+
def iteratorGroupMapReduce(): Unit = {
27+
def occurrences[A](coll: IterableOnce[A]): Map[A, Int] =
28+
coll.iterator.groupMapReduce(identity)(_ => 1)(_ + _)
29+
30+
val xs = Seq('a', 'b', 'b', 'c', 'a', 'a', 'a', 'b')
31+
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1)
32+
assertEquals(expected, occurrences(xs))
33+
}
34+
35+
@Test
36+
def iterableOnceOpsGroupMapReduce(): Unit = {
37+
def occurrences[A](coll: IterableOnceOps[A, Any, Any]): Map[A, Int] =
38+
coll.groupMapReduce(identity)(_ => 1)(_ + _)
39+
40+
val xs = Seq('a', 'b', 'b', 'c', 'a', 'a', 'a', 'b')
41+
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1)
42+
assertEquals(expected, occurrences(xs))
43+
}
44+
45+
@Test
46+
def anyLikeIterableOnceGroupMapReduce(): Unit = {
47+
def occurrences[Repr](coll: Repr)(implicit it: IsIterableOnce[Repr]): Map[it.A, Int] =
48+
it(coll).iterator.groupMapReduce(identity)(_ => 1)(_ + _)
49+
50+
val xs = "abbcaaab"
51+
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1)
52+
assertEquals(expected, occurrences(xs))
53+
}
54+
55+
@Test
56+
def customIterableOnceOpsGroupMapReduce(): Unit = {
57+
def occurrences(coll: LowerCaseString): Map[Char, Int] =
58+
coll.groupMapReduce(identity)(_ => 1)(_ + _)
59+
60+
val xs = LowerCaseString("abBcAaAb")
61+
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1)
62+
assertEquals(expected, occurrences(xs))
63+
}
64+
}
65+
66+
object TestIterableOnceExtensions {
67+
final case class LowerCaseString(source: String) extends IterableOnce[Char] with IterableOnceOps[Char, Iterable, String] {
68+
override def iterator: Iterator[Char] = source.iterator.map(_.toLower)
69+
70+
override def scanLeft[B](z: B)(op: (B, Char) => B): Iterable[B] = ???
71+
override def filter(p: Char => Boolean): String = ???
72+
override def filterNot(pred: Char => Boolean): String = ???
73+
override def take(n: Int): String = ???
74+
override def takeWhile(p: Char => Boolean): String = ???
75+
override def drop(n: Int): String = ???
76+
override def dropWhile(p: Char => Boolean): String = ???
77+
override def slice(from: Int, until: Int): String = ???
78+
override def map[B](f: Char => B): Iterable[B] = ???
79+
override def flatMap[B](f: Char => IterableOnce[B]): Iterable[B] = ???
80+
override def flatten[B](implicit asIterable: Char => IterableOnce[B]): Iterable[B] = ???
81+
override def collect[B](pf: PartialFunction[Char,B]): Iterable[B] = ???
82+
override def zipWithIndex: Iterable[(Char, Int)] = ???
83+
override def span(p: Char => Boolean): (String, String) = ???
84+
override def tapEach[U](f: Char => U): String = ???
85+
}
86+
}

src/test/scala/scala/collection/next/TestIteratorExtensions.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)