Skip to content

Instantly share code, notes, and snippets.

View bluegraybox's full-sized avatar

Colin MacDonald bluegraybox

View GitHub Profile
#!/usr/bin/bash
function wrapInGraph() {
# print beginning of DOT file, including formatting commands
cat <<END
digraph G {
rankdir=LR;
graph [
K=1.0,
overlap=false,
# For reference: 1>&2 redirects output to stderr
# This script should only be sourced by other scripts
if [[ "$0" != '-bash' ]] ; then
echo 'WARNING: functions.sh should be sourced, not executed' 1>&2
fi
# Check for an error, and optionally display a message
function check_ok() {
@bluegraybox
bluegraybox / bash_multiproc.sh
Last active November 29, 2016 23:40
Process a set of data in N concurrent processes
#!/usr/local/bin/bash
# Homebrew bash because macOS standard bash is too old for 'wait -n'.
max_jobs=10
data=`seq 1 20`
# Process to be run in background
function batch_job() {
echo "Starting $1"
@bluegraybox
bluegraybox / find_dupes.rb
Created May 13, 2016 17:09
Ruby script to find duplicate files
#!/usr/bin/ruby -w
require 'find'
require 'md5'
dir = ARGV[0]
dir = "." unless dir
digests = {}
Find.find( dir ) do |f|
@bluegraybox
bluegraybox / export_osx_contacts.py
Created January 23, 2016 11:12
Script to export contact information from OS X Contacts
#!/usr/local/bin/python
from collections import defaultdict
import pprint
import sqlite3
import sys
ADDRESS_DB_FILE = 'Contacts-2016-01-23.abbu/AddressBook-v22.abcddb'
def main():
@bluegraybox
bluegraybox / test.adoc
Last active August 29, 2015 14:17
Asciidoc Sandbox

Demo Page

This is the preamble.

Table of Contents

Summary / Key Features

  • single quotes

@bluegraybox
bluegraybox / parse_indents.hs
Last active May 3, 2020 21:34
script that parses indented text into a tree structure
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
bluegraybox / file_io_example.hs
Created November 11, 2013 18:55
Shows how lazy evaluation of hGetContents interacts with the withFile's automatic handle close.
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
bluegraybox / cheat_sheet_ipc.erl
Created August 25, 2012 20:14
Cheat sheet for Erlang IPC syntax & functions
-module(cheat_sheet_ipc).
-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
bluegraybox / cheat_sheet.erl
Created August 25, 2012 02:41
Cheat sheet for Erlang syntax
-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) ->