Skip to content

Instantly share code, notes, and snippets.

View pmarreck's full-sized avatar

Peter Marreck pmarreck

  • formerly desk.com, thredup.com and lifebooker.com. currently Director of Engineering @addigence
  • Long Island, NY
  • 13:11 (UTC -04:00)
View GitHub Profile
@pmarreck
pmarreck / collaboration_fixation.rb
Created February 6, 2014 18:12
A discussion about "when is it necessary to test which combinations of collaborators?", in Ruby code.
require 'test/unit'
def f1(a=0)
1 + a
end
def f2(a=0)
2 + a
end
@pmarreck
pmarreck / gist:e0c9c4e506cb46094b01
Created August 8, 2014 16:49
Public verification of onename.io bitcoin username.
Verifying myself: My Bitcoin username is +pmarreck. https://onename.io/pmarreck
@pmarreck
pmarreck / kryptokit_pgp_public_key.txt
Created August 18, 2014 20:37
My KryptoKit public key (import this to send messages to me via KryptoKit)
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: KryptoKit v0.24
Comment: http://kryptokit.com
xsBNBFN8xLMBCACQtRCK6AWhR9mGpoHUXJi8/gK/asXPQ1H2fbTFdBjPpJFE
RrlYO44jgfM8HoUxYVHiRwy4K3d08DigqrHlPdrGB3jdLcUFtDDrjyjBOlhM
UkPGMnw3JRC1CyAXIFw7RC9s8t4guXW/Iz6O8s/7JkGFNMliH/4OstDo3TB1
jNlo9yAOkdFX9wSHBXyPDnp9zxk2UqJzvs66MAorvv8+8NRboCXNQbJ8nJaN
AUsSX0bmwpB1XvMhAOdbZiSc8bMOU4CBZS69itgtedMF5OjckI2EoX6S7xhg
0gBikvW8InCOJ3xyeTZTMOkOA5K4/ssbUrwAjlbUiWYo2c4nZMuiCbLjABEB
@pmarreck
pmarreck / sparkline.exs
Created January 15, 2015 18:28
How to unit-test a small Elixir library inline, without any project management cruft (at least in the early stages)
defmodule Sparkline do
def draw(str) do
values = str |> String.split(~r/[, ]+/)
|> Enum.map(&(elem(Float.parse(&1), 0)))
{min, max} = {Enum.min(values), Enum.max(values)}
Enum.map(values, &(round((&1 - min) / (max - min) * 7 + 0x2581)))
end
end
# run this inline suite with "elixir #{__ENV__.file} test"
@pmarreck
pmarreck / hash_get.rb
Last active August 29, 2015 14:16
Deep-index a Ruby hash
class Hash
def get(*keys)
keys.inject(self){|h, k| h[k] if h}
end
end
h = {:a=>{:b=>{:c=>"d"}}}
puts h.get(:a).inspect
puts h.get(:a, :b).inspect
puts h.get(:a, :b, :c).inspect
@pmarreck
pmarreck / mandelbrot.erl
Last active August 29, 2015 14:16
A better [EDIT: prettier but WAY SLOWER!?!?!] Mandelbrot erlang algorithm for the Computer Language Benchmarks Game site?
% I found the following code in the benchmark game here:
% http://benchmarksgame.alioth.debian.org/u64q/program.php?test=mandelbrot&lang=hipe#sourcecode
% As you can see where the algorithm is defined below, I replaced it with a more functional,
% tail-call-optimized (I think?), pattern-matching guard-clause-using version.
% It's very pretty.
% The problem is it's THIRTY PERCENT SLOWER! WHY??
% Thanks to @radioxid on irc.freenode.net/#erlang for getting me to actually benchmark the change.
% The Computer Language Benchmarks Game
% http://benchmarksgame.alioth.debian.org/
@pmarreck
pmarreck / default_args_fun.exs
Last active August 29, 2015 14:19
so this compiled. interesting...
def concurrent_factorial(n) when is_integer(n) and n > 0 do
concurrent_factorial(1..n)
end
def concurrent_factorial(range, worker_pool_by_nodenames \\ worker_pool_by_nodenames)
def concurrent_factorial(%Range{first: start, last: finish}, worker_pool_by_nodenames) do
pmap(split_range_of_numbers(start..finish), fn(range) -> range |> Enum.reduce(&(&1*&2)) end, worker_pool_by_nodenames) |>
# one more reduction...
Enum.reduce(&(&1*&2))
end
@pmarreck
pmarreck / pooled_concurrency_demo.exs
Last active August 29, 2015 14:19
Elixir attempt at pooled concurrency demo
defmodule ConcurrencyDemo do
def num_procs(erlang \\ :erlang) do
erlang.system_info(:logical_processors)
end
def query_node(nodename, func, node \\ Node) do
me = self
pid = node.spawn(nodename, fn -> (send me, { self, func.() }) end)
receive do {^pid, result} -> result end
@pmarreck
pmarreck / fizzbuzz.exs
Last active August 29, 2015 14:20
My version of FizzBuzz in Elixir
defmodule FizzBuzz do
def fizzbuzz(n) when is_integer(n), do: fizzbuzz(n, {rem(n,3), rem(n,5)})
def fizzbuzz(_, {0,0}), do: :fizzbuzz
def fizzbuzz(_, {0,_}), do: :fizz
def fizzbuzz(_, {_,0}), do: :buzz
def fizzbuzz(n, {_,_}), do: n
def run(i \\ 100), do: (1..i) |> Enum.map(&fizzbuzz/1)
@pmarreck
pmarreck / better_way_to_do_this.exs
Created April 28, 2015 15:41
elixir argument error...
test "single digits" do
%{zero: 0, one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9}
|>
Enum.each(fn {word, num} ->
IO.puts "asserting #{word} == #{num}"
assert to_string(word) == NumbersToWords.parse(num)
end)
end
# I keep getting an argument error as it ends up trying to do :erlang.integer_to_binary("8")