Skip to content

Instantly share code, notes, and snippets.

View 5alamander's full-sized avatar

ForgiveMyAwfulEngilsh 5alamander

View GitHub Profile
@5alamander
5alamander / binding0.rb
Created May 12, 2016 15:24
Ruby binding of a Proc
# take a note of http://stackoverflow.com/questions/10058996/changing-the-binding-of-a-proc-in-ruby
class Proc
def call_with_vars(vars, *args)
Struct.new(*vars.keys).new(*vars.values).instance_exec(*args, &self)
end
end
# test
lambda { foo }.call_with_vars(:foo => 3) # => 3
lambda { |a| foo + a }.call_with_vars({:foo => 3}, 1) # => 4
@5alamander
5alamander / orderedhash.rb
Created May 13, 2016 13:29
ruby Ordered-hash, add method to a eigen-class
hash={}
hash.instance_eval do # if you want this for all hashes, replace this line with class Hash
def []=(key,val)
ordered_keys << key
super(key,val)
end
def ordered_keys
@ordered_keys ||= []
@5alamander
5alamander / CardIdentifier.cs
Last active June 1, 2016 03:17
TargetTrack for HiAR-SDK
using UnityEngine;
using System.Collections.Generic;
public interface ICardIdentifierListener {
void OnTargetChange(Transform t);
void OnTargetFound(Transform t);
void OnTargetTracked(Transform t);
void OnTargetLost(Transform t);
}
csp = require 'js-csp'
player = (name, table) ->
loop
ball = yield csp.take table
if ball is csp.CLOSED
console.log name + ": table's gone"
return
ball.hits += 1
console.log name + " " + ball.hits
@5alamander
5alamander / csp_test.cs
Last active October 6, 2016 05:17
Unity, goroutine-like csp, tests
using UnityEngine;
using System.Collections;
using Sa1;
public class CspTest : MonoBehaviour {
// Use this for initialization
void Start () {
csp.go(this, simple());
csp.go(this, timeout());
@5alamander
5alamander / DaisyChain.coffee
Last active October 17, 2016 03:06
chinese whispers game, daisy-chain in go, coffee/js-csp, erlang/elixir
csp.go ->
console.time 'daisy chain'
n = 100000
leftmost = csp.chan()
right = leftmost
left = leftmost
for i in [0...n]
right = csp.chan()
csp.go (left, right) ->
# bash proxy
# export HTTP_PROXY=http://127.0.0.1:16823/
# export http_proxy=http://127.0.0.1:16823/
# export HTTPS_PROXY=https://127.0.0.1:16823/
# export https_proxy=https://127.0.0.1:16823/
# export ALL_PROXY=http://127.0.0.1:16823/
# export all_proxy=https://127.0.0.1:16823/
# go path
export GOROOT=/usr/local/opt/go/libexec
@5alamander
5alamander / gulpfile.coffee
Last active October 31, 2016 13:03
customize gulp file
through = require('through2')
gulp.task 'src', ->
gulp.src './src/*.coffee'
.pipe coffee({bare: true})
.pipe gulp.dest('./dest/')
.pipe through.obj((file, encoding, callback) ->
callback(null, doSomethingWithTheFile(file))
)
.on 'error', gutil.log
@5alamander
5alamander / node_readline.js
Created November 8, 2016 10:18
a prompt with history
// IMPORTANT: choose one
var RL_LIB = "libreadline"; // NOTE: libreadline is GPL
//var RL_LIB = "libedit";
var HISTORY_FILE = require('path').join(process.env.HOME, '.mal-history');
var ffi = require('ffi'),
fs = require('fs');
var rllib = ffi.Library(RL_LIB, {
@5alamander
5alamander / js_csp.d.ts
Created November 9, 2016 10:40
typescript: this is a dts file for js-csp
declare module 'js-csp' {
namespace Boxes {
class Box<T> {
value: T
constructor(value: T)
}
class PutBox<T> {
handler: Handlers.HandlerType
value: T