Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
rsslldnphy / peg_spec.rb
Created December 4, 2012 19:56
Some ways of piping and composing methods
require 'rspec'
class Object
def peg(&block)
block.call(self)
end
def wend(arg)
Wender.new(self, arg)
@rsslldnphy
rsslldnphy / sml-tdd-loop.el
Last active December 11, 2015 08:18
Emacs function to help create a TDD loop when writing SML
(defun restart-sml ()
(sml-prog-proc-switch-to)
(end-of-buffer)
(comint-delchar-or-maybe-eof 0))
(defun run-sml-tests-now ()
(let* ((code-file-name
(replace-regexp-in-string "_tests" "" (buffer-file-name)))
(test-file-name
(replace-regexp-in-string "\.sml" "_tests.sml" code-file-name)))
"Nils - what are your Options?" - Russell Dunphy
Tony Hoare, inventor of the null reference, calls it his "billion dollar mistake".
Although Ruby goes some way to correcting it by making `nil` at least be a class
with some reasonably helpful stuff on it, you'll no doubt have been hit by a
`NoMethodError: undefined method 'foo'` just as many times as me.
So, given this problem exists, I want to convince you of a few things.
First, that there are not one, but two types of `nil`.
@rsslldnphy
rsslldnphy / optional_records.rb
Created May 20, 2013 10:03
Options used for dealing with missing records
## Model code
class Customer < ActiveRecord::Base
def find_by_name(name)
Option[where(name: name).first]
end
end
## Controller code
@rsslldnphy
rsslldnphy / infinite.erl
Last active December 17, 2015 12:59
Basic implementation of infinite series in Erlang, take, filter and nth.
-module(infinite).
-compile(export_all).
%%% Usage
%
% Ints = infinite:stream(0, fun (X) -> X + 1 end).
%
% % Numbers from 0 to 9
% infinite:take(Ints, 10).
%
class DCode
def initialize(assessment, user)
@assessment = assessment
@user = user
end
def code
"#{site.name[0,2]}-#{department_code}".upcase
end
@rsslldnphy
rsslldnphy / gist:5759425
Last active December 18, 2015 09:09
bowling kata
normal score: 41 (4 then 1)
spare: 3/
strike: X
score then gutter: 4-
34X--3/34
@rsslldnphy
rsslldnphy / gist:5862686
Last active December 18, 2015 23:39
GAAAAAAVVVIIIIIIIIIIIINNNNNNN!!!!!!!
## Making Option[[]], Option[{}] and Option[""] return None
Looked at a certain way, it does make a kind of sense. I can see how you got the idea.
A None is itself a kind of empty collection, so why wouldn't an Option of an empty collection also be a None?
However, I think there are some important things we need to consider.
The first one is - why would you be creating an optional collection - what value would you be getting from doing so?
And what value would you be getting from empty collections being interpreted as None?
@rsslldnphy
rsslldnphy / Main.hs
Created July 2, 2013 18:49
Merge sort in Haskell
module Main where
import System.Environment
main :: IO ()
main = do
args <- getArgs
mapM_ putStrLn $ sort args
sort :: (Ord a) => [a] -> [a]
@rsslldnphy
rsslldnphy / Main.hs
Created July 3, 2013 19:03
Improved merge sort in haskell!
module Main where
import System.Environment
main :: IO ()
main = do
args <- getArgs
mapM_ putStrLn $ sort args
sort :: (Ord a) => [a] -> [a]