-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fd-option-API
- Loading branch information
Showing
8 changed files
with
519 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptySet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package arrow.core | ||
|
||
import kotlin.jvm.JvmInline | ||
|
||
@JvmInline | ||
public value class NonEmptySet<out T> private constructor( | ||
private val elements: Set<T> | ||
) : Set<T> by elements { | ||
|
||
public constructor(first: T, rest: Set<T>) : this(setOf(first) + rest) | ||
|
||
public operator fun plus(set: Set<@UnsafeVariance T>): NonEmptySet<T> = | ||
NonEmptySet(elements + set) | ||
|
||
public operator fun plus(element: @UnsafeVariance T): NonEmptySet<T> = | ||
NonEmptySet(elements + element) | ||
|
||
public fun <R> map(transform: (@UnsafeVariance T) -> R): NonEmptySet<R> = | ||
NonEmptySet(elements.mapTo(mutableSetOf(), transform)) | ||
|
||
override fun isEmpty(): Boolean = false | ||
|
||
override fun toString(): String = "NonEmptySet(${this.joinToString()})" | ||
|
||
@Suppress("RESERVED_MEMBER_INSIDE_VALUE_CLASS") | ||
override fun equals(other: Any?): Boolean = when (other) { | ||
is NonEmptySet<*> -> elements == other.elements | ||
else -> elements == other | ||
} | ||
|
||
@Suppress("RESERVED_MEMBER_INSIDE_VALUE_CLASS") | ||
override fun hashCode(): Int = | ||
elements.hashCode() | ||
} | ||
|
||
public fun <T> nonEmptySetOf(first: T, vararg rest: T): NonEmptySet<T> = | ||
NonEmptySet(first, rest.toSet()) | ||
|
||
public fun <T> Collection<T>.toNonEmptySetOrNull(): NonEmptySet<T>? = | ||
firstOrNull()?.let { NonEmptySet(it, minus(it).toSet()) } | ||
|
||
public fun <T> Collection<T>.toNonEmptySetOrNone(): Option<NonEmptySet<T>> = | ||
toNonEmptySetOrNull().toOption() |
53 changes: 53 additions & 0 deletions
53
arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptySetTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package arrow.core | ||
|
||
import arrow.core.test.nonEmptySet | ||
import io.kotest.assertions.withClue | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.booleans.shouldBeTrue | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.int | ||
import io.kotest.property.arbitrary.next | ||
import io.kotest.property.checkAll | ||
|
||
class NonEmptySetTest : StringSpec({ | ||
|
||
"iterable.toNonEmptySetOrNull should round trip" { | ||
checkAll(Arb.nonEmptySet(Arb.int())) { nonEmptySet -> | ||
nonEmptySet.toNonEmptySetOrNull().shouldNotBeNull() shouldBe nonEmptySet | ||
} | ||
} | ||
|
||
"iterable.toNonEmptySetOrNone should round trip" { | ||
checkAll(Arb.nonEmptySet(Arb.int())) { nonEmptySet -> | ||
nonEmptySet.toNonEmptySetOrNone() shouldBe nonEmptySet.some() | ||
} | ||
} | ||
|
||
"emptyList.toNonEmptySetOrNull should be null" { | ||
listOf<Int>().toNonEmptySetOrNull() shouldBe null | ||
} | ||
|
||
"emptyList.toNonEmptySetOrNone should be none" { | ||
listOf<Int>().toNonEmptySetOrNone() shouldBe none() | ||
} | ||
|
||
"adding an element already present doesn't change the set" { | ||
val element = Arb.int().next() | ||
val initialSet: NonEmptySet<Int> = nonEmptySetOf(element) + Arb.nonEmptySet(Arb.int()).next() | ||
initialSet.plus(element) shouldBe initialSet | ||
} | ||
"NonEmptySet equals Set" { | ||
checkAll( | ||
Arb.nonEmptySet(Arb.int()) | ||
) { nes -> | ||
val s = nes.toSet() | ||
withClue("$nes should be equal to $s") { | ||
(nes == s).shouldBeTrue() // `shouldBe` doesn't use the `equals` methods on `Iterable` | ||
nes.hashCode() shouldBe s.hashCode() | ||
} | ||
} | ||
} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.