Skip to content

Instantly share code, notes, and snippets.

@wheaties
wheaties / exportgen.scala
Created December 3, 2015 20:38
Autogen Issues
// auto-generated boilerplate
package export
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
import macrocompat.bundle
@bundle
class ExportsImplExpr(val c: whitebox.Context) {
import c.universe._
@wheaties
wheaties / mkdef.scala
Created December 2, 2015 15:19
MkExportDefMacro
def mkExportDefMacro(tcTpe: Type, exportTc: Type, nme: TermName): Tree = {
val tcRef = mkAttributedRef(tcTpe)
val kind = tcTpe.typeParams.map(_.asType.typeParams.length)
val suffix =
if(kind.exists(_ > 1) || kind.size > 22)
c.abort(c.enclosingPosition, s"$tcTpe has an unsupported kind")
else kind.mkString("")
val (f, fd) = {
val t = TypeName(c.freshName)
@wheaties
wheaties / example.scala
Created November 3, 2015 02:32
failed use of imports
import export._
//contrived example
trait LF[A] extends DepFn[A]
@imports[LF]
object LF{
//stuff
}
//historical chain of value.
import java.concurrent.ConcurrentSkipListMap
class History[Token, A, B](tk: => Token, prev: ConcurrentSkipListMap[Token, A], id: A => B){
def apply(f: A => A): (Token, B) ={
val token = tk
(token, id(update(token, f)))
}
def map[C](f: B => C) = new History(prev, id andThen f)
@wheaties
wheaties / future.scala
Last active August 29, 2015 14:24
Too Many Types
trait Query[F[_],Key,Value]{
def get[T](key: Key)(implicit store: Store[Get, Value, T]): F[T]
def set[T](key: Key)(value: Value)(implicit store: Store[Set, Value, T]): F[T]
}
@wheaties
wheaties / highlight.scala
Last active August 29, 2015 14:24
Post 1
trait Container[F[_], G[_,_], A, B, C]{
def get(idx: Int): F[A]
def set(value: A, idx: Int): G[B, A]
def replaceIf(f: A => Boolean, value: A): G[B, C]
}
class Many[A](list: List[A]) extends Container[Option,Either,A,Throwable,List[A]]{
//code here...
}
@wheaties
wheaties / mistake.scala
Last active August 29, 2015 14:23
Mistake on 2/1 of Dep Type Talk
trait IsFuture[F]{
type T
def apply(f: F): Future[T]
}
object IsFuture{
def apply[F](implicit isf: IsFuture[F]) = isf
implicit def mk[A] = new IsFuture[Future[A]]{
@wheaties
wheaties / FlatMap.scala
Created May 29, 2015 02:24
Automatic Transformer-like flatMap on a stack of Monads
//So close but no cigar!
sealed private[autolift] trait SideEffectFM[Obj, Function] extends DFunction2[Obj, Function]
private[autolift] object SideEffectFM extends LowPrioritySideEffectFM{
def apply[Obj, Function](implicit se: SideEffectFM[Obj, Function]): Aux[Obj, Function, se.Out] = se
implicit def recur[F[_], G, Function, Out0 >: Null](implicit bind: Bind[F], se: SideEffectFM.Aux[G, Function, Out0]): Aux[F[G], Function, se.Out] =
new SideEffectFM[F[G], Function]{
type Out = Out0
@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]
@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) ={