Skip to content

Instantly share code, notes, and snippets.

@pocketberserker
pocketberserker / Calculator.g
Created January 4, 2012 17:13
ANTLRのtree grammarでΣ演算
grammar Calculator;
options {
output = AST;
ASTLabelType=CommonTree;
}
tokens { SUM; NEG; VARIABLE; }
@members { CommonTree subExpr; }
package ast;
import visitor.ExpressionVisitor;
public final class BinaryExpression implements Expression {
public final Expression left;
public final Op operator;
public final Expression right;
@pocketberserker
pocketberserker / Calculator.scala
Created January 4, 2012 17:48
ScalaのパーサコンビネータでΣ演算
package parser;
import util.parsing.combinator.JavaTokenParsers
import java.math.MathContext
case class Sum(init:Expression, max:Expression, expr:Expression) extends Expression
case class Value(digit:BigDecimal) extends Expression
case class Variable() extends Expression
case class Add(left:Expression, right:Expression) extends Expression
case class Sub(left:Expression, right:Expression) extends Expression
@pocketberserker
pocketberserker / ApplicationSpec.scala
Created March 21, 2012 16:32
ScalaTodoListのテストコード試し書き
package controllers
import org.specs2._
import specification._
import play.api.test._
import play.api.test.Helpers._
class ApplicationSpec extends Specification with AroundExample { def is =
@pocketberserker
pocketberserker / VendingMachine.fs
Created May 1, 2012 03:43
tddbc東京1.5のお題をF#でやってみた(途中)
module VendingMachine
type Yen =
| Thousand
| FiveHundred
| Hundred
| Fifty
| Ten
@pocketberserker
pocketberserker / gist:3330153
Created August 12, 2012 06:30
素数のときにJOJO
let isPrime = function
| n when n < 2 -> false
| n -> [2 .. n-1] |> List.forall ((%) n >> (<>) 0)
[0..100]
|> List.map (fun x -> if isPrime x then "JOJO!" else string x)
|> List.iter (printfn "%s")
@pocketberserker
pocketberserker / bktreeBenchmark.fs
Created October 29, 2012 18:55
simple benchmarking BK-tree with Dictionary, Map and IntMap in F#
open FSharpx
let test () =
let rand = Random()
let list = Seq.init 20000 (fun _ -> rand.Next()) |> Seq.distinct |> Seq.toList
let d = 3
let n = 1
let t1 = DateTime.Now
@pocketberserker
pocketberserker / bktree_distance_example.fs
Created November 21, 2012 13:54
FSharpx.BKTree.(List | ByteString).distance short examples
(*
Reference: http://en.wikipedia.org/wiki/Levenshtein_distance#Example
kitten → sitten (substitution of 's' for 'k')
sitten → sittin (substitution of 'i' for 'e')
sittin → sitting (insertion of 'g' at the end).
*)
open FSharpx.DataStructures
open FSharpx.ByteString
@pocketberserker
pocketberserker / Iteratee.fsi
Created December 6, 2012 08:37
FSharpx.Iterateeのシグネチャを書いてみた
namespace FSharpx
type Stream<'a> =
| Chunk of 'a
| Empty
| EOF
type Iteratee<'el,'a> =
| Done of 'a * Stream<'el>
| Error of exn
| Continue of (Stream<'el> -> Iteratee<'el,'a>)
@pocketberserker
pocketberserker / UnquoteNaturalSpec.fs
Created December 20, 2012 04:30
NaturalSpecとunquoteを併用したら出力がリッチにならないかな、と思ったなれの果て
// NaturalSpec: https://github.com/forki/NaturalSpec
// unquote: http://code.google.com/p/unquote/
open NaturalSpec.Utils
open NaturalSpec
open NUnit.Framework
open Swensen.Unquote
let check (assertType,a,b,value) =
let outputFail f message =