Skip to content

Instantly share code, notes, and snippets.

View yannick-cw's full-sized avatar

Yannick Gladow yannick-cw

View GitHub Profile
@yannick-cw
yannick-cw / phantomTypes.scala
Last active August 13, 2017 15:02
Examples with two phantom types patterns
object DbRepo {
sealed trait State
sealed trait Fresh extends State
sealed trait IndexCreated extends State
sealed trait IndexReady extends State
sealed trait ReadyToRead extends State
def apply(): DbRepo[Fresh] = new DbRepo
}
@yannick-cw
yannick-cw / ThrottleF.scala
Last active August 6, 2017 16:02
Monad to throttle execution
import cats.{FlatMap, Monad}
import cats.data.StateT
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import cats.implicits._
object ThrottleExp extends App {
@yannick-cw
yannick-cw / Kleisli.scala
Created August 13, 2017 14:48
Kleisli vs Methods vs Functions
/**
* Methods vs Functions vs Kleisli
*/
trait TradingMethods[Account, Market, Order, ClientOrder, Execution, Trade] {
def clientOrders(order: ClientOrder): List[Order]
def execute(order: Order, m: Market, a: Account): List[Execution]
def allocate(execution: Execution, as: List[Account]): List[Trade]
def tradeGeneration(clientOrder: ClientOrder,
market: Market,
@yannick-cw
yannick-cw / KleisliDDDTransformerComplete.scala
Created August 13, 2017 17:39
Complete example for a DDD implementation using Kleisli and EitherT with Future
import cats.data.{EitherT, Kleisli}
import common.{Amount, Valid}
import scala.concurrent.ExecutionContext.Implicits.global
import cats.implicits._
import scala.collection.mutable
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.annotation.tailrec
object IOExample extends App {
import ioInstances._
case class Player(name: String, score: Int)
val winner: (Player, Player) => Option[Player] = (p1, p2) =>
if (p1.score > p2.score) Some(p1)
else if (p2.score > p1.score) Some(p2)
else None
package com.holidaycheck.userflow
import scala.util.Try
object UrlKata {
case class Protocol(value: String) extends AnyVal
case class Domain(value: String) extends AnyVal
case class Params(params: List[(String, String)]) extends AnyVal
case class Path(value: String) extends AnyVal
case class Url(protocol: Protocol, domain: Domain, params: Params, path: Option[Path] = None)
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.Duration
object FinalTaglessMath extends App {
trait Math[Container[_]] {
def value(i: Int): Container[Int]
def add(a: Container[Int], b: Container[Int]): Container[Int]
}
@yannick-cw
yannick-cw / shared-state-in-fp.md
Created November 17, 2018 08:34 — forked from gvolpe/shared-state-in-fp.md
Shared State in pure Functional Programming

Shared State in pure Functional Programming

Newcomers to Functional Programming are often very confused about the proper way to share state without breaking purity and end up having a mix of pure and impure code that defeats the purpose of having pure FP code in the first place.

Reason why I decided to write up a beginner friendly guide :)

Use Case

We have a program that runs three computations at the same time and updates the internal state to keep track of the

import Ordering.Implicits._
def quicksort[A: Ordering](l: List[A]): List[A] = l match {
case x :: (xs @ (y :: z)) =>
val (smallerEq, greater) = xs.partition(_ <= x)
quicksort(smallerEq) ++ List(x) ++ quicksort(greater)
case base => base
}
select rental_id, return_date
from rental
where return_date is null;
select title, rating
from film
where ( rating != 'G' and rating != 'PG' ) or rating is null;
select first_name , last_name
from customer