Skip to content

Instantly share code, notes, and snippets.

View mattroberts297's full-sized avatar

Matt Roberts mattroberts297

View GitHub Profile
@mattroberts297
mattroberts297 / SignupSpec.scala
Created January 4, 2019 20:25
SignupSpec.scala
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 =>
@mattroberts297
mattroberts297 / SignupCore.scala
Created January 4, 2019 20:19
SignupCore.scala
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 {
@mattroberts297
mattroberts297 / SignupCore.scala
Created January 4, 2019 20:13
SignupCore.scala
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
@mattroberts297
mattroberts297 / SignupModel.scala
Created January 4, 2019 20:09
SignupModel.scala
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))
@mattroberts297
mattroberts297 / SignupInfra.scala
Last active January 4, 2019 20:07
SignupInfra.scala
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)
@mattroberts297
mattroberts297 / EvenHarderExample.scala
Last active May 31, 2020 10:19
Cats and monad transformers
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
object EvenHarderExample {
import Types._
trait FooRepo {
def foo()(implicit ec: ExecutionContext): Future[Error Or Foo]
}
@mattroberts297
mattroberts297 / AlternativeFutureExample.scala
Created August 24, 2017 06:55
AlternativeFutureExample.scala and WontCompileFutureExample.scala
import scala.concurrent.{ExecutionContext, Future}
object AlternativeFutureExample {
def foobar(implicit ec: ExecutionContext): Future[String] = {
for {
f <- Future.failed(new RuntimeException("Foo"))
} yield {
f
}
} recover {
@mattroberts297
mattroberts297 / FutureExample.scala
Created August 23, 2017 07:21
Enriching the Future type in Scala
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 {
@mattroberts297
mattroberts297 / README.md
Created August 19, 2017 07:14
Scala for comprehensions and withFilter
@mattroberts297
mattroberts297 / package.scala
Last active August 16, 2017 21:50
Playing with cats
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)