Skip to content

Instantly share code, notes, and snippets.

class Op {
constructor(symbol, precedence, leftAssociate, apply) {
Object.assign(this, {symbol, precedence, leftAssociate, apply})
}
process(stack) {
if (this.apply === null) return
const args = []
for (let i = 0; i < this.apply.length; i++) {
const arg = stack.pop()
if (arg === undefined) {
const min = (a,b) => (a < b) ? a : b // this works on big ints too, unlike Math.min
const err = message => {throw new Error(message)} // useful if you want to throw an error in an expression
const castToNumber = bigInt => (BigInt(bigInt) <= Number.MAX_SAFE_INTEGER) ? Number(bigInt) : err(`Number too big: ${bigInt} > MAX_SAFE_INTEGER`)
const prefixTo = (array, length) => (array.length >= length) ? array : [...Array(length - array.length).fill(0), ...array]
const factorial = (() => {
const lookupTableSize = 20
let lastVal = 1n
const lookUp = Array.from({length: lookupTableSize}, (_,i) => lastVal = i === 0 ? lastVal : BigInt(i) * lastVal)
return function factorial(n) {
@kybernetikos
kybernetikos / index.html
Created February 9, 2021 14:53
Calculate Age in Minutes
<html>
<head>
<style>
body {
background-color: turquoise;
}
#age {
color: white;
font-size: xxx-large;
<html>
<head>
<style>
body {
background-color: turquoise;
}
#age {
color: white;
font-size: xxx-large;
@kybernetikos
kybernetikos / index.html
Created April 16, 2020 21:06
React no build
<html>
<head>
<title>React</title>
<script type="application/javascript" crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script type="application/javascript" crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
</head>
<body>
<div id="root"></div>
<script type="application/javascript">
@kybernetikos
kybernetikos / QuantumLottery.js
Last active February 5, 2019 22:04
Generate some random tickets from quantum numbers.
const fetch = require('node-fetch');
const lotteries = {
lotto: {
balls: [
{number: 6, from:1, to: 59}
]
},
euromillions: {
balls: [
const line = '"" "a b" "a b c" "a \\" marvellous" \'we like\'a"b a""b abab abb "hi"';
const gre = /\s*(?:(?:"((?:\\"|[^"])*)")|(?:'((?:\\'|[^'])*)')|([^\s]+))/g;
function split(str) {
const result = [];
str.replace(gre, (part, dquoteMatch, squoteMatch, wsMatch, offset, fullString) => {
result.push(dquoteMatch !== undefined ? dquoteMatch : squoteMatch !== undefined ? squoteMatch : wsMatch)
});
return result;
}
@kybernetikos
kybernetikos / repl.js
Last active March 2, 2018 11:08
REPL with types and autocomplete
// You should probably use Vorpal.js instead of this.
// usage demo down at the bottom of the file...
const Repl = require('repl');
const util = require('util');
const fs = require('fs');
const path = require('path');
// Don't be a repli-can't, be a...
class Replican {
@kybernetikos
kybernetikos / readme.md
Last active March 25, 2017 20:24
Receivers and Receiver Transformers

Transducers Primer

function reducerFn(output, value) {
	// reducer functions know how to build a structure when repeatedly given
	// that structure and values (think of it as 'builder')
	return output
}
function reduceFn(collection, reducerFn, output) {
@kybernetikos
kybernetikos / keybase.md
Created August 4, 2016 14:56
keybase.md

Keybase proof

I hereby claim:

  • I am kybernetikos on github.
  • I am kybernetikos (https://keybase.io/kybernetikos) on keybase.
  • I have a public key whose fingerprint is 4D54 659A DBC4 D807 220C 03E4 7DD3 2D18 A8D8 2630

To claim this, I am signing this object: