Skip to content

Instantly share code, notes, and snippets.

View jwosty's full-sized avatar

John Wostenberg jwosty

View GitHub Profile
@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
@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 / 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