Skip to content

Instantly share code, notes, and snippets.

View gerardpaapu's full-sized avatar
🏠
Working from home

Gerard Paapu gerardpaapu

🏠
Working from home
View GitHub Profile
var Parser = function() {
"use strict";
function require(name) {
if (!(name in modules)) {
throw new Error("Module not found: " + name);
}
return modules[name];
}
var modules = {};
(function(exports) {
// A simple example of higher-order functions
function get(property) {
return function (obj) {
return obj[property];
};
}
function maybe(fun) {
return function (value) {
return value === null ? null
(function () {
var apply = Function.prototype.apply;
var flatten = apply.bind(Array.prototype.concat, []);
Array.prototype.selectMany = function (fn) {
return flatten(this.map(fn));
};
}());
// usage
@gerardpaapu
gerardpaapu / binary-search-tree.coffee
Created January 14, 2013 07:14
A simple binary search tree in coffeescript Uses djb hash on string keys
class Tree
constructor: ->
throw new Error "Tree is an abstract class"
hasKey: (key) ->
(@lookup key).length is 1
get: (key, _default=null) ->
result = @lookup key
if result.length is 0
@gerardpaapu
gerardpaapu / gist:4528317
Last active December 11, 2015 02:09 — forked from fitzgen/gist:749904
(function () {
// conceal the Thunk class to avoid
// so that the only way to make one is
// to call Function::thunk
function Thunk(fn, args) {
this.fn = fn;
this.args = args;
}
Thunk.prototype.force = function () {
// You have a sentence with several words with spaces
// removed and words having their character order
// shuffled. You have a dictionary. Write an algorithm
// to produce the sentence back with spaces and words
// with normal character order
function Result(){}
function Success(words, remaining) {
this.words = words;
@gerardpaapu
gerardpaapu / promises.js
Created December 16, 2012 08:05
Having a play with typescript, seems likeable enough so far.
var Promise = (function () {
function Promise() {
this.isResolved = false;
this.didSucceed = false;
this.value = null;
this.error = null;
this.resolveListeners = [];
this.progressHandlers = [];
}
Promise.prototype.then = function (successHandler, errorHandler, progressHandler) {

This snippet illustrates how you can allow your users to put your library into whatever global they see fit.

This technique currently relies on your script tag being the last on the page at time of execution. This means that it will not probably not play nicely with async or defer

// ==UserScript==
// @id dft-postion-unfixed
// @name Remove position fixed
// @version 1.0
// @namespace dft
// @author Matthew Goodall
// @description Changes position:fixed to position:relative
// @include *
// @run-at document-idle
// ==/UserScript==
;;; OrderedSet
;;; ----------
;;;
;;; A Collection class that wraps an Array.
;;; It maintains immutability, an order, and
;;; does not contain duplicates
;;;
;;; Items of the Ordered Set should implement 'Ord'
(class OrderedSet
(constructor (items ordered distinct)