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
// 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 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 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 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 |
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
package example | |
trait Functor[F[_]] { | |
def map[A, B](fa: F[A])(f: A => B): F[B] | |
def lift[A, B](f: A => B): F[A] => F[B] = map(_)(f) | |
} | |
object Functor { | |
def apply[F[_] : Functor]: Functor[F] = implicitly[Functor[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
import monocle.{Iso, Prism} | |
import monocle.macros.syntax.lens._ | |
import monocle.macros.GenIso | |
object LensApp extends App{ | |
case class Street(number: Int, name: String) | |
case class Address(city: String, street: Street, postcode: Option[String]=None) | |
case class Company(name: String, address: Address) |
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 | |
// Simple example of an IO effect Monad, no Cats | |
object MyIO extends App{ | |
// IO encapsulates a side effecting operation | |
final class IO[A](val run: () => A) { | |
def map[B](f: A => B): IO[B] = IO(f(run())) |
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
// Just a little example, but frankly, avoid Futures, they are eager and do not compose well | |
import scala.concurrent.Await | |
import scala.concurrent.Future | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
object FutOrElse extends App{ | |
def getSource1(s: String): Future[Option[String]]= |
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
object Unapp extends App { | |
case class MyClass(age: Int, name: String) | |
private object Age { | |
def unapply(myClass: MyClass): Option[Int] = myClass match { | |
case MyClass(age, _) if (age > 0) => Option(age) | |
case _ => None | |
} | |
} |
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.Eval | |
object Ev extends App{ | |
// eager, method call | |
val always: Eval[Int] =Eval.always{ | |
println("running always") | |
123 | |
} | |
// lazy, memoized |
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 simulacrum._ | |
// Example usage of Simulacrum, v similar to math.Ordering/Numeric in standard SDK | |
object SimType extends App { | |
@typeclass trait Showing[T] { | |
def show(x: T): String | |
} | |
@typeclass trait Ordering[T] extends Showing[T]{ |
OlderNewer