Skip to content

Instantly share code, notes, and snippets.

@vothane
vothane / command.exs
Created October 11, 2015 20:55
replacing command pattern with functions in Elixir
defmodule CashRegister do
def new do
Agent.start_link(fn -> 0 end)
end
def add(pid, amount) do
Agent.update(pid, fn(register) -> register + amount end)
end
def reset(pid) do
@vothane
vothane / template.exs
Created October 11, 2015 20:52
replacing template pattern with functions in Elixir
ExUnit.start
defmodule Grader do
def grade(grade) do
cond do
grade <= 5.0 and grade > 4.0 -> "A"
grade <= 4.0 and grade > 3.0 -> "B"
grade <= 3.0 and grade > 2.0 -> "C"
grade <= 2.0 and grade > 0 -> "D"
grade == 0 -> "F"
@vothane
vothane / _test.rb
Created November 9, 2012 09:07 — forked from jcoglan/_test.rb
$VERBOSE = nil
require File.expand_path('../rooby', __FILE__)
Person = Rooby::Class.new 'Person' do
define :initialize do |name|
@name = name
end
define :name do
@andkerosine
andkerosine / raskell.rb
Created August 15, 2012 05:56
Haskell-like list comprehensions in Ruby
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@