Skip to content

Instantly share code, notes, and snippets.

@matthewcrews
matthewcrews / 0_reuse_code.js
Created September 7, 2017 03:05
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
type BuilderTest () =
member this.Yield (evalResult: bool) =
evalResult
member this.For(source: seq<'a>, body:'a -> seq<'b * bool>) =
source
|> Seq.collect (fun x -> body x |> Seq.map (fun (idx, expr) -> (x, idx), expr))
member this.For(source: seq<'a>, body:'a -> bool) =
source |> Seq.map (fun x -> x, body x)
type Food =
| Chicken
| Rice
type Step<'next> =
| GetFood of Food * 'next
| Eat of Food * 'next
| Sleep of hours:int * 'next
module Step =
type StateId = StateId of int
type PlanId = PlanId of int
type StepId = StepId of int
type State = {
LastStateId : StateId
LastPlanId : PlanId
LastStepId : StepId
}
type Step =
/// State is a function type that takes a state and
/// returns a value and the new state.
/// Single case union of the same type.
type State<'a, 's> = ('s -> 'a * 's)
module State =
// Explicit
// let result x : State<'a, 's> = fun s -> x, s
// Less explicit but works better with other, existing functions:
let result x s =
type State<'a, 's> = ('s -> 'a * 's)
module State =
// Explicit
// let result x : State<'a, 's> = fun s -> x, s
// Less explicit but works better with other, existing functions:
let result x s =
x, s
let bind (f:'a -> State<'b, 's>) (m:State<'a, 's>) : State<'b, 's> =
@matthewcrews
matthewcrews / CsvProvider2Column.fsx
Created May 6, 2021 19:18
CsvProvider failing on 2 column data
#r "nuget: FSharp.Data, 4.1.1"
open FSharp.Data
type TimeData = CsvProvider<"ExampleDates.csv">
let data = TimeData.Load $"{__SOURCE_DIRECTORY__}\\ExampleDates.csv"
let analysis =
data.Rows
|> Seq.map (fun row -> row.ExampleDateTime.Hour)
@matthewcrews
matthewcrews / SliceMapWithSpan.fsx
Created August 9, 2021 17:38
Compiler throws error saying it doesn't have a `Item` method for `ReadOnlySpan<'T>`
open System
open System.Collections.Generic
module rec SliceMap =
[<Struct>]
type IndexRange = {
Start : int
Length : int
}
@matthewcrews
matthewcrews / HadamardProductExpression.fsx
Last active August 10, 2021 17:49
Example of what I'm trying to be able to express
module rec TestTypes =
type Chicken = {
Size : int
} with
static member ( * ) (c: Chicken, i: int) =
{ Size = c.Size * i }
static member ( + ) (c1: Chicken, c2: Chicken) =
Flock [c1; c2]
static member ( + ) (c: Chicken, Flock f) =
@matthewcrews
matthewcrews / Program.Benchmarks-20210818-112327.log
Created August 18, 2021 18:25
Log of SliceMap performance
// Validating benchmarks:
// ***** BenchmarkRunner: Start *****
// ***** Found 3 benchmark(s) in total *****
// ***** Building 1 exe(s) in Parallel: Start *****
// start dotnet restore /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 /p:Deterministic=true /p:Optimize=true in C:\Users\matth\source\repos\SliceMapPerformanceExploration\bin\Release\net5.0\4e898864-ad69-478b-a9ec-168807204bd8
// command took 1.14s and exited with 0
// start dotnet build -c Release --no-restore /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 /p:Deterministic=true /p:Optimize=true in C:\Users\matth\source\repos\SliceMapPerformanceExploration\bin\Release\net5.0\4e898864-ad69-478b-a9ec-168807204bd8
// command took 1.86s and exited with 0
// ***** Done, took 00:00:03 (3.09 sec) *****
// Found 3 benchmarks: