Skip to content

Instantly share code, notes, and snippets.

@abuseofnotation
abuseofnotation / readerCompose.js
Last active September 3, 2021 10:23
Invokes functions that are given as arguments in succession. Works like flow/pipe/compose, but all functions are given an additional "state" argument which may contain any additional values that are needed by all functions e.g. configuration. Inspired by the Reader monad.
// Invokes functions that are given as arguments in succession.
// Works like flow/pipe/compose, but all functions are given an additional "state" argument
// which may contain any additional values that are needed by all functions e.g. configuration.
// Inspired by the Reader monad.
const readerCompose = (functions) => functions
.reduce((fn1, fn2) => (state) => (...args) =>
fn2(state)(fn1(state)(...args))
)
const state = {
@abuseofnotation
abuseofnotation / pattern.html
Created July 27, 2021 18:48
Creates a grid, filled with squares of random color
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>random pattern generator</title>
<!--<link rel="stylesheet" href="style.css" /> -->
</head>
<body id="home">
<canvas id="canvas" width="1000px" height="1000px"></canvas>
@abuseofnotation
abuseofnotation / compare-and-find-common-element.ts
Created December 17, 2020 10:22
Compare two arrays and find common elements
/*
Given two arrays of elements, say 1 and 2, write a function that finds the
common elements between them and returns the following three arrays:
- An array containing all elements in 1 that aren't also contained in 2.
- An array containing all common elements that are both in 1 and 2.
- An array containing all elements in 2 that aren't also contained in 1.
*/
// Simple solution - O(n^2)
@abuseofnotation
abuseofnotation / insertion-sort.ts
Last active December 14, 2020 21:04
Insertion sort implementation in JS
const array = [1, 2, 10, 5, 4, 0, 9, -1, 3];
const insertionSort = (array: Array<number>) => {
for (let i = 1; i < array.length; i++) {
const newElement = array[i];
const prevElement = array[i - 1];
// If the current element is smaller than the previous one,
// we have to insert it in its proper place
if (prevElement > newElement) {
console.log('Found an unordered element',newElement)
@abuseofnotation
abuseofnotation / advent-of-code-2020-01.js
Last active January 22, 2021 11:58
Advent of code 2020 Day 1 - functional JS
/*
https://adventofcode.com/2020/day/1
After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you.
The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room.
To save your vacation, you need to get all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up.
Specifically, they need you to find the two entries that su
@abuseofnotation
abuseofnotation / cachedFetch.ts
Last active May 16, 2019 10:25
A drop-in replacement for the JS `fetch` method which caches all GET requests. Supports an additional parameter `cachePeriod` allowing you to specify how recent should the response be.
const maxCachePeriod = 3000;
const cache = new Map();
setInterval(() => {
const now = +new Date();
cache.forEach((value, key, map) => {
if (now - value.time > maxCachePeriod) {
cache.delete(key);
}
});
}, maxCachePeriod);
@abuseofnotation
abuseofnotation / .bashrc
Last active May 8, 2019 10:01
Custom command which opens all files matching a given pattern in vim
#!/bin/bash
# uses ripgrep to search for all matches of a given phrase in the current directory and open all
# files which contain matches in vim
function rv() {
vim -p $(rg -l "$1" | sed 'sa\\a/ag') -c "/$1"
}
@abuseofnotation
abuseofnotation / .gitconfig
Created April 8, 2019 07:35
An interactive command for switching branches.
[alias]
switch = "!branchesString=$(git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short),' refs/heads/) && branches=(${branchesString//,/ }) && for i in ${!branches[@]}; do echo \"$i - ${branches[$i]}\"; done && read -p which: branch && git checkout ${branches[$branch]}"
/*
Given a list of objects which form one or many trees, this function builds and returns these trees. Works in linear time.
*/
const idField = 'id';
const parentIdField = 'parent'
const buildTree = (objectList) => {
let mainObjects = [];
let objects = {};
/*
* Implementation
*/
// Takes an object containing several effectful functions,
// Returns a similar object where function does not do IO, but just returns an object with its name and args.
const constructDsl = (obj) => Object.keys(obj).reduce((valueObj, fn) => {
valueObj[fn] = (...args) =>({fn, args})