Skip to content

Instantly share code, notes, and snippets.

@thedeemon
thedeemon / gist:5afe7fe65a30165aae23
Created January 6, 2015 08:38
Micro interpreter in OCaml, take 3
type exp = IfGt of int * int * block * block
| Swap of int * int
| Copy of int * int
and block = exp list;;
let evalBlock a blk =
let rec eval = function
| IfGt(i, j, b1, b2) -> go 1 (if a.(i) > a.(j) then b1 else b2)
| Swap(i, j) ->
let ai = a.(i) and aj = a.(j) in
@thedeemon
thedeemon / gist:c2c3e436d78592dabf63
Created January 7, 2015 04:10
Micro interpreter, gds version
type exp = IfGt of int * int * block * block
| Swap of int * int
| Copy of int * int
and block = exp list;;
let rec eval (a : int array) = function
| IfGt(i, j, b1, b2) -> go a 1 (if a.(i) > a.(j) then b1 else b2)
| Swap(i, j) ->
let ai = a.(i) and aj = a.(j) in
a.(i) <- aj; a.(j) <- ai; 1
#![allow(unstable)]
enum Exp {
IfGt(usize, usize, Vec<Exp>, Vec<Exp>),
Swap(usize, usize),
Copy(usize, usize)
}
fn eval(a : &mut Vec<i32>, e : &Exp) -> i32 {
match *e {
If [0] > [1]
Swap [0] [1]
If [2] > [3]
Swap [2] [3]
If [4] > [5]
Swap [4] [5]
If [6] > [7]
Swap [6] [7]
If [0] > [2]
Swap [0] [2]
If [0] > [1]
Swap [0] [1]
If [2] > [3]
Swap [2] [3]
If [4] > [5]
Swap [4] [5]
If [6] > [7]
Swap [6] [7]
If [0] > [2]
Swap [0] [2]
(function doReplace() {
$("span").each(function(ind, span) {
var re = /\d+\.\d+\.\d+\.\d+/i;
var org = span.innerHTML;
var found = span.textContent.match(re);
if (found) {
span.textContent = span.textContent + " address search in progress ... ";
import std.stdio, std.digest.md, std.range, std.algorithm, std.ascii, std.conv,
std.concurrency, std.parallelism : totalCPUs;
char nextByte(char x) {
switch(x) { // this could be a one-liner: return x == '9' ? 'a' : ...
case '9' : return 'a';
case 'z' : return '0';
default : return cast(char)(x+1);
}
}
newtype Qeust b a = Qeust ((a -> b) -> a)
instance Functor (Qeust b) where
fmap f (Qeust aba) = Qeust (\ cb -> f (aba (\a -> cb (f a) ) ))
-- aba :: ((a -> b) -> a)
-- f :: a -> c
-- res :: Qeust b c
-- cb :: c -> b
-- f a :: c
package main
import (
"fmt"
"time"
)
func player(from chan int, to chan int, end chan int) {
for {
v := <-from
import std.stdio, core.thread, std.concurrency, std.datetime, core.atomic;
class Chan(T) {
T store;
bool haveData = false;
this() {}
void send(T v) shared {
while(haveData) yield();