Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / FreeMonadExample.scala
Created October 14, 2019 13:53
Example usage of the Cats Free Monad data type
package effects
import cats._
import cats.data._
import cats.free.Free
import cats.implicits._
// Example usage of the Cats Free Monad data type
// A free monad lets you build a Monad from any Functor
@fancellu
fancellu / IorExample.scala
Created October 13, 2019 16:51
Example usage of the Cats Ior data type
import cats._
import cats.data._
import cats.implicits._
import cats.data.{NonEmptyList => NEL}
import cats.data.{NonEmptyChain => NEC}
// Example usage of the Cats Ior data type. Similar to Validated/Either/Xor, but we can
// return both types should we wish
// Note the similarity/difference with Validated
// https://gist.github.com/fancellu/b6deaed2068ff5bd91f544d178568560
@fancellu
fancellu / ValidatedExample.scala
Last active February 25, 2021 13:48
Example usage of the Cats Validated data type
import cats._
import cats.data._
import cats.implicits._
import cats.data.{NonEmptyList => NEL}
// Example usage of the Cats Validated data type. It does not form a Monad (no flatMap)
// unlike Either, so does not stop on first failure
object ValidatedExample extends App {
@fancellu
fancellu / StateExample.scala
Created October 9, 2019 12:13
Example of how to use the Cats State datatype to encapsulate state changes in an immutable manner
import cats._
import cats.data._
import cats.implicits._
// Example of how to use the Cats State datatype to encapsulate state changes in an immutable manner
// Helps us remove error prone boilerplate when chaining state transitions
object StateExample extends App {
type Stack = List[String]
@fancellu
fancellu / ReaderExample.scala
Last active September 5, 2021 12:58
Example of how to use the Cats Reader Monad to compose functionality and process some fixed type. DI in a functional style
import cats._
import cats.data.{Reader, _}
import cats.implicits._
import scala.util.Random
// Example of how to use the Cats Reader Monad to compose functionality and process some fixed type
// We compose pure functions with for comprehensions or map/Flatmap combinators
// Useful for when you find yourself passing some context again and again
@fancellu
fancellu / WriterExample.scala
Created October 6, 2019 20:06
Example code to show how Cats Writer can be used to add useful inline logging to some computation
// Example code to show how Cats Writer can be used to add useful inline logging to some computation
import cats._
import cats.data._
import cats.implicits._
object WriterExample extends App {
type LOG = Vector[String]
type LOGWRITER[A] = Writer[LOG, A]
@fancellu
fancellu / Typeclasses.scala
Created October 3, 2019 10:24
Example usage of Cats typeclasses that should be easy enough to follow
import cats._
import cats.data._
import cats.implicits._
object Typeclasses extends App {
case class Item(name: String, price: Double)
object Item {
// note we are using SAM support to define these instances
@fancellu
fancellu / SingleAbstractMethodSyntax.scala
Last active November 15, 2019 08:55
Some nice tricks with single abstract method interface, Scala 2.11+
object SingleAbstractMethodSyntax extends App{
// Some nice tricks with single abstract method interface, Scala 2.11+
trait DoIt{
def doSomething(x:Int): Int
}
// I am creating an instance of DoIt, and supplying doSomething
val doit: DoIt= _+1
@fancellu
fancellu / InterestCalculator.scala
Created July 17, 2019 08:45
Scala Interest calculator based on bands, rounded to precision
case class Band(top: Double, interest: Double)
class InterestCalculator(bands: List[Band], precision: Int = 2) {
def calc(amount: Double) = {
val interest = bands.collectFirst {
case Band(top, interest) if top >= amount => interest * amount
}.getOrElse(0.0)
@fancellu
fancellu / CaseClassEquality.scala
Last active July 13, 2019 15:54
case class equality: Making scala only check some params, not all
case class Product(name: String)(val price: Double)
// we can choose to only supply the name, i.e. partially applied
val banana=Product("banana") _
val banana10=banana(10)
val bananafree=banana(0)
val apple=Product("apple")(5)
val list=List(banana10, apple, bananafree)