Skip to content

Instantly share code, notes, and snippets.

Bran van der Meer branneman

Block or report user

Report or block branneman

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@branneman
branneman / better-nodejs-require-paths.md
Last active Sep 18, 2019
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 / fp.js
Last active Sep 18, 2019
JavaScript functional programming library
View fp.js
const { curryN, curry } = require('./curry') // https://gist.github.com/branneman/4ffb7ec3fc4a2091849ba5d56742960c
// Array higher-order
const map = curry((fn, list) => list.map(fn))
const filter = curry((fn, list) => list.filter(fn))
const foldl = curry((fn, init, list) => list.reduce(fn, init))
const foldr = curry((fn, init, list) => list.reduceRight(fn, init))
const find = curry((fn, list) => list.find(fn))
const sort = curry((fn, list) => list.slice().sort(fn))
const includes = curry((fn, list) => list.includes(fn))
@branneman
branneman / primitive-reference-types-javascript.md
Last active Sep 15, 2019
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 / curry.js
Last active Sep 14, 2019
Higher order auto curry function
View curry.js
'use strict'
const curryN = (() => {
const isPlaceholder = x => x['@@functional/placeholder'] === true
const filterPlaceholders = xs => xs.filter(x => isPlaceholder(x))
const filterValues = xs => xs.filter(x => !isPlaceholder(x))
return (arity, fn, accIn = []) => (...args) => {
const accOut = accIn.slice()
@branneman
branneman / 0-katas.md
Last active Sep 12, 2019
Software Craftsmanship Katas in Clojure
View 0-katas.md

Software Craftsmanship Katas in Clojure

To run these as-is, you'll need to have Clojure and Leiningen with the lein-exec plugin installed. Then run from the terminal:

git clone https://gist.github.com/258926ff07c85b4e6f5cafc4967740ec.git katas-clojure
cd katas-clojure
lein exec fizzbuzz.clj
lein exec leapyears.clj
lein exec rpn-calculator.test.clj
@branneman
branneman / create-zip.js
Last active Sep 7, 2019
Node.js script to create a zip file from a list of files and directories
View create-zip.js
const stat = require('fs').statSync;
const AdmZip = require('adm-zip');
/**
* Example usage
*/
newArchive(`test-${+new Date}.zip`, [
'index.js',
'package.json',
'node_modules'
@branneman
branneman / client.js
Created Nov 8, 2014
Node.js TCP Socket - if either of them breaks, they'll keep trying to reconnect
View client.js
var net = require('net');
//
// Client
//
function openSocket() {
var socket = net.connect(3e3);
socket.setKeepAlive(true);
socket.on('connect', onConnect.bind({}, socket));
@branneman
branneman / fp-lenses.js
Last active Aug 30, 2019
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 / algebraic-data-types.js
Last active Aug 22, 2019
Simple Algebraic Data Types (ADTs) implementing fantasy-land: Identity, Const, Maybe, Either, IO
View algebraic-data-types.js
const fl = require('fantasy-land')
const inspect = require('util').inspect.custom
// Fantasy Land
// of :: Applicative f => a -> f a
// map :: Functor f => f a ~> (a -> b) -> f b
// ap :: Apply f => f a ~> f (a -> b) -> f b
// chain :: Chain m => m a ~> (a -> m b) -> m b
/**
You can’t perform that action at this time.