Skip to content

Instantly share code, notes, and snippets.

View bitwalker's full-sized avatar

Paul Schoenfelder bitwalker

View GitHub Profile
@bitwalker
bitwalker / Dockerfile
Created September 2, 2016 15:05
Release container image
FROM bitwalker/alpine-erlang:latest
MAINTAINER Paul Schoenfelder <paulschoenfelder@gmail.com>
ENV REFRESHED_AT=2016-06-21 \
HOME=/opt/app/ \
MIX_ENV=prod \
TERM=xterm \
APP=myapp \
APPVER=0.1.0
@bitwalker
bitwalker / config.ex
Created June 6, 2016 17:33
A nice config wrapper which allows using the {:system, "VAR"} convention comfortably
defmodule Config do
@moduledoc """
This module handles fetching values from the config with some additional niceties
"""
@doc """
Fetches a value from the config, or from the environment if {:system, "VAR"}
is provided.
An optional default value can be provided if desired.
@bitwalker
bitwalker / app.ex
Created March 23, 2016 22:48
A simple solution to executing code as part of a release
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
:ok = handle_args!
children = []
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
@bitwalker
bitwalker / time_vs_datatime.md
Created March 11, 2016 17:41 — forked from pixeltrix/time_vs_datatime.md
When should you use DateTime and when should you use Time?

When should you use DateTime and when should you use Time?

It's a common misconception that [William Shakespeare][1] and [Miguel de Cervantes][2] died on the same day in history - so much so that UNESCO named April 23 as [World Book Day because of this fact][3]. However because England hadn't yet adopted [Gregorian Calendar Reform][4] (and wouldn't until [1752][5]) their deaths are actually 10 days apart. Since Ruby's Time class implements a [proleptic Gregorian calendar][6] and has no concept of calendar reform then there's no way to express this. This is where DateTime steps in:

>> shakespeare = DateTime.iso8601('1616-04-23', Date::ENGLAND)
=> Tue, 23 Apr 1616 00:00:00 +0000
>> cervantes = DateTime.iso8601('1616-04-23', Date::ITALY)
=> Sat, 23 Apr 1616 00:00:00 +0000
@bitwalker
bitwalker / withinRange.js
Last active December 31, 2015 04:49
Need to determine if two physical points on a map are within a certain number of statute miles of each other? I got you covered.
function withinRange(sourceLong, sourceLat, targetLong, targetLat, range) {
var nauticalMilesPerStatuteMile = 0.868976;
// Latitude is always the same distance. Divide degrees by 60 to get arcminutes
var milesPerMinLat = 69.047 / 60;
// Distance in miles per arcminute longitude depends on the cosine of the arcminutes of latitude
var milesPerMinLong = (1 * Math.cos(sourceLat)) / nauticalMilesPerStatuteMile;
// Get delta in minutes between source and target longitude, convert to statute miles
var deltaLong = Math.abs((sourceLong * 60) - (targetLong * 60));
// Convert `deltaLong` to statute miles, rounding to 2 decimal places
var deltaLong = Math.round((milesPerMinLong * deltaLong) * 100) / 100;
@bitwalker
bitwalker / extract.js
Created December 3, 2013 22:45
Just in case I ever need to extract values from a string using a mask
/**
* Extracts values from a string using a pattern mask.
*
* Example:
* var greetFormat = 'Hello {{name}}! My name is {{greeter}}!'
* extract(greetFormat, 'Hello Paul! My name is Robotron!') => { name: 'Paul', greeter: 'Robotron'}
*/
function extract(format, s) {
var parser = getParser(format);
var parsed = parser.mask.exec(s);
@bitwalker
bitwalker / dabblet.css
Created November 7, 2013 19:30 — forked from giuseppeg/dabblet.css
Dead Center an element with display: inline-block
/**
* Dead Center an element with display: inline-block
*/
html,
body { margin: 0; height: 100%; }
.container {
letter-spacing: -0.31em;
text-rendering: optimizespeed;
@bitwalker
bitwalker / update
Last active December 23, 2015 22:39
Update hook for git which prevents files from having their line endings set to CRLF.
#!/bin/sh
# AUTHOR: Paul Schoenfelder
#
# This post-receive hook validates that committed files use proper (LF) line endings.
verify_lf()
{
echo "Validating line endings..."
oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)
@bitwalker
bitwalker / App.scala
Last active December 20, 2015 09:58
Reverse Polish Notation Calculator in Scala
import scala.util.parsing.combinator._
/**
* This trait provides the mathematical operations which the calculator can perform.
*/
trait Maths {
def add(x: Float, y: Float) = x + y
def sub(x: Float, y: Float) = x - y
def mul(x: Float, y: Float) = x * y
def div(x: Float, y: Float) = if (y > 0) (x / y) else 0.0f
/**
* JavaScript Module Pattern Example with "Passive Attachment"
*/
(function (root, builder, undefined) {
if (typeof define === 'function') {
// CommonJS AMD
define('MyModule', ['jQuery', 'DependencyA', 'DependencyB'], function($, a, b) {
return builder(root, $, a, b, null);
});
}