Skip to content

Instantly share code, notes, and snippets.

@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
open System
/// Asynchronously loads some data from the DB
let fetchDataFromDb () = async {
printfn "Loading DB data..."
return 99
}
let random = Random()
#r "nuget: SchlenkR.FsHttp"
open FsHttp.DslCE
type Switch = On | Off member this.AsBool = match this with On -> true | Off -> false
type LightId = LightId of int
module Cmd =
let username = "<access username>"
let ip = "<local ip address>"
open Meadow
open System
open System.Threading
open Meadow.Devices
open Meadow.Foundation
open Meadow.Foundation.Leds
module HelloWorld =
let colors = [ Color.AliceBlue; Color.AntiqueWhite; Color.Black; Color.DarkGray; Color.Cyan; Color.Green; Color.GreenYellow; Color.Yellow; Color.Orange; Color.OrangeRed; Color.Red; Color.MediumVioletRed; Color.Purple; Color.Magenta; Color.Pink ]