Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Schoenfelder bitwalker

View GitHub Profile
View IndexedState.md

The Indexed State Monad in Haskell, Scala, and C#

Have you ever had to write code that made a complex series of succesive modifications to a single piece of mutable state? (Almost certainly yes.)

Did you ever wish you could make the compiler tell you if a particular operation on the state was illegal at a given point in the modifications? (If you're a fan of static typing, probably yes.)

If that's the case, the indexed state monad can help!

Motivation

View passive-attach-module-pattern-example.js
/**
* 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);
});
}
@bitwalker
bitwalker / App.scala
Last active Dec 20, 2015
Reverse Polish Notation Calculator in Scala
View App.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
@bitwalker
bitwalker / update
Last active Dec 23, 2015
Update hook for git which prevents files from having their line endings set to CRLF.
View update
#!/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 / dabblet.css
Created Nov 7, 2013 — forked from giuseppeg/dabblet.css
Dead Center an element with display: inline-block
View dabblet.css
/**
* Dead Center an element with display: inline-block
*/
html,
body { margin: 0; height: 100%; }
.container {
letter-spacing: -0.31em;
text-rendering: optimizespeed;
@bitwalker
bitwalker / extract.js
Created Dec 3, 2013
Just in case I ever need to extract values from a string using a mask
View extract.js
/**
* 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 / withinRange.js
Last active Dec 31, 2015
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.
View withinRange.js
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 / convert.sql
Created Apr 16, 2014
Convert a hex-encoded string to varbinary - MSSQL
View convert.sql
declare @hexstring varchar(max)
declare @bin varbinary(max)
set @hexstring = '0x10.....'
set @bin = (
select cast('' as xml).value('xs:hexBinary(substring(sql:variable("@hexstring"), sql:column("t.pos")))', 'varbinary(max)')
from (select case substring(@hexstring, 1, 2) when '0x' then 3 else 0 end) as t(pos)
)
select @bin
@bitwalker
bitwalker / Option.cs
Created Jul 11, 2014
Implementation of Option<T> in C#
View Option.cs
using System;
namespace Types
{
public static class Option
{
public static Option<T> ToOption<T>(this T @this)
{
if (@this == null)
return Option<T>.None;