commonMain.target.core.Functions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of target-core-jvm Show documentation
Show all versions of target-core-jvm Show documentation
Functional domain modeling in Kotlin
package target.core
import arrow.core.*
/**
* Validates this list and returns either a list of all failures, or a validated list.
*/
fun List>.validate(): Either, List> {
val failureList = mutableListOf()
val successList = mutableListOf()
forEach {
it.fold(failureList::add, successList::add)
}
return failureList.toNonEmptyListOrNull()?.left() ?: successList.right()
}
/**
* Validates this list and returns either a list of all failures, or a validated list.
*/
fun Nel>.validate(): Either, Nel> {
val failureList = mutableListOf()
val successList = mutableListOf()
forEach {
it.fold(failureList::add, successList::add)
}
return failureList.toNonEmptyListOrNull()?.left() ?: successList.toNonEmptyListOrNull()!!.right()
}