Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
patrickgombert / gist:5470573
Created April 26, 2013 21:28
Key/Val Store in Elixir
defmodule Db do
use GenServer.Behaviour
## API
def start() do
:gen_server.start({:local, :db}, __MODULE__, [], [])
end
def store(key, val) do
anwer(foo, Args) -> % pattern match the arguments
answer_to_foo; % notice the semi-colon
answer(bar, Args) ->
case Delicious of
veggies ->
false; % Again, notice the semi colon
bacon -> % case is another form of pattern matching
true % The last statement in the case has no punctuation
end;
@patrickgombert
patrickgombert / gist:4694802
Last active December 12, 2015 01:58
erlang coin changer with absolute priority premise scoring
change(Amount) -> % => Binding 2 x 1
{Coins, _} = lists:foldl(fun(Denom, {Coins, Total}) -> % => Invocation 2 x 1, Binding 4 x 1, Constant 1 x 1
Floor = floor(Total / Denom), % => Binding 1 x 1, Invocation 2 x 2
{Coins ++ lists:duplicate(Floor, Denom), Total - (Floor * Denom)} % => Invocation 2 x 4
end, {[], Amount}, [25, 10, 5, 1]), % => Constant 2 x 1
Coins.
% Total = 2 + 2 + 4 + 1 + 1 + 4 + 8 + 2 = 24
@patrickgombert
patrickgombert / gist:4694122
Created February 1, 2013 21:02
Spec helper timer saver
require 'timer'
unless defined?(REQUIRED_SPEC_HELPER)
REQUIRED_SPEC_HELPER = true
File.open("spec_helper_times.yml", 'a+') do |file|
date_time = DateTime.now.strftime("%m%d%y%H%M")
time = Time.now
Kernel.load("spec_helper.rb", true)
final_time = Time.now - time
file << "#{date_time}: #{final_time}\n"
@patrickgombert
patrickgombert / 8LASCII
Created September 28, 2012 18:40
8th Light ASCII Art
MMMM MMM
MM: 8M MMM$I7
MM: OM MMM7I7MM
MM, $M MMD7IIDMM
MM, 7M MM8III8M
MO. ?MM MMOIIIOM
M$ =MM MMMZIIIZMM
M7 =MM MMM$III$MM
MM? :MM MMM7III7MM
MM+ :MM MMN7III7M
@patrickgombert
patrickgombert / gist:2371209
Created April 12, 2012 21:44
Interactor Example
### We are highlighting a workflow to retrieve Employees who
### have the title of department leaders.
### The web entry point comes through the controller,
### but it's important for the controller to have no logic.
### It simply knows that it must call the department leaders
### method on the interactor.
class EmployeeController < ApplicationController
def get_department_leaders
leaders = Interactor::Employees.get_deparment_leaders
class Reader
def read
file = File.new("readfile.rb", "r")
return file.gets
end
end
class Writer
def write(chars)
file = File.new("readfile.rb", "w")
class Reader
def read
return get.chomp()
end
end
class Writer
def write(chars)
puts chars
end
@patrickgombert
patrickgombert / gist:1975268
Created March 4, 2012 23:05
Java Hotswapping example
/** Message.java **/
public interface Message {
public void sayIt();
}
/** HelloWorld.java **/
@patrickgombert
patrickgombert / gist:1929440
Created February 28, 2012 04:18
Proc eval example
module Foo
class Bar
end
class Baz
def gen_bar