Some examples related to my tweet rant https://twitter.com/dsymetweets/status/1294276915260522496
In project programming this hit me this week with a bug:
| 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>() |
| open BenchmarkDotNet.Attributes | |
| open BenchmarkDotNet.Running | |
| type Rec = {N: int} | |
| [<MemoryDiagnoser>] | |
| type Tests() = | |
| [<Benchmark(Baseline=true)>] | |
| member _.OptionSomeRec() = |
| type StepfulBuilder() = | |
| member _.Zero() = () | |
| member _.Yield x = x | |
| [<CustomOperation("toInt")>] | |
| member inline _.ToInt(_, value) = | |
| int value | |
| [<CustomOperation("transform")>] | |
| member _.Transform(x, 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 |
| [<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) |
Some examples related to my tweet rant https://twitter.com/dsymetweets/status/1294276915260522496
In project programming this hit me this week with a bug:
| // 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"). |
| #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) |
| let rec loop () = | |
| async { | |
| // do! loop () // memory leak | |
| return! loop () | |
| } |
| open System | |
| open System.IO | |
| open System.Text.Json | |
| open System.Threading.Tasks | |
| open FSharp.Control.Tasks.V2 | |
| type MyRecord = { | |
| Name: string | |
| Age: int |