Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
tonyonodi / functional_batching.js
Created December 21, 2016 18:09
Functional batching
// batch([1,2,3,4,5,6,7,8,9,10], 5) -> [[1,2,3,4,5], [6,7,8,9,10]]
const batch = (collection, batchSize) => {
return collection.reduce((accumulator, value, index) => {
const newAccumulator = index % batchSize === 0 ?
[...accumulator, []] :
accumulator;
const init = newAccumulator.slice(0, -1);
const last = newAccumulator.slice(-1)[0];
return [...init, [...last, value]];
// This is my solution to the "Little JavaScript Problem" from http://lisperator.net/blog/a-little-javascript-problem/
// Apparently this means Mihai Bazon will hire me... Woo!
var pair = (head, tail) => (f => f(head, tail))
var car = (pair) => pair((head, tail) => head)
var cdr = (pair) => pair((head, tail) => tail)
var range = (min, max) => pair(min, min == max ? null : range(min + 1, max))
var map = (list, func) => pair(func(car(list)), cdr(list) === null ? null : map(cdr(list), func))
var reverse = (list, newList=null) => list === null ? newList : reverse(cdr(list),pair(car(list), newList))
var foreach = (list, func) => list === null ? null : (func(car(list)), foreach(cdr(list), func))
@tonyonodi
tonyonodi / brainfuck.js
Last active October 8, 2016 17:58
Brainfuck interpreter
// Here's a relatively verbose version that is somewhat easier to read
////////////////////////////////////////
let program = '+++>++<[->+<]>.';
let arr = [];
let dataPointer = 0;
let instructionPointer = 0;
let output = '';
while (program[instructionPointer]) {
@tonyonodi
tonyonodi / rpnCalc.js
Last active August 20, 2016 02:31
Reverse Polish Notation Calculator
rpnCalc = (val) => {
const ops = {
"+": (a, b) => a + b,
"-": (a, b) => a - b,
"*": (a, b) => a * b,
"/": (a, b) => a / b,
};
return val.split(" ")
.reduce((stack, val) => (
@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))
@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 / 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 / bookmarklet.html
Created February 23, 2016 11:26
Bookmarklet
<a href="javascript: (function() {/* ... */})();">Bookmarklet</a>
@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 / 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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