Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / .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 / 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.
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 / 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 ->