Skip to content

Instantly share code, notes, and snippets.

@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.
@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 / 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 / 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 / conway.js
Created February 2, 2016 17:23
Conway's game of life spaghetti code
R = 1000/10;
s = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@tonyonodi
tonyonodi / gist:3b4549bc95c23b448e8d
Created February 8, 2016 11:12
Enter HTML directly into address bar
data:text/html,<h1>Hello, world! etc.
@tonyonodi
tonyonodi / bookmarklet.html
Created February 23, 2016 11:26
Bookmarklet
<a href="javascript: (function() {/* ... */})();">Bookmarklet</a>
@tonyonodi
tonyonodi / string_reduce.js
Created May 5, 2016 21:04
Functional way of reducing array to list.
// Not really sure how I feel about this but it's kinda cool...
const prettyList = function(list, conjunction) {
return list.reduce((acc, str, i, arr) => (
arr.length - i == 1 ?
`${acc}${conjunction} "${str}"` :
`${acc}"${str}", `
),
"");
}
@tonyonodi
tonyonodi / repl.html
Last active May 23, 2016 17:12
Attempt at implementing a js repl using es6 generators
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<!-- https://raw.githubusercontent.com/babel/babel/master/packages/babel-regenerator-runtime/runtime.js -->
<script src="http://codepen.io/anon/pen/BKXJKy.js"></script>
<ul id="console-outputs"></ul>
<form id="console-form">
<input type="text" />
</form>
<script>
function submitted() {
@tonyonodi
tonyonodi / bytes_to_int.clj
Created July 21, 2016 14:05
Convert byte array to integer in Clojure
; Adapted from http://stackoverflow.com/a/13041851/3122246 the way it goes about converting (converts to list of
; strings, then to a string then reads the string) is kind of ugly but the code is nice at least.
(defn bytes->int [bytes]
"Converts a byte array into an integer."
(->>
bytes
(map (partial format "%02x"))
(apply (partial str "0x"))
read-string))