Skip to content

Instantly share code, notes, and snippets.

View TheAngryByrd's full-sized avatar
🐦
😠 🐦

Jimmy Byrd TheAngryByrd

🐦
😠 🐦
View GitHub Profile
@TheAngryByrd
TheAngryByrd / LogMetaData.fs
Last active February 18, 2025 14:15
F# Collecting logging metadata like namespace, function name, filepath, and line number
open System.Runtime.CompilerServices
type Log() =
static member inline GatherLogMeta
(
?name_space: string,
[<CallerMemberName>] ?cmb: string,
[<CallerLineNumber>] ?cln: int,
[<CallerFilePath>] ?cfp: string
) =
@TheAngryByrd
TheAngryByrd / CyclomaticComplexity.fsx
Created January 8, 2025 13:52
CyclomaticComplexity.fsx
#r "nuget: FSharp.Compiler.Service, 43.8.300"
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open FSharp.Compiler.Xml
open FSharp.Compiler.CodeAnalysis
open System.IO
type Node =
{ Data : Data
@TheAngryByrd
TheAngryByrd / ResultChoiceCE.fs
Created December 24, 2024 16:59
Choice Combining Result CE
type ResultBuilder () =
member inline _.Return (x) = Ok x
member inline _.Bind(x,f) =
match x with
| Ok x -> f x
| Error e -> Error <| e
@TheAngryByrd
TheAngryByrd / graph-based-parser.fsx
Last active December 3, 2024 14:58
f# graph based parser
open System
open System.IO
open System.Collections.Generic
let root = __SOURCE_DIRECTORY__
let srcDir = Path.Combine(root, "src")
// Add <OtherFlags>$(OtherFlags) --test:GraphBasedChecking --test:DumpCheckingGraph --times:timings.csv</OtherFlags> to the .fsproj or Directory.Build.props
@TheAngryByrd
TheAngryByrd / FooCe.fs
Created July 6, 2023 22:39
F# Computation Expression Method Overload Resolution Ordering
open System.Threading.Tasks
type FooBuilder() =
member _.Return(x) = Task.FromResult(Ok x)
member _.Bind(x, f) : Task<Result<'TResult, 'Error list>> =
task {
let! x = x
@TheAngryByrd
TheAngryByrd / STRP-Aliases.fs
Last active June 11, 2023 19:28
SRTP Aliases of types in the runtime that can be expressed by duck typing.
namespace FsToolkit.ErrorHandling.Ducks
open System
open System.Collections.Generic
open System.Threading.Tasks
open System.Runtime.CompilerServices
open Microsoft.FSharp.Core.CompilerServices
type Disposable<'Disposable when 'Disposable: (member Dispose: unit -> unit)> = 'Disposable
@TheAngryByrd
TheAngryByrd / SemaphoreLock.fs
Created March 31, 2023 22:20
Disposable SemaphoreLock
open System
open System.Threading
open System.Threading.Tasks
// Based on https://gist.github.com/StephenCleary/7dd1c0fc2a6594ba0ed7fb7ad6b590d6
// and https://gist.github.com/brendankowitz/5949970076952746a083054559377e56
/// <summary>
/// An awaitable wrapper around a task whose result is disposable. The wrapper is not disposable, so this prevents usage errors like "use _lock = myAsync()" when the appropriate usage should be "use! _lock = myAsync())".
@TheAngryByrd
TheAngryByrd / SomeDIExample.fs
Created March 28, 2023 15:03
F# Dependency Injection
#r "nuget: Serilog, 2.9.0"
open System.Threading.Tasks
[<Interface>]
type ILogger =
abstract Debug: string -> unit
abstract Error: string -> unit
[<Interface>]
@TheAngryByrd
TheAngryByrd / BlockingObjectPool.fs
Created March 3, 2023 23:45
BlockingObjectPool
type BlockingObjectPool<'T when 'T : (new : unit -> 'T)>(maxCount : int) =
let pool = Stack<'T>(maxCount)
let semaphoreSlim = new SemaphoreSlim(maxCount, maxCount)
do for i=0 to maxCount do
let foo = new 'T()
pool.Push(foo)
member _.Get() =
printf $"Getting from pool : {semaphoreSlim.CurrentCount}"
semaphoreSlim.Wait()
@TheAngryByrd
TheAngryByrd / StubBuilderCE.fs
Last active August 19, 2024 21:52
Computation Expression Stub for F#
open System
// Replace with your type
type MyType<'a> = Async<'a>
type Internal<'a> = MyType<'a>
// Replace with types that feel similar and can be converted to `MyType`
type External1<'a> = System.Threading.Tasks.Task<'a>
type External2<'a> = System.Threading.Tasks.ValueTask<'a>