Skip to content

Instantly share code, notes, and snippets.

View jwosty's full-sized avatar

John Wostenberg jwosty

View GitHub Profile
@jwosty
jwosty / stuff2.fsx
Last active September 25, 2016 23:14
let rec genPairs list =
match list with
| [] -> []
| x::ys -> [for y in ys -> x, y] @ genPairs ys
let items = ["joe"; "jim"; "jack"; "jud"]
let r = new System.Random()
let shuffle cards = cards |> List.map (fun card -> r.Next(),card) |> List.sort |> List.map snd
items |> shuffle
@jwosty
jwosty / stuff.fsx
Last active September 25, 2016 23:14
let rec pairs list =
match list with
| [] -> []
| x::ys -> [for y in ys -> x, y] @ pairs ys
pairs [1..4]
let rec pairslc list = [
match list with
| [] -> ()
@jwosty
jwosty / harmless
Last active September 23, 2016 22:16
true
@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'.
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 / 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 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
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
@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"
// 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