Skip to content

Instantly share code, notes, and snippets.

View bluegraybox's full-sized avatar

Colin MacDonald bluegraybox

View GitHub Profile
@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 / 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 ->
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
(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)
@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 / 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 / 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),
@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).