Skip to content

Instantly share code, notes, and snippets.

View gabefinch's full-sized avatar
💅

Gabe gabefinch

💅
View GitHub Profile
@gabefinch
gabefinch / shuffle.js
Created April 22, 2021 05:14
Fisher–Yates shuffle
// Great explanation https://bost.ocks.org/mike/shuffle/
// This relies on mutation. Could the algorithm be recreated using immutable data?
function shuffle(array) {
var count = array.length
while (count) {
const current = count - 1
// exchange current card with a random card below the current card
swapIndices(array, current, Math.floor(Math.random() * current))
// decrement count
count = count - 1
@gabefinch
gabefinch / styles.plantuml
Created April 30, 2020 06:13
PlantUML skinparams
@startuml
' Basic styles that improve on defaults
skinparam BackgroundColor Bisque
skinparam NoteBorderColor DeepSkyBlue
skinparam ArrowColor Navy
skinparam Default {
FontName Consolas
FontSize 16
}
skinparam Title {
// from this excellent tutorial on decorators:
// https://javascript.info/call-apply-decorators
let worker = {
slow(min, max) {
alert(`Called with ${min},${max}`);
return min + max;
}
};
export default class PersistentObject {
constructor(name, contents) {
this._validateContents(contents);
this.name = name;
this.contents = contents;
}
set(contents) {
this.contents = { ...contents };
localStorage.setItem(this.name, this.contents);
// Identity functor
const Box = x => ({
map: f => Box(f(x)),
fold: f => f(x),
inspect: () => `Box(${x})`
});
// const Either = Right || Left
const Right = x => ({
function nums(n = 1) {
return { first: n, rest: () => nums(n + 1) };
}
nums().first; // => 1
function take(stream, n) {
if (n <= 0) return [];
else {
const { first, rest } = stream();
@gabefinch
gabefinch / reverse-list.js
Last active October 27, 2020 22:43
Linked list reverser
function reverseList(input, accumulator) {
var reversed = {
id: input.id,
next: accumulator || null
};
return input.next
? reverseList(input.next, reversed)
: reversed;
}
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.fontSize": 18,
"editor.lineHeight": 22,
"editor.fontWeight": "300",
"editor.fontFamily": "Iosevka",
// "editor.fontFamily": "Office Code Pro Medium",
//"editor.fontFamily": "Office Code Pro",
var crypto = require('crypto');
const CRYPTO_KEY = 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'; // Must be 256 bytes (32 characters)
const message = `I see a little silhouetto of a man,
Scaramouch, scaramouch will you do the fandango,
Thunderbolt and lightning very very frightening me,
Gallileo, Gallileo,
Gallileo, Gallileo,
Gallileo Figaro - magnifico`
function decrypt(text) {
const [ivStr, encodedStr] = text.split(':')
const initVector = new Buffer(ivStr, 'base64');
const decipher = crypto.createDecipheriv(
'aes-256-cbc', CRYPTO_KEY, initVector
);
const encrypted = new Buffer(encodedStr, 'base64');
let decrypted = Buffer.concat(
[decipher.update(encrypted), decipher.final()]
);