Skip to content

Instantly share code, notes, and snippets.

View bluegraybox's full-sized avatar

Colin MacDonald bluegraybox

View GitHub Profile
@bluegraybox
bluegraybox / countdown.erl
Created July 10, 2011 20:22
Erlang countdown timer which demonstrates code reloading
-module(countdown).
-export([init/0, reload/0]).
-export([tick/1]). % so we can spawn this properly.
%% spawn a countdown process with a default start time of 10 seconds.
init() -> init(10).
init(Time) ->
register(ticker, spawn(?MODULE, tick, [Time])).
tick(Time) when Time >= 0 ->
(defun parse-text-line (line)
;; converts " text" to (:content "text" :indent 4)
(let ((content (string-left-trim '(#\Space #\Tab) line)))
(list :content content :indent (- (length line) (length content)))))
(defun split-list (criterion data-list)
;; break a list into two lists; the second begins with the first element that matches the criterion.
(if data-list
(if (funcall criterion (first data-list))
(list () data-list)
build_nodes(Parent, Group, []) -> Parent ! {Group, []};
build_nodes(Parent, Group, Lines) ->
[First|Rest] = Lines,
%% split off our children from the rest of the Lines.
%% the first line with an indent =< ours is a sibling, not a child
Criterion = fun(X) -> X#line.indent =< First#line.indent end,
[ChildLines, SiblingLines] = split_list(Criterion, Rest),
spawn(?MODULE, build_nodes, [self(), children, ChildLines]),
spawn(?MODULE, build_nodes, [self(), siblings, SiblingLines]),
receive
@bluegraybox
bluegraybox / parse_indents.erl
Created July 15, 2011 03:04
This is the core functionality from the indent parser (sequential Erlang version)
-record(line, {content, indent}).
-record(node, {content, children}).
parse_text_line(Text) ->
%% converts " text" to (:content "text" :indent 4)
Content = string:strip(string:strip(Text, left), left, $\t),
Indent = length(Text) - length(Content),
#line{content=string:strip(Content, right, $\n), indent=Indent}.
%% Split a list of lines in two, breaking on the first line whose indent is =< the minimum.
@bluegraybox
bluegraybox / .vimrc
Created July 22, 2011 19:42
Basic Vim config
set nocompatible
" Don't use Ex mode, use Q for formatting
map Q gq
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
@bluegraybox
bluegraybox / game.erl
Created August 2, 2011 02:20
Code for scoring a bowling game.
-module(game).
-export([score/1]).
score(Rolls) -> frame(1, 0, Rolls).
%% frame/3 takes Frame #, Score accumulator, and list of remaining Rolls.
%% It tail-recurses to the next frame with an updated Score and Rolls list.
%% Game complete.
@bluegraybox
bluegraybox / options.erl
Created August 14, 2011 17:56
Erlang command-line option handling
#!/usr/bin/escript
-module(options).
% main/1 calls main/3 with default values
main(Args) -> main(false, "", Args).
main(_, _, ["-h" | _ ] ) -> io:format("Help text...~n"); % help
main(_, Value, ["-f" | Args ] ) -> main(true, Value, Args); % set flag
main(Flag, _, ["-v", Value | Args ] ) -> main(Flag, Value, Args); % set value
@bluegraybox
bluegraybox / update.sh
Created August 29, 2011 12:52
SVN update wrapper for teams
#!/bin/bash
# Update current SVN directory, creating diff & log files from previous version.
# For seeing what the rest of your team did.
last_rev=$(svn info | grep "^Revision: ")
last_rev=${last_rev#Revision: }
echo "Updating from $last_rev..."
svn up
@bluegraybox
bluegraybox / wizard.sh
Created August 31, 2011 21:49
Framework for shell script wizard.
#!/bin/bash
# Interactive wizard to walk the user through a process.
# Tracks steps completed; allows user to exit at any point and pick back up there.
function step1 () {
read -p "step 1 ok, $1? " ok
}
function step2 () {
@bluegraybox
bluegraybox / kvstore.erl
Created October 4, 2011 00:11
A simple REST key-value store using the spooky framework.
-module(hello_world).
-behaviour(spooky).
-export([init/1, get/2, loop/1]).
init([])->
%% register a process that holds our dict in memory
case whereis(store) of
undefined ->
Pid = spawn(?MODULE, loop, [dict:new()]),
register(store, Pid),