Skip to content

Instantly share code, notes, and snippets.

View bluegraybox's full-sized avatar

Colin MacDonald bluegraybox

View GitHub Profile
@bluegraybox
bluegraybox / pubsub.erl
Created January 18, 2012 02:22
Simple publish-subscribe service (for erlangdc)
-module(pubsub).
-export([handle/0, server/0]).
server() ->
register(ps, handle()).
%% handle/0
handle() ->
spawn(fun () -> handle([]) end).
@bluegraybox
bluegraybox / for_lines.sh
Created February 15, 2012 03:01
Bash 'for' loop over lines, not words
#!/bin/bash
# Only split on newlines, so each line of data is one record.
default_IFS=$IFS
IFS="$(echo -e "\n\r")"
# each $line will be one line, even if there are spaces in it.
for line in $(cat sample_data.txt) ; do
# do stuff...
done
@bluegraybox
bluegraybox / .screenrc
Created March 1, 2012 03:19
Config file for 'screen' utility.
altscreen on
termcap * OP
@bluegraybox
bluegraybox / .bashrc_ssh
Created March 8, 2012 02:22
Initialize ssh-agent in .bashrc
# Set up ssh-agent
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
@bluegraybox
bluegraybox / lib.sh
Created March 9, 2012 14:52
Stack traces in bash
#!/bin/bash
function stacktrace() {
frame=0
while caller $frame ; do frame=$((frame + 1)) ; done
}
function lib_inner() {
stacktrace
}
@bluegraybox
bluegraybox / find_missing_modules.sh
Created March 14, 2012 16:40
Find cpanm modules that failed to install
#!/bin/bash
# Report on modules that failed to install.
#
# Look for various "FAIL" messages in the cpanm build.log files.
# (FIXME: Explain the difference, and what they mean.)
# Parse out the module names and report any that are not installed.
logs=$HOME/.cpanm/work/*/build.log
@bluegraybox
bluegraybox / Batbone-excerpt.coffee
Created March 29, 2012 15:53
Extending Backbone collections
@LIST = (path="", model=null, opts={}) ->
opts.model = model if model
Backbone.Collection.extend $.extend opts,
parse: ({data}) -> data
comparator: (item) ->
compare_item(item)
url: API path
@REVERSE_LIST = (path="", model=null, opts={}) ->
list = @LIST(path, model, opts)
$.extend list,
@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) ->
@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 / 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