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 Main extends App { | |
object Recursive { | |
sealed trait List[+A] | |
case object Nil extends List[Nothing] | |
case class Cons[+A](head: A, tail: List[A]) extends List[A] | |
object List { | |
// def apply[A](as: A*): List[A] = | |
// if (as.isEmpty) Nil | |
// else Cons(as.head, apply(as.tail: _*)) |
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 Main extends App { | |
import scalaz._ | |
import scalaz.std.AllInstances._ | |
import scalaz.syntax.monoid._ | |
val m = Map('x -> 1) | |
val n = Map('y -> 2) | |
println(m |+| n) | |
} |
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
// Why you can’t always flatMap to Option | |
// http://dev.sortable.com/flatmap-to-option/ | |
object Main extends App { | |
val list = List("123") | |
def parse(x: String): Option[Int] = util.Try { x.toInt }.toOption | |
val parse2 = parse _ | |
import shapeless.test.illTyped |
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 Main extends App { | |
import cats._, cats.std.all._, cats.free.{Free, Trampoline} | |
import Trampoline._ | |
def even[A](ns: List[A]): Trampoline[Boolean] = | |
ns match { | |
case Nil => done(true) | |
case x :: xs => suspend(odd(xs)) | |
} |
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
// http://stackoverflow.com/questions/37583877/transform-all-keys-from-underscore-to-camel-case-of-json-objects-in-circe/37619752#37619752 | |
import cats.free.Trampoline | |
import cats.std.list._ | |
import cats.syntax.traverse._ | |
import io.circe.{Json, JsonObject} | |
object Main extends App { | |
def transformObjectKeys(obj: JsonObject, f: String => String): JsonObject = | |
JsonObject.fromIterable( |
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 Main extends App { | |
// | |
// Create an ADT representing your grammer | |
// | |
sealed trait KVStoreA[A] | |
case class Put[T](key: String, value: T) extends KVStoreA[Unit] | |
case class Get[T](key: String) extends KVStoreA[Option[T]] | |
case class Delete(key: String) extends KVStoreA[Unit] | |
// |
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
// https://blog.codecentric.de/en/2016/02/phantom-types-scala/ | |
object Main extends App { | |
object Hacker { | |
sealed trait State | |
object State { | |
sealed trait Caffeinated extends State | |
sealed trait Decaffeinated extends State | |
} | |
def caffeinated: Hacker[State.Caffeinated] = new Hacker |
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
// Discovering Types (from Strings) with Cats and Shapeless - Jonathan Merritt | |
// https://www.youtube.com/watch?v=RecGZB3tclE | |
// https://github.com/lancelet/typequest | |
object Main extends App { | |
import scala.util.Try | |
import shapeless._, poly._, ops.hlist._, ops.nat._, UnaryTCConstraint._ | |
import cats.Monoid |
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 Main extends App { | |
// Expressing OO Data Type as Typeclass? | |
// https://www.reddit.com/r/scala/comments/4jnohx/expressing_oo_data_type_as_typeclass/ | |
/* | |
sealed trait ResourceType | |
case object Api extends ResourceType | |
case object App extends ResourceType | |
sealed trait Resource { |
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
trait FunBuilder[S, -Elem, +To] { | |
def init: S | |
def +=(s: S, elem: Elem): S | |
def ++=(s: S, elems: TraversableOnce[Elem]): S | |
def result(s: S): To | |
} | |
class VectorBuilderList[A] extends FunBuilder[List[A], A, Vector[A]] { | |
def init = List() |