Emil Kantis
04/27/2023, 1:37 PMval squares = (1..10).mapOrAccumulate { squareService.square(it).bind() } // EitherNel<E1, Square>
val roots = (1..10).mapOrAccumulate { rootService.root(it).bind() } // EitherNel<E2, Root>
zipOrAccumulate(
{ // check squares? },
{ // check roots? },
{ ensureSomethingElse() },
) { validSquares, validRoots, somethingElse ->
// ...
}
simon.vergauwen
04/27/2023, 2:18 PMbind
Emil Kantis
04/27/2023, 2:20 PMdata class InvalidOperation(val problems: List<String>)
InvalidOperation(listOf(invalidRoots.map { "Invalid root: $it" } + invalidSquares.map { "Invalid square: $it }))
simon.vergauwen
04/27/2023, 2:35 PMEither
I would just use mapLeft + bind
.
val squares = (1..10).mapOrAccumulate { squareService.square(it).bind() } // EitherNel<E1, Square>
val roots = (1..10).mapOrAccumulate { rootService.root(it).bind() } // EitherNel<E2, Root>
zipOrAccumulate(
{ squares.mapLeft { }.bind() },
{ roots.mapLeft { }.bind() },
{ ensureSomethingElse() },
) { validSquares, validRoots, somethingElse ->
// ...
}
Either.zipOrAccumulate(
squares.mapLeft { },
roots.mapLeft { },
either { ensure(...) { } }
) { a, b, c -> }
Emil Kantis
04/27/2023, 6:39 PM