Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / gist:2240921
Created March 29, 2012 17:57
/etc/init.d/ircd
#! /bin/sh
### BEGIN INIT INFO
# Provides: ircd
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the charybdis ircd
# Description: starts charybdis using start-stop-daemon
@SegFaultAX
SegFaultAX / fib.lua
Created May 23, 2012 00:48
Lua Fibonacci
-- Author: Michael-Keith Bernard
-- Date: May 22, 2012
-- Description: Various implementations of the Fibonacci sequence in Lua. Lua
-- has native support for tail-call elimination which is why `tail_call` and
-- `continuation` run in near constant time. For sufficiently large numbers of n
-- you can start to see linear performace characteristics (particularly for the
-- `continuation` implementation), but ultimately the `tail_call` implementation
-- is an order of magnitude faster than iteration even for values of n as small
-- as 500k.
@SegFaultAX
SegFaultAX / gist:2847710
Created June 1, 2012 00:56
Erlang sorting algorithms
-module(sortlib).
-export([qsort/1, bsort/1, ssort/1, isort/1, msort/2,
msort_lte/1, msort_gte/1]).
-export([test_sort/0, test_sort/1, shuffle/1, split/1, merge/3]).
-import(lists, [reverse/1]).
%% Various sorting algorithms implemented in Erlang
@SegFaultAX
SegFaultAX / gist:3186788
Created July 27, 2012 08:10
Lua Conway's Game of Life
-- Author: Michael-Keith Bernard
-- Date: July 26, 2012
--
-- Game of Life
--
-- Ported from this Clojure implementation:
--
-- (defn neighbours [[x y]]
-- (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
-- [(+ dx x) (+ dy y)]))
@SegFaultAX
SegFaultAX / gist:3190270
Created July 27, 2012 20:22
Lua Zipper Example
-- Author: Michael-Keith Bernard
-- Date: July 27, 2012
-- Notes: An example Binary Tree Zipper implementation in Lua
function node(v, left, right)
local n = {}
n.value = v
n.left = left
n.right = right
return n
@SegFaultAX
SegFaultAX / gist:3190354
Created July 27, 2012 20:34
Python Conway's Game of Life
#!/usr/bin/env python
# Author: Michael-Keith Bernard
# Date: July 27, 2012
#
# Game of Life
#
# Ported from this clojure implementation:
#
# (defn neighbours [[x y]]
@SegFaultAX
SegFaultAX / gist:3300858
Created August 9, 2012 04:05
Any and All in Lua
-- Return true if any element of the iterable is true (or false if the iterable
-- is empty). Note that all values other than nil and false are considered true.
--
-- ... - any number of values
--
-- Examples
--
-- any(false, false, true) -- true
-- any(false, false, false) -- false
-- any(true, true, true) -- true
@SegFaultAX
SegFaultAX / gist:3318559
Created August 10, 2012 22:12
Lua Trie
-- Author: Michael-Keith Bernard
-- Date: August 10, 2012
--
-- Notes: This Trie implementation is a port of the Clojure implementation
-- below. I had to implement quite a few functions from clojure.core to keep the
-- same basic functionality. Probably very little if any of this code is
-- production worthy, but it's interesting all the same. Finally, all of the
-- documentation strings are pulled directly from clojure.core and may not
-- accurately reflect the Lua implementation.
--
@SegFaultAX
SegFaultAX / gist:3364849
Created August 16, 2012 00:03
Lua Multimethods (Clojure-style)
function defmulti(keyfn)
if not keyfn then
keyfn = function(...) return ... end
end
local multi = {
_keyfn = keyfn,
_methods = {}
}
setmetatable(multi, {
__call = function(self, ...)
@SegFaultAX
SegFaultAX / gist:3373635
Created August 16, 2012 20:59
Function composition and threading in Python
def compose(f, g):
def inner(*args, **kwargs):
return f(g(*args, **kwargs))
return inner
# Clojure: (comp #(+ 1 %) #(* 2 %))
times2plus1 = compose(lambda n: n + 1, lambda n: n * 2)
print times2plus1(4)
# OUT: 9