Skip to content

Instantly share code, notes, and snippets.


Colin MacDonald bluegraybox

View GitHub Profile
# For reference: 1>&2 redirects output to stderr
# This script should only be sourced by other scripts
if [[ "$0" != '-bash' ]] ; then
echo 'WARNING: should be sourced, not executed' 1>&2
# Check for an error, and optionally display a message
function check_ok() {
bluegraybox /
Last active Nov 29, 2016
Process a set of data in N concurrent processes
# Homebrew bash because macOS standard bash is too old for 'wait -n'.
data=`seq 1 20`
# Process to be run in background
function batch_job() {
echo "Starting $1"
bluegraybox / find_dupes.rb
Created May 13, 2016
Ruby script to find duplicate files
View find_dupes.rb
#!/usr/bin/ruby -w
require 'find'
require 'md5'
dir = ARGV[0]
dir = "." unless dir
digests = {}
Find.find( dir ) do |f|
bluegraybox /
Created Jan 23, 2016
Script to export contact information from OS X Contacts
from collections import defaultdict
import pprint
import sqlite3
import sys
ADDRESS_DB_FILE = 'Contacts-2016-01-23.abbu/AddressBook-v22.abcddb'
def main():
bluegraybox / test.adoc
Last active Aug 29, 2015
Asciidoc Sandbox
View test.adoc

Demo Page

This is the preamble.

Table of Contents

Summary / Key Features

  • single quotes

bluegraybox / parse_indents.hs
Last active May 3, 2020
script that parses indented text into a tree structure
View parse_indents.hs
import Data.Char
import qualified Data.String as S
import System.IO
import System.Environment
-- Parses indented text into a tree structure, where each node is represented as a (content, children) tuple
-- Example invocation:
-- echo -e "a\n b\n c\n d\n d2\n e\nf\n g\n h\n i\n j\n k\nl\n" | ./parse_indents
-- a {b, c {d, d2}, e}, f {g {h {i}}, j, k}, l
bluegraybox / file_io_example.hs
Created Nov 11, 2013
Shows how lazy evaluation of hGetContents interacts with the withFile's automatic handle close.
View file_io_example.hs
import Data.Char
import System.IO
readFirstLine = withFile "test.txt" ReadMode (\handle -> do
line <- hGetLine handle
return line)
readAndPrintTestFile = withFile "test.txt" ReadMode (\handle -> do
contents <- hGetContents handle
-- unless we output contents in some way, it isn't evaluated, and will be empty
bluegraybox / cheat_sheet_ipc.erl
Created Aug 25, 2012
Cheat sheet for Erlang IPC syntax & functions
View cheat_sheet_ipc.erl
-export([init/0, incr/1, decr/1, current/0]). % not loop/0
%% Define a constant.
-define(SVC_NAME, counter_service). % atom, not Variable or string
%% Start a new process to manage our counter.
init() ->
bluegraybox / cheat_sheet.erl
Created Aug 25, 2012
Cheat sheet for Erlang syntax
View cheat_sheet.erl
-module(cheat_sheet). % end with a period
%% Let these functions be called externally.
-export([countdown/1, countdown/0]). % number of parameters - it matters!
%% atoms begin with a lower-case letter
%% Variables begin with an upper-case letter
%% Start defining a function with the most specific case first
countdown(0) ->
bluegraybox /
Created Mar 29, 2012
Extending Backbone collections
@LIST = (path="", model=null, opts={}) ->
opts.model = model if model
Backbone.Collection.extend $.extend opts,
parse: ({data}) -> data
comparator: (item) ->
url: API path
@REVERSE_LIST = (path="", model=null, opts={}) ->
list = @LIST(path, model, opts)
$.extend list,