Created
April 10, 2023 16:55
-
-
Save nomisRev/729c2f9192fb7fd68b6bf5de3bd9e93a to your computer and use it in GitHub Desktop.
StackOverflow Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arrow.core.Nel | |
import arrow.core.raise.Raise | |
import arrow.core.raise.recover | |
import arrow.fx.coroutines.parZipOrAccumulate | |
object ApplicationError | |
object UserLegalId | |
object User | |
object DepartmentCode | |
object Department | |
object AddUserToDepartmentInfo { | |
val userLegalId = UserLegalId | |
val departmentCode = DepartmentCode | |
} | |
context(Raise<ApplicationError>) | |
suspend fun getUser(legalId: UserLegalId): User = User | |
context(Raise<ApplicationError>) | |
suspend fun getDepartment(departmentCode: DepartmentCode): Department = Department | |
context(Raise<Nel<ApplicationError>>) | |
suspend fun execute(param: AddUserToDepartmentInfo): Department { | |
val pair: Pair<User, Department> = | |
parZipOrAccumulate( | |
{ getUser(param.userLegalId) }, | |
{ getDepartment(param.departmentCode) } | |
) { a, b -> Pair(a, b) } | |
return pair.second | |
} | |
suspend fun main() { | |
recover({ | |
val res = execute(AddUserToDepartmentInfo) | |
println(res) | |
}) { error -> println(error) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment