For comprehensions don't do a good job with type inference when exploding tuples. Instead they pattern match. See https://stackoverflow.com/questions/30401942/why-does-a-for-comprehension-expand-to-a-withfilter/45768795#45768795
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def accCompiler[A: IsParseable[Id, ?]] | |
(f: Fixtures): SignupA ~> ArgsLog = | |
new (SignupA ~> ArgsLog) { | |
val l = Success.asRight[LogError] | |
def apply[C](fa: SignupA[C]): AL[C] = fa match { | |
case p: Parse[Id, A] => | |
(p, f.request) |> ArgsLog.lift | |
case g: GenSalt => | |
(g, f.salt) |> ArgsLog.lift | |
case g: GenHash => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def signup | |
[F[_], A: IsParseable[F, ?], B: HasCodes] | |
(a: A): Signup[B] = { | |
for { | |
errorOrCode <- signupImpl(a) | |
_ <- errorOrCode.fold( | |
logError, | |
_ => Success.asRight[LogError] |> Signup.pure | |
) | |
} yield { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def signupImpl | |
[F[_], A: IsParseable[F, ?], B: HasCodes] | |
(a: A): Signup[SignupError Or B] = | |
OrT.value { | |
for { | |
r <- parse(a) |> OrT.lift | |
_ <- log(info("Parsed body")) |> OrT.lift | |
s <- salt(512) |> OrT.lift | |
_ <- log(info("Created salt")) |> OrT.lift | |
h <- hash(r.p, s, 10000) |> OrT.lift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class WriteLog(log: Log) | |
extends SignupA[LogError Or Success] | |
case object LogError extends SignupError | |
type LogError = LogError.type | |
def log(l: Log): | |
Signup[LogError Or Success] = | |
liftS(WriteLog(l)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def signup | |
(request: HttpRequest): Future[HttpResponse] = | |
for { | |
r <- parse(request) | |
_ = info(“Parsed body”) | |
s <- salt(512) | |
_ = info(“Created salt”) | |
h <- hash(r.p, s, 10000) | |
_ = info(“Created hash”) | |
u = User(r.e, h, s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.ExecutionContext | |
import scala.concurrent.Future | |
object EvenHarderExample { | |
import Types._ | |
trait FooRepo { | |
def foo()(implicit ec: ExecutionContext): Future[Error Or Foo] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.{ExecutionContext, Future} | |
object AlternativeFutureExample { | |
def foobar(implicit ec: ExecutionContext): Future[String] = { | |
for { | |
f <- Future.failed(new RuntimeException("Foo")) | |
} yield { | |
f | |
} | |
} recover { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.syntax | |
import scala.concurrent.{ExecutionContext, Future} | |
object FutureExample { | |
import future._ | |
def foobar(implicit ec: ExecutionContext): Future[String] = Future.recover { | |
for { | |
f <- Future.failed(new RuntimeException("Foo")) | |
} yield { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object types { | |
type Or[+A, +B] = Either[A, B] | |
object Or { | |
import cats.syntax.either._ | |
import cats.Traverse | |
def left[A, B](a: A): A Or B = Either.left(a) | |
def right[A, B](b: B): A Or B = Either.right(b) |
NewerOlder