Skip to content

Instantly share code, notes, and snippets.

@matthewcrews
matthewcrews / PrivateUoM.fsx
Created April 30, 2022 21:28
Define a UoM which can only be created from a "constructor"
module Units =
[<Measure>] type private Chicken
module Chicken =
let create x =
if x < 0 then
invalidArg (nameof x) "Cannot have a Chicken <0"
open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open BenchmarkDotNet.Diagnosers
[<HardwareCounters(HardwareCounter.BranchMispredictions, HardwareCounter.CacheMisses)>]
[<DisassemblyDiagnoser(printSource = true, exportHtml = true)>]
type Benchmarks () =
let rng = Random 123
// Validating benchmarks:
// ***** BenchmarkRunner: Start *****
// ***** Found 2 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\TypedArrayIndexing\bin\Release\net6.0\af583c40-01ab-4113-bd10-b7f8ac106bac
// command took 1s 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\TypedArrayIndexing\bin\Release\net6.0\af583c40-01ab-4113-bd10-b7f8ac106bac
// command took 0.9s and exited with 1
// start dotnet build -c Release --no-restore --no-dependencies /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 /p:Deterministic=true /p:Optimize=true in C:\Users\matth\source\repos\TypedArrayIndexing\bin\Release\net6.0\af583c40-01ab-4113-bd10-b7f8ac106bac
// command took 1.62s and exite
[<Measure>] type Chicken
[<Measure>] type Cow
type Arr<[<Measure>] 'Measure>(v: array<_>) =
let values = v
member _.Item
with inline get (i: int<'Measure>) =
v.[int i]
[<Measure>] type Chicken
[<Measure>] type Cow
type Arr<[<Measure>] 'Measure>(v: array<'T>) =
member _.Item
with get (i: int<'Measure>) =
v.[int i]
#r "nuget: Flips,2.4.7"
open Flips
open Flips.Types
let x =
DecisionBuilder "x" {
for index in 1..5 ->
Continuous (0.0, infinity)
} |> Map
@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:
@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 / 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 / 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)