Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / PlayJsonAnyVal.scala
Last active May 18, 2020 13:27
PlayJson Example of how to use a strongly typed string via case class, yet not change json
import play.api.libs.json._
object PlayJsonAnyVal extends App {
final case class Address(path: String) extends AnyVal
object Address {
// this allows Address to be used as a map key, even though it is not a String
implicit val keyReads: KeyReads[Address]= key => JsSuccess(Address(key))
implicit val keyWrites: KeyWrites[Address] = _.path
@fancellu
fancellu / CatFunc.scala
Created May 14, 2020 16:07
Example of Functor usage in Cats for a custom type vs standard List
import cats._
import cats.data._
import cats.syntax._
import cats.implicits._
object CatFunc extends App {
// a very naive linked list implementation and functor for it
trait MyList[+A]
case class MyNode[A]( head: A, tail: MyList[A]) extends MyList[A]
@fancellu
fancellu / SimType.scala
Last active May 18, 2020 14:05
Example usage of Simulacrum Typeclass, v similar to math.Ordering/Numeric in standard SDK
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]{
@fancellu
fancellu / Ev.scala
Created May 13, 2020 15:17
Cats Eval example
import cats.Eval
object Ev extends App{
// eager, method call
val always: Eval[Int] =Eval.always{
println("running always")
123
}
// lazy, memoized
@fancellu
fancellu / Unapp.scala
Last active May 18, 2020 14:05
Simple example of custom extractor objects vs existing case class extractors
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
}
}
@fancellu
fancellu / FutOrElse.scala
Created May 7, 2020 14:38
Scala Future[Option[T]] example. Returns first successful future with Something in the Option of T
// 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]]=
@fancellu
fancellu / MyIO.scala
Last active October 5, 2024 13:24
Simple example of an IO effect Monad, no Cats, no ZIO
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()))
@fancellu
fancellu / HK1.scala
Last active May 18, 2020 14:08
Simple Scala higher kinded type example plus type class resolution, pure scala, no Cats
// Higher kinded type example
object HK1 extends App{
// Container C must take a type parameter
trait Maker[C[_]] {
def apply(i: Int): C[Int]
}
implicit object MakerList extends Maker[List] {
@fancellu
fancellu / LensApp.scala
Created April 28, 2020 14:29
Example usage of the Monocle Optics library showing manipulation of deeply nested immutable objects
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)
@fancellu
fancellu / FunctorApp.scala
Created April 26, 2020 12:45
Simple Scala Functor example, no cats
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]]