Skip to content

Instantly share code, notes, and snippets.

View yannick-cw's full-sized avatar

Yannick Gladow yannick-cw

View GitHub Profile
import cats.{Applicative, Eval, Functor, Traverse}
import cats.implicits.{toFunctorOps, toTraverseOps}
import com.sksamuel.elastic4s.requests.searches.queries.term.TermQuery
import com.sksamuel.elastic4s.requests.searches.queries.{BoolQuery, Query}
import io.circe.Decoder.Result
import io.circe.{Decoder, Json}
import orderindexer.api.graphql.QueryLang.{Root, fromDslToEsAlgebra, fromJsonCoalgebra}
object ReCurse {
type Algebra[F[_], A] = F[A] => A
let mapleader = " "
" :actionlist for all commands
set surround
set multiple-cursors
set history=1000
set visualbell
set noerrorbells
set incsearch " Highlight search results when typing
set hlsearch " Highlight search results
set relativenumber " relative numbers
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
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
}
@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 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]
}
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.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
@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}
@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,