Skip to content

Instantly share code, notes, and snippets.

View branneman's full-sized avatar

Bran van der Meer branneman

View GitHub Profile
@branneman
branneman / count-lint-errors.js
Last active July 31, 2023 17:59
Group-By-Count ESLint errors
// :: (String, String) => String
const spawn = require('child_process').spawnSync;
// :: String => [String]
const getRules = raw => raw
.split('\n')
.map(line => line.trim())
.filter(line => !!line)
.filter(line => line[0] !== '/' && line[0] !== '✖')
.map(line => line.match(/[a-z-]+$/)[0]);
// Array = ordered + indexed + duplicate values
// a.k.a. List
const arr = [1, 2, 3]
// size
arr.length //=> 3
// random-access
arr[1] // 2
@branneman
branneman / fp-lenses.js
Last active May 17, 2023 00:56
JavaScript: Lenses (Functional Programming)
// FP Lenses
const lens = get => set => ({ get, set });
const view = lens => obj => lens.get(obj);
const set = lens => val => obj => lens.set(val)(obj);
const over = lens => fn => obj => set(lens)(fn(view(lens)(obj)))(obj);
const lensProp = key => lens(prop(key))(assoc(key));
@branneman
branneman / 1.js
Last active May 6, 2023 08:11
JavaScript examples: No coupling, Loose coupling, Tight coupling
{
// Tight coupling from A to B => A needs B to be defined, callable and adhere to an interface
// No coupling from B to A => B does not need A in any way (pure fn)
function a() {
b()
}
function b() {}
}
{
@branneman
branneman / example-mod-wsgi.py
Last active April 6, 2023 07:11
pyinfo(). License: MIT
def application(environ, start_response):
import sys
path = 'YOUR_WWW_ROOT_DIRECTORY'
if path not in sys.path:
sys.path.append(path)
from pyinfo import pyinfo
output = pyinfo()
start_response('200 OK', [('Content-type', 'text/html')])
return [output]
@branneman
branneman / git.js
Last active March 23, 2023 13:15
How Git works - super simplified! — https://www.youtube.com/watch?v=T9Nag5IXVQ0
// blob = file contents, identified by hash
const blobs = {
'73d8a': 'import x from "y"; console.log("some file contents")',
'9c6bd': 'D8 A1 31 0F ...',
'547d4': '# Readme\nThis is documentation',
'a0302': '# Readme\nThis is some updated documentation',
}
// tree = references to blobs and trees, identified by hash
const trees = {
@branneman
branneman / 2-declarative.js
Last active December 29, 2022 07:49
Code Simplicity is the Ultimate Sophistication - https://youtu.be/zxJnyMXhyvw
// Imperative: 'what' + 'how'
const makes1 = []
for (let i = 0; i < cars.length; i += 1) {
makes1.push(cars[i].make)
}
// Declarative: only 'what'
const makes2 = cars.map((car) => car.make)
@branneman
branneman / 1-function.js
Last active December 22, 2022 07:57
Functions, Iterators, Promises, Observables - It's all connected - https://youtu.be/5GCo8AGQ9Ck
// Producer
const range = (min, max, acc = []) => {
if (max < min) throw new Error('not supported!')
if (min >= max) return acc
return range(min + 1, max, acc.concat(min))
}
// Consumer
const lt100 = range(0, 100)
//=> [1, 2, 3, 4, ..., 97, 98, 99]
@branneman
branneman / 1-globals.js
Last active December 7, 2022 22:01
A history of different JavaScript module formats - https://youtu.be/GebL7ToOohE
var MY_CONSTANT = 42
// Implementation of a class
var MyClass = function () {
// constructor
}
MyClass.prototype.getAnswer = function () {
return MY_CONSTANT
}
@branneman
branneman / 1.js
Created October 17, 2022 19:47
Interfaces
// Interface of a primitive variable: its name and type
const DEFAULT_DB_HOST = 'localhost'
// Interface of a container variable: its name, types and structures
const config = {
user: 'readuser',
pass: '******',
db: 'test-env',
}