@@ -16,62 +16,65 @@ import org.junit.Assert._
1616import org .junit .Test
1717import scala .collection .IterableOnceOps
1818import scala .collection .generic .IsIterableOnce
19- import scala .collection .immutable .{SortedMap , SortedSet }
19+ import scala .collection .immutable .{ArraySeq , BitSet , SortedMap , SortedSet }
2020
2121final class TestIterableOnceExtensions {
2222 import TestIterableOnceExtensions ._
2323
2424 // groupMapReduce --------------------------------------------
2525 @ Test
2626 def iteratorGroupMapReduce (): Unit = {
27- def occurrences [A ](coll : IterableOnce [A ]): Map [A , Int ] =
28- coll .iterator.groupMapReduce(identity)(_ => 1 )(_ + _)
27+ def occurrences [A ](data : IterableOnce [A ]): Map [A , Int ] =
28+ data .iterator.groupMapReduce(identity)(_ => 1 )(_ + _)
2929
30- val xs = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
30+ val data = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
3131 val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
32- assertEquals(expected, occurrences(xs))
32+
33+ assertEquals(expected, occurrences(data))
3334 }
3435
3536 @ Test
3637 def iterableOnceOpsGroupMapReduce (): Unit = {
37- def occurrences [A , CC [_], C ](coll : IterableOnceOps [A , CC , C ]): Map [A , Int ] =
38- coll .groupMapReduce(identity)(_ => 1 )(_ + _)
38+ def occurrences [A , CC [_], C ](data : IterableOnceOps [A , CC , C ]): Map [A , Int ] =
39+ data .groupMapReduce(identity)(_ => 1 )(_ + _)
3940
40- val xs = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
41+ val data = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
4142 val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
42- assertEquals(expected, occurrences(xs))
43+
44+ assertEquals(expected, occurrences(data))
4345 }
4446
4547 @ Test
4648 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+ def occurrences [Repr ](data : Repr )(implicit it : IsIterableOnce [Repr ]): Map [it.A , Int ] =
50+ it(data ).iterator.groupMapReduce(identity)(_ => 1 )(_ + _)
4951
50- val xs = " abbcaaab"
52+ val data = " abbcaaab"
5153 val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
52- assertEquals(expected, occurrences(xs))
54+
55+ assertEquals(expected, occurrences(data))
5356 }
5457
5558 @ Test
5659 def customIterableOnceOpsGroupMapReduce (): Unit = {
57- def occurrences (coll : LowerCaseString ): Map [Char , Int ] =
58- coll .groupMapReduce(identity)(_ => 1 )(_ + _)
60+ def occurrences (data : LowerCaseString ): Map [Char , Int ] =
61+ data .groupMapReduce(identity)(_ => 1 )(_ + _)
5962
60- val xs = LowerCaseString (" abBcAaAb" )
63+ val data = LowerCaseString (" abBcAaAb" )
6164 val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
62- assertEquals(expected, occurrences(xs))
65+
66+ assertEquals(expected, occurrences(data))
6367 }
6468 // -----------------------------------------------------------
6569
66- // groupMapTo ---- --------------------------------------------
70+ // GroupMapGenGen --------------------------------------------
6771 @ Test
68- def anyCollectionGroupMapToFull (): Unit = {
72+ def anyCollectionGroupMapGenResultAs (): Unit = {
6973 def getUniqueUsersByCountrySorted (data : List [Record ]): List [(String , List [String ])] =
7074 data
71- .groupMapTo (_.country)(_.user)
75+ .groupMapGenGen (_.country)(_.user)
7276 .collectValuesAs(SortedSet )
73- .collectResultsAs(SortedMap )
74- .result
77+ .resultAs(SortedMap )
7578 .view
7679 .mapValues(_.toList)
7780 .toList
@@ -95,15 +98,11 @@ final class TestIterableOnceExtensions {
9598 }
9699
97100 @ Test
98- def anyCollectionGroupByToFull (): Unit = {
99- def getUniqueWordsByFirstLetterSorted (data : List [String ]): List [(Char , List [ String ] )] =
101+ def anyCollectionGroupMapGenGenReduce (): Unit = {
102+ def getAllWordsByFirstLetterSorted (data : List [String ]): List [(Char , String )] =
100103 data
101- .groupByTo(_.head)
102- .collectValuesAs(SortedSet )
103- .collectResultsAs(SortedMap )
104- .result
105- .view
106- .mapValues(_.toList)
104+ .groupByGenGen(_.head)
105+ .reduceValuesAs(SortedMap )(_ ++ " " ++ _)
107106 .toList
108107
109108 val data = List (
@@ -116,23 +115,51 @@ final class TestIterableOnceExtensions {
116115 " Winter" ,
117116 " Banana"
118117 )
119-
120118 val expected = List (
121- 'A' -> List ( " Apple " , " April" , " Autumn " ) ,
122- 'B' -> List ( " Banana" ) ,
123- 'W' -> List ( " Wilson" , " Winter" )
119+ 'A' -> " Autumn April Apple Apple " ,
120+ 'B' -> " Banana Banana " ,
121+ 'W' -> " Wilson Winter"
124122 )
125123
126- assertEquals(expected, getUniqueWordsByFirstLetterSorted (data))
124+ assertEquals(expected, getAllWordsByFirstLetterSorted (data))
127125 }
128126
129127 @ Test
130- def anyCollectionGroupByToReduceFull (): Unit = {
131- def getAllWordsByFirstLetterSorted (data : List [String ]): List [(Char , String )] =
128+ def iterableOnceOpsGroupByGenSpecificFactory (): Unit = {
129+ def bitsByEven (data : BitSet ): Map [Boolean , BitSet ] =
130+ data.groupByGen(x => (x % 2 ) == 0 ).result
131+
132+ val data = BitSet (1 , 2 , 3 , 4 , 5 )
133+ val expected = Map (
134+ true -> BitSet (2 , 4 ),
135+ false -> BitSet (1 , 3 , 5 )
136+ )
137+
138+ assertEquals(expected, bitsByEven(data))
139+ }
140+
141+ @ Test
142+ def iterableOnceOpsGroupMapGenIterableFactory (): Unit = {
143+ def bitsByEvenAsChars (data : BitSet ): Map [Boolean , Set [Char ]] =
144+ data.groupMapGen(x => (x % 2 ) == 0 )(_.toChar).result
145+
146+ val data = BitSet (100 , 101 , 102 , 103 , 104 , 105 )
147+ val expected = Map (
148+ true -> Set ('d' , 'f' , 'h' ),
149+ false -> Set ('e' , 'g' , 'i' )
150+ )
151+
152+ assertEquals(expected, bitsByEvenAsChars(data))
153+ }
154+
155+ @ Test
156+ def iteratorGroupBy (): Unit = {
157+ def getUniqueWordsByFirstLetter (data : IterableOnce [String ]): List [(Char , Set [String ])] =
132158 data
133- .groupByTo(_.head)
134- .collectResultsAs(SortedMap )
135- .reduce(_ ++ " " ++ _)
159+ .iterator
160+ .groupBy(_.head)
161+ .view
162+ .mapValues(_.toSet)
136163 .toList
137164
138165 val data = List (
@@ -147,12 +174,12 @@ final class TestIterableOnceExtensions {
147174 )
148175
149176 val expected = List (
150- 'A' -> " Autumn April Apple Apple " ,
151- 'B' -> " Banana Banana " ,
152- 'W' -> " Wilson Winter"
177+ 'A' -> Set ( " Apple " , " April" , " Autumn " ) ,
178+ 'B' -> Set ( " Banana" ) ,
179+ 'W' -> Set ( " Wilson" , " Winter" )
153180 )
154181
155- assertEquals(expected, getAllWordsByFirstLetterSorted (data))
182+ assertEquals(expected, getUniqueWordsByFirstLetter (data))
156183 }
157184 // -----------------------------------------------------------
158185}
0 commit comments