Skip to content

Instantly share code, notes, and snippets.

View mattroberts297's full-sized avatar

Matt Roberts mattroberts297

View GitHub Profile
@mattroberts297
mattroberts297 / akka-docs.sh
Created August 16, 2017 21:19
Generate akka docs for offline use
git clone [email protected]:akka/akka.git
cd akka
sbt ";project akka-docs; paradox"
open akka-docs/target/paradox/site/main/index.html
@mattroberts297
mattroberts297 / akka-http-docs.sh
Created August 16, 2017 21:29
Generate akka http docs
git clone git clone https://github.com/akka/akka-http.git
cd akka-http
sbt ";project docs; paradox"
open docs/target/paradox/site/main/index.html
@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)
@mattroberts297
mattroberts297 / README.md
Created August 19, 2017 07:14
Scala for comprehensions and withFilter
@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 / 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 / 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 / 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 / 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 / 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