@@ -28,32 +28,128 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
28
28
private implicit def ordering : Ordering [K ] = value.ordering
29
29
private implicit def order : Order [K ] = Order .fromOrdering
30
30
31
+ /**
32
+ * Alias for [[concat ]]
33
+ */
31
34
def ++ (as : NonEmptyMap [K , A ]): NonEmptyMap [K , A ] = concat(as)
35
+ /**
36
+ * Appends this NEM to another NEM, producing a new `NonEmptyMap`.
37
+ */
32
38
def concat (as : NonEmptyMap [K , A ]): NonEmptyMap [K , A ] = new NonEmptyMap (value ++ as.value)
39
+
40
+ /**
41
+ * Removes a key from this map, returning a new SortedMap.
42
+ */
33
43
def - (key : K ): SortedMap [K , A ] = value - key
44
+
45
+ /**
46
+ * Adds a key-value pair to this map, returning a new `NonEmptyMap`.
47
+ * */
34
48
def + (ka : (K , A )): NonEmptyMap [K , A ] = new NonEmptyMap (value + ka)
35
49
50
+ /**
51
+ * Applies f to all the elements
52
+ */
36
53
def map [B ](f : A ⇒ B ): NonEmptyMap [K , B ] =
37
54
new NonEmptyMap (Functor [SortedMap [K , ? ]].map(value)(f))
38
55
56
+ /**
57
+ * Optionally returns the value associated with the given key.
58
+ * {{{
59
+ * scala> import cats.data.NonEmptyMap
60
+ * scala> import cats.implicits._
61
+ * scala> val nem = NonEmptyMap.of("A" -> 1, "B" -> 2)
62
+ * scala> nem.get("B")
63
+ * res0: Option[Int] = Some(2)
64
+ * }}}
65
+ */
39
66
def get (k : K ): Option [A ] = value.get(k)
40
67
68
+ /**
69
+ * Returns a `SortedSet` containing all the keys of this map.
70
+ * {{{
71
+ * scala> import cats.data.NonEmptyMap
72
+ * scala> import cats.implicits._
73
+ * scala> val nem = NonEmptyMap.of(1 -> "A", 2 -> "B")
74
+ * scala> nem.keys
75
+ * res0: scala.collection.immutable.SortedSet[Int] = Set(1, 2)
76
+ * }}}
77
+ */
41
78
def keys : SortedSet [K ] = value.keySet
42
79
80
+ /**
81
+ * Converts this map to a `NonEmptyList` of key-value pairs.
82
+ *
83
+ * {{{
84
+ * scala> import cats.data.NonEmptyMap
85
+ * scala> import cats.implicits._
86
+ * scala> val nem = NonEmptyMap.of(1 -> "A", 2 -> "B")
87
+ * scala> nem.toNonEmptyList
88
+ * res0: cats.data.NonEmptyList[(Int, String)] = NonEmptyList((1,A), (2,B))
89
+ * }}}
90
+ */
43
91
def toNonEmptyList : NonEmptyList [(K , A )] = NonEmptyList .fromListUnsafe(value.toList)
44
92
93
+ /**
94
+ * Returns the first key-value pair of this map.
95
+ */
45
96
def head : (K , A ) = value.head
97
+ /**
98
+ * Returns the first key-value pair of this map.
99
+ */
46
100
def last : (K , A ) = value.last
101
+
102
+ /**
103
+ * Returns all the key-value pairs, except for the first.
104
+ */
47
105
def tail : SortedMap [K , A ] = value.tail
48
106
107
+ /**
108
+ * Alias for [[get ]]
109
+ *
110
+ * {{{
111
+ * scala> import cats.data.NonEmptyMap
112
+ * scala> import cats.implicits._
113
+ * scala> val nem = NonEmptyMap.of("A" -> 1, "B" -> 2)
114
+ * scala> nem("A")
115
+ * res0: Option[Int] = Some(1)
116
+ * }}}
117
+ */
49
118
def apply (key : K ): Option [A ] = get(key)
119
+
120
+ /**
121
+ * Checks whether this map contains a binding for the given key.
122
+ */
50
123
def contains (key : K ): Boolean = value.contains(key)
51
124
125
+ /**
126
+ * Returns the amount of key-value pars in this map.
127
+ */
52
128
def size : Int = value.size
129
+
130
+ /**
131
+ * Tests whether a predicate holds for all elements of this map.
132
+ */
53
133
def forall (p : A ⇒ Boolean ): Boolean = value.forall { case (_, a) => p(a) }
134
+
135
+ /**
136
+ * Tests whether a predicate holds for at least one element of this map.
137
+ */
54
138
def exists (f : A ⇒ Boolean ): Boolean = value.exists { case (_, a) => f(a) }
139
+
140
+ /**
141
+ * Returns the first value along with its key, that matches the given predicate.
142
+ */
55
143
def find (f : A ⇒ Boolean ): Option [(K , A )] = value.find { case (_, a) => f(a) }
144
+
145
+ /**
146
+ * Filters all elements of this map that do not satisfy the given predicate.
147
+ */
56
148
def filter (p : A ⇒ Boolean ): SortedMap [K , A ] = value.filter { case (_, a) => p(a) }
149
+
150
+ /**
151
+ * Filters all elements of this map that satisfy the given predicate.
152
+ */
57
153
def filterNot (p : A ⇒ Boolean ): SortedMap [K , A ] = filter(t => ! p(t))
58
154
59
155
@@ -75,6 +171,11 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
75
171
def reduceLeft (f : (A , A ) => A ): A =
76
172
reduceLeftTo(identity)(f)
77
173
174
+
175
+ /**
176
+ * Apply `f` to the "initial element" of this map and lazily combine it
177
+ * with every other value using the given function `g`.
178
+ */
78
179
def reduceLeftTo [B ](f : A => B )(g : (B , A ) => B ): B =
79
180
tail.foldLeft(f(head._2))((b, a) => g(b, a._2))
80
181
@@ -84,6 +185,10 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
84
185
def reduceRight (f : (A , Eval [A ]) => Eval [A ]): Eval [A ] =
85
186
reduceRightTo(identity)(f)
86
187
188
+ /**
189
+ * Apply `f` to the "initial element" of this map and lazily combine it
190
+ * with every other value using the given function `g`.
191
+ */
87
192
def reduceRightTo [B ](f : A => B )(g : (A , Eval [B ]) => Eval [B ]): Eval [B ] =
88
193
Always ((head, tail)).flatMap { case ((_, a), ga) =>
89
194
Foldable [SortedMap [K , ? ]].reduceRightToOption(ga)(f)(g).flatMap {
@@ -107,6 +212,11 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
107
212
}
108
213
}
109
214
215
+ /**
216
+ * Given a function which returns a G effect, thread this effect
217
+ * through the running of this function on all the values in this map,
218
+ * returning an NonEmptyMap[K, B] in a G context.
219
+ */
110
220
def nonEmptyTraverse [G [_], B ](f : A => G [B ])(implicit G : Apply [G ]): G [NonEmptyMap [K , B ]] =
111
221
reduceRightToOptionWithKey[A , G [SortedMap [K , B ]]](tail)({ case (k, a) =>
112
222
G .map(f(a))(b => SortedMap .empty[K , B ] + ((k, b)))
@@ -117,7 +227,10 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
117
227
case Some (gtail) => G .map2(f(head._2), gtail)((a, bs) => NonEmptyMap ((head._1, a), bs))
118
228
}.value
119
229
120
- def toMap : SortedMap [K , A ] = value
230
+ /**
231
+ * Converts this map to a `SortedMap`.
232
+ */
233
+ def toSortedMap : SortedMap [K , A ] = value
121
234
122
235
/**
123
236
* Typesafe stringification method.
@@ -129,6 +242,8 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
129
242
def show (implicit A : Show [A ], K : Show [K ]): String =
130
243
s " NonEmpty ${Show [SortedMap [K , A ]].show(value)}"
131
244
245
+ override def toString : String = s " NonEmpty ${toSortedMap.toString}"
246
+
132
247
/**
133
248
* Typesafe equality operator.
134
249
*
@@ -138,8 +253,11 @@ final class NonEmptyMap[K, A] private(val value: SortedMap[K, A]) extends AnyVal
138
253
* universal equality provided by .equals.
139
254
*/
140
255
def === (that : NonEmptyMap [K , A ])(implicit A : Eq [A ]): Boolean =
141
- Eq [SortedMap [K , A ]].eqv(value, that.toMap )
256
+ Eq [SortedMap [K , A ]].eqv(value, that.toSortedMap )
142
257
258
+ /**
259
+ * Alias for [[size ]]
260
+ */
143
261
def length : Int = size
144
262
145
263
}
0 commit comments