Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
tonyonodi / functor.ts
Created June 17, 2019 22:42
An attempt at higher kinded types in TypeScript
// Maybe
class Nothing {
readonly tag: "None" = "None";
}
class Just<A> {
readonly tag: "Just" = "Just";
constructor(readonly value: A) {}
}
{--
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
--}
let fib = 1 : 2 : zipWith (+) fib (tail fib)
foldr (+) 0 $ filter even $ takeWhile (< 4e6) fib
const head = <A>(l: A[]) => l[0];
const tail = <A>(l: A[]) => l.slice(1);
const splitList = <A>(l: A[]) => [l.slice(0, l.length / 2), l.slice(l.length / 2)];
export const mergeSortedLists = (a: number[], b: number[], accumulator: number[] = []): number[] => {
if (a.length === 0) {
return [...accumulator, ...b];
}
if (b.length === 0) {
const cond = <A, B>(
pairs: [(input: A) => boolean, (input: A) => B][],
fallback: (input: A) => B
) => (value: A): B => {
const pair = pairs.find(([predicate]) => predicate(value));
return pair ? pair[1](value) : fallback(value);
};
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(() => {
@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";
@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,

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 / morse.js
Created April 17, 2017 15:04
Quick morse code reader
const morseMap = {
'.-': 'A',
'-...': 'B',
'-.-.': 'C',
'-..': 'D',
'.': 'E',
'..-.': 'F',
'--.': 'G',
'....': 'H',
'..': 'I',
@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}`
);