Some examples related to my tweet rant https://twitter.com/dsymetweets/status/1294276915260522496
In project programming this hit me this week with a bug:
#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) |
// 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"). |
Some examples related to my tweet rant https://twitter.com/dsymetweets/status/1294276915260522496
In project programming this hit me this week with a bug:
[<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 |
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 |