Skip to content

Instantly share code, notes, and snippets.

View baronfel's full-sized avatar

Chet Husk baronfel

View GitHub Profile
@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
}
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
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 =
@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())
@baronfel
baronfel / newtonsoft.fsx
Created March 29, 2022 20:09
F# DU json roundtrip
#r "nuget: Newtonsoft.Json"
open Newtonsoft.Json
type Person = { First: string; Last: string }
type Employee = Employee of person: Person | Manager of manager: Person * employees: Employee list
let chet = Employee { First = "Chet"; Last = "Husk"}
let tim = Manager ({ First = "Tim"; Last = "Heuer"}, [chet])
let data = Map.ofList [
@baronfel
baronfel / Clean.csproj
Created March 28, 2022 20:35
Sample project file that demonstrates FileWrites for automatic clean
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<!-- Super critical - ensure your generation target runs _before_ the IncrementalClean target, which happens before `CoreCompile` -->
<IncrementalCleanDependsOn>GenerateFile;$(IncrementalCleanDependsOn)</IncrementalCleanDependsOn>
</PropertyGroup>
<Target
@baronfel
baronfel / test.fsx
Last active March 17, 2022 03:13
expecto dependent tests
#r "nuget: Expecto"
open Expecto
let dependentTest testName condition testCase =
test testName {
if condition then
testCase ()
else
skiptestf "Expecto: condition failed"
#r "nuget: System.CommandLine, 2.0.0-beta3.22106.2"
open System.CommandLine
open System.CommandLine.Builder
open System.CommandLine.Parsing
open System.CommandLine.Invocation
open System.Threading.Tasks
open System
let args = fsi.CommandLineArgs[1..]
#r "nuget: RestoreSources=https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-libraries/nuget/v3/index.json"
#r "nuget: System.CommandLine, 2.0.0-beta3.22076.2"
open System.CommandLine
open System.CommandLine.Parsing
open System
[<return:Struct>]
let inline (|EqualsIC|_|) l r = if String.Equals(l, r, StringComparison.OrdinalIgnoreCase) then ValueSome () else ValueNone
[<return:Struct>]
@baronfel
baronfel / script.fsx
Created January 3, 2022 17:32
S.CL F# example
#r "nuget: System.CommandLine, 2.0.0-beta2.21623.1"
#r "nuget: RestoreSources=https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-libraries/nuget/v3/index.json"
open System.CommandLine
open System.IO
open System.Threading.Tasks
let tagCommand = Command("tag-extract", "Get content with the given tag")
let inputfileArg = Argument<FileInfo>("input-file", "the file to extract content from")
tagCommand.AddArgument inputfileArg