This file contains hidden or 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
| 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 |
This file contains hidden or 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._ | |
| 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 |
This file contains hidden or 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._ | |
| 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 { |
This file contains hidden or 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._ | |
| 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] |
This file contains hidden or 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._ | |
| 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 |
This file contains hidden or 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
| // 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] |
This file contains hidden or 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._ | |
| 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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) |
This file contains hidden or 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
| 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) |