Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
@kmizu
kmizu / Main.scala
Created September 5, 2011 05:37
ほむほむ言語処理系 in Scala (by id:yuroyoroさん)
import java.io.File
import scala.io.Source
import scala.util.matching.Regex
import scala.util.parsing.combinator._
import scala.util.parsing.input.{Position, NoPosition}
sealed abstract class Insn extends ( CED => CED ){
val pos:Position
}
case class App( m:Int, n:Int, pos:Position ) extends Insn{
@kmizu
kmizu / BiKleisliSampleTypeError.scala
Created September 23, 2011 08:59
Difference between n-arity function and n-tupled function + limitation of type constructor parameter inference.
package com.github.everpeace
/**
* commented by @kmizu
* @author everpeace _at_ gmail _dot_ com
* @date 11/09/23
*/
class {
def main(args:Array[String]):Unit = {
@kmizu
kmizu / Generator.scala
Created September 24, 2011 08:27
Yet another "generator" library implementation in Scala.
//To compile this, "-P:continuations:enable" option is needed.
import scala.util.continuations._
import scala.collection._
object Generator {
abstract sealed class Computation[+A]
case class Yielded[A](k: Unit => Computation[A], v: A) extends Computation[A]
case object Done extends Computation[Nothing]
type yieldable[A] = cps[Computation[A]]
@kmizu
kmizu / MarshallingContinuations.scala
Created September 24, 2011 15:50
An example program to express that you can marshalling/unmarhalling delimited continuations in Scala.
//scalac -P:continuations:enable MarshallingContinuations.scala
import scala.util.continuations._
import scala.util.control.Exception._
import java.io._
object MarshallingContinuations {
var y = 10
def using[A <: Closeable, B](resource: A)(block: A => B): B = try {
block(resource)
} finally {
@kmizu
kmizu / FactParallel.scala
Created September 25, 2011 00:49
An example benchmark program using parallel collection framework
import scala.testing.Benchmark
import scala.collection.immutable.NumericRange
object FactParallel extends Benchmark {
def run {
def fac(n: BigInt): BigInt = NumericRange(BigInt(1), n, BigInt(1)).foldLeft(BigInt(1))(_*_)
(1 to 2000).par.map{x => /* printf("x = %02d:%s%n", x, Thread.currentThread.toString); */ fac(x) }
}
}
@kmizu
kmizu / Lottery.scala
Created September 25, 2011 13:29
A simple lottery program
val people = Iterator.continually(readLine()).takeWhile(_ != null).toArray
val random = new scala.util.Random(System.currentTimeMillis)
println("elected person is: " + people(random.nextInt(people.length)))
@kmizu
kmizu / NestablePropertyParser.scala
Created September 25, 2011 14:05
Parses .property like configuration file(But nestable) and get an instance of java.util.Properties, nesting is extracted.
import java.io.StringReader
import scala.util.parsing.combinator._
import scala.util.parsing.input._
import scala.util.parsing.combinator.syntactical._
import java.util.Properties
object NestablePropertyParser {
object Parser extends StandardTokenParsers {
lexical.delimiters ++= List("{", "}", "=")
def parse(input: String): Either[String, Properties] = {
@kmizu
kmizu / Cont.scala
Created September 25, 2011 14:34
A naive continuation monad implementation in Scala. It is useless. However, it maybe useful to understand continuation monad.
//This implementation is a porting of naive continuation monad in Haskell in "All About Monads" pages.
class Cont[R, +A](val runCont: (A => R) => R) {
def map[B](f: A => B): Cont[R, B] = {
new Cont[R, B](k => runCont(a => Cont.ret[R, B](f(a)).runCont(k)))
}
def flatMap[B](f: A => Cont[R, B]) :Cont[R, B] = {
new Cont[R, B](k => runCont(a => f(a).runCont(k)))
}
}
@kmizu
kmizu / TypeConstructorCurrying.scala
Created September 25, 2011 15:03
Scala's type constructor is not "curried" form. In this example, it is explained that we can emulate type constructor currying using abstract type member.
trait TF {
type Apply[A]
}
type Curry2[F[_, _]] = TF { type Apply[X] = TF { type Apply[Y] = F[X, Y] } }
type Curry3[F[_, _, _]] = TF { type Apply[X] = Curry2[(TF { type Apply[Y, Z] = F[X, Y, Z] })#Apply] }
// ...
type CurriedMap = Curry2[Map]
val x: CMap#Apply[String]#Apply[Int] = Map("x" -> 1, "y" -> 1) //It is valid code.
@kmizu
kmizu / Jsson.scala
Created September 25, 2011 15:54
A concise JSON DSL in Scala.
import scala.util.DynamicVariable
/**
* A concise JSON DSL in Scala.
* When you want to use, only
* import Jsson._ is needed.
* Note that this program change the semantics of standard -&gt; operator.
* I recommend that you use enclosing block with Jsson as followings:
* {
* import Jsson._
* val obj = %{