• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Kleisli类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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]]
    }
  }
} 
开发者ID:j5ik2o,项目名称:scala-ddd-base-functional,代码行数:61,代码来源:UserSlickDBIOStorageDriver.scala


示例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)
      }
  }

} 
开发者ID:pepegar,项目名称:wookie,代码行数:40,代码来源:S3.scala


示例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)
      }
  }

} 
开发者ID:pepegar,项目名称:wookie,代码行数:41,代码来源:S3.scala


示例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)
      }
  }
} 
开发者ID:pepegar,项目名称:wookie,代码行数:38,代码来源:DynamoDB.scala


示例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))
    }
} 
开发者ID:notxcain,项目名称:aecor,代码行数:29,代码来源:CaptureFuture.scala


示例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))
    }
} 
开发者ID:notxcain,项目名称:aecor,代码行数:24,代码来源:Capture.scala


示例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 
开发者ID:jimnybob,项目名称:lightwave-akka,代码行数:38,代码来源:Device.scala


示例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"

} 
开发者ID:magneticio,项目名称:vamp-lifter,代码行数:60,代码来源:SqlPersistenceInitializationActor.scala



注:本文中的cats.data.Kleisli类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala TextUtils类代码示例发布时间:2022-05-23
下一篇:
Scala SecretKeyFactory类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap