Skip to content

Instantly share code, notes, and snippets.

Avatar

Bran van der Meer branneman

View GitHub Profile
View 1-array.js
// 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)
View fp-lenses.js
// 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
View 1.js
{
// 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 / primitive-reference-types-javascript.md
Last active May 5, 2023 11:12
Primitive Types & Reference Types in JavaScript
View primitive-reference-types-javascript.md

Primitive Types & Reference Types in JavaScript

An explanation of JavaScript's pass-by-value, which is unlike pass-by-reference from other languages.

Facts

  • JavaScript has 2 kinds of variable types: primitive and reference.
  • A fixed amount of memory is reserved after creation of every variable.
  • When a variable is copied, it's in-memory value is copied.
  • Passing a variable to a function via a call also creates a copy of that variable.

Primitive Types

@branneman
branneman / better-nodejs-require-paths.md
Last active April 15, 2023 18:57
Better local require() paths for Node.js
View better-nodejs-require-paths.md

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@branneman
branneman / count-lint-errors.js
Last active April 11, 2023 07:49
Group-By-Count ESLint errors
View count-lint-errors.js
// :: (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]);
@branneman
branneman / example-mod-wsgi.py
Last active April 6, 2023 07:11
pyinfo(). License: MIT
View example-mod-wsgi.py
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
View git.js
// 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
View 2-declarative.js
// 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
View 1-function.js
// 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]