@@ -16,62 +16,65 @@ import org.junit.Assert._
16
16
import org .junit .Test
17
17
import scala .collection .IterableOnceOps
18
18
import scala .collection .generic .IsIterableOnce
19
- import scala .collection .immutable .{SortedMap , SortedSet }
19
+ import scala .collection .immutable .{ArraySeq , BitSet , SortedMap , SortedSet }
20
20
21
21
final class TestIterableOnceExtensions {
22
22
import TestIterableOnceExtensions ._
23
23
24
24
// groupMapReduce --------------------------------------------
25
25
@ Test
26
26
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 )(_ + _)
29
29
30
- val xs = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
30
+ val data = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
31
31
val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
32
- assertEquals(expected, occurrences(xs))
32
+
33
+ assertEquals(expected, occurrences(data))
33
34
}
34
35
35
36
@ Test
36
37
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 )(_ + _)
39
40
40
- val xs = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
41
+ val data = Seq ('a' , 'b' , 'b' , 'c' , 'a' , 'a' , 'a' , 'b' )
41
42
val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
42
- assertEquals(expected, occurrences(xs))
43
+
44
+ assertEquals(expected, occurrences(data))
43
45
}
44
46
45
47
@ Test
46
48
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 )(_ + _)
49
51
50
- val xs = " abbcaaab"
52
+ val data = " abbcaaab"
51
53
val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
52
- assertEquals(expected, occurrences(xs))
54
+
55
+ assertEquals(expected, occurrences(data))
53
56
}
54
57
55
58
@ Test
56
59
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 )(_ + _)
59
62
60
- val xs = LowerCaseString (" abBcAaAb" )
63
+ val data = LowerCaseString (" abBcAaAb" )
61
64
val expected = Map ('a' -> 4 , 'b' -> 3 , 'c' -> 1 )
62
- assertEquals(expected, occurrences(xs))
65
+
66
+ assertEquals(expected, occurrences(data))
63
67
}
64
68
// -----------------------------------------------------------
65
69
66
- // groupMapTo ---- --------------------------------------------
70
+ // GroupMapGenGen --------------------------------------------
67
71
@ Test
68
- def anyCollectionGroupMapToFull (): Unit = {
72
+ def anyCollectionGroupMapGenResultAs (): Unit = {
69
73
def getUniqueUsersByCountrySorted (data : List [Record ]): List [(String , List [String ])] =
70
74
data
71
- .groupMapTo (_.country)(_.user)
75
+ .groupMapGenGen (_.country)(_.user)
72
76
.collectValuesAs(SortedSet )
73
- .collectResultsAs(SortedMap )
74
- .result
77
+ .resultAs(SortedMap )
75
78
.view
76
79
.mapValues(_.toList)
77
80
.toList
@@ -95,15 +98,11 @@ final class TestIterableOnceExtensions {
95
98
}
96
99
97
100
@ 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 )] =
100
103
data
101
- .groupByTo(_.head)
102
- .collectValuesAs(SortedSet )
103
- .collectResultsAs(SortedMap )
104
- .result
105
- .view
106
- .mapValues(_.toList)
104
+ .groupByGenGen(_.head)
105
+ .reduceValuesAs(SortedMap )(_ ++ " " ++ _)
107
106
.toList
108
107
109
108
val data = List (
@@ -116,23 +115,51 @@ final class TestIterableOnceExtensions {
116
115
" Winter" ,
117
116
" Banana"
118
117
)
119
-
120
118
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"
124
122
)
125
123
126
- assertEquals(expected, getUniqueWordsByFirstLetterSorted (data))
124
+ assertEquals(expected, getAllWordsByFirstLetterSorted (data))
127
125
}
128
126
129
127
@ 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 ])] =
132
158
data
133
- .groupByTo(_.head)
134
- .collectResultsAs(SortedMap )
135
- .reduce(_ ++ " " ++ _)
159
+ .iterator
160
+ .groupBy(_.head)
161
+ .view
162
+ .mapValues(_.toSet)
136
163
.toList
137
164
138
165
val data = List (
@@ -147,12 +174,12 @@ final class TestIterableOnceExtensions {
147
174
)
148
175
149
176
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" )
153
180
)
154
181
155
- assertEquals(expected, getAllWordsByFirstLetterSorted (data))
182
+ assertEquals(expected, getUniqueWordsByFirstLetter (data))
156
183
}
157
184
// -----------------------------------------------------------
158
185
}
0 commit comments