-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathNonEmptyList.kt
554 lines (489 loc) · 17.8 KB
/
NonEmptyList.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
@file:OptIn(ExperimentalTypeInference::class)
package arrow.core
import arrow.core.raise.RaiseAccumulate
import arrow.core.raise.either
import arrow.core.raise.option
import arrow.typeclasses.Semigroup
import arrow.typeclasses.SemigroupDeprecation
import arrow.typeclasses.combine
import kotlin.experimental.ExperimentalTypeInference
import kotlin.jvm.JvmStatic
public typealias Nel<A> = NonEmptyList<A>
/**
* `NonEmptyList` is a data type used in __Λrrow__ to model ordered lists that guarantee to have at least one value.
*
* ## Constructing NonEmptyList
*
* A `NonEmptyList` guarantees the list always has at least 1 element.
*
* ```kotlin
* import arrow.core.nonEmptyListOf
* import arrow.core.toNonEmptyListOrNull
*
* fun main() {
* println(nonEmptyListOf(1, 2, 3, 4, 5))
* println(listOf(1, 2, 3).toNonEmptyListOrNull())
* println(emptyList<Int>().toNonEmptyListOrNull())
* }
* ```
* <!--- KNIT example-nonemptylist-01.kt -->
* ```text
* NonEmptyList(1, 2, 3, 4, 5)
* NonEmptyList(1, 2, 3)
* null
* ```
*
* ## head
*
* Unlike `List[0]`, `NonEmptyList.head` it's a safe operation that guarantees no exception throwing.
*
* ```kotlin
* import arrow.core.nonEmptyListOf
*
* val value =
* //sampleStart
* nonEmptyListOf(1, 2, 3, 4, 5).head
* //sampleEnd
* fun main() {
* println(value)
* }
* ```
* <!--- KNIT example-nonemptylist-02.kt -->
*
* ## foldLeft
*
* When we fold over a `NonEmptyList`, we turn a `NonEmptyList< A >` into `B` by providing a __seed__ value and a __function__ that carries the state on each iteration over the elements of the list.
* The first argument is a function that addresses the __seed value__, this can be any object of any type which will then become the resulting typed value.
* The second argument is a function that takes the current state and element in the iteration and returns the new state after transformations have been applied.
*
* ```kotlin
* import arrow.core.NonEmptyList
* import arrow.core.nonEmptyListOf
*
* //sampleStart
* fun sumNel(nel: NonEmptyList<Int>): Int =
* nel.foldLeft(0) { acc, n -> acc + n }
* val value = sumNel(nonEmptyListOf(1, 1, 1, 1))
* //sampleEnd
* fun main() {
* println("value = $value")
* }
* ```
* <!--- KNIT example-nonemptylist-03.kt -->
*
* ## map
*
* `map` allows us to transform `A` into `B` in `NonEmptyList< A >`
*
* ```kotlin
* import arrow.core.nonEmptyListOf
*
* val value =
* //sampleStart
* nonEmptyListOf(1, 1, 1, 1).map { it + 1 }
* //sampleEnd
* fun main() {
* println(value)
* }
* ```
* <!--- KNIT example-nonemptylist-04.kt -->
*
* ## Combining NonEmptyLists
*
* ### flatMap
*
* `flatMap` allows us to compute over the contents of multiple `NonEmptyList< * >` values
*
* ```kotlin
* import arrow.core.NonEmptyList
* import arrow.core.nonEmptyListOf
*
* //sampleStart
* val nelOne: NonEmptyList<Int> = nonEmptyListOf(1, 2, 3)
* val nelTwo: NonEmptyList<Int> = nonEmptyListOf(4, 5)
*
* val value = nelOne.flatMap { one ->
* nelTwo.map { two ->
* one + two
* }
* }
* //sampleEnd
* fun main() {
* println("value = $value")
* }
* ```
* <!--- KNIT example-nonemptylist-05.kt -->
*
* ### zip
*
* Λrrow contains methods that allow you to preserve type information when computing over different `NonEmptyList` typed values.
*
* ```kotlin
* import arrow.core.NonEmptyList
* import arrow.core.nonEmptyListOf
* import kotlin.random.Random
*
* data class Person(val id: Long, val name: String, val year: Int)
*
* // Note each NonEmptyList is of a different type
* val nelId: NonEmptyList<Long> = nonEmptyListOf(Random.nextLong(), Random.nextLong())
* val nelName: NonEmptyList<String> = nonEmptyListOf("William Alvin Howard", "Haskell Curry")
* val nelYear: NonEmptyList<Int> = nonEmptyListOf(1926, 1900)
*
* val value = nelId.zip(nelName, nelYear) { id, name, year ->
* Person(id, name, year)
* }
* //sampleEnd
* fun main() {
* println("value = $value")
* }
* ```
* <!--- KNIT example-nonemptylist-06.kt -->
*
* ### Summary
*
* - `NonEmptyList` is __used to model lists that guarantee at least one element__
* - We can easily construct values of `NonEmptyList` with `nonEmptyListOf`
* - `foldLeft`, `map`, `flatMap` and others are used to compute over the internal contents of a `NonEmptyList` value.
* - `a.zip(b, c) { ... }` can be used to compute over multiple `NonEmptyList` values preserving type information and __abstracting over arity__ with `zip`
*
*/
public class NonEmptyList<out A>(
public val head: A,
public val tail: List<A>
) : AbstractList<A>() {
private constructor(list: List<A>) : this(list[0], list.drop(1))
override val size: Int =
1 + tail.size
public val all: List<A>
get() = toList()
public override operator fun get(index: Int): A {
if (index < 0 || index >= size) throw IndexOutOfBoundsException("$index is not in 1..${size - 1}")
return if (index == 0) head else tail[index - 1]
}
override fun isEmpty(): Boolean = false
public fun toList(): List<A> = listOf(head) + tail
public inline fun <B> map(f: (A) -> B): NonEmptyList<B> =
NonEmptyList(f(head), tail.map(f))
public inline fun <B> flatMap(f: (A) -> NonEmptyList<B>): NonEmptyList<B> =
f(head) + tail.flatMap { f(it).all }
public operator fun plus(l: NonEmptyList<@UnsafeVariance A>): NonEmptyList<A> =
NonEmptyList(all + l.all)
public operator fun plus(l: List<@UnsafeVariance A>): NonEmptyList<A> =
NonEmptyList(all + l)
public operator fun plus(a: @UnsafeVariance A): NonEmptyList<A> =
NonEmptyList(all + a)
public inline fun <B> foldLeft(b: B, f: (B, A) -> B): B =
this.tail.fold(f(b, this.head), f)
public fun <B> coflatMap(f: (NonEmptyList<A>) -> B): NonEmptyList<B> {
val buf = mutableListOf<B>()
tailrec fun consume(list: List<A>): List<B> =
if (list.isEmpty()) {
buf
} else {
val tail = list.subList(1, list.size)
buf += f(NonEmptyList(list[0], tail))
consume(tail)
}
return NonEmptyList(f(this), consume(this.tail))
}
public fun extract(): A =
this.head
override fun equals(other: Any?): Boolean =
super.equals(other)
override fun hashCode(): Int =
super.hashCode()
override fun toString(): String =
"NonEmptyList(${all.joinToString()})"
public fun <B> align(b: NonEmptyList<B>): NonEmptyList<Ior<A, B>> =
NonEmptyList(Ior.Both(head, b.head), tail.align(b.tail))
@Deprecated(SemigroupDeprecation, ReplaceWith("padZip(b, ::identity, ::identity, {a1, a2 -> a1 + a2})"))
public fun salign(SA: Semigroup<@UnsafeVariance A>, b: NonEmptyList<@UnsafeVariance A>): NonEmptyList<A> =
padZip(b, ::identity, ::identity, SA::combine)
public fun <B> padZip(other: NonEmptyList<B>): NonEmptyList<Pair<A?, B?>> =
padZip(other, { it to null }, { null to it }, { a, b -> a to b })
public inline fun <B, C> padZip(other: NonEmptyList<B>, left: (A) -> C, right: (B) -> C, both: (A, B) -> C): NonEmptyList<C> =
NonEmptyList(both(head, other.head), tail.padZip(other.tail, left, right, both))
public companion object {
@Deprecated(
"Use toNonEmptyListOrNull instead",
ReplaceWith(
"l.toNonEmptyListOrNull().toOption()",
"import arrow.core.toNonEmptyListOrNull",
"import arrow.core.toOption"
)
)
@JvmStatic
public fun <A> fromList(l: List<A>): Option<NonEmptyList<A>> =
if (l.isEmpty()) None else Some(NonEmptyList(l))
@Deprecated(
"Use toNonEmptyListOrNull instead",
ReplaceWith(
"l.toNonEmptyListOrNull() ?: throw IndexOutOfBoundsException(\"Empty list doesn't contain element at index 0.\")",
"import arrow.core.toNonEmptyListOrNull"
)
)
@JvmStatic
public fun <A> fromListUnsafe(l: List<A>): NonEmptyList<A> =
NonEmptyList(l)
@PublishedApi
internal val unit: NonEmptyList<Unit> =
nonEmptyListOf(Unit)
}
public fun <B> zip(fb: NonEmptyList<B>): NonEmptyList<Pair<A, B>> =
zip(fb, ::Pair)
public inline fun <B, Z> zip(
b: NonEmptyList<B>,
map: (A, B) -> Z
): NonEmptyList<Z> =
NonEmptyList(
map(head, b.head),
tail.zip(b.tail, map)
)
public inline fun <B, C, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
map: (A, B, C) -> Z
): NonEmptyList<Z> =
NonEmptyList(
map(head, b.head, c.head),
tail.zip(b.tail, c.tail, map)
)
public inline fun <B, C, D, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
map: (A, B, C, D) -> Z
): NonEmptyList<Z> =
NonEmptyList(
map(head, b.head, c.head, d.head),
tail.zip(b.tail, c.tail, d.tail, map)
)
public inline fun <B, C, D, E, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
e: NonEmptyList<E>,
map: (A, B, C, D, E) -> Z
): NonEmptyList<Z> =
NonEmptyList(
map(head, b.head, c.head, d.head, e.head),
tail.zip(b.tail, c.tail, d.tail, e.tail, map)
)
public inline fun <B, C, D, E, F, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
e: NonEmptyList<E>,
f: NonEmptyList<F>,
map: (A, B, C, D, E, F) -> Z
): NonEmptyList<Z> =
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, map)
)
public inline fun <B, C, D, E, F, G, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
e: NonEmptyList<E>,
f: NonEmptyList<F>,
g: NonEmptyList<G>,
map: (A, B, C, D, E, F, G) -> Z
): NonEmptyList<Z> =
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, map)
)
public inline fun <B, C, D, E, F, G, H, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
e: NonEmptyList<E>,
f: NonEmptyList<F>,
g: NonEmptyList<G>,
h: NonEmptyList<H>,
map: (A, B, C, D, E, F, G, H) -> Z
): NonEmptyList<Z> =
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, map)
)
public inline fun <B, C, D, E, F, G, H, I, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
e: NonEmptyList<E>,
f: NonEmptyList<F>,
g: NonEmptyList<G>,
h: NonEmptyList<H>,
i: NonEmptyList<I>,
map: (A, B, C, D, E, F, G, H, I) -> Z
): NonEmptyList<Z> =
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, map)
)
public inline fun <B, C, D, E, F, G, H, I, J, Z> zip(
b: NonEmptyList<B>,
c: NonEmptyList<C>,
d: NonEmptyList<D>,
e: NonEmptyList<E>,
f: NonEmptyList<F>,
g: NonEmptyList<G>,
h: NonEmptyList<H>,
i: NonEmptyList<I>,
j: NonEmptyList<J>,
map: (A, B, C, D, E, F, G, H, I, J) -> Z
): NonEmptyList<Z> =
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, map)
)
}
public fun <A> nonEmptyListOf(head: A, vararg t: A): NonEmptyList<A> =
NonEmptyList(head, t.asList())
public inline fun <A> A.nel(): NonEmptyList<A> =
nonEmptyListOf(this)
public operator fun <A : Comparable<A>> NonEmptyList<A>.compareTo(other: NonEmptyList<A>): Int =
all.compareTo(other.all)
public fun <A> NonEmptyList<NonEmptyList<A>>.flatten(): NonEmptyList<A> =
this.flatMap(::identity)
public inline fun <A, B : Comparable<B>> NonEmptyList<A>.minBy(selector: (A) -> B): A =
minByOrNull(selector)!!
public inline fun <A, B : Comparable<B>> NonEmptyList<A>.maxBy(selector: (A) -> B): A =
maxByOrNull(selector)!!
public inline fun <T : Comparable<T>> NonEmptyList<T>.min(): T =
minOrNull()!!
public inline fun <T : Comparable<T>> NonEmptyList<T>.max(): T =
maxOrNull()!!
public fun <A, B> NonEmptyList<Pair<A, B>>.unzip(): Pair<NonEmptyList<A>, NonEmptyList<B>> =
this.unzip(::identity)
public fun <A, B, C> NonEmptyList<C>.unzip(f: (C) -> Pair<A, B>): Pair<NonEmptyList<A>, NonEmptyList<B>> =
this.map(f).let { nel ->
nel.tail.unzip().let {
NonEmptyList(nel.head.first, it.first) to
NonEmptyList(nel.head.second, it.second)
}
}
@Deprecated(
"Traverse for Either is being deprecated in favor of Either DSL + NonEmptyList.map.\n$NicheAPI",
ReplaceWith(
"let<NonEmptyList<A>, Either<E, NonEmptyList<B>>> { nel -> either<E, NonEmptyList<B>> { nel.map<A, B> { f(it).bind<B>() } } }",
"arrow.core.raise.either")
)
public inline fun <E, A, B> NonEmptyList<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>> =
traverse(f)
@Deprecated(
"Traverse for Either is being deprecated in favor of Either DSL + NonEmptyList.map.\n$NicheAPI",
ReplaceWith(
"let<NonEmptyList<A>, Either<E, NonEmptyList<B>>> { nel -> either<E, NonEmptyList<B>> { nel.map<B> { f(it).bind<B>() } } }",
"arrow.core.raise.either")
)
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E, A, B> NonEmptyList<A>.traverse(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>> =
let { nel -> either { nel.map { f(it).bind() } } }
@Deprecated(
"Sequence for Either is being deprecated in favor of Either DSL + NonEmptyList.map.\n$NicheAPI",
ReplaceWith("either<E, NonEmptyList<A>> { this.map<A> { it.bind<A>() } }", "arrow.core.raise.either")
)
public fun <E, A> NonEmptyList<Either<E, A>>.sequenceEither(): Either<E, NonEmptyList<A>> =
sequence()
@Deprecated(
"Sequence for Either is being deprecated in favor of Either DSL + NonEmptyList.map.\n$NicheAPI",
ReplaceWith("either<E, NonEmptyList<A>> { this.map<A> { it.bind<A>() } }", "arrow.core.raise.either")
)
public fun <E, A> NonEmptyList<Either<E, A>>.sequence(): Either<E, NonEmptyList<A>> =
traverse(::identity)
@Deprecated(
ValidatedDeprMsg + "Use the mapOrAccumulate API instead",
ReplaceWith(
"this.mapOrAccumulate<E, A, B>({ a, b -> a + b}) { f(it).bind<B>() }.toValidated()",
"arrow.core.mapOrAccumulate"
)
)
public inline fun <E, A, B> NonEmptyList<A>.traverseValidated(
semigroup: Semigroup<E>,
f: (A) -> Validated<E, B>
): Validated<E, NonEmptyList<B>> =
mapOrAccumulate({ a, b -> semigroup.run { a.combine(b) } }) { f(it).bind() }.toValidated()
@Deprecated(
ValidatedDeprMsg + "Use the mapOrAccumulate API instead",
ReplaceWith(
"this.mapOrAccumulate<E, A, B>({ a, b -> a + b}) { f(it).bind<B>() }.toValidated()",
"arrow.core.mapOrAccumulate"
)
)
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E, A, B> NonEmptyList<A>.traverse(
semigroup: Semigroup<E>,
f: (A) -> Validated<E, B>
): Validated<E, NonEmptyList<B>> =
mapOrAccumulate({ a, b -> semigroup.run { a.combine(b) } }) { f(it).bind() }.toValidated()
@Deprecated(
ValidatedDeprMsg + "Use the mapOrAccumulate API instead",
ReplaceWith(
"this.mapOrAccumulate<E, A>({ e1, e2 -> e1 + e2 }) { it.bind<A>() }.toValidated()",
"arrow.core.mapOrAccumulate"
)
)
public fun <E, A> NonEmptyList<Validated<E, A>>.sequenceValidated(semigroup: Semigroup<E>): Validated<E, NonEmptyList<A>> =
mapOrAccumulate(semigroup::combine) { it.bind() }.toValidated()
@Deprecated(
ValidatedDeprMsg + "Use the mapOrAccumulate API instead",
ReplaceWith(
"this.mapOrAccumulate<E, A>({ e1, e2 -> e1 + e2 }) { it.bind<A>() }.toValidated()",
"arrow.core.mapOrAccumulate"
)
)
public fun <E, A> NonEmptyList<Validated<E, A>>.sequence(semigroup: Semigroup<E>): Validated<E, NonEmptyList<A>> =
mapOrAccumulate(semigroup::combine) { it.bind() }.toValidated()
public inline fun <E, A, B> NonEmptyList<A>.mapOrAccumulate(
combine: (E, E) -> E,
@BuilderInference transform: RaiseAccumulate<E>.(A) -> B
): Either<E, NonEmptyList<B>> =
all.mapOrAccumulate(combine, transform).map { requireNotNull(it.toNonEmptyListOrNull()) }
public inline fun <E, A, B> NonEmptyList<A>.mapOrAccumulate(
@BuilderInference transform: RaiseAccumulate<E>.(A) -> B
): Either<NonEmptyList<E>, NonEmptyList<B>> =
all.mapOrAccumulate(transform).map { requireNotNull(it.toNonEmptyListOrNull()) }
@Deprecated(
"Traverse for Option is being deprecated in favor of Option DSL + NonEmptyList.map.\\n$NicheAPI",
ReplaceWith(
"let<NonEmptyList<A>, Option<NonEmptyList<B>>> { nel -> option<NonEmptyList<B>> { nel.map<B> { f(it).bind<B>() } } }",
"arrow.core.raise.option")
)
public inline fun <A, B> NonEmptyList<A>.traverseOption(f: (A) -> Option<B>): Option<NonEmptyList<B>> =
traverse(f)
@Deprecated(
"Traverse for Option is being deprecated in favor of Option DSL + NonEmptyList.map.\\n$NicheAPI",
ReplaceWith(
"let<NonEmptyList<A>, Option<NonEmptyList<B>>> { nel -> option<NonEmptyList<B>> { nel.map<B> { f(it).bind<B>() } } }",
"arrow.core.raise.option")
)
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <A, B> NonEmptyList<A>.traverse(f: (A) -> Option<B>): Option<NonEmptyList<B>> =
let { nel -> option { nel.map { f(it).bind() } } }
@Deprecated(
"Sequence for Option is being deprecated in favor of Option DSL + NonEmptyList.map.\\n$NicheAPI",
ReplaceWith(
"option<NonEmptyList<A>> { this.map<A> { it.bind<A>() } }",
"arrow.core.raise.option")
)
public fun <A> NonEmptyList<Option<A>>.sequenceOption(): Option<NonEmptyList<A>> =
sequence()
@Deprecated(
"Sequence for Option is being deprecated in favor of Option DSL + NonEmptyList.map.\\n$NicheAPI",
ReplaceWith(
"option<NonEmptyList<A>> { this.map<A> { it.bind<A>() } }",
"arrow.core.raise.option")
)
public fun <A> NonEmptyList<Option<A>>.sequence(): Option<NonEmptyList<A>> =
traverse(::identity)
public fun <A> Iterable<A>.toNonEmptyListOrNull(): NonEmptyList<A>? =
firstOrNull()?.let { NonEmptyList(it, drop(1)) }
public fun <A> Iterable<A>.toNonEmptyListOrNone(): Option<NonEmptyList<A>> =
toNonEmptyListOrNull().toOption()