All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.harmony.kotlin.common.either.EitherExtensions.kt Maven / Gradle / Ivy

package com.harmony.kotlin.common.either

/**
 * Transform a List> to Either>
 * @param discardLeft - true if left values should be ignored
 */
fun  List>.asSingleEither(discardLeft: Boolean = false): Either> {
  val rightValue = mutableListOf()
  var leftValue: L? = null

  this.forEach {
    when {
      it.isLeft() && !discardLeft -> {
        leftValue = it.getLeftOrNull()!!
        return@forEach
      }
      it.isRight() -> {
        rightValue.add(it.get())
      }
    }
  }
  return leftValue?.let {
    Either.Left(leftValue!!)
  } ?: run {
    Either.Right>(rightValue)
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy