Skip to content

Instantly share code, notes, and snippets.

Dino Fancellu fancellu

Block or report user

Report or block fancellu

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@fancellu
fancellu / FoldableFoldMExample.scala
Created Oct 15, 2019
Example usage of the Cats Foldable FoldM function
View FoldableFoldMExample.scala
import cats._
import cats.data._
import cats.implicits._
// Example usage of the Cats Foldable FoldM function
object FoldableFoldMExample extends App{
def addIfNotTooBig(acc: Int, x: Int): Option[Int] = if (x > 8) none[Int] else (acc + x).some
@fancellu
fancellu / FreeMonadExample.scala
Created Oct 14, 2019
Example usage of the Cats Free Monad data type
View FreeMonadExample.scala
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 Oct 13, 2019
Example usage of the Cats Ior data type
View IorExample.scala
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 Oct 11, 2019
Example usage of the Cats Validated data type
View ValidatedExample.scala
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 Oct 9, 2019
Example of how to use the Cats State datatype to encapsulate state changes in an immutable manner
View StateExample.scala
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
Created Oct 8, 2019
Example of how to use the Cats Reader Monad to compose functionality and process some fixed type
View ReaderExample,scala
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 Oct 6, 2019
Example code to show how Cats Writer can be used to add useful inline logging to some computation
View WriterExample.scala
// 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 Oct 3, 2019
Example usage of Cats typeclasses that should be easy enough to follow
View Typeclasses.scala
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 / build.sbt
Last active Oct 2, 2019
EveryLife scalatest
View build.sbt
name := "everylifetest"
version := "0.1"
scalaVersion := "2.13.1"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
libraryDependencies += "org.typelevel" %% "cats-core" % "2.0.0"
@fancellu
fancellu / SingleAbstractMethodSyntax.scala
Last active Oct 2, 2019
Some nice tricks with single abstract method interface, Scala 2.11+
View SingleAbstractMethodSyntax.scala
object SingleAbstractMethodSyntax extends App{
// Some nice tricks with single abstract method interface, Scsala 2.11+
trait DoIt{
def doSomething(x:Int): Int
}
// I am creating an instance of DoIt, and supplying doSomething
val doit: DoIt= _+1
You can’t perform that action at this time.