Skip to content

Instantly share code, notes, and snippets.

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: _*))
@mathhun
mathhun / MapMonoid.scala
Created July 4, 2016 08:01
MapMonoid.scala
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)
}
@mathhun
mathhun / OptionFlatMap.scala
Last active June 17, 2016 01:47
OptionFlatMap.scala
// 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
@mathhun
mathhun / Trampoline.scala
Created June 8, 2016 03:50
Trampoline.scala
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))
}
@mathhun
mathhun / TransformKeysCirce.scala
Created June 8, 2016 02:17
TransformKeysCirce.scala
// 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(
@mathhun
mathhun / Main.scala
Created June 8, 2016 01:23
LearningFreeMonadCats.scala
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]
//
// 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
@mathhun
mathhun / Main.scala
Created May 17, 2016 22:31
Discovering Types
// 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
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 {
@mathhun
mathhun / Main.scala
Last active May 14, 2016 10:34
CanBuildFrom functionally
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()