Skip to content

Instantly share code, notes, and snippets.

#nowarn "9"
open System
open Microsoft.FSharp.NativeInterop
let inline stackalloc<'a when 'a: unmanaged> size =
let p = NativePtr.stackalloc<'a> size |> NativePtr.toVoidPtr
Span<'a>(p, size)
@medigor
medigor / ConstrainedTypesExamples.fsx
Created October 2, 2020 12:47 — forked from swlaschin/ConstrainedTypesExamples.fsx
Examples of creating constrained types in F#
// General hints on defining types with constraints or invariants
//
// Just as in C#, use a private constructor
// and expose "factory" methods that enforce the constraints
//
// In F#, only classes can have private constructors with public members.
//
// If you want to use the record and DU types, the whole type becomes
// private, which means that you also need to provide:
// * a constructor function ("create").
@medigor
medigor / rant.md
Created August 14, 2020 19:30 — forked from dsyme/rant.md
[<AutoOpen>]
module AsyncBuilder =
open System.Threading.Tasks
type AsyncBuilder with
member inline _.Bind(task: Task<_>, f) = async.Bind(task |> Async.AwaitTask, f)
member inline _.Bind(task: Task, f) = async.Bind(task |> Async.AwaitTask, f)
module Seq =
let chunkBy f source =
seq {
let chunk = ResizeArray()
for x in source do
if (chunk.Count <> 0) && (f x) then
chunk.ToArray()
chunk.Clear()
chunk.Add(x)
if chunk.Count <> 0 then
@medigor
medigor / stepfulBuilder.fs
Created April 30, 2020 15:16 — forked from Szer/stepfulBuilder.fs
Computational expression with state being passed through steps
type StepfulBuilder() =
member _.Zero() = ()
member _.Yield x = x
[<CustomOperation("toInt")>]
member inline _.ToInt(_, value) =
int value
[<CustomOperation("transform")>]
member _.Transform(x, f) =
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
type Rec = {N: int}
[<MemoryDiagnoser>]
type Tests() =
[<Benchmark(Baseline=true)>]
member _.OptionSomeRec() =
open System
open System.Collections.Concurrent
open Microsoft.FSharp.Reflection
open Microsoft.FSharp.Quotations.Patterns
type UnionUtils =
static member private isTypeUnionCache = ConcurrentDictionary<Type, bool>()
static member private tagGetterCache = ConcurrentDictionary<Type, obj -> int>()
module Parser
open System
open System.IO
open System.Text
open System.Text.RegularExpressions
open FSharp.Control.AsyncSeqExtensions
open FSharp.Control
type Event = {
module Tests
open System
open System.Text
open System.Threading.Tasks
open System.Diagnostics
open Xunit
open RabbitMQ.Client
open RabbitMQ.Client.Events