Skip to content

Instantly share code, notes, and snippets.

View koozdra's full-sized avatar

Dimitri Tishchenko koozdra

View GitHub Profile
@koozdra
koozdra / factorial.js
Last active October 19, 2015 00:31
lodash factorial
// Imperetive:
function factorial(n) {
var result = 1;
for (var i = 1; i <= n; i++) {
result *= i
}
return result;
}
@koozdra
koozdra / curry.js
Created October 10, 2015 16:30
Javascript Curry
// curry function and accepts lists of parameters
function curry(fn) {
// begin with an empty array of collected arguments
var partialArgs = [];
var f = function() {
// append all passed in arguments to the arguments collection
Array.prototype.push.apply(partialArgs, arguments);
// if we have matched or exceeded the argument length of the containing function
// call the function with the required length (extra parameters are ignored)
@koozdra
koozdra / f.js
Last active October 16, 2015 22:39
Functionalizing
Practicing underscore foo...
checkErrors: function(attrs) {
var errors = [];
_.each(this.basicValidationFields, function(key) {
errors.push(this.validatePresence(key, attrs[key]));
}.bind(this));
errors = _.compact(errors);
if (errors.length) return errors;
@koozdra
koozdra / mandelbrot.js
Last active October 18, 2015 04:47
Canvas Mandelbrot scratch code
var CANVAS_CTX = canvas.getContext('2d');
var DRAW_PIXEL_ID = CANVAS_CTX.createImageData(1,1);
var DRAW_PIXEL_DATA = DRAW_PIXEL_ID.data;
function drawPixel(ctx, x, y, r, g, b, a) {
DRAW_PIXEL_DATA[0] = r;
DRAW_PIXEL_DATA[1] = g;
DRAW_PIXEL_DATA[2] = b;
DRAW_PIXEL_DATA[3] = a;
ctx.putImageData( DRAW_PIXEL_ID, x, y );
@koozdra
koozdra / rank.js
Last active October 18, 2015 08:41
Mandelbrot Rank
function mandelbrotRank (x, y, timeout) {
var i = 0,
zx = x,
zy = y;
while (zx*zx + zy*zy < 4 && i < timeout){
zx = zx*zx - zy*zy + x + offx;
zy = 2*zx*zy + y + offy;
i += 1;
}
@koozdra
koozdra / mandelbrot.html
Created October 18, 2015 15:45
old mandelbrot code
<html>
<head>
<script type="text/javascript" src="../lib/jqueryui/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../lib/drawer.js"></script>
</head>
<body>
@koozdra
koozdra / can.js
Last active October 20, 2015 00:08
[SCRATCH] Cancellable Intermittent Function
function animate(fn) {
var halted = false;
var f = function() {
fn();
if (!halted) requestAnimationFrame(f);
}
f();
@koozdra
koozdra / objects.js
Created October 20, 2015 04:24
Object design
function point(x, y) {
return {
x: x, y: y,
shift: function(x, y) {
return _.extend({}, this, {
x: this.x + x,
y: this.y + y
})
}
@koozdra
koozdra / fun.js
Last active October 20, 2015 18:27
Apply, Call, This and That
function add(a, b, c) {
return a + b + c;
}
console.log( add.apply(null, [1, 2, 3]) );
// 6
console.log( add.call(null, 2, 3, 4) );
// 9
@koozdra
koozdra / spreadable_functions.js
Last active November 11, 2017 16:25
Javascript Spreadable Functions
function spreadable(f) {
return function() {
return f.apply(null, _.flattenDeep(arguments))
};
}
var a = spreadable(function (a, b, c, d, e, f, g, h) {
return [a, b, c, d, e, f, g, h];
});