diff options
Diffstat (limited to 'aoc-2022-dotnet/Common/Util.fs')
-rw-r--r-- | aoc-2022-dotnet/Common/Util.fs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs index acf89af..568f2c2 100644 --- a/aoc-2022-dotnet/Common/Util.fs +++ b/aoc-2022-dotnet/Common/Util.fs @@ -38,7 +38,11 @@ module Util = let mAt matrix (Vec2 (col, row)) = Array2D.get matrix row col - let composition n f = List.replicate n f |> List.reduce (>>) + let rec composition n f x = + if n = 0 then + x + else + composition (n - 1) f (f x) let notIn set element = not <| Set.contains element set @@ -51,6 +55,16 @@ module Util = | h :: t -> min h x :: (insertSorted (max h x) t) | [] -> [ x ] + let liftList xs = + match List.forall Option.isSome xs with + | true -> Some(List.map Option.get xs) + | false -> None + + let cycle = + function + | h :: t -> h, t @ [ h ] + | [] -> failwith "Empty list!" + let topN n xs = Seq.fold (fun acc x -> |