Skip to content

Instantly share code, notes, and snippets.

View JoolsF's full-sized avatar

Julian Fenner JoolsF

  • London
View GitHub Profile
@JoolsF
JoolsF / free-monad-example-1.scala
Created January 10, 2023 19:59
Free monad example 1
/*
https://typelevel.org/cats/datatypes/freemonad.html
Free your ADT
There are five basic steps to "freeing" the ADT:
1 Create a type based on Free[_] and KVStoreA[_].
2 Create smart constructors for KVStore[_] using liftF.
3 Build a program out of key-value DSL operations.
@JoolsF
JoolsF / MontyHallProblem.scala
Last active June 2, 2022 21:20
Monty Hall problem simulator
import scala.util.Random.nextInt
//https://en.wikipedia.org/wiki/Monty_Hall_problem
case class MontyHallProblem(printActive: Boolean) {
def playMany(gameCount: Int, stick: Boolean): Double = {
val results = for (_ <- 1 to gameCount) yield playOne(nextLetter, stick)
val wins = results.count(_ == true)
val percentWon = (wins.doubleValue / gameCount.doubleValue()) * 100
@JoolsF
JoolsF / KleisliExample1.scala
Last active June 2, 2022 21:19
Kleisli Example 1
import cats.data.Kleisli
import cats.implicits._
import scala.util.Try
case class Foo(i: Int)
case class Bar(f: Foo)
val makeFoo: String => Option[Foo] = (s: String) => Try(s.toInt).toOption.map(Foo)
val makeBar: Foo => Option[Bar] = (f: Foo) => if (f.i < 0) None else Some(Bar(f))
@JoolsF
JoolsF / MonoidExample1.scala
Last active June 2, 2022 21:20
Monoids practical real-world example
/*
* Modelling business rules using Monoids
* https://medium.com/@shashankbaravani/understanding-monoids-using-real-life-examples-6ec3cb349f2f
*
* Creating business rules, in this case filters, in a wholly composable way such that they can be combined
* in arbitrary orders given they fit the definition of Monoids and are associative.
*
* The crux of the pattern is the List[Cab] => List[Cab] 'Filter' type which allows the composition.
*/
object CoolTaxi {
@JoolsF
JoolsF / AkkaStreamsExample2.scala
Last active June 2, 2022 21:21
Akka streams example 2
import akka.actor._
import akka.stream._
import akka.stream.scaladsl.Source
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
/*
* Run using foldAsync to accumulate results
@JoolsF
JoolsF / CatsValidatedExample2.scala
Last active June 2, 2022 21:21
Cats validated example 2
import cats.data.Validated
import cats.implicits._
case class User(name: String, age: Int)
type FailFast[A] = Either[List[String], A]
type FailSlow[A] = Validated[List[String], A]
def createUser(html: Map[String, String]): FailSlow[User] =
(
@JoolsF
JoolsF / type-constraints-example1.scala
Created May 30, 2019 11:18
Type constraints example 1
/*
* Problem - Constraining a parameter to only take a specific type, not its subtype
*
* https://herringtondarkholme.github.io/2014/09/30/scala-operator/
*/
class Foo()
class Bar() extends Foo
def onlyTakesFoo[A](f: A)(implicit ev: A =:= Foo) = "Foo"
def onlyTakesBar[A](f: A)(implicit ev: A =:= Bar) = "Bar"
@JoolsF
JoolsF / cats-validated-example-1.scala
Last active May 22, 2019 08:47
Cats Validated example 1
import cats.data._
import cats.implicits._
//Left side is better as non empty list
type MyValidation[A] = Validated[Vector[String], A]
case class Person(name: String, age: Int)
def validateName(name: String): MyValidation[String] =
if (name.isEmpty) {
@JoolsF
JoolsF / state-monad-example-1
Created March 12, 2019 09:45
State Monad example 1
/**
* State Monad
* State[S,A] represents function of type S => (S , A)
* S is the state and A is the result
*/
import cats.data.State
val aState = State[Int, String] { state =>
val result = state * 2
@JoolsF
JoolsF / reader-monad-example-1.scala
Last active February 4, 2019 10:35
Reader Monad example 1
/**
*
* The classic use of Readers is to build programs that accept a configuration as a
* parameter. Let’s ground this with a complete example of a simple login system.
* Our configuration will consist of two databases: a list of valid users and a
* list of their passwords:
*/
case class Db(usernames: Map[Int, String],
passwords: Map[String, String]