Skip to content

Instantly share code, notes, and snippets.

package main
import (
"time"
)
func main() {
}
// The essential of the code piece is this interface.
package main
import (
"fmt"
)
type Expression struct {
OP byte
X interface{}
Y interface{}
@tiancaiamao
tiancaiamao / switch.go
Last active January 13, 2018 12:13
Performance comparison between switch statement and jump table
package main
import "fmt"
import "time"
import "math/rand"
var jumpTable = [32]func(){
noop, noop, noop, noop,
noop, noop, noop, noop,
noop, noop, noop, noop,
@tiancaiamao
tiancaiamao / typing.ml
Created October 5, 2016 01:46
类型推导
module Ast = struct
type t =
Int of int
| Bool of bool
| Var of string
| App of t * t
| Fun of string * t
| If of t * t * t
| Plus of t * t
| Equal of t * t
@tiancaiamao
tiancaiamao / parsec.ml
Last active April 8, 2017 15:32
a toy parser combinator
type 'a parser_t = char list -> ('a * char list) option
let satisfy pred = function [] -> None | x::xs -> if pred x then Some(x,xs) else None;;
let range a b = satisfy (fun x -> x>=a && x <=b);;
let exactly x = satisfy ((=) x);;
let (<|>) p q = fun x -> match p x with Some _ as res -> res | None -> q x;;
let (>>=) m f = fun l -> match m l with
| None -> None
| Some(res, l1) -> f res l1;;
let return x = fun l -> Some(x, l);;
// search a trie to scan a token
ch := ch0
node := &ruleTable
for {
if node.childs[ch] == nil || s.r.eof() {
break
}
node = node.childs[ch]
s.r.inc()
ch = s.r.peek()
switch ch0 {
case '|':
s.r.inc()
if s.r.peek() == '|' {
s.r.inc()
return oror
}
return '|'
case '&':
s.r.inc()
#lang racket
(define ready-queue '())
(define list-append
(lambda (ls x)
(if (null? ls)
(cons x '())
(cons (car ls) (list-append (cdr ls) x)))))
;; 定义队列操作的函数
@tiancaiamao
tiancaiamao / ycombinator.rkt
Created December 17, 2014 01:16
y combinator推导
#lang racket
;;定义阶乘函数
(define (fact n)
(if (= n 1)
1
(* n (fact (- n 1)))))
;;由于我们不能使用define,那么把fact作为参数名
(lambda (f)
(lambda (n)
@tiancaiamao
tiancaiamao / accept.c
Created December 9, 2014 06:14
单进程架构
#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int child(int, int);