如何使用Kotlin和Arrow执行程序

时间:2019-11-25 06:46:02

标签: kotlin functional-programming io-monad arrow-kt

我正在尝试使用Kotlin和Arrow学习一些函数式编程,通过这种方式,我已经阅读了一些博客文章,例如以下文章:https://jorgecastillo.dev/kotlin-fp-1-monad-stack,这很好,我已经了解了主要内容。这个想法,但是在创建程序时,我不知道如何运行它。

让我更明确:

我有以下代码:

typealias EitherIO<A, B> = EitherT<ForIO, A, B>

sealed class UserError(
        val message: String,
        val status: Int
) {
    object AuthenticationError : UserError(HttpStatus.UNAUTHORIZED.reasonPhrase, HttpStatus.UNAUTHORIZED.value())
    object UserNotFound : UserError(HttpStatus.NOT_FOUND.reasonPhrase, HttpStatus.NOT_FOUND.value())
    object InternalServerError : UserError(HttpStatus.INTERNAL_SERVER_ERROR.reasonPhrase, HttpStatus.INTERNAL_SERVER_ERROR.value())
}


@Component
class UserAdapter(
        private val myAccountClient: MyAccountClient
) {
    @Lazy
    @Inject
    lateinit var subscriberRepository: SubscriberRepository

    fun getDomainUser(ssoId: Long): EitherIO<UserError, User?> {
        val io = IO.fx {
            val userResource = getUserResourcesBySsoId(ssoId, myAccountClient).bind()
            userResource.fold(
                    { error -> Either.Left(error) },
                    { success ->
                        Either.right(composeDomainUserWithSubscribers(success, getSubscribersForUserResource(success, subscriberRepository).bind()))
                    })
        }
        return EitherIO(io)
    }

    fun composeDomainUserWithSubscribers(userResource: UserResource, subscribers: Option<Subscribers>): User? {
        return subscribers.map { userResource.toDomainUser(it) }.orNull()
    }
}

private fun getSubscribersForUserResource(userResource: UserResource, subscriberRepository: SubscriberRepository): IO<Option<Subscribers>> {
    return IO {
        val msisdnList = userResource.getMsisdnList()
        Option.invoke(subscriberRepository.findAllByMsisdnInAndDeletedIsFalse(msisdnList).associateBy(Subscriber::msisdn))
    }
}

private fun getUserResourcesBySsoId(ssoId: Long, myAccountClient: MyAccountClient): IO<Either<UserError, UserResource>> {
    return IO {
        val response = myAccountClient.getUserBySsoId(ssoId)
        if (response.isSuccessful) {
            val userResource = JacksonUtils.fromJsonToObject(response.body()?.string()!!, UserResource::class.java)
            Either.Right(userResource)
        } else {
            when (response.code()) {
                401 -> Either.Left(UserError.AuthenticationError)
                404 -> Either.Left(UserError.UserNotFound)
                else -> Either.Left(UserError.InternalServerError)
            }
        }
    }.handleError { Either.Left(UserError.InternalServerError) }
}
如您所见,

正在将一些结果累积到IO monad中。我应该使用箭头上的unsafeRunSync()运行该程序,但是在javadoc上声明如下:**NOTE** this function is intended for testing, it should never appear in your mainline production code!。 我应该提到我对unsafeRunAsync有所了解,但就我而言,我想保持同步。

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该支持unsafeRunSync,而不是运行unsafeRunAsync

如果您有myFun(): IO<A>并想运行它,那么您可以在myFun().unsafeRunAsync(cb)处调用cb: (Either<Throwable, A>) -> Unit

例如,如果您的函数返回IO<List<Int>>,则可以调用

myFun().unsafeRunAsync {  /* it (Either<Throwable, List<Int>>) -> */
  it.fold(
    { Log.e("Foo", "Error! $it") },
    { println(it) })
}

这将异步运行IO中包含的程序,并将结果安全地传递给回调,如果IO抛出则将记录错误,否则将打印整数列表。

出于here的讨论,您应出于多种原因避免使用unsafeRunSync。它处于阻塞状态,可能导致崩溃,可能导致死锁,并且可能导致应用程序停止运行。

如果您真的想将IO作为阻塞计算运行,则可以在此之前加上attempt(),使IO<A>成为与IO<Either<Throwable, A>>类似的unsafeRunAsync回调参数。至少那样您就不会崩溃。

但是unsafeRunAsync是首选。另外,请确保传递给unsafeRunAsync的回调不会抛出任何错误,因为它不会。 Docs