Skip to content

Instantly share code, notes, and snippets.

View casperin's full-sized avatar

Gorm Casper casperin

View GitHub Profile
@casperin
casperin / tcpspy.erl
Created October 6, 2020 21:50
Erlang TCP Spy -- a little broken
-module(tcpspy).
-export([start/3, stop/1, start_spy/3, listener/3, handler/2]).
start(Port, ServerHost, ServerPort) ->
spawn(?MODULE, start_spy, [Port, ServerHost, ServerPort]).
stop(Spy) ->
exit(Spy, shutdown),
ok.
@casperin
casperin / index.js
Created October 24, 2018 09:11
Using CONSTANTS in JS is not problem free
/*
I've seen this happen more than once. The data structure changes
its implementation, and suddenly some other code that you forgot
to update fails silently.
Here, Constants used to have "TYPE" instead of "STATE". We
updated it, but forgot to update the user of thing.
*/
// thing.js
function h(type, props, children) {
children = children || []
return {
type,
props: props || {},
children: Array.isArray(children) ? children : [children]
}
}
function createElement(node) {
@casperin
casperin / index.js
Last active October 6, 2016 10:46
requirebin sketch
const assert = require('assert')
const api = () => 'SOME_RETURN_FROM_API'
const call = (fn, ...args) => ({type: 'call', fn, args})
const update = thing => ({type: 'update', thing})
function * mySaga () {
const foo = yield call(api, 1, 2)
yield update(foo)
}
@casperin
casperin / index.js
Created October 6, 2016 05:47
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
const daggy = require('daggy')
const {Just, Nothing} = require('data.maybe')
const RemoteData = daggy.taggedSum({
NotAsked: [],
Loading: [],
Failure: ['error'],
Success: ['items'],
@casperin
casperin / index.js
Created March 15, 2016 08:21
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var xhr = require('xhr');
var container = document.querySelector('.time-line');
var books = [];
var earliestDate = Infinity;
function int (date) {
return Number(new Date(date))/(24*60*60*1000);
}
@casperin
casperin / addSuper.js
Last active August 29, 2015 14:26
Adding a `this._super()` that passes arguments along to a function's scope
function addSuper (subFn, superFn) {
return function () {
var self = this,
args = arguments;
this._super = function () {
return superFn.apply(self, args);
};
return subFn.apply(self, args);
@casperin
casperin / index.js
Last active December 4, 2019 08:09
Karma, PhantomJs, Jasmine setup using npm
/**
* I couldn't find a super simple example of how to run Karma, PhantomJs, with
* Jasmine tests via npm, so eventually I decided to create my own. Maybe
* someone will find it useful.
*
*
* Installation (using npm):
*
* npm install -g karma-cli
* npm install -g karma --save-dev
@casperin
casperin / Extract parameter names
Last active August 29, 2015 14:08
Get parameter names from a function
//+ fn -> [a]
var getParameterNames = (function () {
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
ARGUMENT_NAMES = /([^\s,]+)/g;
return function (fn) {
var fnStr = fn.toString().replace(STRIP_COMMENTS, ''),
names = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
return names || [];
@casperin
casperin / func.js
Last active July 9, 2020 07:12
Some examples of currying and composing functions in javascript
var log = console.log.bind(console);
// Core component (not curried).
// compose(g, f) === function (x) { return g(f(x)); }
// Takes any number of functions as arguments
// from underscore.js
function compose (/* fn1, fn2, ... */) {
var funcs = arguments;
return function() {
var args = arguments;