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
}
@isaacabraham
isaacabraham / gist:abc499625af63d798bcd
Last active September 30, 2023 11:25
FSXaml Converter
type ThingConverter() =
interface IValueConverter with
member x.Convert(value, _, _, _) = if (value.ToString()) = "Joe Bloggs" then (10. :> obj) else (20. :> obj)
member x.ConvertBack(_, _, _, _) = null
// This converter will always fail and just return 0.
type CustomThingConverter() =
inherit Converter<string, float>((fun value _ -> if value = "Joe Bloggs" then 10. else 20.), 0.)
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 ([], [])
@isaacabraham
isaacabraham / io-monad.fsx
Last active November 1, 2022 23:58
F# port of the first half of John De Goes "FP to the max" (https://www.youtube.com/watch?v=sxudIMiOo68)
#load @".paket\load\net452\FSharpPlus.fsx"
open FSharpPlus
open System
[<AutoOpen>]
module rec IO =
let run (IO computation) = computation()
type IO<'T> =
// Change this to your profile folder for nuget packages
#I @"C:\Users\Isaac\.nuget\packages\"
#r @"XPlot.GoogleCharts\2.0.0\lib\netstandard2.0\XPlot.GoogleCharts.dll"
#r @"Newtonsoft.Json\12.0.3\lib\netstandard2.0\Newtonsoft.Json.dll"
#r @"Google.DataTable.Net.Wrapper\4.0.0\lib\netstandard2.0\Google.DataTable.Net.Wrapper.dll"
open System
open XPlot.GoogleCharts
/// Executes an asynchronous workflow and provides some simple statistics on the results.
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 =
// 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
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)
{
@isaacabraham
isaacabraham / idiomaticjsonserialiser.fs
Created September 7, 2014 21:17
This JSON.Net converter handles F# discriminated unions with more "idiomatic" JSON than what is generated by the current version of JSON .NET. Option types and single case DUs are transparently handled, and tuple-style properties are used rather than array notation.
namespace Newtonsoft.Json.Converters
open Microsoft.FSharp.Reflection
open Newtonsoft.Json
open System
type IdiomaticDuConverter() =
inherit JsonConverter()
[<Literal>]
#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 =