Skip to content

Instantly share code, notes, and snippets.

View kpuputti's full-sized avatar

Kimmo Puputti kpuputti

View GitHub Profile
@kpuputti
kpuputti / lazy.js
Last active December 15, 2015 20:19
Example of lazy promises ( https://github.com/nathan7/lazy-promise )
var Q = require('q'),
LazyPromise = require('lazy-promise');
function async(name) {
console.log('start async: ' + name);
var deferred = Q.defer();
// Resolve the promise in the next turn of the event loop
process.nextTick(function () {
console.log('finish async: ' + name);
@kpuputti
kpuputti / exercise-1.10.clj
Created April 2, 2013 05:03
SICP exercise 1.10 in Clojure
; compute the Ackermann's function
(defn A [x y]
(cond (= y 0) 0
(= x 0) (*' 2 y)
(= y 1) 2
:else (A (- x 1)
(A x (- y 1)))))
; f(x) = 2x
(defn f [n]
function combinations(arr1, arr2) {
return arr1.reduce(function (prev, curr) {
return prev.concat(arr2.map(function (item) {
return [curr, item];
}));
}, []);
}
> combinations([1,2,3], ['a', 'b', 'c', 'd']);
[ [ 1, 'a' ],
@kpuputti
kpuputti / nan.js
Last active December 15, 2015 02:19
to NaN or not to NaN
function canHazInt(s) {
var n = parseInt(s, 10);
if (!isNaN(n)) {
return n;
} else {
throw new Error('no can do');
}
}
@kpuputti
kpuputti / range.js
Last active December 15, 2015 02:09
// range0(n) -> [0, ... , (n - 1)]
function range0(n) {
return Array.apply(null, Array(n)).map(function (val, i) {
return i;
});
}
// range(end) -> [0, ... , (end - 1)]
// or
// range(start, end) -> [start, ... , (end - 1)]
@kpuputti
kpuputti / gist:3793564
Created September 27, 2012 11:39
Flattenezmagic
var data = [
{ name: 'a', other: 'ignore me', sub: [{name: 'a1'}, {name: 'a2'}] },
{ name: 'b', sub: [{name: 'b1'}, {name: 'b2', another: 'ignore me too'}] }
];
// Wanted: ['a', 'a1', 'a2', 'b', 'b1', 'b2']
// Flattenezify!
_.chain(data).map(function (d) {
@kpuputti
kpuputti / gist:2659607
Created May 11, 2012 13:27
JSLint bug or feature?
(function () {
'use strict';
var func1 = function () {},
func2 = function (callback) { callback(); };
// JSLint does not complain about the lines below
func2(func1.
invalid);
@kpuputti
kpuputti / gist:2488124
Created April 25, 2012 08:15
Oh no I didn't
// Set the drag being busy for 500ms
that.dragBusy = true;
window.setTimeout(function () {
that.dragBusy = false;
}, 100);
@kpuputti
kpuputti / gist:1339460
Created November 4, 2011 14:34
touchstart handlers for backbone clicks
// In the events object of the view:
// 'touchstart nav a': 'onNavTouchStart',
// 'touchmove nav a': 'onNavTouchMove',
// 'touchend nav a': 'onNavTouchEnd',
onNavTouchStart: function (event) {
var href = $(event.target).attr('href');
this.navTouchHref = (href && href !== '#') ? href : null;
},
@kpuputti
kpuputti / gist:1338982
Created November 4, 2011 09:19
URL GEt params from location.hash parsing
// one liner
_.tap({}, function (o) { _.each(location.hash.split('&'), function (p) { var s = p.split('='); o[s[0]] = s[1]; }); });
// indented
_.tap({}, function (o) {
_.each(location.hash.split('&'), function (p) {
var s = p.split('=');
o[s[0]] = s[1];