Skip to content

Instantly share code, notes, and snippets.

@hexx
hexx / implicit.scala
Created May 7, 2014 11:51
implicit compile error
case class B()
object App extends A {
def main(args: Array[String]): Unit = {
printImplicit
}
def printImplicit(implicit b: B) = b
}
@hexx
hexx / gist:5734520
Last active December 18, 2015 05:39
:- module ml.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
:- implementation.
:- import_module bool, int, string, list, pair, assoc_list, lex, regex.
main(!IO) :-
Lexer = lex.init(lexemes, lex.read_from_stdin, ignore(space)),
State0 = lex.start(Lexer, !.IO),
@hexx
hexx / macro.scala
Created April 21, 2013 09:42
これで落ちるかー
import language.experimental.macros
package object macros
@hexx
hexx / listtree.scala
Created March 19, 2013 17:38
List Tree Difference
scala> showRaw(reify(Nil.::("hoge").::(123)).tree).length
res0: Int = 188
scala> showRaw(reify(123 :: "hoge" :: Nil).tree).length
res1: Int = 392
import scala.language.dynamics
import scala.util.DynamicVariable
sealed trait Node {
def render: String
}
case class ElementNode(name: String, attributes: Map[String, String], children: List[Node]) extends Node {
def render = {
@hexx
hexx / htmldsl.scala
Created March 1, 2013 18:51
HtmlDSL using Dynamic
import scala.language.dynamics
import scala.util.DynamicVariable
sealed trait Node {
def toXml: String
}
case class ElementNode(name: String, attributes: Map[String, String], children: List[Node]) extends Node {
def toXml = {
@hexx
hexx / tbtest.scala
Created February 23, 2013 08:23
Making a macro tree using ToolBox.
object Macros {
def tbtest = macro tbtestImpl
def tbtestImpl(c:Context) = {
import c.universe._
import scala.reflect.runtime.{currentMirror => cm}
import scala.reflect.runtime.{universe => ru}
val toolBox = cm.mkToolBox()
val importer = c.universe.mkImporter(ru)
@hexx
hexx / fold.scala
Created February 22, 2013 05:26
Option fold problem
scala> Some(1).map(_ :: Nil) getOrElse Nil
res0: List[Int] = List(1)
scala> Some(1).fold(Nil)(_ :: Nil)
<console>:8: error: type mismatch;
found : List[Int]
required: scala.collection.immutable.Nil.type
Some(1).fold(Nil)(_ :: Nil)
^
@hexx
hexx / 2_a_surpassing_problem.scala
Created February 3, 2013 10:04
2 A surpassing_problem
package surpassing.problem
object Specification {
def msc[A: Ordering](xs: List[A]) = (for (z :: zs <- tails(xs)) yield scount(z, zs)).max
def scount[A](x: A, xs: List[A])(implicit o: Ordering[A]) = {
import o._
xs.filter(x < _).length
}
@hexx
hexx / 1_the_smallest_free_number.scala
Created February 3, 2013 08:52
1 The smallest free number
package smallest.free.number
trait Bench {
def bench[A](body: => A) = {
val start = System.currentTimeMillis()
val result = body
val end = System.currentTimeMillis()
println(s"time: ${end - start}")
result
}