Skip to content

Commit 65f817c

Browse files
authored
Align generic name of non-empty collections with the underlying ones (#3549)
1 parent e612c10 commit 65f817c

File tree

3 files changed

+122
-122
lines changed

3 files changed

+122
-122
lines changed

arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyCollection.kt

+16-16
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@ package arrow.core
44
* Common interface for collections that always have
55
* at least one element (available from [head]).
66
*/
7-
public interface NonEmptyCollection<out A> : Collection<A> {
7+
public interface NonEmptyCollection<out E> : Collection<E> {
88
override fun isEmpty(): Boolean = false
9-
public val head: A
9+
public val head: E
1010

11-
public operator fun plus(element: @UnsafeVariance A): NonEmptyCollection<A>
12-
public operator fun plus(elements: Iterable<@UnsafeVariance A>): NonEmptyCollection<A>
11+
public operator fun plus(element: @UnsafeVariance E): NonEmptyCollection<E>
12+
public operator fun plus(elements: Iterable<@UnsafeVariance E>): NonEmptyCollection<E>
1313

14-
public fun toNonEmptySet(): NonEmptySet<A> = toNonEmptySetOrNull()!!
15-
public fun toNonEmptyList(): NonEmptyList<A> = toNonEmptyListOrNull()!!
14+
public fun toNonEmptySet(): NonEmptySet<E> = toNonEmptySetOrNull()!!
15+
public fun toNonEmptyList(): NonEmptyList<E> = toNonEmptyListOrNull()!!
1616

1717
// These functions take precedence over the extensions in [Collection].
1818
// This way non-emptiness is tracked by the type system.
1919

20-
public fun firstOrNull(): A = head
21-
public fun lastOrNull(): A
20+
public fun firstOrNull(): E = head
21+
public fun lastOrNull(): E
2222

23-
public fun distinct(): NonEmptyList<A> =
23+
public fun distinct(): NonEmptyList<E> =
2424
delegate { it.distinct() }
25-
public fun <K> distinctBy(selector: (A) -> K): NonEmptyList<A> =
25+
public fun <K> distinctBy(selector: (E) -> K): NonEmptyList<E> =
2626
delegate { it.distinctBy(selector) }
27-
public fun <B> flatMap(transform: (A) -> NonEmptyCollection<B>): NonEmptyList<B> =
27+
public fun <T> flatMap(transform: (E) -> NonEmptyCollection<T>): NonEmptyList<T> =
2828
delegate { it.flatMap(transform) }
29-
public fun <B> map(transform: (A) -> B): NonEmptyList<B> =
29+
public fun <T> map(transform: (E) -> T): NonEmptyList<T> =
3030
delegate { it.map(transform) }
31-
public fun <B> mapIndexed(transform: (index:Int, A) -> B): NonEmptyList<B> =
31+
public fun <T> mapIndexed(transform: (index:Int, E) -> T): NonEmptyList<T> =
3232
delegate { it.mapIndexed(transform) }
33-
public fun <B> zip(other: NonEmptyCollection<B>): NonEmptyCollection<Pair<A, B>> =
33+
public fun <T> zip(other: NonEmptyCollection<T>): NonEmptyCollection<Pair<E, T>> =
3434
delegate { it.zip(other) }
3535

3636
/**
3737
* Convenience method which delegates the implementation to [Collection],
3838
* and wraps the resulting [List] as a non-empty one.
3939
*/
40-
private inline fun <R> delegate(crossinline f: (Collection<A>) -> List<R>): NonEmptyList<R> =
41-
f(this as Collection<A>).toNonEmptyListOrNull()!!
40+
private inline fun <T> delegate(crossinline f: (Collection<E>) -> List<T>): NonEmptyList<T> =
41+
f(this as Collection<E>).toNonEmptyListOrNull()!!
4242
}

arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt

+79-79
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ public typealias Nel<A> = NonEmptyList<A>
153153
*
154154
*/
155155
@JvmInline
156-
public value class NonEmptyList<out A> @PublishedApi internal constructor(
157-
public val all: List<A>
158-
) : List<A> by all, NonEmptyCollection<A> {
156+
public value class NonEmptyList<out E> @PublishedApi internal constructor(
157+
public val all: List<E>
158+
) : List<E> by all, NonEmptyCollection<E> {
159159

160-
public constructor(head: A, tail: List<A>): this(listOf(head) + tail)
160+
public constructor(head: E, tail: List<E>): this(listOf(head) + tail)
161161

162162
@Suppress("RESERVED_MEMBER_INSIDE_VALUE_CLASS")
163163
override fun equals(other: Any?): Boolean = when (other) {
@@ -170,56 +170,56 @@ public value class NonEmptyList<out A> @PublishedApi internal constructor(
170170

171171
override fun isEmpty(): Boolean = false
172172

173-
public fun toList(): List<A> = all
173+
public fun toList(): List<E> = all
174174

175-
public override val head: A
175+
public override val head: E
176176
get() = all.first()
177177

178-
public val tail: List<A>
178+
public val tail: List<E>
179179
get() = all.drop(1)
180180

181-
override fun lastOrNull(): A = when {
181+
override fun lastOrNull(): E = when {
182182
tail.isNotEmpty() -> tail.last()
183183
else -> head
184184
}
185185

186186
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
187-
public override inline fun distinct(): NonEmptyList<A> =
187+
public override inline fun distinct(): NonEmptyList<E> =
188188
NonEmptyList(all.distinct())
189189

190190
@Suppress("OVERRIDE_BY_INLINE")
191-
public override inline fun <K> distinctBy(selector: (A) -> K): NonEmptyList<A> =
191+
public override inline fun <K> distinctBy(selector: (E) -> K): NonEmptyList<E> =
192192
NonEmptyList(all.distinctBy(selector))
193193

194194
@Suppress("OVERRIDE_BY_INLINE")
195-
public override inline fun <B> map(transform: (A) -> B): NonEmptyList<B> =
195+
public override inline fun <T> map(transform: (E) -> T): NonEmptyList<T> =
196196
NonEmptyList(all.map(transform))
197197

198198
@Suppress("OVERRIDE_BY_INLINE")
199-
public override inline fun <B> flatMap(transform: (A) -> NonEmptyCollection<B>): NonEmptyList<B> =
199+
public override inline fun <T> flatMap(transform: (E) -> NonEmptyCollection<T>): NonEmptyList<T> =
200200
NonEmptyList(all.flatMap(transform))
201201

202202
@Suppress("OVERRIDE_BY_INLINE")
203-
public override inline fun <B> mapIndexed(transform: (index: Int, A) -> B): NonEmptyList<B> =
203+
public override inline fun <T> mapIndexed(transform: (index: Int, E) -> T): NonEmptyList<T> =
204204
NonEmptyList(transform(0, head), tail.mapIndexed { ix, e -> transform(ix + 1, e) })
205205

206-
public operator fun plus(l: NonEmptyList<@UnsafeVariance A>): NonEmptyList<A> =
206+
public operator fun plus(l: NonEmptyList<@UnsafeVariance E>): NonEmptyList<E> =
207207
this + l.all
208208

209-
public override operator fun plus(elements: Iterable<@UnsafeVariance A>): NonEmptyList<A> =
209+
public override operator fun plus(elements: Iterable<@UnsafeVariance E>): NonEmptyList<E> =
210210
NonEmptyList(all + elements)
211211

212-
public override operator fun plus(element: @UnsafeVariance A): NonEmptyList<A> =
212+
public override operator fun plus(element: @UnsafeVariance E): NonEmptyList<E> =
213213
NonEmptyList(all + element)
214214

215-
public inline fun <B> foldLeft(b: B, f: (B, A) -> B): B {
215+
public inline fun <Acc> foldLeft(b: Acc, f: (Acc, E) -> Acc): Acc {
216216
contract { callsInPlace(f, InvocationKind.AT_LEAST_ONCE) }
217217
var accumulator = f(b, head)
218218
for (element in tail) accumulator = f(accumulator, element)
219219
return accumulator
220220
}
221221

222-
public inline fun <B> coflatMap(f: (NonEmptyList<A>) -> B): NonEmptyList<B> {
222+
public inline fun <T> coflatMap(f: (NonEmptyList<E>) -> T): NonEmptyList<T> {
223223
contract { callsInPlace(f, InvocationKind.AT_LEAST_ONCE) }
224224
var current = this
225225
return buildList {
@@ -230,19 +230,19 @@ public value class NonEmptyList<out A> @PublishedApi internal constructor(
230230
}.let(::NonEmptyList)
231231
}
232232

233-
public fun extract(): A =
233+
public fun extract(): E =
234234
this.head
235235

236236
override fun toString(): String =
237237
"NonEmptyList(${all.joinToString()})"
238238

239-
public fun <B> align(b: NonEmptyList<B>): NonEmptyList<Ior<A, B>> =
240-
NonEmptyList(all.align(b))
239+
public fun <T> align(other: NonEmptyList<T>): NonEmptyList<Ior<E, T>> =
240+
NonEmptyList(all.align(other))
241241

242-
public fun <B> padZip(other: NonEmptyList<B>): NonEmptyList<Pair<A?, B?>> =
242+
public fun <T> padZip(other: NonEmptyList<T>): NonEmptyList<Pair<E?, T?>> =
243243
padZip(other, { it to null }, { null to it }, { a, b -> a to b })
244244

245-
public inline fun <B, C> padZip(other: NonEmptyList<B>, left: (A) -> C, right: (B) -> C, both: (A, B) -> C): NonEmptyList<C> {
245+
public inline fun <B, C> padZip(other: NonEmptyList<B>, left: (E) -> C, right: (B) -> C, both: (E, B) -> C): NonEmptyList<C> {
246246
contract { callsInPlace(both, InvocationKind.AT_LEAST_ONCE) }
247247
return NonEmptyList(both(head, other.head), tail.padZip(other.tail, left, right) { a, b -> both(a, b) })
248248
}
@@ -253,12 +253,12 @@ public value class NonEmptyList<out A> @PublishedApi internal constructor(
253253
nonEmptyListOf(Unit)
254254
}
255255

256-
public fun <B> zip(fb: NonEmptyList<B>): NonEmptyList<Pair<A, B>> =
257-
zip(fb, ::Pair)
256+
public fun <T> zip(other: NonEmptyList<T>): NonEmptyList<Pair<E, T>> =
257+
zip(other, ::Pair)
258258

259259
public inline fun <B, Z> zip(
260260
b: NonEmptyList<B>,
261-
map: (A, B) -> Z
261+
map: (E, B) -> Z
262262
): NonEmptyList<Z> {
263263
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
264264
return NonEmptyList(map(head, b.head), tail.zip(b.tail) { a, bb -> map(a, bb) })
@@ -267,7 +267,7 @@ public value class NonEmptyList<out A> @PublishedApi internal constructor(
267267
public inline fun <B, C, Z> zip(
268268
b: NonEmptyList<B>,
269269
c: NonEmptyList<C>,
270-
map: (A, B, C) -> Z
270+
map: (E, B, C) -> Z
271271
): NonEmptyList<Z> {
272272
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
273273
return NonEmptyList(map(head, b.head, c.head), tail.zip(b.tail, c.tail) { a, bb, cc -> map(a, bb, cc) })
@@ -277,152 +277,152 @@ public value class NonEmptyList<out A> @PublishedApi internal constructor(
277277
b: NonEmptyList<B>,
278278
c: NonEmptyList<C>,
279279
d: NonEmptyList<D>,
280-
map: (A, B, C, D) -> Z
280+
map: (E, B, C, D) -> Z
281281
): NonEmptyList<Z> {
282282
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
283283
return NonEmptyList(map(head, b.head, c.head, d.head), tail.zip(b.tail, c.tail, d.tail) { a, bb, cc, dd -> map(a, bb, cc, dd) })
284284
}
285285

286-
public inline fun <B, C, D, E, Z> zip(
286+
public inline fun <B, C, D, F, Z> zip(
287287
b: NonEmptyList<B>,
288288
c: NonEmptyList<C>,
289289
d: NonEmptyList<D>,
290-
e: NonEmptyList<E>,
291-
map: (A, B, C, D, E) -> Z
290+
e: NonEmptyList<F>,
291+
map: (E, B, C, D, F) -> Z
292292
): NonEmptyList<Z> {
293293
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
294294
return NonEmptyList(map(head, b.head, c.head, d.head, e.head), tail.zip(b.tail, c.tail, d.tail, e.tail) { a, bb, cc, dd, ee -> map(a, bb, cc, dd, ee) })
295295
}
296296

297-
public inline fun <B, C, D, E, F, Z> zip(
297+
public inline fun <B, C, D, F, G, Z> zip(
298298
b: NonEmptyList<B>,
299299
c: NonEmptyList<C>,
300300
d: NonEmptyList<D>,
301-
e: NonEmptyList<E>,
302-
f: NonEmptyList<F>,
303-
map: (A, B, C, D, E, F) -> Z
301+
e: NonEmptyList<F>,
302+
f: NonEmptyList<G>,
303+
map: (E, B, C, D, F, G) -> Z
304304
): NonEmptyList<Z> {
305305
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
306306
return NonEmptyList(map(head, b.head, c.head, d.head, e.head, f.head), tail.zip(b.tail, c.tail, d.tail, e.tail, f.tail) { a, bb, cc, dd, ee, ff -> map(a, bb, cc, dd, ee, ff) })
307307
}
308308

309-
public inline fun <B, C, D, E, F, G, Z> zip(
309+
public inline fun <B, C, D, F, G, H, Z> zip(
310310
b: NonEmptyList<B>,
311311
c: NonEmptyList<C>,
312312
d: NonEmptyList<D>,
313-
e: NonEmptyList<E>,
314-
f: NonEmptyList<F>,
315-
g: NonEmptyList<G>,
316-
map: (A, B, C, D, E, F, G) -> Z
313+
e: NonEmptyList<F>,
314+
f: NonEmptyList<G>,
315+
g: NonEmptyList<H>,
316+
map: (E, B, C, D, F, G, H) -> Z
317317
): NonEmptyList<Z> {
318318
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
319319
return NonEmptyList(map(head, b.head, c.head, d.head, e.head, f.head, g.head), tail.zip(b.tail, c.tail, d.tail, e.tail, f.tail, g.tail) { a, bb, cc, dd, ee, ff, gg -> map(a, bb, cc, dd, ee, ff, gg) })
320320
}
321321

322-
public inline fun <B, C, D, E, F, G, H, Z> zip(
322+
public inline fun <B, C, D, F, G, H, I, Z> zip(
323323
b: NonEmptyList<B>,
324324
c: NonEmptyList<C>,
325325
d: NonEmptyList<D>,
326-
e: NonEmptyList<E>,
327-
f: NonEmptyList<F>,
328-
g: NonEmptyList<G>,
329-
h: NonEmptyList<H>,
330-
map: (A, B, C, D, E, F, G, H) -> Z
326+
e: NonEmptyList<F>,
327+
f: NonEmptyList<G>,
328+
g: NonEmptyList<H>,
329+
h: NonEmptyList<I>,
330+
map: (E, B, C, D, F, G, H, I) -> Z
331331
): NonEmptyList<Z> {
332332
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
333333
return NonEmptyList(map(head, b.head, c.head, d.head, e.head, f.head, g.head, h.head), tail.zip(b.tail, c.tail, d.tail, e.tail, f.tail, g.tail, h.tail) { a, bb, cc, dd, ee, ff, gg, hh -> map(a, bb, cc, dd, ee, ff, gg, hh) })
334334
}
335335

336-
public inline fun <B, C, D, E, F, G, H, I, Z> zip(
336+
public inline fun <B, C, D, F, G, H, I, J, Z> zip(
337337
b: NonEmptyList<B>,
338338
c: NonEmptyList<C>,
339339
d: NonEmptyList<D>,
340-
e: NonEmptyList<E>,
341-
f: NonEmptyList<F>,
342-
g: NonEmptyList<G>,
343-
h: NonEmptyList<H>,
344-
i: NonEmptyList<I>,
345-
map: (A, B, C, D, E, F, G, H, I) -> Z
340+
e: NonEmptyList<F>,
341+
f: NonEmptyList<G>,
342+
g: NonEmptyList<H>,
343+
h: NonEmptyList<I>,
344+
i: NonEmptyList<J>,
345+
map: (E, B, C, D, F, G, H, I, J) -> Z
346346
): NonEmptyList<Z> {
347347
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
348348
return NonEmptyList(map(head, b.head, c.head, d.head, e.head, f.head, g.head, h.head, i.head), tail.zip(b.tail, c.tail, d.tail, e.tail, f.tail, g.tail, h.tail, i.tail) { a, bb, cc, dd, ee, ff, gg, hh, ii -> map(a, bb, cc, dd, ee, ff, gg, hh, ii) })
349349
}
350350

351-
public inline fun <B, C, D, E, F, G, H, I, J, Z> zip(
351+
public inline fun <B, C, D, F, G, H, I, J, K, Z> zip(
352352
b: NonEmptyList<B>,
353353
c: NonEmptyList<C>,
354354
d: NonEmptyList<D>,
355-
e: NonEmptyList<E>,
356-
f: NonEmptyList<F>,
357-
g: NonEmptyList<G>,
358-
h: NonEmptyList<H>,
359-
i: NonEmptyList<I>,
360-
j: NonEmptyList<J>,
361-
map: (A, B, C, D, E, F, G, H, I, J) -> Z
355+
e: NonEmptyList<F>,
356+
f: NonEmptyList<G>,
357+
g: NonEmptyList<H>,
358+
h: NonEmptyList<I>,
359+
i: NonEmptyList<J>,
360+
j: NonEmptyList<K>,
361+
map: (E, B, C, D, F, G, H, I, J, K) -> Z
362362
): NonEmptyList<Z> {
363363
contract { callsInPlace(map, InvocationKind.AT_LEAST_ONCE) }
364364
return NonEmptyList(map(head, b.head, c.head, d.head, e.head, f.head, g.head, h.head, i.head, j.head), tail.zip(b.tail, c.tail, d.tail, e.tail, f.tail, g.tail, h.tail, i.tail, j.tail) { a, bb, cc, dd, ee, ff, gg, hh, ii, jj -> map(a, bb, cc, dd, ee, ff, gg, hh, ii, jj) })
365365
}
366366
}
367367

368368
@JvmName("nonEmptyListOf")
369-
public fun <A> nonEmptyListOf(head: A, vararg t: A): NonEmptyList<A> =
369+
public fun <E> nonEmptyListOf(head: E, vararg t: E): NonEmptyList<E> =
370370
NonEmptyList(listOf(head) + t)
371371

372372
@JvmName("nel")
373373
@Suppress("NOTHING_TO_INLINE")
374-
public inline fun <A> A.nel(): NonEmptyList<A> =
374+
public inline fun <E> E.nel(): NonEmptyList<E> =
375375
NonEmptyList(listOf(this))
376376

377-
public operator fun <A : Comparable<A>> NonEmptyList<A>.compareTo(other: NonEmptyList<A>): Int =
377+
public operator fun <E : Comparable<E>> NonEmptyList<E>.compareTo(other: NonEmptyList<E>): Int =
378378
all.compareTo(other.all)
379379

380-
public fun <A> NonEmptyList<NonEmptyList<A>>.flatten(): NonEmptyList<A> =
380+
public fun <E> NonEmptyList<NonEmptyList<E>>.flatten(): NonEmptyList<E> =
381381
this.flatMap(::identity)
382382

383-
public inline fun <A, B : Comparable<B>> NonEmptyList<A>.minBy(selector: (A) -> B): A =
383+
public inline fun <E, T : Comparable<T>> NonEmptyList<E>.minBy(selector: (E) -> T): E =
384384
minByOrNull(selector)!!
385385

386-
public inline fun <A, B : Comparable<B>> NonEmptyList<A>.maxBy(selector: (A) -> B): A =
386+
public inline fun <E, T : Comparable<T>> NonEmptyList<E>.maxBy(selector: (E) -> T): E =
387387
maxByOrNull(selector)!!
388388

389389
@Suppress("NOTHING_TO_INLINE")
390-
public inline fun <T : Comparable<T>> NonEmptyList<T>.min(): T =
390+
public inline fun <E : Comparable<E>> NonEmptyList<E>.min(): E =
391391
minOrNull()!!
392392

393393
@Suppress("NOTHING_TO_INLINE")
394-
public inline fun <T : Comparable<T>> NonEmptyList<T>.max(): T =
394+
public inline fun <E : Comparable<E>> NonEmptyList<E>.max(): E =
395395
maxOrNull()!!
396396

397397
public fun <A, B> NonEmptyList<Pair<A, B>>.unzip(): Pair<NonEmptyList<A>, NonEmptyList<B>> =
398398
this.unzip(::identity)
399399

400400
@Suppress("WRONG_INVOCATION_KIND")
401-
public inline fun <A, B, C> NonEmptyList<C>.unzip(f: (C) -> Pair<A, B>): Pair<NonEmptyList<A>, NonEmptyList<B>> {
401+
public inline fun <A, B, E> NonEmptyList<E>.unzip(f: (E) -> Pair<A, B>): Pair<NonEmptyList<A>, NonEmptyList<B>> {
402402
contract { callsInPlace(f, InvocationKind.AT_LEAST_ONCE) }
403403
return map { f(it) }.stdlibUnzip().let { (l1, l2) ->
404404
l1.toNonEmptyListOrNull()!! to l2.toNonEmptyListOrNull()!!
405405
}
406406
}
407407

408-
public inline fun <E, A, B> NonEmptyList<A>.mapOrAccumulate(
409-
combine: (E, E) -> E,
410-
@BuilderInference transform: RaiseAccumulate<E>.(A) -> B
411-
): Either<E, NonEmptyList<B>> =
408+
public inline fun <Error, E, T> NonEmptyList<E>.mapOrAccumulate(
409+
combine: (Error, Error) -> Error,
410+
@BuilderInference transform: RaiseAccumulate<Error>.(E) -> T
411+
): Either<Error, NonEmptyList<T>> =
412412
all.mapOrAccumulate(combine, transform).map { requireNotNull(it.toNonEmptyListOrNull()) }
413413

414-
public inline fun <E, A, B> NonEmptyList<A>.mapOrAccumulate(
415-
@BuilderInference transform: RaiseAccumulate<E>.(A) -> B
416-
): Either<NonEmptyList<E>, NonEmptyList<B>> =
414+
public inline fun <Error, E, T> NonEmptyList<E>.mapOrAccumulate(
415+
@BuilderInference transform: RaiseAccumulate<Error>.(E) -> T
416+
): Either<NonEmptyList<Error>, NonEmptyList<T>> =
417417
all.mapOrAccumulate(transform).map { requireNotNull(it.toNonEmptyListOrNull()) }
418418

419419
@JvmName("toNonEmptyListOrNull")
420-
public fun <A> Iterable<A>.toNonEmptyListOrNull(): NonEmptyList<A>? {
420+
public fun <T> Iterable<T>.toNonEmptyListOrNull(): NonEmptyList<T>? {
421421
val iter = iterator()
422422
if (!iter.hasNext()) return null
423423
return NonEmptyList(iter.next(), Iterable { iter }.toList())
424424
}
425425

426426
@JvmName("toNonEmptyListOrNone")
427-
public fun <A> Iterable<A>.toNonEmptyListOrNone(): Option<NonEmptyList<A>> =
427+
public fun <T> Iterable<T>.toNonEmptyListOrNone(): Option<NonEmptyList<T>> =
428428
toNonEmptyListOrNull().toOption()

0 commit comments

Comments
 (0)