Skip to content

Instantly share code, notes, and snippets.

View owainlewis's full-sized avatar

Owain Lewis owainlewis

View GitHub Profile
@owainlewis
owainlewis / types.hs
Created August 17, 2012 12:54
Haskell types
-- Haskell Types
type Id = Int
type Title = String
type Author = String
data Book = Book Id Title Author
deriving (Eq, Show)
bookID :: Book -> Int
@owainlewis
owainlewis / bst.rb
Created August 22, 2012 07:02
BinarySearchTree
# A Binary Search Tree implementation in Ruby
require 'minitest/spec'
require 'minitest/autorun'
# ========================================
#
# 1
# / \
# / \
@owainlewis
owainlewis / select_from_param.rb
Created September 14, 2012 15:57
Select on certain params for controller request in Rails 2
# Returns new hash and leaves original as is
def select_from_param(hash, *args)
{}.tap do |result|
hash.each {|k, v| result[k] = v if args.include?(k)}
end
end
@model.update_attributes(select_from_param(params, :id, :email))
@owainlewis
owainlewis / binaryTrees.hs
Created October 1, 2012 14:40
Haskell BTree
module BinaryTree
where
-- Haskell Binary Tree Examples
-- A binary tree can either be empty or a node with a left and right branch
data Tree a = EmptyTree | Node a (Tree a) (Tree a)
deriving ( Show, Read, Eq )
@owainlewis
owainlewis / forward.lib.validations.clj
Created October 2, 2012 21:42
Forward validation library start
(ns forward.lib.validation
(:refer-clojure :exclude []))
;; Generic map validation library
(def m
{:first_name "jack"
:last_name "dorsey"
:password "twitter"
:age 25})
@owainlewis
owainlewis / binaryTree.ml
Created October 3, 2012 20:11
OCaml Trees
(* Unbalanced Binary Tree *)
type 'a tree =
Node of 'a * 'a tree * 'a tree
| Leaf;;
(* e.g Node (5, Leaf, Leaf) *)
(* val insert : ’a -> ’a btree -> ’a btree = <fun> *)
let insert x s = Node (x, Leaf, s);;
@owainlewis
owainlewis / helpers.clj
Created October 6, 2012 12:29
Flash message helpers
(ns forward.views.helpers)
;; View helper functions
(defn flash-messages
"Show flash messages if they exist in a request"
[req]
(declare flash-message)
(if-let [flash-message (:flash req)]
[:div {:id "error_explanation"}
@owainlewis
owainlewis / rbtree.lisp
Created October 9, 2012 15:49 — forked from Mozk0/rbtree.lisp
Red Black Tree for Common Lisp.
;; The following implementation of rb-tree is based on http://www.cs.kent.ac.uk/people/staff/smk/redblack/.
(defun change-to-black (tree)
(pattern-match tree
((:pattern (_ . rest) :variable rest :ignore _) `(:B . ,rest))
(:otherwise nil)))
(defun rb-insert (tree obj cmp)
(change-to-black (rb-insert% tree obj cmp)))
@owainlewis
owainlewis / bktree.hs
Created October 11, 2012 12:00 — forked from ehamberg/bktree.hs
Implementation of a BK-Tree in Haskell
import qualified Data.Map as M
import Control.Applicative
import Data.Maybe (mapMaybe)
-- A BK-Tree is has a root word and more trees connected to it with branches of
-- lengths equal to the Levenshtein distance between their root words (i.e. an
-- n-ary tree).
data BKTree s = BKTree s (M.Map Int (BKTree s)) | Empty deriving (Show)
-- Inserting a word is done by inserting it along a branch of lenght
@owainlewis
owainlewis / bst.ml
Created October 27, 2012 11:27
BST OCaml
(***************************************
Binary Search Trees
***************************************)
(* insert delete inorder search min max *)
type 'a binary_tree =
(* A root node and a left and right sub tree *)