本文整理汇总了Scala中cats.data.Kleisli类的典型用法代码示例。如果您正苦于以下问题:Scala Kleisli类的具体用法?Scala Kleisli怎么用?Scala Kleisli使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Kleisli类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: UserSlickDBIOStorageDriver
//设置package包名称以及导入依赖的类
package com.github.j5ik2o.scala.ddd.functional.example.driver.cats
import cats.data.Kleisli
import com.github.j5ik2o.scala.ddd.functional.driver.StorageDriver
import com.github.j5ik2o.scala.ddd.functional.example.domain.{ User, UserId }
import com.github.j5ik2o.scala.ddd.functional.example.driver.slick3.UserDaoComponent
import slick.jdbc.JdbcProfile
import scala.concurrent.ExecutionContext
case class UserSlickDBIOStorageDriver(profile: JdbcProfile, db: JdbcProfile#Backend#Database)
extends UserDaoComponent {
type EvalType[A] = Kleisli[profile.api.DBIO, ExecutionContext, A]
implicit object InternalDriver extends StorageDriver[User, EvalType] {
override type RecordType = UserRecord
protected val dao = UserDao
import profile.api._
override protected def convertToRecord(aggregate: User): UserRecord =
UserRecord(id = aggregate.id.value, name = aggregate.name)
override protected def convertToAggregate(record: Option[UserRecord]): Option[User] =
record.map(e => User(id = UserId(e.id), name = e.name))
override def store(aggregate: User): EvalType[Unit] = Kleisli { implicit ec =>
val record = convertToRecord(aggregate)
val action = (for {
n <- dao.filter(_.id === aggregate.id.value).update(record)
_ <- if (n == 0) dao.forceInsert(record) else DBIO.successful(n)
} yield ()).transactionally
action.asInstanceOf[DBIO[Unit]]
}
override def resolveBy(id: UserId): EvalType[Option[User]] = Kleisli { implicit ec =>
val action =
dao
.filter(_.id === id.value)
.result
.headOption
.map(convertToAggregate)
action.asInstanceOf[DBIO[Option[User]]]
}
override def deleteById(id: UserId): EvalType[Unit] = Kleisli { implicit ec =>
val action = dao.filter(_.id === id.value).delete
action
.flatMap { v =>
if (v == 1)
DBIO.successful(())
else
DBIO.failed(new Exception())
}
.asInstanceOf[DBIO[Unit]]
}
}
}
示例2: SQS
//设置package包名称以及导入依赖的类
package wookie.sqs
import cats.data.Kleisli
import cats.~>
import com.amazonaws.auth.BasicAWSCredentials
import wookie.httpclient._
import wookie.result._
import wookie.service._
import wookie.signer._
import wookie.sqs.algebra.{ SQSIO, SQSOp }
import wookie.sqs.implicits._
import scala.concurrent.Future
case class SQS(props: Properties, client: HttpClient) extends Service {
val endpoint = "https://sqs.amazonaws.com"
val serviceName = "sqs"
def credentials = new BasicAWSCredentials(
props.accessKey,
props.secretAccessKey
)
def run[A](op: SQSIO[A]): Future[A] = {
val result = op foldMap sqsInterpreter
result.run(Signer(endpoint, serviceName, credentials))
}
val sqsInterpreter = new (SQSOp ~> Result) {
def apply[A](command: SQSOp[A]): Result[A] =
Kleisli { signer: Signer ?
client.exec(signer.sign(command.marshalledReq))(command.responseHandler, errorResponseHandler)
}
}
}
示例3: S3
//设置package包名称以及导入依赖的类
package wookie.s3
import cats.data.Kleisli
import cats.~>
import com.amazonaws.auth.BasicAWSCredentials
import wookie.httpclient._
import wookie.result._
import wookie.s3.algebra._
import wookie.service._
import wookie.signer._
import scala.concurrent.Future
import implicits._
case class S3(props: Properties, client: HttpClient) extends Service {
val endpoint = "https://s3.amazonaws.com"
val serviceName = "s3"
def credentials = new BasicAWSCredentials(
props.accessKey,
props.secretAccessKey
)
def run[A](op: S3IO[A]): Future[A] = {
val result = op foldMap s3Interpreter
result.run(Signer(endpoint, serviceName, credentials))
}
val s3Interpreter = new (S3Op ~> Result) {
def apply[A](command: S3Op[A]): Result[A] =
Kleisli { signer: Signer ?
client.exec(signer.sign(command.marshalledReq))(command.responseHandler, errorResponseHandler)
}
}
}
示例4: DynamoDB
//设置package包名称以及导入依赖的类
package wookie.dynamodb
import cats.data.Kleisli
import cats.~>
import com.amazonaws.auth.BasicAWSCredentials
import wookie.dynamodb.algebra._
import wookie.httpclient._
import wookie.result._
import wookie.service._
import wookie.signer._
import scala.concurrent.Future
import scala.language._
import implicits._
case class DynamoDB(props: Properties, client: HttpClient) extends Service {
def endpoint = "https://dynamodb.us-east-1.amazonaws.com"
def serviceName = "dynamodb"
def credentials = new BasicAWSCredentials(props.accessKey, props.secretAccessKey)
def run[A](op: DynamoDBIO[A]): Future[A] = {
val result = op foldMap dynamoDBInterpreter
result.run(Signer(endpoint, serviceName, credentials))
}
val dynamoDBInterpreter = new (DynamoDBOp ~> Result) {
def apply[A](command: DynamoDBOp[A]): Result[A] =
Kleisli { signer: Signer ?
client.exec(signer.sign(command.marshalledReq))(command.responseHandler, errorResponseHandler)
}
}
}
示例5: captureFuture
//设置package包名称以及导入依赖的类
package aecor.effect
import cats.Functor
import cats.data.{ EitherT, Kleisli }
import simulacrum.typeclass
import scala.concurrent.Future
@typeclass
trait CaptureFuture[F[_]] {
def captureFuture[A](future: => Future[A]): F[A]
}
object CaptureFuture extends CaptureFutureInstances
sealed trait CaptureFutureInstances {
implicit def futureCaptureFutureInstance[B]: CaptureFuture[Kleisli[Future, B, ?]] =
new CaptureFuture[Kleisli[Future, B, ?]] {
override def captureFuture[A](future: => Future[A]): Kleisli[Future, B, A] =
Kleisli(_ => future)
}
implicit def captureFutureEitherT[F[_]: CaptureFuture: Functor, B]
: CaptureFuture[EitherT[F, B, ?]] =
new CaptureFuture[EitherT[F, B, ?]] {
override def captureFuture[A](future: => Future[A]): EitherT[F, B, A] =
EitherT.right(CaptureFuture[F].captureFuture(future))
}
}
示例6: capture
//设置package包名称以及导入依赖的类
package aecor.effect
import cats.data.{ EitherT, Kleisli }
import cats.{ Applicative, Functor }
import simulacrum.typeclass
@typeclass
trait Capture[F[_]] {
def capture[A](a: => A): F[A]
}
object Capture extends CaptureInstances
sealed trait CaptureInstances {
implicit def kleisliCapture[F[_]: Applicative, B]: Capture[Kleisli[F, B, ?]] =
new Capture[Kleisli[F, B, ?]] {
override def capture[A](a: => A): Kleisli[F, B, A] = Kleisli(_ => Applicative[F].pure(a))
}
implicit def captureEitherT[F[_]: Capture: Functor, B]: Capture[EitherT[F, B, ?]] =
new Capture[EitherT[F, B, ?]] {
override def capture[A](a: => A): EitherT[F, B, A] = EitherT.right(Capture[F].capture(a))
}
}
示例7: OnOff
//设置package包名称以及导入依赖的类
package smartii.lightwave.model
import cats.Id
import cats.data.{Kleisli, Reader}
import com.lightwaverf.api.model.DeviceMessage
import smartii.lightwave.Lookup
sealed trait Device
object OnOff {
def apply(deviceMessage: DeviceMessage): Reader[Lookup, Kleisli[Id, Lookup, OnOff]] = Reader { lookup: Lookup =>
Room(deviceMessage).map { room =>
OnOff(id = deviceMessage.dev, name = lookup.getDeviceName(deviceMessage.dev), room = room)
}
}
}
case class OnOff(id: Int, name: String, room: Room) extends Device
object Dimmer {
def apply(deviceMessage: DeviceMessage): Reader[Lookup, Kleisli[Id, Lookup, Dimmer]] = Reader { lookup: Lookup =>
Room(deviceMessage).map { room =>
Dimmer(id = deviceMessage.dev, name = lookup.getDeviceName(deviceMessage.dev), room = room)
}
}
def apply2(deviceMessage: DeviceMessage): Reader[Lookup, Kleisli[Id, Lookup, Dimmer]] = Reader { lookup: Lookup =>
Room(deviceMessage).map { room =>
Dimmer(id = deviceMessage.dev, name = lookup.getDeviceName(deviceMessage.dev), room = room)
}
}
}
case class Dimmer(id: Int, name: String, room: Room) extends Device
示例8: SqlPersistenceInitializationActor
//设置package包名称以及导入依赖的类
package io.vamp.lifter.persistence
import io.vamp.common.Config
import io.vamp.common.akka.CommonSupportForActors
import io.vamp.lifter.notification.{ LifterNotificationProvider, PersistenceInitializationFailure, PersistenceInitializationSuccess }
import io.vamp.lifter.persistence.LifterPersistenceDSL.LiftAction
import io.vamp.model.resolver.NamespaceValueResolver
import LifterPersistenceDSL._
import SqlInterpreter.{ LifterResult, SqlResult }
import cats.data.Kleisli
import io.vamp.lifter.SqlLifterSeed
import cats.instances.future.catsStdInstancesForFuture
import cats.~>
import io.vamp.lifter.persistence.SqlDSL.SqlAction
import scala.io.Source
class SqlPersistenceInitializationActor(
val sqlDialectInterpreter: SqlDSL ~> SqlResult,
val sqlResource: String
) extends CommonSupportForActors with NamespaceValueResolver with LifterNotificationProvider {
def receive = {
case "init" ?
val createUrl = resolveWithNamespace(Config.string("vamp.lifter.sql.connection.table-url")())
val vampDatabaseUrl = Config.string("vamp.lifter.sql.connection.database-url")()
val db = resolveWithNamespace(Config.string("vamp.lifter.sql.database")())
val user = Config.string("vamp.lifter.sql.user")()
val password = Config.string("vamp.lifter.sql.password")()
val sqlLifterSeed = SqlLifterSeed(db, user, password, createUrl, vampDatabaseUrl)
val tableQueries: List[String] = Source
.fromInputStream(getClass.getResourceAsStream(sqlResource))
.mkString
.split(';')
.toList
.map(_.trim)
.filterNot(_.isEmpty)
val sqlInitCommand: LiftAction[Boolean] = for {
databaseCreated ? createDatabase
tablesCreated ? createTables(tableQueries)
} yield databaseCreated && tablesCreated
val executeSqlActions: Kleisli[LifterResult, SqlLifterSeed, Boolean] =
sqlInitCommand
.foldMap[SqlAction](sqlInterpreter)
.foldMap[SqlResult](sqlDialectInterpreter)
executeSqlActions(sqlLifterSeed).value.foreach {
case Left(errorMessage) ? reportException(PersistenceInitializationFailure(errorMessage))
case Right(_) ? info(PersistenceInitializationSuccess)
}
}
override def preStart(): Unit = self ! "init"
}