本文整理汇总了Scala中cats.free.Inject类的典型用法代码示例。如果您正苦于以下问题:Scala Inject类的具体用法?Scala Inject怎么用?Scala Inject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Inject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: deskewImg
//设置package包名称以及导入依赖的类
package llsm.algebras
import cats.free.{Free, Inject}
import llsm.InterpolationMethod
import llsm.io.LLSMImg
trait ProcessAPI[F[_]] {
def deskewImg(img: LLSMImg, shearDim: Int, refDim: Int, shearFactor: Double, interpolation: InterpolationMethod): F[LLSMImg]
}
sealed trait ProcessF[A]
object ProcessAPI {
case class DeskewImg[A](img: LLSMImg,
shearDim: Int,
refDim: Int,
shearFactor: Double,
interpolation: InterpolationMethod,
next: LLSMImg => A) extends ProcessF[A]
implicit val process = new ProcessAPI[ProcessF] {
def deskewImg(img: LLSMImg, shearDim: Int, refDim: Int, shearFactor: Double, interpolation: InterpolationMethod): ProcessF[LLSMImg] =
DeskewImg(img, shearDim, refDim, shearFactor, interpolation, identity)
}
implicit def processInject[F[_], G[_]](implicit F: ProcessAPI[F], I: Inject[F, G]): ProcessAPI[Free[G, ?]] =
new ProcessAPI[Free[G, ?]] {
def deskewImg(img: LLSMImg, shearDim: Int, refDim: Int, shearFactor: Double, interpolation: InterpolationMethod): Free[G, LLSMImg] =
Free.inject[F, G](F.deskewImg(img, shearDim, refDim, shearFactor, interpolation))
}
}
示例2: get
//设置package包名称以及导入依赖的类
package llsm.algebras
import cats.free.{Free, Inject}
trait ConfigAPI[F[_]] {
def get[A](key: String): F[Option[A]]
def put[A](key: String, value: A): F[Unit]
def delete(key: String): F[Unit]
def update[A](key: String, f: A => A): F[Unit]
}
sealed trait ConfigF[A]
case class Get[A, T](key: String, next: Option[T] => A) extends ConfigF[A]
case class Put[A, T](key: String, value: T, next: Unit => A) extends ConfigF[A]
case class Delete[A](key: String, next: Unit => A) extends ConfigF[A]
object ConfigAPI {
def apply[F[_]](implicit ev: ConfigAPI[Free[F, ?]]): ConfigAPI[Free[F, ?]] = ev
implicit val config = new ConfigAPI[ConfigF] {
def get[A](key: String): ConfigF[Option[A]] = Get[Option[A], A](key, identity)
def put[A](key: String, value: A): ConfigF[Unit] = Put(key, value, identity)
def delete(key: String): ConfigF[Unit] = Delete(key, identity)
def update[A](key: String, f: A => A): ConfigF[Unit] =
Get(key, (a: Option[A]) => a match {
case Some(a) => { put[A](key, f(a)); ()}
case None => ()
})
}
implicit def configInject[F[_], G[_]](implicit F: ConfigAPI[F], I: Inject[F, G]): ConfigAPI[Free[G, ?]] =
new ConfigAPI[Free[G, ?]] {
def get[A](key: String): Free[G, Option[A]] = Free.inject[F, G](F.get[A](key))
def put[A](key: String, value: A): Free[G, Unit] = Free.inject[F, G](F.put[A](key, value))
def delete(key: String): Free[G, Unit] = Free.inject[F, G](F.delete(key))
def update[A](key: String, f: A => A): Free[G, Unit] = Free.inject[F, G](F.update(key, f))
}
}
示例3: progress
//设置package包名称以及导入依赖的类
package llsm.algebras
import cats.free.{Free, Inject}
trait ProgressAPI[F[_]] {
def progress(value: Int, max: Int): F[Unit]
def status(message: String): F[Unit]
}
sealed trait ProgressF[A]
object ProgressAPI {
case class Progress(value: Int, max: Int) extends ProgressF[Unit]
case class Status(message: String) extends ProgressF[Unit]
implicit val progress = new ProgressAPI[ProgressF] {
def progress(value: Int, max: Int): ProgressF[Unit] =
Progress(value, max)
def status(message: String): ProgressF[Unit] =
Status(message)
}
implicit def progressInject[F[_], G[_]](
implicit
F: ProgressAPI[F],
I: Inject[F, G]
): ProgressAPI[Free[G, ?]] =
new ProgressAPI[Free[G, ?]] {
def progress(value: Int, max: Int): Free[G, Unit] =
Free.inject[F, G](F.progress(value, max))
def status(message: String): Free[G, Unit] =
Free.inject[F, G](F.status(message))
}
}
示例4: info
//设置package包名称以及导入依赖的类
package llsm.algebras
import cats.free.{Free, Inject}
trait LoggingAPI[F[_]] {
def info(msg: String): F[Unit]
def infoCause(msg: String, cause: Throwable): F[Unit]
def warn(msg: String): F[Unit]
def warnCause(msg: String, cause: Throwable): F[Unit]
def debug(msg: String): F[Unit]
def debugCause(msg: String, cause: Throwable): F[Unit]
def error(msg: String): F[Unit]
def errorCause(msg: String, cause: Throwable): F[Unit]
def trace(msg: String): F[Unit]
def traceCause(msg: String, cause: Throwable): F[Unit]
}
sealed trait LoggingF[A]
object LoggingAPI {
def apply[F[_]](implicit ev: LoggingAPI[F]): LoggingAPI[F] = ev
case class Info(msg: String) extends LoggingF[Unit]
case class InfoCause(msg: String, cause: Throwable) extends LoggingF[Unit]
case class Warn(msg: String) extends LoggingF[Unit]
case class WarnCause(msg: String, cause: Throwable) extends LoggingF[Unit]
case class Debug(msg: String) extends LoggingF[Unit]
case class DebugCause(msg: String, cause: Throwable) extends LoggingF[Unit]
case class Error(msg: String) extends LoggingF[Unit]
case class ErrorCause(msg: String, cause: Throwable) extends LoggingF[Unit]
case class Trace(msg: String) extends LoggingF[Unit]
case class TraceCause(msg: String, cause: Throwable) extends LoggingF[Unit]
implicit val logging = new LoggingAPI[LoggingF] {
def info(msg: String): LoggingF[Unit] = LoggingAPI.Info(msg)
def infoCause(msg: String, cause: Throwable): LoggingF[Unit] = LoggingAPI.InfoCause(msg, cause)
def warn(msg: String): LoggingF[Unit] = LoggingAPI.Warn(msg)
def warnCause(msg: String, cause: Throwable): LoggingF[Unit] = LoggingAPI.WarnCause(msg, cause)
def debug(msg: String): LoggingF[Unit] = LoggingAPI.Debug(msg)
def debugCause(msg: String, cause: Throwable): LoggingF[Unit] = LoggingAPI.DebugCause(msg, cause)
def error(msg: String): LoggingF[Unit] = LoggingAPI.Error(msg)
def errorCause(msg: String, cause: Throwable): LoggingF[Unit] = LoggingAPI.ErrorCause(msg, cause)
def trace(msg: String): LoggingF[Unit] = LoggingAPI.Trace(msg)
def traceCause(msg: String, cause: Throwable): LoggingF[Unit] = LoggingAPI.TraceCause(msg, cause)
}
implicit def loggingInject[F[_], G[_]](implicit F: LoggingAPI[F], I: Inject[F, G]): LoggingAPI[Free[G, ?]] =
new LoggingAPI[Free[G, ?]] {
def info(msg: String): Free[G, Unit] = Free.inject[F, G](F.info(msg))
def infoCause(msg: String, cause: Throwable): Free[G, Unit] = Free.inject[F, G](F.infoCause(msg, cause))
def warn(msg: String): Free[G, Unit] = Free.inject[F, G](F.warn(msg))
def warnCause(msg: String, cause: Throwable): Free[G, Unit] = Free.inject[F, G](F.warnCause(msg, cause))
def debug(msg: String): Free[G, Unit] = Free.inject[F, G](F.debug(msg))
def debugCause(msg: String, cause: Throwable): Free[G, Unit] = Free.inject[F, G](F.debugCause(msg, cause))
def error(msg: String): Free[G, Unit] = Free.inject[F, G](F.error(msg))
def errorCause(msg: String, cause: Throwable): Free[G, Unit] = Free.inject[F, G](F.errorCause(msg, cause))
def trace(msg: String): Free[G, Unit] = Free.inject[F, G](F.trace(msg))
def traceCause(msg: String, cause: Throwable): Free[G, Unit] = Free.inject[F, G](F.traceCause(msg, cause))
}
}
示例5: writeImg
//设置package包名称以及导入依赖的类
package llsm.algebras
import java.nio.file.Path
import cats.free.{Free, Inject}
import llsm.io.LLSMImg
import llsm.io.metadata.FileMetadata
trait ImgWriterAPI[F[_]] {
def writeImg(path: Path, img: LLSMImg): F[FileMetadata]
}
sealed trait ImgWriterF[A]
object ImgWriterAPI {
def apply[F[_]](implicit ev: ImgWriterAPI[F]): ImgWriterAPI[F] = ev
case class WriteImg[A](path: Path, img: LLSMImg, next: FileMetadata => A) extends ImgWriterF[A]
implicit val imgWriter = new ImgWriterAPI[ImgWriterF] {
def writeImg(path: Path, img: LLSMImg): ImgWriterF[FileMetadata] = WriteImg(path, img, identity)
}
implicit def imgWriterInject[F[_], G[_]](implicit F: ImgWriterAPI[F], I: Inject[F, G]): ImgWriterAPI[Free[G, ?]] =
new ImgWriterAPI[Free[G, ?]] {
def writeImg(path: Path, img: LLSMImg): Free[G, FileMetadata] =
Free.inject[F, G](F.writeImg(path, img))
}
}
sealed trait LowWriterF[A]
case class WriteOMETIFF(path: Path, img: LLSMImg) extends LowWriterF[FileMetadata]
case class WriteHDF5(path: Path, img: LLSMImg) extends LowWriterF[FileMetadata]
object LowWriterAPI {
def writeOMETIFF(path: Path, img: LLSMImg): Free[LowWriterF, FileMetadata] =
Free.liftF[LowWriterF, FileMetadata](WriteOMETIFF(path, img))
def writeHDF5(path: Path, img: LLSMImg): Free[LowWriterF, FileMetadata] =
Free.liftF[LowWriterF, FileMetadata](WriteHDF5(path, img))
}
示例6: free
//设置package包名称以及导入依赖的类
import cats._
import cats.free.{Free, Inject}
import sio.core._
import sio.teletype
import sio.core.instances.all._
import sio.core.syntax.st._
object free {
sealed abstract class Interact[A] extends Product with Serializable
object Interact {
final case class Ask(message: String) extends Interact[String]
final case class Tell(message: String) extends Interact[Unit]
}
class Interacts[F[_]](implicit F: Inject[Interact, F]) {
def ask(message: String): Free[F, String] = Free.inject(Interact.Ask(message))
def tell(message: String): Free[F, Unit] = Free.inject(Interact.Tell(message))
}
object Interacts {
implicit def interacts[F[_]](implicit I: Inject[Interact, F]): Interacts[F] = new Interacts[F]
}
type App[A] = Interact[A]
def program(implicit I: Interacts[App]): Free[App, Unit] = {
import I._
for {
name <- ask("Hello, what's your name?")
_ <- tell(s"Hey, $name")
} yield ()
}
object InteractIOInterpreter extends (Interact ~> ST[RW, ?]) {
def apply[A](i: Interact[A]) = i match {
case Interact.Ask(x) => teletype.putStrLn(x) >> teletype.getLine
case Interact.Tell(x) => teletype.putStrLn(x)
}
}
def main: IO[Unit] = program.foldMap(InteractIOInterpreter)
}
示例7: ReadLine
//设置package包名称以及导入依赖的类
package me.panavtec.tictactoe
import cats.free.{Free, Inject}
import cats.{Id, ~>}
import me.panavtec.tictactoe.Domain._
import scala.io.StdIn
sealed trait Console[A]
case object ReadLine extends Console[Coordinate]
case class PrintLine(line: String) extends Console[Unit]
object Console {
val interpreter: Console ~> Id = new (Console ~> Id) {
override def apply[A](fa: Console[A]): Id[A] = {
fa match {
case ReadLine => Coordinate.fromString(StdIn.readLine).get
case PrintLine(line) => println(line)
}
}
}
}
class ConsoleOps[F[_]](implicit I: Inject[Console, F]) {
def readLine: Free[F, Coordinate] = Free.inject(ReadLine)
def printLine(line: String): Free[F, Unit] = Free.inject(PrintLine(line))
}
object ConsoleOps {
implicit def console[F[_]](implicit I: Inject[Console, F]): ConsoleOps[F] = new ConsoleOps[F]
}
示例8: Start
//设置package包名称以及导入依赖的类
package me.panavtec.tictactoe
import cats.free.{Free, Inject}
import cats.{Id, ~>}
import me.panavtec.tictactoe.Domain.{Board, Mark, O_, X_}
import me.panavtec.tictactoe.GameEngine.{GameState, PlayerInput}
sealed trait Game[A]
case object Start extends Game[GameState]
case class PlayAt(board: Board, playerInput: PlayerInput) extends Game[GameState]
object Game {
val interpreter: Game ~> Id = new (Game ~> Id) {
override def apply[A](fa: Game[A]): Id[A] = {
fa match {
case Start => (X_, Board.emptyBoard)
case PlayAt(board, (coordinate, X_)) => (O_, Board.playAt(board, coordinate, X_))
case PlayAt(board, (coordinate, O_)) => (X_, Board.playAt(board, coordinate, O_))
}
}
}
def hasFinished(gs: GameState): Boolean = gs match {
case (_ , b) => Board.hasPlayerWon(b) || Board.isBoardFull(b)
}
}
class GameOps[F[_]](implicit I: Inject[Game, F]) {
def startGame: Free[F, GameState] = Free.inject(Start)
def playNext(board: Board, playerInput: PlayerInput): Free[F, GameState] = Free.inject(PlayAt(board, playerInput))
}
object GameOps {
implicit def turn[F[_]](implicit I: Inject[Game, F]): GameOps[F] = new GameOps[F]
}
示例9: Evaluates
//设置package包名称以及导入依赖的类
package org.scalaexercises.evaluator.free.algebra
import cats.free.{Free, Inject}
import org.scalaexercises.evaluator.EvaluatorResponses.EvaluationResponse
import org.scalaexercises.evaluator.{Dependency, EvalResponse}
sealed trait EvaluatorOp[A]
final case class Evaluates(
url: String,
authKey: String,
resolvers: List[String] = Nil,
dependencies: List[Dependency] = Nil,
code: String)
extends EvaluatorOp[EvaluationResponse[EvalResponse]]
class EvaluatorOps[F[_]](implicit I: Inject[EvaluatorOp, F]) {
def evaluates(
url: String,
authKey: String,
resolvers: List[String] = Nil,
dependencies: List[Dependency] = Nil,
code: String
): Free[F, EvaluationResponse[EvalResponse]] =
Free.inject[EvaluatorOp, F](Evaluates(url, authKey, resolvers, dependencies, code))
}
object EvaluatorOps {
implicit def instance[F[_]](implicit I: Inject[EvaluatorOp, F]): EvaluatorOps[F] =
new EvaluatorOps[F]
}
示例10: User
//设置package包名称以及导入依赖的类
package com.gilesc
import java.time.ZonedDateTime
import cats.free.{Inject, Free}
import cats.free.Free.liftF
import cats.{Id, ~>}
case class User(id: Long, username: String, email: String, password: String, createdAt: ZonedDateTime)
sealed trait RegistrationAction[A]
case class RegistrationContext(username: String, email: String, password: String, confirmation: String)
case class Register(cxt: RegistrationContext) extends RegistrationAction[Option[User]]
class RegistrationActions[F[_]](implicit I: Inject[RegistrationAction, F]) {
def register(cxt: RegistrationContext) = Free.inject[RegistrationAction, F](Register(cxt))
}
object RegistrationActions {
implicit def actions[F[_]](implicit I: Inject[RegistrationAction, F]): RegistrationActions[F] = new RegistrationActions[F]
}
object TestRegistrationInterpreter extends (RegistrationAction ~> Id) {
val users = List.empty[User]
override def apply[A](fa: RegistrationAction[A]): Id[A] = fa match {
case Register(cxt) =>
val time = ZonedDateTimeInterpreter.apply(GetCurrentTime)
Some(User(users.size, cxt.username, cxt.email, cxt.password, time))
}
}
示例11: Debug
//设置package包名称以及导入依赖的类
package com.gilesc
import cats.free.{Free, Inject}
import cats.{Id, ~>}
trait LoggingAction[A]
sealed trait LogLevel
case object Debug extends LogLevel { override def toString = "[debug] - "}
case object Info extends LogLevel { override def toString = "[info] - "}
case object Warn extends LogLevel { override def toString = "[warn] - "}
case object Error extends LogLevel { override def toString = "[error] - "}
case class Logger(message: String, level: LogLevel) extends LoggingAction[Unit] {
override def toString = level.toString + message
}
class LoggingActions[F[_]](implicit I: Inject[LoggingAction, F]) {
def log(message: String, level: LogLevel) = Free.inject[LoggingAction,F](Logger(message, level))
case object log {
def debug(message: String) = Free.inject[LoggingAction,F](Logger(message, Debug))
def info(message: String) = Free.inject[LoggingAction,F](Logger(message, Info))
def warn(message: String) = Free.inject[LoggingAction,F](Logger(message, Warn))
def error(message: String) = Free.inject[LoggingAction,F](Logger(message, Error))
}
}
object LoggingActions {
implicit def actions[F[_]](implicit I: Inject[LoggingAction, F]): LoggingActions[F] = new LoggingActions[F]
}
object ConsoleLoggingInterpreter extends (LoggingAction ~> Id) {
def apply[A](fa: LoggingAction[A]): Id[A] = fa match {
case Logger(a,b) => println(b+a)
}
}
示例12: DataOps
//设置package包名称以及导入依赖的类
package services.free
import cats.free.{Free, Inject}
import cats.~>
import monix.eval.Task
object DataOps {
sealed trait DSL[A]
final case class Add(value: String) extends DSL[Option[String]]
final case class FindAll() extends DSL[List[String]]
}
final class DataOpService[F[_]](implicit I: Inject[DataOps.DSL, F]) {
import DataOps._
def add(value: String): Free[F, Option[String]] = Free.inject[DSL, F](Add(value))
def findAll: Free[F, List[String]] = Free.inject[DSL, F](FindAll())
}
object DataOpService {
implicit def dataOps[F[_]](implicit I: Inject[DataOps.DSL, F]): DataOpService[F] = new DataOpService[F]
}
final class InMemoryDataOpInterpreter extends (DataOps.DSL ~> Task) {
import DataOps._
private[this] val storage = new scala.collection.mutable.HashSet[String]
def apply[A](d: DSL[A]) = d match {
case Add(a) => Task { if (storage.add(a)) Some(a) else None }
case FindAll() => Task { storage.toList.sorted }
}
}
示例13: Logs
//设置package包名称以及导入依赖的类
package services.free
import cats.free.{Free, Inject}
import cats.~>
import monix.eval.Task
object Logs {
sealed trait DSL[A]
final case class Add(a: String) extends DSL[Unit]
final case class Show() extends DSL[List[String]]
}
final class LogService[F[_]](implicit I: Inject[Logs.DSL, F]) {
import Logs._
def add(a: String): Free[F, Unit] = Free.inject[DSL, F](Add(a))
def show: Free[F, List[String]] = Free.inject[DSL, F](Show())
}
object LogService {
implicit def logs[F[_]](implicit I: Inject[Logs.DSL, F]): LogService[F] = new LogService[F]
}
final class LogInterpreter extends (Logs.DSL ~> Task) {
import Logs._
private[this] val storage = new scala.collection.mutable.ListBuffer[String]
def apply[A](l: DSL[A]) = l match {
case Add(a) => Task { storage.append(a) }
case Show() => Task { storage.toList }
}
}
示例14: Interactions
//设置package包名称以及导入依赖的类
package services.free
import cats.free.{Free, Inject}
import cats.~>
import monix.eval.Task
object Interactions {
sealed trait DSL[A]
final case class Get(input: String) extends DSL[String]
final case class Print(msg: String) extends DSL[Unit]
}
final class InteractionService[F[_]](implicit I: Inject[Interactions.DSL, F]) {
import Interactions._
def get(input: String): Free[F, String] = Free.inject[DSL, F](Get(input))
def print(msg: String): Free[F, Unit] = Free.inject[DSL, F](Print(msg))
}
object InteractionService {
implicit def actions[F[_]](implicit I: Inject[Interactions.DSL, F]): InteractionService[F] = new InteractionService[F]
}
final class InteractionInterpreter(read: () => String) extends (Interactions.DSL ~> Task) {
import Interactions._
def apply[A](a: DSL[A]) = a match {
case Get(input) => Task { println(input); read() }
case Print(msg) => Task { println(msg) }
}
}
示例15: CounterA
//设置package包名称以及导入依赖的类
package control.free
import cats.free.{Free, Inject}
import cats.{Id, ~>}
import CounterA._
sealed trait CounterA[A]
object CounterA {
final case class Set(n: Int) extends CounterA[Unit]
final case class Add(n: Int) extends CounterA[Unit]
final case class Subtract(n: Int) extends CounterA[Unit]
final case object Get extends CounterA[Int]
final case object Reset extends CounterA[Unit]
}
class Counter[F[_]](implicit I: Inject[CounterA, F]) {
def set(n: Int): Free[F, Unit] = Free.inject[CounterA, F](Set(n))
def add(n: Int): Free[F, Unit] = Free.inject[CounterA, F](Add(n))
def subtract(n: Int): Free[F, Unit] = Free.inject[CounterA, F](Subtract(n))
def get: Free[F, Int] = Free.inject[CounterA, F](Get)
def reset: Free[F, Unit] = Free.inject[CounterA, F](Reset)
}
object Counter {
implicit def counter[F[_]](implicit I: Inject[CounterA, F]): Counter[F] = new Counter[F]
def interpreter: Interpreter = new Interpreter
class Interpreter extends (CounterA ~> Id) {
var curN: Int = _
def apply[A](fa: CounterA[A]): Id[A] = fa match {
case Set(n) =>
curN = n
()
case Add(n) =>
curN = curN + n
()
case Subtract(n) =>
curN = curN - n
()
case Get =>
curN
case Reset =>
curN = 0
()
}
}
object Interpreter {
def copy: Interpreter => Interpreter = i => {
val res = new Interpreter
res.curN = i.curN
res
}
}
}