Skip to content

Instantly share code, notes, and snippets.

View bent-rasmussen's full-sized avatar

Bent Rasmussen bent-rasmussen

View GitHub Profile
@bent-rasmussen
bent-rasmussen / vide-todo-list.linq
Created November 25, 2023 09:57
Vide todo-list sample in a LINQPad script (F#, Avalonia)
<Query Kind="FSharpProgram">
<NuGetReference>Avalonia</NuGetReference>
<NuGetReference>Avalonia.Desktop</NuGetReference>
<NuGetReference>Avalonia.Themes.Fluent</NuGetReference>
<NuGetReference>Vide.UI.Avalonia</NuGetReference>
<Namespace>Avalonia</Namespace>
<Namespace>Vide</Namespace>
<Namespace>Vide.UI.Avalonia</Namespace>
</Query>
@bent-rasmussen
bent-rasmussen / OrdinalIgnoreCaseString.fs
Last active June 8, 2023 08:40
OrdinalIgnoreCaseString - an F# string type that is case-insensitive
// Experiment
open System
open System.Collections.Generic
open System.Runtime.CompilerServices
// Case-insensitive strings for ValueTuple embedding.
[<IsReadOnly; Struct; CustomEquality; CustomComparison>]
type OrdinalIgnoreCaseString =
{ Value: string }
@bent-rasmussen
bent-rasmussen / EarlyReturn.fs
Created April 26, 2022 09:01
F# async workflow early return
// No early return
// Prints "gotcha"
async {
return ()
printfn "gotcha"
}
|> Async.RunSynchronously
// Early return
// Does not print anything
@bent-rasmussen
bent-rasmussen / NoBoxStruct.fs
Last active February 21, 2022 17:16
How I learned to love the struct and stop worrying (part II)...
// Use the Test solution configuration and now test cases will fail if they are using '=' on structs.
#if STRUCT_EQ_CHECK
[<AutoOpen>]
#endif
module StructEqCheck =
type StructEqualityException() =
inherit exn()
@bent-rasmussen
bent-rasmussen / NoBoxStruct.fs
Last active February 19, 2022 17:34
How I learned to love the struct and stop worrying (part I)...
/// Equatable operators - avoid boxing operands when comparing structs.
/// Source: https://github.com/dotnet/fsharp/issues/526#issuecomment-119755563
module EquatableOperators =
let inline eq<'a when 'a :> System.IEquatable<'a>> (x:'a) (y:'a) = x.Equals y
let inline (==) x y = eq x y
let inline (!=) x y = not (eq x y)
/// We get a fair warning that it's odd that we impl. custom equality
/// when we deny the equality operator.
[<Struct; NoEquality; NoComparison>]
@bent-rasmussen
bent-rasmussen / Code.fs
Last active January 4, 2022 11:14
Compile-time error on contradictive conditional compilation symbols
// Compile-time error on contradictive conditional compilation symbols.
// Make conditional compilation symbols form a discriminated union.
#if (SITE_DEV && SITE_TEST)
§"Contradictive conditional compilation symbols"
#endif
@bent-rasmussen
bent-rasmussen / TraceHttpRequest.linq
Last active January 2, 2022 15:30
LINQPad HTTP request tracing tool
<Query Kind="Statements">
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>System.Dynamic</Namespace>
</Query>
await TestAsync("Foo", http => http.GetStringAsync("http://www.google.com/"), HttpFormatOptions.Verbose);
static object Title(string message, bool isError = false)
@bent-rasmussen
bent-rasmussen / EventSourceBuilder.linq
Last active January 4, 2022 01:08
EventSource fluent source code generator - eliminates boilerplate and ensures consistency
<Query Kind="Program">
<NuGetReference>Observito.Trace.EventSourceFormatter</NuGetReference>
<Namespace>Observito.Trace.EventSourceFormatter</Namespace>
<Namespace>System.Collections.Immutable</Namespace>
<Namespace>System.Diagnostics.Tracing</Namespace>
</Query>
#nullable enable
void Main()
@bent-rasmussen
bent-rasmussen / AdaptiveFSharpMagic.linq
Created May 8, 2021 02:04
FSharp.Data.Adaptive example in linqpad
<Query Kind="FSharpProgram">
<NuGetReference>FSharp.Data.Adaptive</NuGetReference>
<Namespace>FSharp.Data.Adaptive</Namespace>
<Namespace>FSharp.Data.Traceable</Namespace>
</Query>
// F# - what the F* is this sorcery?! :-)
// create a temporary directory
@bent-rasmussen
bent-rasmussen / DynamicSerilog.linq
Created September 29, 2020 16:43
An logger experiment for C# using the dynamic language feature.
<Query Kind="Program">
<NuGetReference>Serilog</NuGetReference>
<NuGetReference>Serilog.Sinks.LINQPad</NuGetReference>
<Namespace>System.Dynamic</Namespace>
<Namespace>Serilog</Namespace>
</Query>
// Just an experiment; you should probably never use this. :-)
// Would be nicer with compile-time function creation but not possible atm afaik.