2021-04-06 21:52:30 +03:00
|
|
|
Nat.maxNat = 18446744073709551615
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
(Nat.-) : Nat -> Nat -> Int
|
|
|
|
(Nat.-) = Nat.sub
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Int.maxInt = +9223372036854775807
|
|
|
|
Int.minInt = -9223372036854775808
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- use Universal == < > >=
|
|
|
|
-- use Optional None Some
|
2020-10-19 17:34:23 +03:00
|
|
|
|
|
|
|
-- Function composition
|
|
|
|
dot : (b -> c) -> (a -> b) -> a -> c
|
|
|
|
dot f g x = f (g x)
|
|
|
|
|
|
|
|
-- Function composition
|
|
|
|
andThen : (a -> b) -> (b -> c) -> a -> c
|
|
|
|
andThen f g x = g (f x)
|
|
|
|
|
|
|
|
const : a -> b -> a
|
|
|
|
const a _ = a
|
|
|
|
|
|
|
|
use Tuple Cons
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- namespace Tuple where
|
|
|
|
Tuple.at1 : Tuple a b -> a
|
|
|
|
Tuple.at1 = cases Cons a _ -> a
|
|
|
|
Tuple.at2 : Tuple a (Tuple b c) -> b
|
|
|
|
Tuple.at2 = cases Cons _ (Cons b _) -> b
|
|
|
|
Tuple.at3 : Tuple a (Tuple b (Tuple c d)) -> c
|
|
|
|
Tuple.at3 = cases Cons _ (Cons _ (Cons c _)) -> c
|
|
|
|
Tuple.at4 : Tuple a (Tuple b (Tuple c (Tuple d e))) -> d
|
|
|
|
Tuple.at4 = cases Cons _ (Cons _ (Cons _ (Cons d _))) -> d
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- namespace List where
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.map : (a -> b) -> [a] -> [b]
|
|
|
|
List.map f a =
|
2020-10-19 17:34:23 +03:00
|
|
|
go i as acc = match List.at i as with
|
|
|
|
None -> acc
|
|
|
|
Some a -> go (i + 1) as (acc `snoc` f a)
|
|
|
|
go 0 a []
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.zip : [a] -> [b] -> [(a,b)]
|
|
|
|
List.zip as bs =
|
2020-10-19 17:34:23 +03:00
|
|
|
go acc i = match (at i as, at i bs) with
|
|
|
|
(None,_) -> acc
|
|
|
|
(_,None) -> acc
|
|
|
|
(Some a, Some b) -> go (acc `snoc` (a,b)) (i + 1)
|
|
|
|
go [] 0
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.insert : Nat -> a -> [a] -> [a]
|
|
|
|
List.insert i a as = take i as ++ [a] ++ drop i as
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.replace : Nat -> a -> [a] -> [a]
|
|
|
|
List.replace i a as = take i as ++ [a] ++ drop (i + 1) as
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.slice : Nat -> Nat -> [a] -> [a]
|
|
|
|
List.slice start stopExclusive s =
|
2020-10-19 17:34:23 +03:00
|
|
|
take (stopExclusive `Nat.drop` start) (drop start s)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.unsafeAt : Nat -> [a] -> a
|
|
|
|
List.unsafeAt n as = match at n as with
|
2020-10-19 17:34:23 +03:00
|
|
|
Some a -> a
|
|
|
|
None -> Debug.watch "oh noes" (unsafeAt n as) -- Debug.crash "oh noes!"
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.foldl : (b -> a -> b) -> b -> [a] -> b
|
|
|
|
List.foldl f b as =
|
2020-10-19 17:34:23 +03:00
|
|
|
go b i = match List.at i as with
|
|
|
|
None -> b
|
|
|
|
Some a -> go (f b a) (i + 1)
|
|
|
|
go b 0
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.foldb : (a -> b) -> (b -> b -> b) -> b -> [a] -> b
|
|
|
|
List.foldb f op z as =
|
2020-10-19 17:34:23 +03:00
|
|
|
if List.size as == 0 then z
|
|
|
|
else if List.size as == 1 then f (unsafeAt 0 as)
|
|
|
|
else match halve as with (left, right) ->
|
|
|
|
foldb f op z left `op` foldb f op z right
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.reverse : [a] -> [a]
|
|
|
|
List.reverse as = foldl (acc a -> List.cons a acc) [] as
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.indexed : [a] -> [(a, Nat)]
|
|
|
|
List.indexed as = as `zip` range 0 (size as)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.sortBy : (a -> b) -> [a] -> [a]
|
|
|
|
List.sortBy f as =
|
2020-10-19 17:34:23 +03:00
|
|
|
tweak p = match p with (p1,p2) -> (f p1, p2, p1)
|
|
|
|
Heap.sort (map tweak (indexed as)) |> map Tuple.at3
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.halve : [a] -> ([a], [a])
|
|
|
|
List.halve s =
|
2020-10-19 17:34:23 +03:00
|
|
|
n = size s / 2
|
|
|
|
(take n s, drop n s)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.unfold : s -> (s -> Optional (a, s)) -> [a]
|
|
|
|
List.unfold s0 f =
|
2020-10-19 17:34:23 +03:00
|
|
|
go f s acc = match f s with
|
|
|
|
None -> acc
|
|
|
|
Some (a, s) -> go f s (acc `snoc` a)
|
|
|
|
go f s0 []
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.uncons : [a] -> Optional (a, [a])
|
|
|
|
List.uncons as = match at 0 as with
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> None
|
|
|
|
Some a -> Some (a, drop 1 as)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.unsnoc : [a] -> Optional ([a], a)
|
|
|
|
List.unsnoc as =
|
2020-10-19 17:34:23 +03:00
|
|
|
i = size (drop 1 as)
|
|
|
|
match at i as with
|
|
|
|
None -> None
|
|
|
|
Some a -> Some (take i as, a)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.join : [[a]] -> [a]
|
|
|
|
List.join = foldl (++) []
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.flatMap : (a -> [b]) -> [a] -> [b]
|
|
|
|
List.flatMap f as = join (map f as)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.range : Nat -> Nat -> [Nat]
|
|
|
|
List.range start stopExclusive =
|
2020-10-19 17:34:23 +03:00
|
|
|
f i = if i < stopExclusive then Some (i, i + 1) else None
|
|
|
|
unfold start f
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
List.distinct : [a] -> [a]
|
|
|
|
List.distinct as =
|
2020-10-19 17:34:23 +03:00
|
|
|
go i seen acc = match List.at i as with
|
|
|
|
None -> acc
|
|
|
|
Some a -> if Set.contains a seen then go (i + 1) seen acc
|
|
|
|
else go (i + 1) (Set.insert a seen) (acc `snoc` a)
|
|
|
|
go 0 Set.empty []
|
|
|
|
|
|
|
|
-- Joins a list of lists in a "fair diagonal" fashion.
|
|
|
|
-- Adapted from the Haskell version written by Luke Palmer.
|
2021-04-06 21:52:30 +03:00
|
|
|
List.diagonal : [[a]] -> [a]
|
|
|
|
List.diagonal =
|
2020-10-19 17:34:23 +03:00
|
|
|
let
|
|
|
|
x = 23
|
|
|
|
stripe = cases
|
|
|
|
[] -> []
|
|
|
|
[] +: xxs -> stripe xxs
|
|
|
|
(x +: xs) +: xxs -> cons [x] (zipCons xs (stripe xxs))
|
|
|
|
zipCons xs ys = match (xs, ys) with
|
|
|
|
([], ys) -> ys
|
|
|
|
(xs, []) -> map (x -> [x]) xs
|
|
|
|
(x +: xs, y +: ys) -> cons (cons x y) (zipCons xs ys)
|
|
|
|
List.join `dot` stripe
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- -- > List.foldb "" (t t2 -> "(" ++ t ++ " " ++ t2 ++ ")") (x -> x) ["Alice", "Bob", "Carol", "Dave", "Eve", "Frank", "Gerald", "Henry"]
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- -- Sorted maps, represented as a pair of sequences
|
|
|
|
-- -- Use binary search to do lookups and find insertion points
|
|
|
|
-- -- This relies on the underlying sequence having efficient
|
|
|
|
-- -- slicing and concatenation
|
2020-10-19 17:34:23 +03:00
|
|
|
type Map k v = Map [k] [v]
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- use Map Map
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- namespace Search where
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Search.indexOf : a -> [a] -> Optional Nat
|
|
|
|
Search.indexOf a s =
|
2020-10-19 17:34:23 +03:00
|
|
|
ao = Some a
|
|
|
|
Search.exact (i -> ao `compare` List.at i s) 0 (size s)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Search.lubIndexOf' : a -> Nat -> [a] -> Nat
|
|
|
|
Search.lubIndexOf' a start s =
|
2020-10-19 17:34:23 +03:00
|
|
|
ao = Some a
|
|
|
|
Search.lub (i -> ao `compare` List.at i s) start (size s)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Search.lubIndexOf : a -> [a] -> Nat
|
|
|
|
Search.lubIndexOf a s = lubIndexOf' a 0 s
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Search.lub : (Nat -> Int) -> Nat -> Nat -> Nat
|
|
|
|
Search.lub hit bot top =
|
2020-10-19 17:34:23 +03:00
|
|
|
if bot >= top then top
|
|
|
|
else
|
|
|
|
mid = (bot + top) / 2
|
|
|
|
match hit mid with
|
|
|
|
+0 -> mid
|
|
|
|
-1 -> lub hit bot mid
|
|
|
|
+1 -> lub hit (mid + 1) top
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Search.exact : (Nat -> Int) -> Nat -> Nat -> Optional Nat
|
|
|
|
Search.exact hit bot top =
|
2020-10-19 17:34:23 +03:00
|
|
|
if bot >= top then None
|
|
|
|
else
|
|
|
|
mid = (bot + top) / 2
|
|
|
|
match hit mid with
|
|
|
|
+0 -> Some mid
|
|
|
|
-1 -> exact hit bot mid
|
|
|
|
+1 -> exact hit (mid + 1) top
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- -- > ex = [0,2,4,6,77,192,3838,12000]
|
|
|
|
-- -- > List.map (e -> indexOf e ex) ex
|
|
|
|
-- -- > lubIndexOf 193 ex
|
2020-10-19 17:34:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
(|>) : a -> (a -> b) -> b
|
|
|
|
a |> f = f a
|
|
|
|
|
|
|
|
(<|) : (a -> b) -> a -> b
|
|
|
|
f <| a = f a
|
|
|
|
|
|
|
|
id : a -> a
|
|
|
|
id a = a
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
-- namespace Map where
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.empty : Map k v
|
|
|
|
Map.empty = Map [] []
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.singleton : k -> v -> Map k v
|
|
|
|
Map.singleton k v = Map [k] [v]
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.fromList : [(k,v)] -> Map k v
|
|
|
|
Map.fromList kvs =
|
2020-10-19 17:34:23 +03:00
|
|
|
go acc i = match List.at i kvs with
|
|
|
|
None -> acc
|
2021-04-06 21:52:30 +03:00
|
|
|
Some (k,v) -> go (Map.insert k v acc) (i + 1)
|
2020-10-19 17:34:23 +03:00
|
|
|
go empty 0
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.toList : Map k v -> [(k,v)]
|
|
|
|
Map.toList m = List.zip (keys m) (values m)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.size : Map k v -> Nat
|
|
|
|
Map.size s = List.size (keys s)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.lookup : k -> Map k v -> Optional v
|
|
|
|
Map.lookup k = cases
|
2020-10-19 17:34:23 +03:00
|
|
|
Map ks vs -> match Search.indexOf k ks with
|
|
|
|
None -> None
|
|
|
|
Some i -> at i vs
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.contains : k -> Map k v -> Boolean
|
|
|
|
Map.contains k = cases Map ks _ ->
|
|
|
|
match Search.indexOf k ks with
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> false
|
|
|
|
_ -> true
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.insert : k -> v -> Map k v -> Map k v
|
|
|
|
Map.insert k v = cases Map ks vs ->
|
2020-10-19 17:34:23 +03:00
|
|
|
use Search lubIndexOf
|
|
|
|
i = lubIndexOf k ks
|
|
|
|
match at i ks with
|
|
|
|
Some k' ->
|
|
|
|
if k == k' then Map ks (List.replace i v vs)
|
|
|
|
else Map (List.insert i k ks) (List.insert i v vs)
|
|
|
|
None -> Map (ks `snoc` k) (vs `snoc` v)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.map : (v -> v2) -> Map k v -> Map k v2
|
|
|
|
Map.map f m = Map (keys m) (List.map f (values m))
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.mapKeys : (k -> k2) -> Map k v -> Map k2 v
|
|
|
|
Map.mapKeys f m = Map (List.map f (keys m)) (values m)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.union : Map k v -> Map k v -> Map k v
|
|
|
|
Map.union = unionWith (_ v -> v)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.unionWith : (v -> v -> v) -> Map k v -> Map k v -> Map k v
|
|
|
|
Map.unionWith f m1 m2 = match (m1, m2) with (Map k1 v1, Map k2 v2) ->
|
2020-10-19 17:34:23 +03:00
|
|
|
go i j ko vo = match (at i k1, at j k2) with
|
|
|
|
(None, _) -> Map (ko ++ drop j k2) (vo ++ drop j v2)
|
|
|
|
(_, None) -> Map (ko ++ drop i k1) (vo ++ drop i v1)
|
|
|
|
(Some kx, Some ky) ->
|
|
|
|
use List slice unsafeAt
|
|
|
|
use Search lubIndexOf'
|
|
|
|
if kx == ky then
|
|
|
|
go (i + 1) (j + 1)
|
|
|
|
(ko `snoc` kx)
|
|
|
|
(vo `snoc` f (unsafeAt i v1) (unsafeAt j v2))
|
|
|
|
else if kx < ky then
|
|
|
|
i' = lubIndexOf' ky i k1
|
|
|
|
go i' j (ko ++ slice i i' k1) (vo ++ slice i i' v1)
|
|
|
|
else
|
|
|
|
j' = lubIndexOf' kx j k2
|
|
|
|
go i j' (ko ++ slice j j' k2) (vo ++ slice j j' v2)
|
|
|
|
go 0 0 [] []
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.intersect : Map k v -> Map k v -> Map k v
|
|
|
|
Map.intersect = intersectWith (_ v -> v)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.intersectWith : (v -> v -> v2) -> Map k v -> Map k v -> Map k v2
|
|
|
|
Map.intersectWith f m1 m2 = match (m1, m2) with (Map k1 v1, Map k2 v2) ->
|
2020-10-19 17:34:23 +03:00
|
|
|
go i j ko vo = match (at i k1, at j k2) with
|
|
|
|
(None, _) -> Map ko vo
|
|
|
|
(_, None) -> Map ko vo
|
|
|
|
(Some kx, Some ky) ->
|
|
|
|
if kx == ky then
|
|
|
|
go (i + 1) (j + 1)
|
|
|
|
(ko `snoc` kx)
|
|
|
|
(vo `snoc` f (List.unsafeAt i v1) (List.unsafeAt j v2))
|
|
|
|
else if kx < ky then
|
|
|
|
i' = Search.lubIndexOf' ky i k1
|
|
|
|
go i' j ko vo
|
|
|
|
else
|
|
|
|
j' = Search.lubIndexOf' kx j k2
|
|
|
|
go i j' ko vo
|
|
|
|
go 0 0 [] []
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.keys : Map k v -> [k]
|
|
|
|
Map.keys = cases Map ks _ -> ks
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Map.values : Map k v -> [v]
|
|
|
|
Map.values = cases Map _ vs -> vs
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Multimap.insert : k -> v -> Map k [v] -> Map k [v]
|
|
|
|
Multimap.insert k v m = match Map.lookup k m with
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> Map.insert k [v] m
|
|
|
|
Some vs -> Map.insert k (vs `snoc` v) m
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Multimap.lookup : k -> Map k [v] -> [v]
|
|
|
|
Multimap.lookup k m = Optional.orDefault [] (Map.lookup k m)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
|
|
|
type Set a = Set (Map a ())
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.empty : Set k
|
|
|
|
Set.empty = Set Map.empty
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.underlying : Set k -> Map k ()
|
|
|
|
Set.underlying = cases Set s -> s
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.toMap : (k -> v) -> Set k -> Map k v
|
|
|
|
Set.toMap f = cases Set (Map ks vs) -> Map ks (List.map f ks)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.fromList : [k] -> Set k
|
|
|
|
Set.fromList ks = Set (Map.fromList (List.map (k -> (k,())) ks))
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.toList : Set k -> [k]
|
|
|
|
Set.toList = cases Set (Map ks _) -> ks
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.contains : k -> Set k -> Boolean
|
|
|
|
Set.contains k = cases Set m -> Map.contains k m
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.insert : k -> Set k -> Set k
|
|
|
|
Set.insert k = cases Set s -> Set (Map.insert k () s)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.union : Set k -> Set k -> Set k
|
|
|
|
Set.union s1 s2 = Set (Map.union (underlying s1) (underlying s2))
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.size : Set k -> Nat
|
|
|
|
Set.size s = Map.size (underlying s)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Set.intersect : Set k -> Set k -> Set k
|
|
|
|
Set.intersect s1 s2 = Set (Map.intersect (underlying s1) (underlying s2))
|
2020-10-19 17:34:23 +03:00
|
|
|
|
|
|
|
type Heap k v = Heap Nat k v [Heap k v]
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.singleton : k -> v -> Heap k v
|
|
|
|
Heap.singleton k v = Heap 1 k v []
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.size : Heap k v -> Nat
|
|
|
|
Heap.size = cases Heap n _ _ _ -> n
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.union : Heap k v -> Heap k v -> Heap k v
|
|
|
|
Heap.union h1 h2 = match (h1, h2) with
|
2020-10-19 17:34:23 +03:00
|
|
|
(Heap n k1 v1 hs1, Heap m k2 v2 hs2) ->
|
|
|
|
if k1 >= k2 then Heap (n + m) k1 v1 (cons h2 hs1)
|
|
|
|
else Heap (n + m) k2 v2 (cons h1 hs2)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.pop : Heap k v -> Optional (Heap k v)
|
|
|
|
Heap.pop h =
|
2020-10-19 17:34:23 +03:00
|
|
|
go h subs =
|
|
|
|
use List drop size unsafeAt
|
|
|
|
if size subs == 0 then h
|
2021-04-06 21:52:30 +03:00
|
|
|
else if size subs == 1 then h `Heap.union` unsafeAt 0 subs
|
|
|
|
else union h (unsafeAt 0 subs) `Heap.union` go (unsafeAt 1 subs) (drop 2 subs)
|
2020-10-19 17:34:23 +03:00
|
|
|
match List.uncons (children h) with
|
|
|
|
None -> None
|
|
|
|
Some (s0, subs) -> Some (go s0 subs)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.children : Heap k v -> [Heap k v]
|
|
|
|
Heap.children = cases Heap _ _ _ cs -> cs
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.max : Heap k v -> (k, v)
|
|
|
|
Heap.max = cases Heap _ k v _ -> (k, v)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.maxKey : Heap k v -> k
|
|
|
|
Heap.maxKey = cases Heap _ k _ _ -> k
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.fromList : [(k,v)] -> Optional (Heap k v)
|
|
|
|
Heap.fromList kvs =
|
2020-10-19 17:34:23 +03:00
|
|
|
op a b = match a with
|
|
|
|
None -> b
|
|
|
|
Some a -> match b with
|
|
|
|
None -> Some a
|
2021-04-06 21:52:30 +03:00
|
|
|
Some b -> Some (Heap.union a b)
|
2020-10-19 17:34:23 +03:00
|
|
|
single = cases
|
2021-04-06 21:52:30 +03:00
|
|
|
(k, v) -> Some (Heap.singleton k v)
|
2020-10-19 17:34:23 +03:00
|
|
|
List.foldb single op None kvs
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.fromKeys : [a] -> Optional (Heap a a)
|
|
|
|
Heap.fromKeys as = fromList (List.map (a -> (a,a)) as)
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.sortDescending : [a] -> [a]
|
|
|
|
Heap.sortDescending as =
|
2020-10-19 17:34:23 +03:00
|
|
|
step = cases
|
|
|
|
None -> None
|
|
|
|
Some h -> Some (max h, pop h)
|
|
|
|
List.unfold (fromKeys as) step |> List.map Tuple.at1
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Heap.sort : [a] -> [a]
|
|
|
|
Heap.sort as = sortDescending as |> List.reverse
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
> sort [11,9,8,4,5,6,7,3,2,10,1]
|
2020-10-19 17:34:23 +03:00
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Optional.map : (a -> b) -> Optional a -> Optional b
|
|
|
|
Optional.map f = cases
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> None
|
|
|
|
Some a -> Some (f a)
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Optional.orDefault : a -> Optional a -> a
|
|
|
|
Optional.orDefault a = cases
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> a
|
|
|
|
Some a -> a
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Optional.orElse : Optional a -> Optional a -> Optional a
|
|
|
|
Optional.orElse a b = match a with
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> b
|
|
|
|
Some _ -> a
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Optional.flatMap : (a -> Optional b) -> Optional a -> Optional b
|
|
|
|
Optional.flatMap f = cases
|
2020-10-19 17:34:23 +03:00
|
|
|
None -> None
|
|
|
|
Some a -> f a
|
|
|
|
|
2021-04-06 21:52:30 +03:00
|
|
|
Optional.map2 : (a -> b -> c) -> Optional a -> Optional b -> Optional c
|
|
|
|
Optional.map2 f oa ob = flatMap (a -> map (f a) ob) oa
|