Skip to content

Instantly share code, notes, and snippets.

data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show
append :: Ord a => a -> Tree a -> Tree a
append a Empty = (Node a Empty Empty)
append a (Node v left right)
| a > v = Node v left (append a right)
| a < v = Node v (append a left) right
| a == v = Node v left right
nodes = foldr append Empty [8,6,4,1,7,3,1,5]
@sshark
sshark / numbers.scala
Last active January 23, 2016 16:07
Demonstrate how typeclasses are used to manage existing types and implement new unforeseen types
trait Num[A] extends Ordering[A] {
def plus(a:A,b:A):A
}
def plus[T: Num](a: T, b: T):T = implicitly[Num[T]].plus(a,b)
def cmp[T:Ordering](a: T,b: T):Int = implicitly[Ordering[T]].compare(a,b)
implicit object DoublePlus extends Num[Double] {
override def plus(a:Double, b:Double) = a + b
override def compare(a:Double, b:Double) = a.compare(b)
trait Functor[F[_]] {
def map[A, B](x: F[A])(f: A => B): F[B]
}
implicit def optionFunctor: Functor[Option] = new Functor[Option] {
override def map [A, B](x: Option[A])(f: A => B): Option[B] = x map f
}
implicit def tuple2Functor[X] = new Functor[({type Alias[A] = Tuple2[X, A]})#Alias] {
override def map[A, B](t: Tuple2[X, A])(f: A => B): Tuple2[X, B] = (t._1, f(t._2))
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
import scalaz._
import Scalaz._
type IntState[A] = State[Int, A]
case class Song(name: String, durationInSeconds: Int)
@sshark
sshark / MonadicAndApplicationValidation.scala
Last active May 18, 2016 11:38
How to use Scalaz EitherT and \/ both monadically and applicatively to validate inputs
import scalaz._
import Scalaz._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Order(id: Long, desc: String)
case class Vendor(id: Long, name: String)
def delay[A](f: => Future[A], duration: Long, message: String) = {
println(message)
@sshark
sshark / ReaderWithFutureOption.scala
Last active June 7, 2016 12:45
Uses of Reader Monad in combination with other monads
import scalaz._
import Scalaz._
import scala.language.higherKinds
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
trait Session {
def doSomething(): Unit
}
@sshark
sshark / kleisli.scala
Last active June 5, 2016 14:58 — forked from jto/kleisli.scala
For future reference on Kleisli
import scalaz._
import Scalaz._
import scala.concurrent.Future
import scalaz.std.scalaFuture._
import play.api.libs.concurrent.Execution.Implicits._
val f1 = (s: String) => Option("foo").point[Future]
def f2 = (s: String) => Option(1).point[Future]
type OptionTFuture[T] = OptionT[Future, T]
import iomonad.Pure
import iomonad.Pure.IO
val io =
for {
_ <- Pure.println("Starting work now.")
// Do some pure work
x = 1 + 2 + 3
_ <- Pure.println("All done. Home time.")
} yield x
@sshark
sshark / trampoline.scala
Last active June 23, 2016 11:59
Trampoline for Recursion between 2 Functions (Bouncing)
import scala.annotation.tailrec
import scala.math.Numeric.Implicits._
sealed trait Thunk[A]
case class Done[A](x: A) extends Thunk[A]
case class Cont[A](f: () => Thunk[A]) extends Thunk[A]
def evenT[A: Numeric](i: A)(implicit f: Int => A): Thunk[Boolean] = if (i == 0) Done(true) else Cont(() => oddT(i - 1))
def oddT[A: Numeric](i: A)(implicit f: Int => A): Thunk[Boolean] = if (i == 0) Done(false) else Cont(() => evenT(i - 1))
@sshark
sshark / Employee.java
Last active October 23, 2017 07:24
How <T extends Comparable<T>> or similar declaration is used
package acme;
public class Employee implements Comparable<Employee> {
@Override
public int compareTo(Employee o) {
return 0;
}
}