Skip to content

Instantly share code, notes, and snippets.

@wheaties
wheaties / depfun.scala
Created October 25, 2014 13:14
Dependent Function1
trait DepFunction1[T]{ self =>
type R
def apply(t: T): R
def andThen(dep: DepFunction1[R]): Aux[T, dep.R] = new DepFunction1[T]{
type R = dep.R
def apply(t: T) = dep(self(t))
}
trait IsNotFuture[+F]
object IsNotFuture{
def apply[F](implicit isf: IsNotFuture[F]) = isf
implicit def mkFuture[A] = new IsNotFuture[Future[A]] {}
implicit object isf extends IsNotFuture[Any] {}
}
//No need for a dependent type. All you need is the implicit ambiguity.
@wheaties
wheaties / Or.scala
Last active August 29, 2015 14:08
Type Level Booleans
trait Or[X <: Boolean, Y <: Boolean]{
type Out <: Boolean
def value: bool
}
object Or extends LowPriorityOr{
def apply[X <: Boolean, Y <: Boolean](implicit or: Or[X, Y]): Aux[X, Y, or.Out] = or
implicit object OrFalse extends Or[False, False]{
@wheaties
wheaties / IsBase.scala
Last active August 29, 2015 14:08
Is Impls
trait IsBase[F]{
type T
type W[X]
def to(f: F): W[T]
def from(wt: W[T]): F
def apply(f: F): W[T] = to(f)
}
@wheaties
wheaties / Contains.scala
Created October 31, 2014 02:52
Type Contains
sealed trait TList
case object TNil extends TList
case class <+>[A, B <: TList]() extends TList //some weird symbol for ::
trait Contains[A, TL <: TList]
object Contains extends LowPriorityContains{
def apply[A, TL <: TList](implicit cnt: Contains[A, TL]) = cnt
implicit def has[F[_], A, T <: TList] = new Contains[F, F[A] <+> T]{}
@wheaties
wheaties / PostFunction.scala
Created November 24, 2014 02:32
Pre-Post Conditionals on Functions
//This even reasonably useable?
implicit class AddPost[T, R](f: T => R){
def withPostCondition(pred: R => Boolean) = {x: T =>
val out = f(x)
if(pred(out)) out else throw new MatchException("Did not satisfy post condition")
}
}
@wheaties
wheaties / With.scala
Last active August 29, 2015 14:10
Dependently typed Trampoline (WIP, just jotting my ideas down)
import shapeless._
import poly._
trait Next[T]{
type R <: Trampoline
def apply(t: T): R
}
object Next{
def apply[T](implicit nxt: Next[T]): Aux[T, nxt.R] = nxt
@wheaties
wheaties / Cont.scala
Last active August 29, 2015 14:10
Dependently Typed Continuation Monad
//for reference
trait Cont[T, R]{ self =>
def apply(f: T => R): R
def map[B](f: T => B): Cont[B, R] = new Cont[B, R]{
def apply(g: B => R): R = self(f andThen g)
}
@wheaties
wheaties / DepState.scala
Created December 7, 2014 17:53
Dependently Typed StateMonad
//alright, not completely perfect but getting close enough
//Still relying too heavily on Aux types to tie type knots
trait DepState[S]{ self =>
type R
def apply(s: S): (S, R)
def map(f: DepFun1[R]): DepState[S] = new DepState[S]{
type R = f.R
def apply(s: S): (S, f.R) ={
@wheaties
wheaties / CKnot.scala
Created January 31, 2015 20:27
Compilation error
/*
So here's the error which seems to be straight forward. That said, I've tried a bunch of things and this is the "bets" I've been
able to do:
[error] CKnot.scala:18: Cannot construct a collection of type That with elements of type B based on a collection of type C[A].
[error] def apply[A, C[A] <: GenTraversableLike[A, C[A]]](ma: C[A], f: A => C[B]) = ma flatMap f
[error]
[error] CKnot.scala:26: Cannot construct a collection of type That with elements of type B based on a collection of type C[A].
[error] a <- ma
[error]