This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://adventofcode.com/2021 | |
let readings = | |
[| | |
199 | |
227 | |
229 | |
|] | |
// part1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let scanLeft (s:'s) (f:'s -> 'a -> 's) (lst:'a list) : 's list = | |
lst | |
|> List.mapi (fun i _ -> | |
lst | |
|> List.take i | |
|> List.fold f s ) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// non-recursive implementation of Y-combinator, based on example from https://en.wikipedia.org/wiki/Fixed-point_combinator#Type_for_the_Y_combinator | |
// with memoization | |
using System; | |
using System.Diagnostics; | |
using System.Collections.Concurrent; | |
//type 'a Recc = In of ('a Recc -> 'a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Monadic DI: Reader and Free monad | |
// https://www.youtube.com/watch?v=ZasXwtTRkio | |
module Reader = | |
type Reader<'d, 'a> = {Apply:('d -> 'a)} | |
let reader f = {Reader.Apply = f} | |
let (|Reader|) ({Reader.Apply = apply}) = apply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module List | |
let rec diffDups minuend subtrahend = | |
let rec removeFirst i = function | |
| h::t -> if h = i then t else h::(removeFirst i t) | |
| [] -> [] | |
match subtrahend with | |
| h::t -> diffDups (removeFirst h minuend) t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let optionListMap f = (Option.map >> List.map) f | |
let square x = x * x | |
let squareLifted = optionListMap square | |
squareLifted [Some 2; None; Some 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open FsCheck | |
let positiveDecimal = gen { | |
let! low = Gen.elements [0..1000] | |
let! mid = Gen.elements [0..100] | |
let! scale = Gen.elements [0..5] | |
return! | |
Gen.elements [ | |
Decimal(low, mid, 0, false, byte scale) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An example of Kleisli composition | |
let (>>=) m f = Option.bind f m | |
let (>=>) mf mg = fun x -> mf x >>= mg | |
let tryParseInt s = | |
match System.Int32.TryParse s with | |
| true, v -> Some v | |
| _ -> None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Option = | |
let (|??) = defaultArg | |
let (|?) nullable d = if isNull nullable then d else nullable | |
// usage | |
open Option | |
[] |> List.tryHead |?? 0 |
NewerOlder