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

Refactor - #2812 iterable separate performance #2815

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -997,23 +997,27 @@ public fun <A, B> Iterable<Validated<A, B>>.uniteValidated(): List<B> =
* @receiver Iterable of Validated
* @return a tuple containing List with [Either.Left] and another List with its [Either.Right] values.
*/
public fun <A, B> Iterable<Either<A, B>>.separateEither(): Pair<List<A>, List<B>> {
val asep = flatMap { gab -> gab.fold({ listOf(it) }, { emptyList() }) }
val bsep = flatMap { gab -> gab.fold({ emptyList() }, { listOf(it) }) }
return asep to bsep
}
public fun <A, B> Iterable<Either<A, B>>.separateEither(): Pair<List<A>, List<B>> =
fold(listOf<A>() to listOf<B>()) { (lefts, rights), either ->
Copy link
Member

Choose a reason for hiding this comment

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

Should we use mutableListOf here and use add inside of the fold body? That would avoid having to recreate Pair on every element of the List. The returned type can still be read-only List.

Copy link
Member

Choose a reason for hiding this comment

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

It can also be rewritten to while if we'd pattern similar to Kotlin Std implementations.

val left = ArrayList<A>(collectionSizeOrDefault(10))
val right = ArrayList<B>(collectionSizeOrDefault(10))
forEach { either ->
  when(either) {
     is Left -> left.add(either.value)
     is Right -> right.add(either.value)
   }
}
Pair(left, right)

Copy link
Member

Choose a reason for hiding this comment

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

This eliminates List.size allocations.

Copy link
Contributor Author

@Khepu Khepu Sep 5, 2022

Choose a reason for hiding this comment

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

I think mimicking the Kotlin stdlib is a good way to go. Looking at the implementation it's gonna be a lot like this (based on map):

public fun <A, B> Iterable<Either<A, B>>.separateEither(): Pair<List<A>, List<B>> {
  val left = ArrayList<A>(collectionSizeOrDefault(10))
  val right = ArrayList<B>(collectionSizeOrDefault(10))

  for (either in this)
    when(either) {
      is Left -> left.add(either.value)
      is Right -> right.add(either.value)
    }

  return Pair(left, right)
}

If that is something we are happy with, let me know!

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that looks perfect to me @Khepu!

when (either) {
is Left -> lefts + either.value to rights
is Right -> lefts to rights + either.value
}
}

/**
* Separate the inner [Validated] values into the [Validated.Invalid] and [Validated.Valid].
*
* @receiver Iterable of Validated
* @return a tuple containing List with [Validated.Invalid] and another List with its [Validated.Valid] values.
*/
public fun <A, B> Iterable<Validated<A, B>>.separateValidated(): Pair<List<A>, List<B>> {
val asep = flatMap { gab -> gab.fold({ listOf(it) }, { emptyList() }) }
val bsep = flatMap { gab -> gab.fold({ emptyList() }, { listOf(it) }) }
return asep to bsep
}
public fun <A, B> Iterable<Validated<A, B>>.separateValidated(): Pair<List<A>, List<B>> =
fold(listOf<A>() to listOf<B>()) { (invalids, valids), validated ->
when (validated) {
is Valid -> invalids to valids + validated.value
is Invalid -> invalids + validated.value to valids
}
}

public fun <A> Iterable<Iterable<A>>.flatten(): List<A> =
flatMap(::identity)
Expand Down