Skip to content

Instantly share code, notes, and snippets.

Avatar

Bran van der Meer branneman

View GitHub Profile
@branneman
branneman / radicals.json
Created Jun 9, 2020
JSON list of 214 Simplified Chinese Radicals, data contains radical number, pinyin, english translation, stroke count
View radicals.json
[
{
"id": 1,
"radical": "",
"pinyin": "",
"english": "one",
"strokeCount": 1
},
{
"id": 2,
View snake.clj
(ns main (:gen-class))
;; Snake :: [[int int]…]
;; vector of x,y tuples
;; head is first, tail is last
;; out-of-bounds? :: (int int int) -> bool
;; grid-size is 1-indexed, x+y are 0-indexed
(defn out-of-bounds? [grid-size x y]
(or (neg? x) (neg? y) (>= x grid-size) (>= y grid-size)))
View clojure-predicate-functions.md

List of all Clojure Predicate functions

A predicate function returns a boolean true or false, and it's name ends with a ?. Ran against Clojure 1.10.0.

({:ns clojure.string,
  :predicates
  ({:fn ends-with?, :arglists ([s substr])}
   {:fn starts-with?, :arglists ([s substr])}
   {:fn includes?, :arglists ([s substr])}
@branneman
branneman / solution1.js
Last active Aug 5, 2019
Declarative asynchonous control flow: promises, observables, futures
View solution1.js
const wrapPromise = fn => (...args) =>
new Promise(resolve => setTimeout(
() => resolve(fn(...args)),
Math.floor(Math.random() * 1500) + 250)
)
const upper = wrapPromise(str => str.toUpperCase())
const underline = str => `__${str}__`
const exclamate = wrapPromise(str => str + '!')
// Util: Create promise chain
@branneman
branneman / coupling.js
Last active Aug 16, 2019
JavaScript examples: No coupling, Loose coupling, Tight coupling
View coupling.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() {}
a()
//
// No coupling from A to B => A and B work independently,
@branneman
branneman / howto.md
Last active Apr 23, 2019
Run Debian as docker container
View howto.md

Run Debian as docker container

Setup

Start a container in detached mode:

docker run -dit --name deb -e "LANG=C.UTF-8" -w "/root" debian:latest bash

Connect to it and do initial setup:

@branneman
branneman / fp.js
Last active Feb 25, 2020
JavaScript functional programming library
View fp.js
const { curryN, curry } = require('./curry') // https://gist.github.com/branneman/4ffb7ec3fc4a2091849ba5d56742960c
// Typechecks
const isUndef = x => typeof x === 'undefined'
const isNull = x => x === null
const isBool = x => typeof x === 'boolean'
const isNum = x => typeof x === 'number' && isFinite(x)
const isInt = x => typeof x === 'number' && isFinite(x) && Math.floor(x) === x
const isStr = x => typeof x === 'string'
const isArr = x => Array.isArray(x)
@branneman
branneman / curry.js
Last active Mar 24, 2020
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 / type-checks.js
Last active Feb 3, 2020
JavaScript strict type checking functions
View type-checks.js
module.exports = factory
function factory() {
const exports = {
isDef,
isUndef,
isNull,
isBool,
isNum,
isInt,
@branneman
branneman / turing-machine-interpreter.js
Last active Feb 12, 2019
Turing Machine Interpreter in JavaScript
View turing-machine-interpreter.js
/**
* Turing Machine Interpreter
*
* Features:
* - Infinite tape, both ways
* - Uses JS values, anything that matches ==, is a valid symbol
*
* Definition:
* - States (Q): { integer, halt }
* - Input symbols read/write (Σ): any integer or string
You can’t perform that action at this time.