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

Calling methods from a constructor's prototype on objects of another type, is a common pattern in javascript: e.g. using Array::slice to use all or part of the arguments object as an array.

function (first/*, rest... */) {
    var rest = Array.prototype.slice.call(arguments, 1);
    ...
}
var Schemi = (function () {
function NamedType(name) {
this.name = name;
}
function Into(schema, convert) {
this.schema = schema;
this.convert = convert;
}
(var OrderedSet)
(set! OrderedSet (function (arr ordered? uniq?)
(set! this.items (arr or #[]))
(if uniq?.not this.uniquify
ordered?.not this.sort)
this))
(OrderedSet implement #{
"length": (function () this.items.length)
;;; 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)
// ==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==

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

@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) {
// 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 / 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 () {
@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