Skip to content

Instantly share code, notes, and snippets.

@artur-s
artur-s / 01.fsx
Last active December 20, 2021 06:09
AoC2021
// https://adventofcode.com/2021
let readings =
[|
199
227
229
|]
// part1
@artur-s
artur-s / scanLeftUsingFoldAndMap.fs
Created November 6, 2021 23:18
`scanLeft` in terms of `fold` and `map`
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 )
@artur-s
artur-s / memoizedYcombinator.csx
Last active October 11, 2020 21:36
A non-recursive implementation of Y-combinator with memoization in C#
// 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)
@artur-s
artur-s / ReaderAndFreeM.fsx
Last active March 18, 2019 04:19
Monadic DI: Reader and Free
// 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
@artur-s
artur-s / ListDiffWithDups.fsx
Last active November 4, 2017 16:13
Diffs two lists preserving duplicates
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
@artur-s
artur-s / functorsComposition.fsx
Created March 19, 2017 17:59
Composing functors is for free
let optionListMap f = (Option.map >> List.map) f
let square x = x * x
let squareLifted = optionListMap square
squareLifted [Some 2; None; Some 3]
@artur-s
artur-s / FsCheckMoneyGen.fsx
Created November 13, 2016 03:58
FsCheck generator for positive decimals constrained to more realistic money amounts
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)
@artur-s
artur-s / KleisliComposition.fsx
Created November 12, 2016 23:23
An example of Kleisli composition
// 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
@artur-s
artur-s / operators.fs
Last active November 13, 2016 04:03
Operators for matching option and nullable
module Option =
let (|??) = defaultArg
let (|?) nullable d = if isNull nullable then d else nullable
// usage
open Option
[] |> List.tryHead |?? 0
@artur-s
artur-s / versionETags.fsx
Last active August 12, 2016 17:12
Encoding version numbers (integers) as ETags using SHA-1
open System
open System.Security.Cryptography
let hasher = SHA1.Create()
let intToSHA1 (n:int) =
let hexString bytes =
[for b:byte in bytes -> b.ToString("x2")] |> String.concat ""
n |> (BitConverter.GetBytes >> hasher.ComputeHash >> hexString)