Skip to content

Instantly share code, notes, and snippets.

View bitwalker's full-sized avatar

Paul Schoenfelder bitwalker

View GitHub Profile

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

/**
* 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 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
@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 / 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 / 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 / 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 / convert.sql
Created April 16, 2014 20:43
Convert a hex-encoded string to varbinary - MSSQL
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 July 11, 2014 20:32
Implementation of Option<T> in C#
using System;
namespace Types
{
public static class Option
{
public static Option<T> ToOption<T>(this T @this)
{
if (@this == null)
return Option<T>.None;