This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = | |
( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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] |
NewerOlder