Skip to content

Instantly share code, notes, and snippets.

scala> type AndThen[F[_], G[_]] = {
| type Apply[A] = G[F[A]]
| }
defined type alias AndThen
scala> val x: (List AndThen Set)#Apply[Int] = null
x: scala.collection.immutable.Set[List[Int]] = null
@milessabin
milessabin / gist:2350399
Created April 10, 2012 10:51
Faux kind-polymorphism in Scala
object KindPoly {
class Eq[A, B]
object Eq {
implicit def witness[A] = new Eq[A, A]
}
implicitly[Eq[{ type K = Int}, { type K = Int }]]
implicitly[Eq[{ type K = Int}, { type K = String }]] // Does not compile
@tonymorris
tonymorris / ReaderWriterStateT.scala
Created April 11, 2012 16:59
Reader/Writer/State transformer in Scala
case class ReaderWriterStateT[R, W, S, F[_], A](
run: (R, S) => F[(W, A, S)]
) {
def map[B](f: A => B)(implicit F: Functor[F])
: ReaderWriterStateT[R, W, S, F, B] =
ReaderWriterStateT {
case (r, s) => F.map(run(r, s)) {
case (w, a, s) => (w, f(a), s)
}
}
def index(id:String) = Action {
getFirstData(id)
}
private def getFirstData(id:String) = {
Cache.get(id) match {
case Some(id2) => getSecondData(id2)
case None => NotFound
}
}
private def getSecondData(id2:String) = {
@retronym
retronym / unimplicitly.scala
Created April 20, 2012 20:59
unimplicitly
scala> import scala.language.experimental.macros
import scala.language.experimental.macros
scala> def unimplicitlyImpl[A: c.TypeTag](c: reflect.makro.Context) = {
| val A = implicitly[c.TypeTag[A]].tpe
| val i = c.inferImplicitValue(A, silent = true)
| if (i == c.mirror.EmptyTree) c.reify(())
| else sys.error("unexpected implicit of type %s: %s".format(A, i))
| }
unimplicitlyImpl: [A](c: scala.reflect.makro.Context)(implicit evidence$1: c.TypeTag[A])c.mirror.Expr[Unit]
@chobie
chobie / gist:2493950
Created April 25, 2012 22:19
php-mruby gets started!

php-mruby gets started!

you know mruby is the lightweight implementation of the Ruby language. it also works with php. I made this extension in my lunch time :)

git clone https://github.com/chobie/php-mruby.git --recursive
cd php-mruby
cd mruby
make
@takezoe
takezoe / NotVar.scala
Created April 28, 2012 15:09 — forked from xuwei-k/NotVar.scala
not var macro scala
def NotVar(expr: Any): Any = macro NotVarImpl
def NotVarImpl(c: Context)(expr: c.Expr[Any]): c.Expr[Any] = {
import c.mirror._
import reflect.api.Modifier.mutable
Expr((new Transformer{
override def transform(tree:Tree):Tree = tree match{
case ValDef(m,_,_,_) if m.hasModifier(mutable) => sys.error("can't use var !!!")
case _ => super.transform(tree)
@retronym
retronym / interpreter-result.scala
Created April 29, 2012 17:04
interpreter result
scala> val intp = new scala.tools.nsc.interpreter.IMain
intp: scala.tools.nsc.interpreter.IMain = scala.tools.nsc.interpreter.IMain@6875577e
scala> val holder = new scala.runtime.ObjectRef[Any]
holder: scala.runtime.ObjectRef[Any] = ()
scala> intp.bind("holder", "scala.runtime.ObjectRef[Any]", holder)
holder: scala.runtime.ObjectRef[Any] = ()
res16: scala.tools.nsc.interpreter.IR.Result = Success
@gseitz
gseitz / ApplicativePlus.scala
Created April 29, 2012 18:51
trying to unbork ApplicativePlus.{some, many}
// original, broken version
def many[A](a: F[A]): F[List[A]] = {
lazy val y: Free.Trampoline[F[List[A]]] = z map (plus(_, point(Nil)))
lazy val z: Free.Trampoline[F[List[A]]] = y map (map2(a, _)(_ :: _))
y.run
}
// trying to make sense of trampoline
@kmizu
kmizu / XMLStringLiteral.scala
Created May 5, 2012 15:21
An application of SIP-11 - String interpolation and formatting. This enables XML enables to make XML element using arbitrary XML parser.
import scala.xml._
object XMLStringLiteral extends App {
implicit class RichStringContext(self: StringContext) {
def xml(params: Any*): Elem = {
val Seq(first, rest@_*) = self.parts
val source = first + (params, rest).zipped.map{_+_}.mkString
XML.loadString(source)
}
}
val p = xml"""<person>