Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
isaacabraham / Linqoo.fsx
Last active June 10, 2024 10:48
An implementation of LINQ using OO composition features.
module LinqOo =
/// Selects a value into another shape.
type ISelectable<'a, 'b> =
abstract Select: 'a -> 'b
/// Filters a value.
type IFilterable<'a> =
abstract Filter: 'a -> bool
type ICollectable<'a, 'b> =
@isaacabraham
isaacabraham / paket.dependencies
Created June 5, 2024 13:54
App Insights lock
source https://api.nuget.org/v3/index.json
framework: net8.0
storage: none
nuget Microsoft.ApplicationInsights.Profiler.AspNetCore
// A generic Record
type Person<'T> = {
Name: string
Age: int
Singleton: 'T -> 'T list
}
// Creating a value with an "open" generic argument (?)
let personRec : Person<'T> = {
Name = "Isaac"
@isaacabraham
isaacabraham / aps.fsx
Last active December 22, 2023 18:11
Active Patterns
open System
let s = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53"
// challenge - split above into values we can reason about e.g.
type Card = {
Id : int // 1
Winning : int list // 41 48 83 86 17
Ticket : int list // 83 86 6 31 17 9 48 53
}
module List =
let partitionMap partitioner =
let rec loop (acc1, acc2) = function
| [] -> List.rev acc1, List.rev acc2
| x::xs ->
match partitioner x with
| Choice1Of2 y -> loop (y::acc1, acc2) xs
| Choice2Of2 y -> loop (acc1, y::acc2) xs
loop ([], [])
// F# version of https://twitter.com/gsferreira/status/1516827091127394309/
open System
open System.IO
type PotentialProcessingError = StringMissing | StringTooShort
let application (str: string) =
match str with
| str when str.Length < 5 -> Error StringTooShort
open System
/// A train carriage can have a number of different features...
type Feature = Quiet | Wifi | Toilet
/// Multiple classes
type CarriageClass = First | Second
/// Carriages can be either for passengers or the buffet cart
type CarriageKind =
namespace ClassLibrary1;
public static class LinqExtensions
{
public static IEnumerable<TOut> Choose<T, TOut>(this IEnumerable<T> input, Func<T, TOut?> chooser)
{
foreach (var i in input)
{
var c = chooser(i);
if (c is { } q)
{
#r "nuget:FSCheck"
open FsCheck
open System
type TurnDirection = Left | Right
type MoveDirection = Forward | Back
type Direction = N | S | E | W
type Coordinates = { X : int; Y : int }
type State =
module Decorators =
type FnDetails<'T> = { Name : string; Args : 'T }
type DecoratedFunction<'a, 'b> = FnDetails<'a> -> 'b
/// Logs the arguments and result of any function call.
let logger next details =
fooLogger.LogInformation ("Inside {Name}, Args {Args}", details.Name, details.Args)
let result = next details
fooLogger.LogInformation ("Result was {Result}.", $"%A{result}")
result