Skip to content

Instantly share code, notes, and snippets.

View d11wtq's full-sized avatar

Chris Corbyn d11wtq

  • Melbourne, Australia
View GitHub Profile
@d11wtq
d11wtq / binary_tree.erl
Created March 21, 2013 12:35
Binary tree implementation in Erlang.
%% @doc A binary tree implementation in Erlang.
%% A binary tree stores keys and values.
-module(binary_tree).
-export([init/0, init/1, insert/3, lookup/2]).
-define(EMPTY_NODE, {node, 'empty'}).
%% @doc Initialize an empty binary tree node.
%% This is how the root of the tree should be established.
%%
@d11wtq
d11wtq / calc.erl
Last active December 15, 2015 07:08
Reverse Polish Notation Calculator in Erlang
%% @doc Reverse Polish Notation Calculator.
%%
%% Parses expressions like "1 2 3 + -" = -4
%%
%% This is an exercise in Learn You some Erlang for Great Good,
%% however I didn't read the text and just implemented it.
%%
%% I guess understanding stack-based parsing helps here.
-module(calc).
-export([rpn/1]).
%% @doc This implements a kitchen fridge that you can put things into
%% and take them out.
%%
%% It is stateful, through the use of recursion.
%% I opted for the sets module only because I wanted to try it.
%% Using the sets module has the side-effect that you can only store
%% one of each food item in the fridge.
-module(kitchen).
-export([new/0, store/2, take/2]).
@d11wtq
d11wtq / functional_ruby.rb
Created March 25, 2013 02:16
Lists, and the concept of "head" and "tail", coupled with recursion are at the core of functional programming. This demonstrates how you can do it in Ruby, without ever writing a loop.
# foldl() is fundamental. With a fold, you can do everything else.
def foldl(list, acc, &fn)
if list == [] # base case, return the accumulator
acc
else
head, *tail = list
foldl(tail, fn.call(acc, head), &fn) #recurse on the remainder
end
end
@d11wtq
d11wtq / queries.md
Last active December 16, 2015 09:29
SQL vs ORM vs QLCs

Comparing the power and expressiveness of various database query systems

Simple select all.

SELECT * FROM users;
User.all
(defun colorize-mode-line (&rest args)
(let ((color (cond ((evil-insert-state-p) "#fa4444")
((evil-emacs-state-p) "#fafa00")
((buffer-modified-p) "#22bff0"))))
(set-face-background 'mode-line (or color "#ffffff"))))
(add-to-list 'post-command-hook 'colorize-mode-line)
@d11wtq
d11wtq / inc-number.el
Created June 22, 2013 07:13
Vim-style increment/decrement numbers under the cursor, for Emacs.
;; vim-style increment/decrement numbers
(defun inc-number-at-point (n)
"Increment the number under the point, if present.
Called with a prefix argument, changes the number by N."
(interactive "p")
(let ((amt (or n 1))
(word (thing-at-point 'word))
(bounds (bounds-of-thing-at-point 'word)))
(when (string-match "^[0-9]+$" word)
(replace-string word
@d11wtq
d11wtq / thread-macro.clj
Created December 13, 2013 10:56
Demonstration implementation of the `->` macro in Clojure.
(defmacro demo-> [val & fns]
(reduce (fn [acc fname] (list fname acc)) val fns))
(macroexpand '(demo-> 42 inc dec inc)) ; (inc (dec (inc 42)))
@d11wtq
d11wtq / schedule.clj
Last active January 4, 2016 08:19
Seeking DSL style review
(defschedule app-schedule "Application tasks"
[:every "5 seconds" (println "Woohoo!")]
[:at "7am tomorrow" (wake-up!)])
@d11wtq
d11wtq / docker-ssh-forward.bash
Created January 29, 2014 23:32
How to SSH agent forward into a docker container
docker run -rm -t -i -v $(dirname $SSH_AUTH_SOCK) -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK ubuntu /bin/bash