Skip to content

Instantly share code, notes, and snippets.

@Softsapiens
Forked from nomisRev/Example.kt
Created May 18, 2023 13:46
Show Gist options
  • Save Softsapiens/5651fc8c1d4b1385516d11dc84c409e0 to your computer and use it in GitHub Desktop.
Save Softsapiens/5651fc8c1d4b1385516d11dc84c409e0 to your computer and use it in GitHub Desktop.
StackOverflow Question
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