Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
tonyonodi / gist:4b2db10170419dc5f18f
Last active August 29, 2015 14:04
Lisp like javascript
var range = function(a, b) {
var array,
direction;
array = new Array();
direction = ( b - a ) / Math.abs( b - a );
var i;
for ( i = a; i <= b; i += direction )
array.push( i );
@tonyonodi
tonyonodi / gist:04dcc3a12adf8105d322
Last active August 29, 2015 14:10
Javascript quine
// source code for a quine (excluding this comment).
console.log(function(string, delimiter) {return string.slice(0, -27) + delimiter + string + delimiter + string.slice(-27)}("console.log(function(string, delimiter) {return string.slice(0, -27) + delimiter + string + delimiter + string.slice(-27)}(, String.fromCharCode(34)))", String.fromCharCode(34)))
@tonyonodi
tonyonodi / gist:a459b13c3657cb59cf82
Created December 17, 2014 02:50
Keep things out of global scope
/* source: http://toddmotto.com/avoiding-anonymous-javascript-functions */
(function (window, document, undefined) {
// la la la
})(window, document);
// Prototypeless objects in JS
var map = Object.create(null); // null has no protype or properties
"toString" in map;
// false
cmd + shift + p
Search "Package Control"
@tonyonodi
tonyonodi / gist:77b26854e458cb6caef4
Created May 1, 2015 22:41
Non-awful array comparison
// compares contents but nor order... which may or may not be what you want
var arr1 = [1,2,3],
arr2 = [1,2,3];
arr1.every(function(item) {
return arr2.indexOf(item) >= 0;
})
Obviously you can't parse XML with regex but if you just need to grab some values from a feed quickly...
(?<=tag>).*(?=<\/tag)
should do the trick. Falls over if anything is nested etc.
@tonyonodi
tonyonodi / lambda calculus
Last active September 12, 2015 16:27
This is a translation of the code in Exercise 5.39 (p. 488) of SICP from Scheme into Javascript. Takes a while to wrap your head around. I think it's really really really pretty.
(function(n) {
return (function(fact_iter) {
return fact_iter(fact_iter, 1, 1)
})(function(f_i, product, counter) {
return counter > n ?
product :
f_i(f_i, counter * product, counter + 1)
})
})(5) // returns 5! or 120
// doesn't work for some reason
lambda {|n|
lambda {|fact_iter|
fact_iter(fact_iter, 1, 1)
}.call(lambda {|f_i, product, counter|
if counter > n
product
else
f_i(f_i, counter * product, counter + 1)
end
// Turn a horrible string into a slug
" My string -is 1234!!5 here".trim()
.toLowerCase()
.replace(/[\s\-]+/g, "-")
.replace(/[^\w\-]+/g, "");