Skip to content

Instantly share code, notes, and snippets.

View davidpeklak's full-sized avatar

David Peklak davidpeklak

  • Winterthur, Switzerland
View GitHub Profile
@davidpeklak
davidpeklak / MatchTee
Last active August 29, 2015 13:56
scalaz stream matchTee
import scalaz.concurrent.Task
import scalaz.stream._
import scalaz.stream.Process._
import scalaz.stream.Process.Emit
import scalaz.stream.Tee
object MatchTee {
def matchTee[I: Ordering]: Tee[I, I, (Option[I], Option[I])] = {
val ord = implicitly[Ordering[I]]
import scala.annotation.tailrec
object MyIo2 {
sealed trait IO[+A] {
def flatMap[B](f: A => IO[B]): IO[B] = this match {
case FlatMap(x, g) => FlatMap(x, (a: Any) => g(a).flatMap(f))
case x => FlatMap(x, f)
}
import scala.util.control.Exception
import scalaz._
import scalaz.Free.FreeC
import scalaz.Scalaz._
object Kasten {
/////////////////////////
// The State stuff
/////////////////////////
@davidpeklak
davidpeklak / OrType.scala
Last active August 29, 2015 13:57
OrType
object Type {
class |:|[A, B]
implicit def orTypeA[A, B](implicit a: A): |:|[A, B] = new |:|[A, B]
implicit def orTypeB[A, B](implicit b: B): |:|[A, B] = new |:|[A, B]
type \:/[T, U] = {type λ[X] = (X <:< T) |:| (X <:< U)}
@davidpeklak
davidpeklak / LazyTraverseFold.scala
Last active August 29, 2015 13:57
Lazy Traverse Fold
type M[_] is a monad
def f(as: List[A])(m: A => M[B](c0: C)(g: (C, B) => C)(implicit MM: Monad[M]): M[C] = {
def go(c: C, ras: List[A]): M[C] = ras match {
case Nil => MM.point(c)
case head :: tail => m(head).flatMap(b => go(g(c, b), tail))
}
go(c0, as)
@davidpeklak
davidpeklak / WE.scala
Created March 17, 2014 21:01
EitherTWriterT
import scalaz._
object WE {
type EA[+A] = (String \/ A)
type WT[+A] = WriterT[EA, Seq[Any], A]
def replace(j: Int)(i: Int): (String \/ Int) = {
if (j > 9) -\/("Failed with " + j.toString)
@davidpeklak
davidpeklak / KleisliTest
Last active August 29, 2015 14:00
KleisliTest
package smt
import scalaz.concurrent.Future
import scalaz.Scalaz._
import org.scalatest.FunSuite
import scalaz._
class KleisliTest extends FunSuite {
lazy val s: List[Int] = List.fill(1000000)(1)
@davidpeklak
davidpeklak / EitherTKleisli.scala
Last active August 29, 2015 14:01
EitherTKleisli
package smt.util
import scalaz.syntax.Ops
import scalaz._
import Kleisli._
object EitherTKleisli {
type EitherTKleisli[M[+ _], D, E, A] = EitherT[({type λ[+α] = Kleisli[M, D, α]})#λ, E, A]
@davidpeklak
davidpeklak / KleisliCombineDep.scala
Last active August 29, 2015 14:01
KleisliCombineDep
package smt.util
import scalaz.{Monad, Bind, Kleisli}
import scalaz.syntax.Ops
import scalaz.Kleisli._
import scalaz.Scalaz._
trait KleisliCombineDep {
type Logger
@davidpeklak
davidpeklak / gist:a72892ae8c67feb8af4a
Created September 10, 2014 05:38
Idris Vect shrinking
module Main
data ElemMV : (f : Fin (S n)) -> a -> Vect n a -> Type where
NotThereMV : ElemMV fZ x xs
HereMV : ElemMV f x xs -> ElemMV (fS f) x (x :: xs)
ThereMV : ElemMV f x xs -> ElemMV (weaken f) x (y :: xs)
elemMV : DecEq a => (x : a) -> (xs : Vect n a) -> (f ** (ElemMV f x xs))
elemMV x [] = (fZ ** NotThereMV)
elemMV x (y :: xs) with (decEq x y)