Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
tonyonodi / image_quine.js
Created December 30, 2015 18:27
Code that outputs its own source to a canvas as grayscale values.
(F=(L,d)=>{(C=d.createElement("canvas")).width=C.height=L;X=C.getContext("2d");for(i=L*L;i--;)X.fillStyle=`rgb(${n=`(F=${F})(16,document)`.charCodeAt(i)||32},${n},${n})`,X.fillRect(i%L,0|(i/L),1,1);d.body.appendChild(C)})(16,document)
// After running this code you should be able to grab the pixel values as a Uint8ClampedArray
// from the canvas context 'X' and use that to verify like so:
var charCodes = Array.from(X.getImageData(0,0,16,16).data).filter((_,i)=>i%4===0)
String.fromCharCode.apply(null, charCodes)
@tonyonodi
tonyonodi / es6_quine.js
Last active December 28, 2015 15:20
Even shorter quine
(y=x=>`(y=${y})()`)()
// if you want to alert it:
(y=x=>alert(`(y=${y})()`))()
@tonyonodi
tonyonodi / pause.js
Created December 5, 2015 22:56
Pause function for JS
// I'm not proud of this but JS has forced my hand
function pause(time) {
if (typeof time !== "number") {
throw("time should be a number in milliseconds");
}
var endTime;
endTime = (new Date()).getTime() + time;
while(1) {
var newTime = (new Date()).getTime();
@tonyonodi
tonyonodi / gist:508ea3cac6ff397d791e
Created November 24, 2015 11:15
Get around El Capitan breaking gem install
El Capitan breaks gem install as per http://stackoverflow.com/questions/30812777/cannot-install-cocoa-pods-after-uninstalling-results-in-error/30851030#30851030
Use:
sudo gem install -n /usr/local/bin gem-name
instead.
// Turn a horrible string into a slug
" My string -is 1234!!5 here".trim()
.toLowerCase()
.replace(/[\s\-]+/g, "-")
.replace(/[^\w\-]+/g, "");
// 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
@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
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 / 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;
})
cmd + shift + p
Search "Package Control"