Skip to content

Instantly share code, notes, and snippets.

@exbotanical
exbotanical / functional.bash
Created February 28, 2021 19:57
light functional programming in bash
#!/usr/bin/env bash
# *light* functional programming in bash
IFS=$'\n'
# not really fp, but including it anyway - get over it
for_each() {
local fn=$1
shift
@exbotanical
exbotanical / serialAxios.js
Last active February 27, 2021 05:46
serially iterate through axios calls
const axios = require('axios'); // npm i axios
async function *asyncForEach (fn, arr) {
for (let prop of arr) {
yield await fn(prop);
}
}
const results = [];
@exbotanical
exbotanical / maybe.js
Last active December 28, 2020 08:42
A simple implementation of a Maybe Monad
const identity = x => x;
function Just (v) {
return { map, chain, ap };
function map (fn) {
return Just(fn(v))
}
function chain (fn) {
@exbotanical
exbotanical / synthetic-promise.js
Created December 6, 2020 20:34
A custom implementation of Promises
/* A custom implementation of Promises */
class SyntheticPromise {
constructor(executor) {
this.chain = [];
this.handleException = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
@exbotanical
exbotanical / tiny-dispatcher.js
Last active November 19, 2020 03:36
A super tiny events dispatcher and data store
const init = (db) => {
db.actions = {};
db.store = {};
db.register = (name, fn) => {
db.actions[name] = fn;
return db;
}
db.dispatch = (opt) => {
@exbotanical
exbotanical / unixFileSystem.js
Created November 15, 2020 03:59
Messing around
/* auxillary helpers */
const setPermissions = (r, w, x) => ({
read: r || false,
write: w || false,
execute: x || false,
});
const permissionsEnum = {
read: "r",
write: "w",
@exbotanical
exbotanical / customAPI.js
Last active September 15, 2020 06:02
code golf cont. - APIs, generators, memoized fns, currying fun, etc
class TestController {
constructor(dataSource) {
this.data = dataSource;
}
dispatch(obj) {
switch(obj.method) {
case "GET":
return this.handleGet(obj.query);
case "DELETE":
// curried generator
const renderStringInterval = value => function*(num) {
yield setInterval(() => console.log(value), num);
};
renderStringInterval('test')(3000).next();
class BinaryHeap {
constructor(fn) {
this.scoreFunction = fn;
this.data = [];
}
push(el) {
this.data.push(el);
this.bubbleUp(this.data.length - 1);
}
// usage of Revealing Constructor / Module Pattern here enables instance versus business logic discretion
const Logger = (function () {
/* Internal Methods and Properties */
// standard color dict for extended options
const _colorDict = {
reset : "\x1b[0m",
bright : "\x1b[1m",
dim : "\x1b[2m",