Skip to content

Instantly share code, notes, and snippets.

@michaeloyer
michaeloyer / script.fsx
Created April 2, 2023 15:03
CSV Reading in F# with CsvHelper
#r "nuget: CsvHelper"
open System
open CsvHelper
type Transaction = { Amount: float; Date: DateOnly; Description: string }
let readTransactions() : Transaction list = [
use reader = new IO.StringReader("""
Account Number,Posted Date,Check,Description,Debit,Credit
@michaeloyer
michaeloyer / Program.fs
Last active May 28, 2024 01:19
Better Support for F# Option<'T> in Swagger
open System
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.DependencyInjection
open Microsoft.FSharp.Core
open Microsoft.OpenApi.Models
open Swashbuckle.AspNetCore.SwaggerGen
type Child = { Text: string }
type Parent = { Child: Child option }
@michaeloyer
michaeloyer / script.fsx
Created February 16, 2022 13:51
Demonstrating how to use a database with F#. Run this with `dotnet fsi script.fsx`
#r "nuget: System.Data.SQLite"
#r "nuget: Dapper"
open System
open Dapper
open System.Data
open System.Data.SQLite
[<CLIMutable>] // This creates private setters for Dapper to work with, but your F# code cannot
type Person = { Id: int; First: string; Last: string }
@michaeloyer
michaeloyer / srtp.fsx
Last active July 1, 2024 03:15
F# SRTP Example
// SRTP: Statically Resolved Type Parameters
// https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/statically-resolved-type-parameters
// SRTP Allows for pulling members out of types that where the member is named and typed the same
// In this example SRTP will be used to pull out the 'First: string' and 'Last: string' members
// from different types
// One example of SRTP in the F# Base Class Library is the (+) operator.
// You'll see that it has this type signature:
@michaeloyer
michaeloyer / CsvProviderStreaming.fs
Last active January 9, 2022 22:47
Example Showing how CSV Provider will use chunks of a stream while iterating
#r "nuget:FSharp.Data"
open System.IO
open System.Text
open FSharp.Data
type Csv = CsvProvider<"A,B,C\n1,2,3", CacheRows=false>
let csvStream =
let stream = new MemoryStream()
@michaeloyer
michaeloyer / Git Rebase Onto Script Example
Last active June 3, 2021 20:24
Git Rebase Onto explained with a script you can run on an empty directory
git init
git checkout -b original
git commit --allow-empty -m 'Commit 1'
git commit --allow-empty -m 'Commit 2'
git commit --allow-empty -m 'Commit 3 (third)'
git tag third
git commit --allow-empty -m 'Commit 4'
git commit --allow-empty -m 'Commit 5'
git tag fifth
git commit --allow-empty -m 'Commit 6 (sixth)'
@michaeloyer
michaeloyer / ComputationExpressionPlayground.fsx
Created April 12, 2021 02:24
Playing around with F# Computation Expressions (Result, Option, and AsyncResult). I think I get monads! Woo!
// Dummy functions to simulate a function that return a result
let getNumber expected resultNumber number =
if number = expected then
Ok resultNumber
else
Error (sprintf $"Expected {expected} but got {number}")
let getOne = getNumber 0 1
let getTwo = getNumber 1 2
let getThree = getNumber 2 3
@michaeloyer
michaeloyer / git-worktree-setup.ps1
Created November 20, 2020 15:57
Git Worktree Setup in Powershell + Git
git init SomeRepo #creates a folder in your current directory called 'SomeRepo'
pushd SomeRepo
git commit -m 'EMPTY' --allow-empty #branching doesn't work without a commit in the repo
git branch regular-branch
git worktree add -b worktree-branch '../SomeRepo#worktree-branch' #folder name example, without -b {BranchName} will create a branch with the name of the directory specified
git branch --list
popd
ls | select -exp Name
<#
@michaeloyer
michaeloyer / Hello.ts
Last active November 21, 2020 01:40
Hello World Program using Result/Pattern Matching in Typescript, F# and C#
type Result = Accepted | Rejected
interface Accepted {
status: "Accepted"
text: string
}
interface Rejected {
status: "Rejected"
}
@michaeloyer
michaeloyer / toDateString.js
Last active February 24, 2023 14:42
Javascript DateString in the format yyyy-mm-dd
function toDateString(date) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${year}-${month.toString().padStart(2,'0')}-${day.toString().padStart(2,'0')}`;
}