Skip to content

Commit 88b0fcd

Browse files
ragnesebalazstothofficial
authored andcommitted
Corrects NonEmptyList.plus(T) accidentally calling one of the locally defined plus operators...
NonEmptyList.plus(List) is redundant with NonEmptyList.plus(Iterable). Optimize NonEmptyList.plus(Sequence) and NonEmptyList.plus(Array) to not allocate a temporary List.
1 parent 346d2ec commit 88b0fcd

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
package com.quickbirdstudios.nonEmptyCollection.list
22

3-
operator fun <T> NonEmptyList<T>.plus(value: T): NonEmptyList<T> = NonEmptyList(full + value)
3+
import kotlin.collections.plus as stdPlus
44

5-
operator fun <T> List<T>.plus(value: T): NonEmptyList<T> = NonEmptyList(this as Collection<T> + value)
5+
operator fun <T> NonEmptyList<T>.plus(value: T): NonEmptyList<T> = full + value
66

7-
operator fun <T> NonEmptyList<T>.plus(other: List<T>): NonEmptyList<T> = NonEmptyList(full as Collection<T> + other)
7+
operator fun <T> List<T>.plus(value: T): NonEmptyList<T> = NonEmptyList(this.stdPlus(value))
8+
9+
operator fun <T> NonEmptyList<T>.plus(other: Iterable<T>): NonEmptyList<T> = NonEmptyList(full.stdPlus(other))
810

911
operator fun <T> List<T>.plus(
1012
other: NonEmptyList<T>
11-
): NonEmptyList<T> = NonEmptyList(this as Collection<T> + other.full)
13+
): NonEmptyList<T> = NonEmptyList(this.stdPlus(other.full))
1214

1315
operator fun <T> NonEmptyList<T>.plus(
1416
other: NonEmptyList<T>
15-
): NonEmptyList<T> = NonEmptyList(full as Collection<T> + other.full)
16-
17-
operator fun <T> NonEmptyList<T>.plus(
18-
other: Iterable<T>
19-
): NonEmptyList<T> = this + other.toList()
17+
): NonEmptyList<T> = full + other
2018

2119
operator fun <T> NonEmptyList<T>.plus(
2220
other: Sequence<T>
23-
): NonEmptyList<T> = this + other.toList()
21+
): NonEmptyList<T> = NonEmptyList(
22+
ArrayList<T>(full.size).apply {
23+
addAll(full)
24+
addAll(other)
25+
}
26+
)
2427

2528
operator fun <T> NonEmptyList<T>.plus(
2629
other: Array<T>
27-
): NonEmptyList<T> = this + other.toList()
30+
): NonEmptyList<T> = NonEmptyList(
31+
ArrayList<T>(full.size + other.size).apply {
32+
addAll(full)
33+
addAll(other)
34+
}
35+
)

0 commit comments

Comments
 (0)