Skip to content

Instantly share code, notes, and snippets.

View jwosty's full-sized avatar

John Wostenberg jwosty

View GitHub Profile
@jwosty
jwosty / gist:0e70ac6ec409e182901a
Last active August 29, 2015 14:11
Useful chemistry things
open Microsoft.FSharp.Data.UnitSystems.SI.UnitNames
open Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols
open System
open System.Text.RegularExpressions
[<Measure>] type g
let (|Integer|_|) str =
let mutable result = 0
if Int32.TryParse (str, &result) then Some(result) else None
@jwosty
jwosty / gist:c594bcb1a1e54faf6cf2
Last active August 29, 2015 14:16
Simple, messy run-length encoder
let inline f tolerance data =
let two = LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne
data |> List.mapi (fun i c -> i, c)
|>function
| (_,c)::tl ->
tl
|> List.fold (fun (rest, (min, max), x) (i, c) ->
if abs (x-c) <= tolerance
then rest, (min, max+1), (x+c)/two
else ((min,max),x) :: rest, (i, i), c)
@jwosty
jwosty / TrigUnits
Created July 9, 2015 01:36
Adds units of measure to common trigonometric functions (so you can't, for example, accidentally pass degrees into `cos` which takes radians)
/// Radians
[<Measure>] type r
let inline unitlessSin value : ^T = sin value
let inline unitlessAsin value : ^T = asin value
let inline unitlessCos value : ^T = cos value
let inline unitlessAcos value : ^T = acos value
let inline unitlessTan value : ^T = tan value
let inline unitlessAtan value : ^T = atan value
let inline unitlessAtan2 (x: ^T1) (y: ^T1) : ^T2 = atan2 x y
// 44 bytes!
let rec s n=seq{yield!string n;yield!s(n+1)}
// Usage
s 0 |> Seq.nth 100
s 0 |> Seq.nth 10000000
@jwosty
jwosty / endo
Last active September 11, 2015 07:09
type Endo = EndsWith of string*string| NoEnding of string
EndsWith ("a","b")
NoEnding "a"
let breakEndingO (w:string) ending = if w.EndsWith ending then w.[0..w.Length-ending.Length-1],ending else w,""
let breakEnding (w:string) ending = if w.EndsWith ending then EndsWith (w.[0..w.Length-ending.Length-1],ending) else NoEnding(w)
breakEnding "hellooByInch" "ByInch"
breakEnding "xInch" "Inch"
breakEnding "Inch" "Inch"
type Cell = | On | Off
let (%) x m = ((x % m) + m) % m
/// Tells you how many neighbors are on
let nOnNeighbors (cells : _ [,]) (x, y) =
[for xOffset in -1..1 do
for yOffset in -1..1 do
if not ((xOffset = 0) && (yOffset = 0)) then
let x = (x + xOffset) % Array2D.length1 cells
open System
open System.IO
module String =
let split chars (s: string) = s.Split chars
/// Lazily returns every file and directory that has the given root path as a parent. Directories paths
/// occur before their children.
let rec getSystemEntriesRec path =
seq { yield path
@jwosty
jwosty / StateBuilder.fsx
Created March 24, 2016 23:44
F# state monad / computation expression builder, and example usage
open System
open System.IO
type State<'s, 'a> = State of ('s -> ('a * 's))
module State =
let inline run state x = let (State(f)) = x in f state
let get = State(fun s -> s, s)
let put newState = State(fun _ -> (), newState)
let map f s = State(fun (state: 's) ->
open System
open Microsoft.FSharp.Reflection
type Suit = | Clubs | Spades | Hearts | Diamonds
type Rank =
| Two | Three | Four | Five
| Six | Seven | Eight | Nine | Ten
| Jack | Queen | King | Ace
type Card = { rank: Rank // Rank field must come before suit so that card comparison is correct
suit: Suit }
@jwosty
jwosty / gist:474e8e57339ee15af27fcdc77340ec3d
Created June 21, 2016 22:39
MG Pipeline debug logging
Mono: Config attempting to parse: '/Library/Frameworks/Mono.framework/Versions/4.4.0/etc/mono/config'.
Mono: Config attempting to parse: '/Users/jwostenberg/.mono/config'.
Mono: _wapi_handle_new: Creating new handle of type Event
Mono: _wapi_handle_new: Allocated new handle 0x400
Mono: process_set_name: using [/Applications/Pipeline.app/Contents/MonoBundle/Pipeline.exe] as prog name
Mono: _wapi_handle_new: Creating new handle of type Process
Mono: _wapi_handle_new: Allocated new handle 0x401
Mono: Assembly Loader probing location: '/Library/Frameworks/Mono.framework/Versions/4.4.0/lib/mono/4.5/mscorlib.dll'.
Mono: Image addref mscorlib[0x78682140] -> /Library/Frameworks/Mono.framework/Versions/4.4.0/lib/mono/4.5/mscorlib.dll[0x79071c00]: 2
Mono: Assembly Loader probing location: '/Library/Frameworks/Mono.framework/Versions/4.4.0/lib/mono/4.5/mscorlib.dll'.