Skip to content

Instantly share code, notes, and snippets.

@goghcrow
goghcrow / compiler.ml
Created May 2, 2025 14:23 — forked from MaskRay/compiler.ml
Implementing Functional Languages: a tutorial, Template instantiation
open Syntax
module IntMap = Map.Make(struct type t = int let compare = compare end)
module List = struct
include List
let zip xs ys =
let rec go acc = function
| [], _ | _, [] -> List.rev acc
| x::xs, y::ys -> go ((x,y)::acc) (xs,ys)
@goghcrow
goghcrow / piano.js
Last active February 19, 2025 18:20
piano.js
const ctx = new globalThis.AudioContext()
// 音符频率(基于 A4 = 440Hz)
const A4 = 440 // Hz
const NOTE_FREQ = new Map([
['Digit1', A4 * 2 ** (-9 / 12)], // C4
['Digit2', A4 * 2 ** (-7 / 12)], // D4
['Digit3', A4 * 2 ** (-5 / 12)], // E4
['Digit4', A4 * 2 ** (-4 / 12)], // F4
['Digit5', A4 * 2 ** (-2 / 12)], // G4
@goghcrow
goghcrow / ExpressionProblem.java
Last active August 2, 2021 05:12
Expression Problem
public interface ExpressionProblem {
/*
问题:
实现一个简单的数学表达式语言,比如 1 + 2, 3 * 4, (1 + 2) * 3
1. 初始的特性
表达式类型:证书字面量,加法
操作:计算表达式的值
@goghcrow
goghcrow / IDObfuscation.java
Created May 26, 2019 23:33 — forked from megayu/IDObfuscation.java
an id obfuscation algorithm to generate a string of length 25
public class IDObfuscation {
private static final char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
private static final int BASE = ALPHABET.length;
private static final byte[] PADDING = {0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19, 0x18, 0x17};
private static final byte NEGATIVE = 0x2A;
private int key;
public IDObfuscation(int key) {
this.key = key;
}
@goghcrow
goghcrow / memorized.js
Last active January 27, 2016 12:46
memorized
/**
* memorized
* @param func function
* @param createKeyFunc function cachedKey使用调用参数生成 默认参数toString & 连接
* @return mixed
* @author xiaofeng
*/
var memorized = function(func, createKeyFunc) {
if(typeof func !== "function") {
throw new Error("argument error")
@goghcrow
goghcrow / curry.js
Last active January 21, 2016 07:02
curry.js
/**
* curry
* @param function func
* @param boolean variableArg 是否可变参数
* @param boolean autoCompleted 参数补全之后是否自动求解
* @return mixed
* @author xiaofeng
*/
var curry = function(func, variableArg, autoCompleted) {
variableArg = !!variableArg
@goghcrow
goghcrow / Lazy.php
Last active December 31, 2015 07:42
延迟计算,流式数据处理,空间换时间~
<?php
error_reporting(E_ALL);
/**
* Class Lazy
* 适合大量数据的流式数组操作类
* Generator::Iterator
* 提示:
* Generator是一次性的,无法rewind
* 同一个Iterator在多个循环中会互相影响
<?php
error_reporting(E_ALL);
class test {
const C = "im const\n";
public static function hello($name) {
echo "hello, $name!" . PHP_EOL;
}
}
<?php
error_reporting(E_ALL);
// require __DIR__ . '/AopClass.php';
class A {
public $property;
public function __construct($x) {
$this->x = $x;
}
<?php
/**
* Class ProxyMethod
* @author xiaofeng
* 代理类的方法
* 可以代理private protected方法,不使用setAccessible,无副作用
*/
class ProxyMethod
{
private static $staticMethods = []; // bug