Skip to content

Instantly share code, notes, and snippets.

@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 / 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]) {
// 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 / 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]];
@tonyonodi
tonyonodi / insertCommas.js
Created March 3, 2017 02:05
Add commas to numbers
const insertCommas = number => number
.toString()
.split("")
.reduce(
(acc, curr, i, arr) => (arr.length - i) % 3 === 0
? `${acc},${curr}`
: `${acc}${curr}`
);
@tonyonodi
tonyonodi / morse.js
Created April 17, 2017 15:04
Quick morse code reader
const morseMap = {
'.-': 'A',
'-...': 'B',
'-.-.': 'C',
'-..': 'D',
'.': 'E',
'..-.': 'F',
'--.': 'G',
'....': 'H',
'..': 'I',

How a Modern Web Text Editor Should Work

The current crop of browser based text editors are difficult to integrate into websites that use state containers - such as Redux, Elm, and React's setState - in a satisfactory way. In a react context we would ideally like to give the editor component a value attribute and update the attribute ourselves, allowing the changes to flow back down to the editor which can then re-render. The editor can maintain a mutable text buffer in its internal state for performance reasons, but as long as its methods are bug free this should look no different to an immutable, state-free component to an outside component.

First let's look at what the initial state of such an editor, with an empty buffer and undo history, would look like assuming we are using immutable.js to maintain state:

{
  historyCursor: null
 history: List([])
@tonyonodi
tonyonodi / prime_anagram.js
Created April 23, 2018 15:29
Find anagrams using the fundamental theorem of arithmetic.
// Find anagrams using the fundamental theorem of arithmetic. Not at all
// efficient and stops working very quickly due to floating point imprecision.
// Inspired by https://twitter.com/fermatslibrary/status/988399621402656773
const primes = {
"a": 2,
"b": 3,
"c": 5,
"d": 7,
"e": 11,
@tonyonodi
tonyonodi / build.js
Created February 15, 2019 14:51
Simple build script to compile, minify and inline javascript and css
const fs = require("fs");
const path = require("path");
const cheerio = require("cheerio");
const UglifyJS = require("uglify-js");
const babel = require("@babel/core");
const CleanCSS = require("clean-css");
const htmlMinify = require("html-minifier").minify;
const inputFileName = "index.html";
import * as React from "react";
import { interval, Observable, fromEvent, merge } from "rxjs";
import { map, scan, mapTo } from "rxjs/operators";
const { useState, useEffect, useRef } = React;
function useStream<T>(streamContainer: () => Observable<T>, initialValue: T) {
const [state, setState] = useState(initialValue);
useEffect(() => {