Skip to content

Instantly share code, notes, and snippets.

@abozhilov
abozhilov / gist:5024185
Last active December 14, 2015 03:49
Hanoi tower
var hanoi = function (n) {
var STATE_TABLE = [
[2, 0, 1],
[1, 2, 0]
];
var arr = [], len = n - 1,
curr_row, idx,
moves = [];
for (var i = n; i--;) arr[i] = 0;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Code container</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
.code-container {
width: 500px;
border: 1px solid #000000;
@abozhilov
abozhilov / gist:4453585
Created January 4, 2013 15:46
Node.js: Extending Array
var array = {
new : function () {
var arr = [].slice.call(arguments);
arr.__proto__ = array.proto;
return arr;
},
proto : {
__proto__ : Array.prototype,
@abozhilov
abozhilov / gist:4163571
Created November 28, 2012 19:41
examples.js
/**
* @example _.map
*/
function map() {
//Here come the examples
//of _.map
}
/**
* @example _.bind
@abozhilov
abozhilov / gist:4074219
Created November 14, 2012 19:33
Best performance
//Isn't it?
@abozhilov
abozhilov / gist:4027724
Created November 6, 2012 21:31
Reordering unique
function unique(sortedArr) {
var arr = [],
i = -1,
j = -1,
len = sortedArr.length;
while (++i < len) {
if (arr[j] !== sortedArr[i]) {
arr[++j] = sortedArr[i];
}
@abozhilov
abozhilov / gist:4011690
Created November 4, 2012 12:27
repeat
function repeat(obj, times) {
var res = typeof obj == 'string' ? '' : [];
times = Math.max(1, times);
do {
if (times & 0x1) res = res.concat(obj);
obj = obj.concat(obj);
} while ((times >>= 1));
return res;
}
@abozhilov
abozhilov / gist:3987029
Created October 31, 2012 13:25
Function.prototype.new
Function.prototype.new = function () {
var ctor = this;
function F(){return ctor.apply(this, arguments);}
F.prototype = ctor.prototype;
return new F;
};
function range(start, end, step) {
step = Math.floor(Math.abs(step)) || 1;
if (typeof end == 'undefined') {
end = start;
start = 0;
}
else if (start > end) {
var s = start;
start = end;
end = s;
@abozhilov
abozhilov / gist:3895019
Created October 15, 2012 20:08
Partial application Value range
function valueRange(start, end) {
return function (value) {
return Math.min(Math.max(value, start), end);
};
}
//Get new value range [0, 20]
var f = valueRange(0, 20);
console.log(f(10)); //10