Skip to content

Instantly share code, notes, and snippets.

@wheaties
wheaties / exists.scala
Last active January 5, 2017 16:25
Implicit existence based Either
//Either based on the existence of a typeclass.
sealed trait Exists[TC[_], N, Y]{
def apply(no: N, yes: Y): Either[N, Y]
}
object Exists extends LowPriorityExists{
def apply[TC[_]] = new{
def apply[N, Y](n: N, y: Y)(implicit exists: Exists[TC, N, Y]) = exists(n, y)
}
@wheaties
wheaties / Poly-Compose.scala
Created October 19, 2016 13:01
Shapeless Poly Compose Attempt
trait ComposeBuilder[F, G] extends DepFn2[F, G]
object ComposeBuilder extends LowPriorityComposeBuilder{
implicit def cc[C1 <: Poly, L1 <: HList, C2 <: Poly, L2 <: HList]
(implicit unpack1: Unpack1[C1, Compose1, L1],
unpack2: Unpack1[C2, Compose1, L2],
pre: hl.Prepend[L1, L2]): Aux[C1, C2, Compose1[pre.Out]] =
new ComposeBuilder[C1, C2]{
type Out = Compose1[pre.Out]
def apply(c1: C1, c2: C2) = new Compose1[pre.Out]
@wheaties
wheaties / Dog.scala
Last active June 23, 2016 12:56
And now we blow up in the erasure phase...
import twotails.mutualrec
class Dog{
@mutualrec final def dog1(count: Int): Int = if (count == 0) count else dog2(count - 1)
@mutualrec final def dog2(count: Int): Int = if (count == 0) count else if (count % 2 == 0) dog1(count - 1) else dog3(count - 1)
@mutualrec final def dog3(count: Int): Int = if (count == 0) count else dog1(count - 1)
}
trait LowPriorityOneOf2{
type Aux[O, I, B <: Bool] = OneOfProof[O, I]{ type Value = B }
implicit def diverge[O, I](implicit truth: Aux[O, I, True]): Aux[O, I, False] = ???
implicit def not[O, I]: Aux[O, I, False] = new OneOfProof[O, I]{
type Value = False
}
}
class ContainsProof[Items]{
def apply[Obj](obj: Obj)(implicit yes: Contains[Obj, Items]) = true
}
class ExcludesProof[Items]{
def apply[Obj](obj: Obj)(implicit no: Excludes[Obj, Items]) = false
}
scala> val yesItDoes = new ContainsProof[Int | Double]
scala> yesItDoes(1)
type |[A, B] = (A, B)
trait OneOfProof[Obj, Items]{
type Value <: Bool
}
object OneOfProof extends LowPriorityOneOf{
def apply[Obj, Items](implicit oneOf: OneOfProof[Obj, Items]): Aux[Obj, Items, oneOf.Value] = oneOf
implicit def left[L, R, Obj <: L]: Aux[Obj, L | R, True] = new OneOfProof[Obj, L | R]{
@wheaties
wheaties / aux.scala
Last active March 17, 2016 00:32
type includes
sealed trait Bool
sealed trait True
sealed trait False
trait OneOfProof[Obj, Items]{
type Value <: Bool
}
object OneOfProof{
type Aux[O, I, B <: Bool] = OneOfProof[O, I]{ type Value = B }
trait Unapply2[TC[_[_]], MA, MB] extends Serializable {
type M[_]
type A
type B
def TC: TC[M]
def substA: MA => M[A]
def substB: MB => M[B]
}
@wheaties
wheaties / merge.scala
Last active February 22, 2016 14:08
with TSet merging
trait Merge[LSet, RSet]{
type Out
}
object Merge extends LowPriorityMerge{
implicit def tset1[T, S](implicit m: Merge[T, S]): Aux[TSet[T], S, m.Out] =
new Merge[TSet[T], S]{ type Out = m.Out }
implicit def tset[L, R, O, S](implicit ev: Aux[TSet[R], S, O], m: Merge[L, O]): Aux[TSet[L|R], S, m.Out] =
new Merge[TSet[L|R], S]{ type Out = m.Out }
class TSet[Items]{
def merge[Obj](implicit m: Merge[Obj, Items]) = new TSet[m.Out]
}
trait Merge[LSet, RSet]{
type Out
}
type |[A, B] = (A, B)