Skip to content

Instantly share code, notes, and snippets.

View shinout's full-sized avatar
🏠
Working from home

Shin Suzuki shinout

🏠
Working from home
View GitHub Profile
@shinout
shinout / deepCopy.js
Created March 5, 2014 16:51
minimal deep copy in JavaScript
function deepCopy(val) {
if (Array.isArray(val)) return val.map(deepCopy);
if (typeof val != "object" || val === null || val === undefined) return val;
var ret = {};
for (var attr in val) if (val.hasOwnProperty(attr)) ret[attr] = deepCopy(val[attr]);
return ret;
}
@shinout
shinout / newton_rahpson.coffee
Created April 28, 2014 11:43
Newton-Raphson method
newton_raphson = (f, x0)->
x0 = Math.random() if typeof x0 isnt "number"
fx0 = f(x0)
x = x0 + 0.01 # for approximating gradient
loop
fx = f(x)
gradient = (fx - fx0) / (x - x0)
delta = fx/gradient
break if Math.abs(delta) < 0.0000001
x0 = x
@shinout
shinout / steepest_descent.coffee
Created April 28, 2014 15:34
steepest descent method
# 最急降下法
steepest = (f, alpha, x0)->
alpha = 1/2 * (Math.abs Math.random()) + 0.1 if typeof alpha isnt "number"
alpha = Math.abs alpha
x0 = Math.random() if typeof x0 isnt "number"
fx0 = f(x0)
x = x0 + 0.01 # for approximating gradient
loop
@shinout
shinout / least_square.coffee
Created May 15, 2014 16:45
least square (最小二乗法)
least_square = (dataset)->
n = dataset.length
sigma_xy = 0
sigma_x = 0
sigma_y = 0
sigma_xsq = 0
for data in dataset
x = data[0]
y = data[1]
@shinout
shinout / smith-waterman.coffee
Created June 5, 2014 07:31
Smith-Waterman Algorithm in CoffeeScript
GAP = -1
MISMATCH = -1
MATCH = 1
class Cell
constructor: (i,j)->
@p1 = i
@p2 = j
@pointers = []
@score = 0
fs = require("fs")
rstream = fs.createReadStream process.argv[2]
wstream = fs.createWriteStream(process.argv[3])
rstream.setEncoding "utf-8"
ended = false
piping = ->
return if ended
d = rstream.read()
return rstream.once "readable", read_write if d is null
@shinout
shinout / parse_3_byte_uint.coffee
Created July 11, 2014 12:48
benchmark: parsing 3byte-uint in CoffeeScript
rt = require "random-tools" # https://github.com/shinout/random-tools
N = 1000000
d = []
i = 0
# data preparation
while i < N
n = rt.randomInt(256 * 256 * 256 - 1)
b = new Buffer(3)
@shinout
shinout / sync_async_fs.coffee
Created July 16, 2014 15:15
comparing performance of fs.readSync() with that of fs.read()
N = 100000
READ_SIZE = 100
fs = require "fs"
fd = fs.openSync __filename, "r"
i = 0
console.time "sync"
while i < N
@shinout
shinout / strongloop
Last active August 29, 2015 14:15
npm install strongloop on OS X (10.10.2)
shinout ~ shinout$ npm install strongloop
> heapdump@0.2.10 install /Users/shinout/node_modules/strongloop/node_modules/strong-supervisor/node_modules/heapdump
> node-gyp rebuild
CXX(target) Release/obj.target/heapdump/src/heapdump.o
CXX(target) Release/obj.target/heapdump/src/platform-posix.o
SOLINK_MODULE(target) Release/heapdump.node
SOLINK_MODULE(target) Release/heapdump.node: Finished
@shinout
shinout / copy.coffee
Created March 3, 2015 09:24
copying class in CoffeeScript
copyClass = (klass) ->
newKlass = (new Function("""
return function (call) {
return function #{klass.name}() {
return call(this, arguments)
};
};
""")())(Function.apply.bind(klass))
for own key, value of klass