Skip to content

Instantly share code, notes, and snippets.

View amacdougall's full-sized avatar

Alan MacDougall amacdougall

View GitHub Profile
@amacdougall
amacdougall / extra_trello_shortcuts.js
Created August 9, 2012 15:53
Use Tampermonkey or something to install this user script in Chrome.
// ==UserScript==
// @name Extra Trello Edit Keys
// @namespace http://www.paperlesspost.com/
// @version 0.1.1
// @description From the card edit window, hit H to edit the description and Y to add a new comment.
// @match https://trello.com/*
// @copyright 2012+, Alan MacDougall
// ==/UserScript==
function main() {
// replacing a function with a one-time suppression using _.wrap
var module = {
execute: function(target) {
doSomeBigAction(target);
}
};
// it might be more clear if I assign this intermediate variable
var skipOnce = _(module.execute).wrap(function(original) {
console.log("Sorry, doing nothing this time.");
4 Cavern of Souls
2 Clifftop Retreat
4 Forest
2 Gavony Township
1 Kessig Wolf Run
1 Mountain
3 Plains
2 Rootbound Crag
4 Temple Garden
Emoji
👍
@amacdougall
amacdougall / gist:5768817
Created June 12, 2013 20:27
underscore.js examples.
// traditional for loop explains HOW to do everything; can you spot the
// interesting logic?
var textAssets = [];
for (var i = 0; i < this.assets.length; i++) {
if (this.assets[i].type == "text") { // <- the ONLY interesting logic is right here
textAssets.push(this.assets[i]);
}
}
@amacdougall
amacdougall / gist:5768895
Last active December 18, 2015 10:29
JS module example.
PP.component = (function() {
var currentValue = null;
var previousValues = [];
/**
* Changes input values, maintaining a record of how often it has done so.
* This is a standard doc comment, and this is how it is phrased; when in
* doubt, write JavaDoc style.
*/
var component = {
@amacdougall
amacdougall / limitQuantity.js
Last active December 21, 2015 01:09
Underscore mixin which rejects items from a list if too many of them match the iterator function. Keeps all other list items.
/**
* Rejects items which match the iterator, beyond the threshold. Accepts all other items.
*
* Example: _([1, 1, 1, 1, 2]).limitQuantity(2, function(n) {return n == 1;}); // [1, 1, 2]
*
* @param list The list to be filtered. Omitted in OO-style (i.e. _(list).limitQuantity).
* @param n The number of matching items to be allowed.
* @param f The filter function against which list items will be matched.
*/
_.limitQuantity || _.mixin({
@amacdougall
amacdougall / barrier.clj
Last active December 25, 2015 03:49
Correct documentation of reactive/barrier.
;; Given a seq of channels, waits until each channel has received a value, and
;; then returns a vec of each value.
(defn barrier [cs]
(go (loop [cs (seq cs) result []]
(if cs
(recur (next cs) (conj result (<! (first cs))))
result))))
;; Without channels:
;; Makes an HTTP request, and executes the callback on the response when it
;; comes. This should be familiar from Javascript.
(defn get-pudding [callback]
(http-get {:url "pudding"
:success callback}))
(get-pudding (fn [response] (eat response)))
// This works, but since it has to take `n, f1, ...` instead of `n, [f1, ...]`,
// we need to do a messy concat/apply at the end.
function pipeline(n) {
var actions = rest(arguments);
if (_.isEmpty(actions)) {
return n;
} else {
return pipeline.apply(null, [first(actions)(n)].concat(rest(actions)));
}
};