Skip to content

Commit

Permalink
NonEmptySet: change 2nd param of constructor to Iterable. Optimize `n…
Browse files Browse the repository at this point in the history
…onEmptySetOf` and `toNonEmptySetOrNull` (#3422)

* NonEmptySet: change 2nd param of constructor to Iterable. Optimize `nonEmptySetOf` and `toNonEmptySetOrNull`

* api dump

---------

Co-authored-by: Alejandro Serrano <trupill@gmail.com>
  • Loading branch information
hoc081098 and serras committed May 20, 2024
1 parent 7077846 commit 86fc0f0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public final class arrow/core/NonEmptySet : arrow/core/NonEmptyCollection, java/
public fun addAll (Ljava/util/Collection;)Z
public static final synthetic fun box-impl (Ljava/util/Set;)Larrow/core/NonEmptySet;
public fun clear ()V
public static fun constructor-impl (Ljava/lang/Object;Ljava/util/Set;)Ljava/util/Set;
public static fun constructor-impl (Ljava/lang/Object;Ljava/lang/Iterable;)Ljava/util/Set;
public fun contains (Ljava/lang/Object;)Z
public static fun contains-impl (Ljava/util/Set;Ljava/lang/Object;)Z
public fun containsAll (Ljava/util/Collection;)Z
Expand Down
2 changes: 1 addition & 1 deletion arrow-libs/core/arrow-core/api/arrow-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ final value class <#A: out kotlin/Any?> arrow.core/NonEmptyList : arrow.core/Non
final fun asJsReadonlyArrayView(): kotlin.js.collections/JsReadonlyArray<#A> // arrow.core/NonEmptyList.asJsReadonlyArrayView|asJsReadonlyArrayView(){}[0]
}
final value class <#A: out kotlin/Any?> arrow.core/NonEmptySet : arrow.core/NonEmptyCollection<#A>, kotlin.collections/Set<#A> { // arrow.core/NonEmptySet|null[0]
constructor <init>(#A, kotlin.collections/Set<#A>) // arrow.core/NonEmptySet.<init>|<init>(1:0;kotlin.collections.Set<1:0>){}[0]
constructor <init>(#A, kotlin.collections/Iterable<#A>) // arrow.core/NonEmptySet.<init>|<init>(1:0;kotlin.collections.Iterable<1:0>){}[0]
final fun <#A1: kotlin/Any?> distinctBy(kotlin/Function1<#A, #A1>): arrow.core/NonEmptyList<#A> // arrow.core/NonEmptySet.distinctBy|distinctBy(kotlin.Function1<1:0,0:0>){0§<kotlin.Any?>}[0]
final fun <#A1: kotlin/Any?> flatMap(kotlin/Function1<#A, arrow.core/NonEmptyCollection<#A1>>): arrow.core/NonEmptyList<#A1> // arrow.core/NonEmptySet.flatMap|flatMap(kotlin.Function1<1:0,arrow.core.NonEmptyCollection<0:0>>){0§<kotlin.Any?>}[0]
final fun <#A1: kotlin/Any?> map(kotlin/Function1<#A, #A1>): arrow.core/NonEmptyList<#A1> // arrow.core/NonEmptySet.map|map(kotlin.Function1<1:0,0:0>){0§<kotlin.Any?>}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public value class NonEmptySet<out A> private constructor(
@PublishedApi internal val elements: Set<A>
) : Set<A> by elements, NonEmptyCollection<A> {

public constructor(first: A, rest: Set<A>) : this(setOf(first) + rest)
public constructor(first: A, rest: Iterable<A>) : this(setOf(first) + rest)

public override operator fun plus(elements: Iterable<@UnsafeVariance A>): NonEmptySet<A> =
NonEmptySet(this.elements + elements)
Expand Down Expand Up @@ -43,20 +43,20 @@ public value class NonEmptySet<out A> private constructor(
public override fun <B> flatMap(transform: (A) -> NonEmptyCollection<B>): NonEmptyList<B> =
NonEmptyList(elements.flatMap(transform))

public override fun <B> mapIndexed(transform: (index:Int, A) -> B): NonEmptyList<B> =
public override fun <B> mapIndexed(transform: (index: Int, A) -> B): NonEmptyList<B> =
NonEmptyList(elements.mapIndexed(transform))

override fun <B> zip(other: NonEmptyCollection<B>): NonEmptyList<Pair<A, B>> =
NonEmptyList(elements.zip(other))
}

public fun <A> nonEmptySetOf(first: A, vararg rest: A): NonEmptySet<A> =
NonEmptySet(first, rest.toSet())
NonEmptySet(first, rest.asIterable())

public fun <A> Iterable<A>.toNonEmptySetOrNull(): NonEmptySet<A>? {
val iter = iterator()
if (!iter.hasNext()) return null
return NonEmptySet(iter.next(), Iterable { iter }.toSet())
return NonEmptySet(iter.next(), Iterable { iter })
}

public fun <A> Iterable<A>.toNonEmptySetOrNone(): Option<NonEmptySet<A>> =
Expand Down

0 comments on commit 86fc0f0

Please sign in to comment.