Skip to content

Instantly share code, notes, and snippets.

@exbotanical
exbotanical / dynamic-arr-eval.js
Last active July 19, 2020 08:07
code golf cont'd
const findInArray = (array, iterator) => {
for (let [i, v] of Object.entries(array)) {
if (iterator(v, Number(i))) {
return Number(i);
}
}
return -1;
};
/*
@exbotanical
exbotanical / destructurings.js
Created July 19, 2020 15:54
experiments with destructuring
const weatherInfo = t => Object.values(((c) => ({ r: `${c} is ${c > 0 ? "above ": ""}freezing temperature` }))(((m) => (m - 32) * (5/9))(t)))[0];
// calc f => c
@exbotanical
exbotanical / uri-builder.js
Created July 20, 2020 16:24
code golf, FP and Objects edition!
class UriBuilder {
constructor(uri) {
this.root = uri;
this.url = this.root.split("?");
this.params = {};
UriBuilder.populate.apply(this);
};
static populate() {
if (!this.url.length < 2) {
const traverseTCPStates = eventList => {
let state = "CLOSED";
const _states = {
"CLOSED" : [
{ event: "APP_PASSIVE_OPEN", output: "LISTEN" },
{ event: "APP_ACTIVE_OPEN", output: "SYN_SENT"}
],
"LISTEN": [
{ event: "RCV_SYN", output: "SYN_RCVD" },
{ event: "APP_SEND", output: "SYN_SENT" },
// 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",
class BinaryHeap {
constructor(fn) {
this.scoreFunction = fn;
this.data = [];
}
push(el) {
this.data.push(el);
this.bubbleUp(this.data.length - 1);
}
// curried generator
const renderStringInterval = value => function*(num) {
yield setInterval(() => console.log(value), num);
};
renderStringInterval('test')(3000).next();
@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":
@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 / 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) => {