Skip to content

Instantly share code, notes, and snippets.

def classify(xts:List[('a,Type.T)], ini:'b, addf:('b,'a)=>'b, addi:('b,'a,Type.T)=>'b):'b = {
xts.foldLeft(ini) {
case (acc, (x, t)) => t match {
case Type.Unit() => acc
case Type.Float() => addf(acc, x)
case _ => addi(acc, x, t)
}
}
}
@hsk
hsk / midi.scala
Last active August 29, 2015 14:00
functional midi file parser 疑似コード
// orignal code http://openpear.org/package/IO_MIDI/downloads
package midi
case class Midi(header:Header,tracks:List[Track])
trait Chunk
case class Header(format:Int,numberOfTracks:Int,divisionFlag:Int,division:Int) extends Chunk
case class Track(List[Event]) extends Chunk
trait Event
class IO_MIDI {
@hsk
hsk / after.rs
Last active August 29, 2015 14:03
trait Eq1 {
fn eq1(&self, o: &Self) -> bool;
}
trait Eq2 {
fn eq2(&self, o: &Self) -> bool;
}
struct I {i:int} impls Eq1, Eq2 {
fn eq1(&self, o: &I) -> bool { (*o).i == (*self).i }
fn eq2(&self, o: &I) -> bool { (*o).i == (*self).i }
}
var traits = {};
function trait(name, methods) {
traits[name] = methods;
}
function struct() {
var args = arguments;
// インターフェイスを作成
@hsk
hsk / set.ml
Last active August 29, 2015 14:03
open Format
let pp_ls f ppf ls =
let rec loop ppf = function
| [] -> ()
| [x] -> fprintf ppf "%a@?" f x
| x::xs -> fprintf ppf "%a, %a" f x loop xs
in
fprintf ppf "{%a}@?" loop ls
let pp_i ppf i = fprintf ppf "%d@?" i
package set
object main extends App {
def power[A](t: Set[A]): Set[Set[A]] = {
@annotation.tailrec
def pwr(t: Set[A], ps: Set[Set[A]]): Set[Set[A]] =
if (t.isEmpty) ps
else pwr(t.tail, ps ++ (ps map (_ + t.head)))
pwr(t, Set(Set.empty[A])) //Powerset of ∅ is {∅}
}
var traits = {};
var insts = {};
function trait(name, methods) {
traits[name] = methods;
insts[name] = {};
}
function struct(name, params, impls, body) {
function findTraitName(x) {
var traits = {};
var insts = {};
function trait(name, methods) {
traits[name] = methods;
insts[name] = {};
}
function struct(typeName, params, traitNames, bodys) {
use std::f64::consts::PI;
trait Shape { fn new(area: f64) -> Self; }
struct Circle { radius: f64 }
struct Square { length: f64 }
impl Shape for Circle {
fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
}
impl Shape for Square {
fn new(area: f64) -> Square { Square { length: area.sqrt() } }
#include <stdio.h>
extern "C" {
void printi(int i) {
printf("%d\n", i);
}
typedef void (*A_p)(struct A*);
struct A {
static void _new(struct A* self) {