Skip to content

Instantly share code, notes, and snippets.

View MattMS's full-sized avatar
🤔

Matt McKellar-Spence MattMS

🤔
View GitHub Profile
@MattMS
MattMS / INI.ps1
Created October 6, 2023 02:28
Parse INI content to Posh hashtable
$ini -split "`n" | % -Begin {$HT = @{}} -End {$HT} -Process {$n, $v = $_ -split '='; $HT[$n] = $v}
@MattMS
MattMS / Levenshtein.fsx
Created July 31, 2023 08:02
Get Levenshtein distance
// Formula from https://en.wikipedia.org/wiki/Levenshtein_distance
let lev (a : string) (b : string) =
let rec loop (a : char list) (b : char list) =
match a, b with
| a, [] -> a.Length
| [], b -> b.Length
| (aHead :: aTail), (bHead :: bTail) when aHead = bHead -> loop aTail bTail
| (_ :: aTail), (_ :: bTail) -> 1 + List.min [loop aTail b ; loop a bTail ; loop aTail bTail]
loop (List.ofSeq a) (List.ofSeq b)
@MattMS
MattMS / jsonb+uuid.sql
Created July 18, 2023 12:22
PostgreSQL table with JSONB referenced by UUID
CREATE TABLE doc (k UUID PRIMARY KEY DEFAULT gen_random_uuid(), v JSONB NOT NULL);
INSERT INTO doc (v) VALUES ('{"ok": true}');
@MattMS
MattMS / Get-DotnetVersion.ps1
Created March 1, 2023 00:36
Get locally installed .NET 6+ runtimes
dotnet --list-runtimes | %{if ($_ -match '^([^ ]+) (\d+\.\d+\.\d+) ') {[PSCustomObject]@{Name = $Matches[1]; Version = [version]$Matches[2]}}} | ? Version -gt ([version]"6.0.0")
@MattMS
MattMS / shrink.fsx
Created December 12, 2022 07:45
Encoding character pairs
let allChars =
Set.ofSeq
<| seq {
for c in (System.Char.MinValue) .. (System.Char.MaxValue) do
if System.Char.IsControl(c) |> not then
yield c
}
let mostCommonPair (s: string) =
s
@MattMS
MattMS / now.ps1
Last active September 3, 2020 13:55
Function Global:Get-Now {
$CI = New-Object System.Globalization.CultureInfo("en-AU")
$Epoch = [DateTimeOffset]::UnixEpoch
$Now = [DateTimeOffset]::Now
$UnixDay = ($Now.UtcDateTime - $Epoch.UtcDateTime).Days
$UnixDaySecond = [int]$Now.UtcDateTime.TimeOfDay.TotalSeconds
$UnixSecond = $Now.ToUnixTimeSeconds()
[PSCustomObject]@{
DayOfYear = $Now.DayOfYear
Global = $Now.UtcDateTime
@MattMS
MattMS / .vscode launch.json
Created October 8, 2019 11:10
Minimal F# project
{
"configurations": [
{
"args": [],
"console": "internalConsole",
"cwd": "${workspaceFolder}",
"name": ".NET Core Launch (console)",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/fsql.dll",
"request": "launch",
@MattMS
MattMS / Fabulous WPF with VS Code.md
Last active August 4, 2019 03:50
Debug Fabulous WPF with VS Code

Debugging WPF application with VS Code

The following code is based on the SqueakyApp demo using Fabulous.XamarinForms

Files

The launch.json and tasks.json are placed inside the .vscode directory beside the sln file. These files are usually created by VS Code, but since there is not a .NET Framework option by default, you can choose the .NET Core options and edit the files after.

launch.json

open System
open System.Drawing
open System.Windows.Forms
let addClickHandler click (button: Button) =
button.Click.AddHandler (new EventHandler (click))
button
let addControl (child: Control) (parent: Control) =
parent.Controls.Add child
@MattMS
MattMS / BasicWinForms.fs
Last active July 20, 2019 07:32
Basic F# WinForms demo
open System
open System.Windows.Forms
let makeButton label click =
let button = new Button()
button.Text <- label
let clickHandler = new EventHandler(click)
button.Click.AddHandler clickHandler
button