Skip to content

Instantly share code, notes, and snippets.

@geofflane
geofflane / timer.ex
Created March 15, 2017 14:28
Elixir function that can be used to wrap a function to log the amount of time it takes to execute. Good for profiling in development.
defmodule App.Timer do
require Logger
def log(name, fun) do
{time, result} = :timer.tc(fun)
Logger.error("#{name}: #{time / 1000}ms")
result
end
end
@geofflane
geofflane / json_to_elixir_map.exs
Last active February 20, 2017 15:04
Convert JSON to a file containing an Elixir Map
map = File.read!("/path/to/file.json") |> Poison.decode!()
{:ok,of} = File.open("/path/to/file.exs", [:write, :utf8])
IO.inspect(of, map, limit: 100000, pretty: true)
File.close!(of)
@geofflane
geofflane / http_posion_client.ex
Last active January 18, 2017 03:42
HTTPoison Stateless API
@doc """
Struct to hold the configurable portions of HTTPoision
"""
defmodule HTTPoison.Client do
defstruct process_url: &__MODULE__.process_url/1,
process_request_body: &__MODULE__.process_request_body/1,
process_request_headers: &__MODULE__.process_request_headers/1,
process_request_options: &__MODULE__.process_request_options/1,
process_response_body: &__MODULE__.process_response_body/1,
process_headers: &__MODULE__.process_headers/1,
@geofflane
geofflane / core-test.clj
Created January 5, 2015 14:31
key-drone
(ns key-drone.core-test
(:use clj-drone.core)
(:require [clojure.test :refer :all]
[key-drone.core :refer :all]))
(deftest key-handler-test
(def navigation-keymap
{
\h :tilt-left
(ns baseball.core
(:require [clojure.core.async :refer (chan >!! <!! >! <! close! go go-loop)]
[clojure.core.match :refer (match)]))
;; http://www.variousandsundry.com/cs/blog/2014/02/20/baseball-processes/
;; It’s like playing catch in the backyard. You don’t want to play by yourself. So you tell a friend
;; to stand about 20 paces away from you. Once they’re there, they wave their arms to be sure you can
;; see them. Having seen them, you throw them the ball. They throw it back as a solid line drive
;; right at your mitt. End of process.
@geofflane
geofflane / cell.rb
Last active August 29, 2015 13:57
Practice Game Of Life
class Cell
DEAD = '.'
ALIVE = 'x'
attr_accessor :value
def self.create(val, position)
if val == DEAD
DeadCell.new(position)
else
LiveCell.new(position)
@geofflane
geofflane / keybase.md
Created March 14, 2014 17:54
keybase.md

Keybase proof

I hereby claim:

  • I am geofflane on github.
  • I am geofflane (https://keybase.io/geofflane) on keybase.
  • I have a public key whose fingerprint is C499 65E1 3128 AAD9 9589 F5EC 3DCE F7AC B68F D3B5

To claim this, I am signing this object:

@geofflane
geofflane / zombie_wasteland.rb
Created January 22, 2014 02:17
Got to get to the safe house and avoid the zombies.
class ZombieWasteland
DEFAULT_WASTELAND = %q(@*^^^
zz*z.
**...
^..*z
zz*zS)
def initialize(wasteland=DEFAULT_WASTELAND)
@wasteland = wasteland.gsub(' ', '')
end
@geofflane
geofflane / load.go
Last active December 21, 2015 23:39
Go JSON HTTP load testing
// This is a program to load test the OAuth Token API in AlphaAuth
// It's written in Go to allow it to take advantage of parallelism and run many requests at the same time
// MRI Ruby doesn't handle parallelism well so isn't very appropriate for this task.
// Install Go v1.1.2
// Build with "go build load.go" to build a native binary for your platform. Go builds statically linked binaries, so you don't
// need the go runtime installed where the app is run (but you do need to build it for the target architecture)
// ./load -h for command line options
// Currently this is only good for testing oauth token API
# Life:
# Rule 1: Any live cell with fewer than 2 live neighbors dies
# Rule 2: Any live cell with more than 3 live neighbors
# Rule 3: Any live cell with two or three live neighbors lives on
# Rule 4: Any dead cell with exatctly three live cells becomes a live cell
require "rspec/given"
describe "grid" do
Given(:grid) { Grid.new(input) }