Skip to content

Instantly share code, notes, and snippets.

View baronfel's full-sized avatar

Chet Husk baronfel

View GitHub Profile
@baronfel
baronfel / roundtrip.fsx
Created March 29, 2022 22:05
F# DU storage with FSharp.SystemTextJson
#r "nuget: FSharp.SystemTextJson, 0.17.4"
module Database =
open System.Text.Json.Serialization
open System.Text.Json
let options =
let o = JsonSerializerOptions()
o.Converters.Add(JsonFSharpConverter())
open System
open System.Text.RegularExpressions
let alternativeCharacters = dict [ ' ', ' ' ]
let bypassCharactersJoined = ""
let escape s =
String.map (fun character -> match alternativeCharacters.TryGetValue(character) with (true, c) -> c | (false, _) -> character) s
let tryMatch pattern s =
let work x y = async {
return x + y // doesn't matter _what_ the work is
}
let sendWorkers x y = async {
let! workerBee1 = work (x) (y) |> Async.StartChild
let! workerBee2 = work (x) (y+1) |> Async.StartChild
let! workerBee3 = work (x) (y+2) |> Async.StartChild
// collect results
@baronfel
baronfel / c-vs-p.fsx
Last active February 27, 2023 21:32
concurrency/parallelism using Asyncs
#r "nuget: Nito.AsyncEx, 5.1.2"
open System.Threading
open System.Threading.Tasks
open Nito.AsyncEx
let work x y =
async {
do! Async.Sleep 1
return 1 + 2
}
@baronfel
baronfel / groupwith.fsx
Created April 18, 2022 01:40
Seq.groupWith
module Seq =
let groupWith grouper items =
seq {
let mutable currentKey = Unchecked.defaultof<'key>
let mutable currentSet = null
for item in items do
let thisKey = grouper item
if currentKey = Unchecked.defaultof<'key>
then
// new grouping
// here's the machinery
/// an SRTP function call that can call any type with a Count member, regardless of the return type of the Count
let inline count (t: ^t): ^u =
(^t : (member Count : ^u) (t))
/// an SRTP function call that can call any Indexer on a type, regardless of the input and output of that indexer
let inline item (t: ^t) (x: ^u) : ^v =
(^t : (member get_Item: ^u -> ^v) (t, x))
@baronfel
baronfel / Test.cs
Created June 10, 2022 21:11
SCL multiple array arguments problem
using System.CommandLine;
using System.CommandLine.Completions;
using System.CommandLine.Parsing;
using System.CommandLine.Builder;
new CommandLineBuilder(new TestCommand())
.UseParseDirective()
.UseSuggestDirective()
.Build()
.InvokeAsync(args);
@baronfel
baronfel / wrap.fsx
Created July 5, 2022 17:55
attempted .Add wrapper
[<Struct>]
type AddWrapper< 't, 'x >(item: 't, adder: 'x -> unit) =
member inline _.Delay(f) = f()
// incorrect, but just doing it for now
member inline _.Combine(left, right) = left
member _.Yield(o) =
adder o
item
@baronfel
baronfel / multitargeting-msbuild-targets.md
Last active August 3, 2022 19:23
changes made to seamlessly move to multitargeting

Multitargeting your MSBuild tasks and targets

Why would you multitarget?

For all practical purposes, you must multitarget. There is a huge chunk of the MSBuild ecosystem that's stuck on the .NET Framework build of MSBuild - everyone using tooling inside Visual Studio meets this criteria. If you don't support both Full and Core MSBuild distributions you're artificially cutting out your user base.

What does multitargeting mean?

For 'normal' .NET SDK projects, multitargeting means setting multiple TargetFrameworks in your project file. When you do this, builds will be triggered for both TFM, and the overall results can be packaged as a single artifact.

@baronfel
baronfel / pack-help.sh
Last active August 5, 2022 16:30
Potential `dotnet pack` help output for nuspecs
➜ dotnet pack --help
Description:
Create NuGet packages out of .NET projects or Nuspec files.
Usage:
dotnet pack [<PROJECT | SOLUTION>...] [Project Packing Options]
dotnet pack <NUSPEC> [Nuspec Packing Options]
Arguments:
<PROJECT | SOLUTION> A project or solution file to operate on. If a file is not specified, the command will search the current directory for one.