Skip to content

Instantly share code, notes, and snippets.

View Announcement's full-sized avatar
💭
Looking for work!

Jacob Francis Powers Announcement

💭
Looking for work!
View GitHub Profile
@Announcement
Announcement / count.js
Created February 11, 2020 17:22
what's the correct way to document this function?
function * count() {
const maximum =
(arguments.length === 1 && typeof arguments[0] === "number") ? arguments[0] :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("maximum")) ? arguments[0].maximum :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("max")) ? arguments[0].max :
(arguments.length === 2 || arguments.length === 3) ? arguments[1] : 0;
const minimum =
(arguments.length === 1 && typeof arguments[0] === "number") ? 0 :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("minimum")) ? arguments[0].minimum :
@Announcement
Announcement / _createReducer.js
Created January 17, 2020 18:46
unexpected token @ row: 2, column: 78
// @flow
function _createReducer<T, K>(handler: { [$PropertyType<K, 'type'>]: ({ action, state }: { action: K, state: T }) => T }): (state: T, action: K) => T {
if (handler.hasOwnProperty(action.type))
return handler[action.type]({
action,
state
});
return state;
}
@Announcement
Announcement / property.ts
Created May 15, 2019 17:41
why is {[key: string]: T} not { x: number }
interface Matrix {
readonly [index: number]: {
readonly [index: number]: number;
};
}
interface CartesianPoint {
x: number;
y: number;
z: number;
}
function formula_xfyz (x: string | symbol, y: string | symbol, z: string | symbol, f: '+' | '-' | '*' | '/' | '^') {
// function fn (it: { [x]: number, [y]: number }): { [z]: number};
// function fn (it: { [x]: number, [z]: number }): { [y]: number};
// function fn (it: { [y]: number, [z]: number }): { [x]: number};
function fn (it: any): any {
const g = ({
'+': [ (x: number, y: number): number /* z */ => x + y,
(x: number, z: number): number /* y */ => z - x,
(y: number, z: number): number /* x */ => z - y ],
'-': [ (x: number, y: number): number /* z */ => x - y,
@Announcement
Announcement / dd3.js
Created December 13, 2018 06:32
substr pivot
function format_large_number (it) {
it = it.toString()
while ((/(?<=\d)(\d{3})($|(?=[,\.]))/gim).test(it)) {
it = it.replace(/(?<=\d)(\d{3})($|(?=[,\.]))/gim, ',$1')
}
return it;
}
function * dd3 (a, b, ax, bx, ay, by, n) {
if (ax === null || ax === undefined) ax = 0;
@Announcement
Announcement / fpga-generated-code.js
Last active November 15, 2018 22:01
this specific example is a 512 bit adder implemented using many 4 bit carry-skip adders.
// const { performance } = require('perf_hooks')
function generateLookupTables (...functions) {
const lookupTables = new Map()
for (const f of functions)
lookupTables.set(f, new Uint8Array(2 ** 8))
for (let input = 0; input < 2 ** 8; input++) {
const bit_0 = (input >> 0) & 1
@Announcement
Announcement / get_moves.js
Created October 17, 2018 23:06
generate possible moves on a tic tac toe board
function * getMove (...moves) {
if (moves.length === 9) yield moves
if (moves.length < 9)
for (let x = 0, X = 9; x < X; x++)
if (moves.length < 9 && !moves.includes(x))
yield* getMove(...moves, x)
}
@Announcement
Announcement / alg.js
Created August 20, 2018 10:12
any performance enhancements greatly appreciated
function * reorder (current, bucket) {
if (bucket.length === 0) yield current;
else if (bucket.length === 1) yield current.concat(bucket)
else if (bucket.length > 1)
for (let i = 0; i < bucket.length; i++) {
yield* reorder([...current, bucket[i]], [...bucket.slice(0, i), ...bucket.slice(i + 1)])
}
}
const pickByName = (obj, prop) => obj[prop];
const pickAndTransformValue = (obj, [prop, transformer]) =>
transformer(obj[prop]);
const addProps = (obj, [prop, secondProp]) => obj[prop] + obj[secondProp];
const $pickByName = object => property => object[property]
const $pickAndTransformValue = object => ([property, transformer]) => transformer(object[property])
const $addProps = object => ([prop, secondProp]) => object[prop] + object[secondProp];
@Announcement
Announcement / extras.js
Last active May 28, 2018 01:50
search lists of text.
function replaceSelection(newString, oldString, stringIndex, stringLength) {
return oldString.substring(0, stringIndex) + newString + oldString.substring(stringIndex + stringLength)
}
function compile2({ source, sourceMap }, transform) {
const generatedMap = new Map()
let offset = 0
let currentRelativePosition = 0
let generated = `${source}`