Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline NonEmpty maps #3120

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,18 @@ public class NonEmptyList<out A>(
public override inline fun <B> map(transform: (A) -> B): NonEmptyList<B> =
NonEmptyList(transform(head), tail.map(transform))

override fun <B> flatMap(transform: (A) -> NonEmptyCollection<B>): NonEmptyList<B> =
@Suppress("OVERRIDE_BY_INLINE")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these suppressions safe to ignore?

public override inline fun <B> mapIndexed(transform: (index: Int, A) -> B): NonEmptyList<B> =
NonEmptyList(transform(0, head), tail.mapIndexed { ix, e -> transform(ix + 1, e) })

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <B> flatMap(transform: (A) -> NonEmptyCollection<B>): NonEmptyList<B> =
transform(head).toNonEmptyList() + tail.flatMap(transform)

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <K> distinctBy(selector: (A) -> K): NonEmptyList<A> =
all.distinctBy(selector).toNonEmptyListOrNull()!!

public operator fun plus(l: NonEmptyList<@UnsafeVariance A>): NonEmptyList<A> =
this + l.all

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlin.jvm.JvmInline

@JvmInline
public value class NonEmptySet<out A> private constructor(
private val elements: Set<A>
@PublishedApi internal val elements: Set<A>
) : Set<A> by elements, NonEmptyCollection<A> {

public constructor(first: A, rest: Set<A>) : this(setOf(first) + rest)
Expand All @@ -21,6 +21,25 @@ public value class NonEmptySet<out A> private constructor(

override fun lastOrNull(): A = elements.last()

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <B> map(transform: (A) -> B): NonEmptyList<B> =
elements.map(transform).toNonEmptyListOrNull()!!

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <B> mapIndexed(transform: (index: Int, A) -> B): NonEmptyList<B> =
elements.mapIndexed(transform).toNonEmptyListOrNull()!!

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <B> flatMap(transform: (A) -> NonEmptyCollection<B>): NonEmptyList<B> =
elements.flatMap(transform).toNonEmptyListOrNull()!!

override fun distinct(): NonEmptyList<A> =
toNonEmptyList()

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <K> distinctBy(selector: (A) -> K): NonEmptyList<A> =
elements.distinctBy(selector).toNonEmptyListOrNull()!!

override fun toString(): String = "NonEmptySet(${this.joinToString()})"

@Suppress("RESERVED_MEMBER_INSIDE_VALUE_CLASS")
Expand Down