aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/list.lux
diff options
context:
space:
mode:
authorEduardo Julian2021-07-14 13:59:02 -0400
committerEduardo Julian2021-07-14 13:59:02 -0400
commitd6c48ae6a8b58f5974133170863a31c70f0123d1 (patch)
tree008eb88328009e2f3f07002f35c0378a8a137ed0 /stdlib/source/lux/data/collection/list.lux
parent2431e767a09894c2f685911ba7f1ba0b7de2a165 (diff)
Normalized the hierarchy of the standard library modules.
Diffstat (limited to 'stdlib/source/lux/data/collection/list.lux')
-rw-r--r--stdlib/source/lux/data/collection/list.lux615
1 files changed, 0 insertions, 615 deletions
diff --git a/stdlib/source/lux/data/collection/list.lux b/stdlib/source/lux/data/collection/list.lux
deleted file mode 100644
index 7bb2d4468..000000000
--- a/stdlib/source/lux/data/collection/list.lux
+++ /dev/null
@@ -1,615 +0,0 @@
-(.module:
- [lux #*
- ["@" target]
- [abstract
- [monoid (#+ Monoid)]
- [apply (#+ Apply)]
- [equivalence (#+ Equivalence)]
- [hash (#+ Hash)]
- [fold (#+ Fold)]
- [predicate (#+ Predicate)]
- ["." functor (#+ Functor)]
- ["." monad (#+ do Monad)]
- ["." enum]]
- [data
- ["." bit]
- ["." product]]
- [math
- [number
- ["n" nat]]]])
-
-## (type: (List a)
-## #Nil
-## (#Cons a (List a)))
-
-(implementation: #export fold
- (Fold List)
-
- (def: (fold f init xs)
- (case xs
- #.Nil
- init
-
- (#.Cons x xs')
- (fold f (f x init) xs'))))
-
-(def: #export (folds f init inputs)
- (All [a b] (-> (-> a b b) b (List a) (List b)))
- (case inputs
- #.Nil
- (list init)
-
- (#.Cons [head tail])
- (#.Cons [init (folds f (f head init) tail)])))
-
-(def: #export (reverse xs)
- (All [a]
- (-> (List a) (List a)))
- (fold (function (_ head tail) (#.Cons head tail))
- #.Nil
- xs))
-
-(def: #export (filter keep? xs)
- (All [a]
- (-> (Predicate a) (List a) (List a)))
- (case xs
- #.Nil
- #.Nil
-
- (#.Cons x xs')
- (if (keep? x)
- (#.Cons x (filter keep? xs'))
- (filter keep? xs'))))
-
-(def: #export (partition satisfies? list)
- {#.doc "Divide the list into all elements that satisfy a predicate, and all elements that do not."}
- (All [a] (-> (Predicate a) (List a) [(List a) (List a)]))
- (case list
- #.Nil
- [#.Nil #.Nil]
-
- (#.Cons head tail)
- (let [[in out] (partition satisfies? tail)]
- (if (satisfies? head)
- [(#.Cons head in) out]
- [in (#.Cons head out)]))))
-
-(def: #export (as_pairs xs)
- {#.doc (doc "Cut the list into pairs of 2."
- "Caveat emptor: If the list has an uneven number of elements, the last one will be skipped.")}
- (All [a] (-> (List a) (List [a a])))
- (case xs
- (^ (list& x1 x2 xs'))
- (#.Cons [x1 x2] (as_pairs xs'))
-
- _
- #.Nil))
-
-(template [<name> <then> <else>]
- [(def: #export (<name> n xs)
- (All [a]
- (-> Nat (List a) (List a)))
- (if (n.> 0 n)
- (case xs
- #.Nil
- #.Nil
-
- (#.Cons x xs')
- <then>)
- <else>))]
-
- [take (#.Cons x (take (dec n) xs')) #.Nil]
- [drop (drop (dec n) xs') xs]
- )
-
-(template [<name> <then> <else>]
- [(def: #export (<name> predicate xs)
- (All [a]
- (-> (Predicate a) (List a) (List a)))
- (case xs
- #.Nil
- #.Nil
-
- (#.Cons x xs')
- (if (predicate x)
- <then>
- <else>)))]
-
- [take_while (#.Cons x (take_while predicate xs')) #.Nil]
- [drop_while (drop_while predicate xs') xs]
- )
-
-(def: #export (split n xs)
- (All [a]
- (-> Nat (List a) [(List a) (List a)]))
- (if (n.> 0 n)
- (case xs
- #.Nil
- [#.Nil #.Nil]
-
- (#.Cons x xs')
- (let [[tail rest] (split (dec n) xs')]
- [(#.Cons x tail) rest]))
- [#.Nil xs]))
-
-(def: (split_with' predicate ys xs)
- (All [a]
- (-> (Predicate a) (List a) (List a) [(List a) (List a)]))
- (case xs
- #.Nil
- [ys xs]
-
- (#.Cons x xs')
- (if (predicate x)
- (split_with' predicate (#.Cons x ys) xs')
- [ys xs])))
-
-(def: #export (split_with predicate xs)
- {#.doc "Segment the list by using a predicate to tell when to cut."}
- (All [a]
- (-> (Predicate a) (List a) [(List a) (List a)]))
- (let [[ys' xs'] (split_with' predicate #.Nil xs)]
- [(reverse ys') xs']))
-
-(def: #export (chunk n xs)
- {#.doc "Segment the list in chunks of size N."}
- (All [a] (-> Nat (List a) (List (List a))))
- (case xs
- #.Nil
- (list)
-
- _
- (let [[pre post] (split n xs)]
- (#.Cons pre (chunk n post)))))
-
-(def: #export (repeat n x)
- {#.doc "A list of the value x, repeated n times."}
- (All [a]
- (-> Nat a (List a)))
- (if (n.> 0 n)
- (#.Cons x (repeat (dec n) x))
- #.Nil))
-
-(def: (iterate' f x)
- (All [a]
- (-> (-> a (Maybe a)) a (List a)))
- (case (f x)
- (#.Some x')
- (#.Cons x (iterate' f x'))
-
- #.None
- (list)))
-
-(def: #export (iterate f x)
- {#.doc "Generates a list element by element until the function returns #.None."}
- (All [a]
- (-> (-> a (Maybe a)) a (List a)))
- (case (f x)
- (#.Some x')
- (#.Cons x (iterate' f x'))
-
- #.None
- (list x)))
-
-(def: #export (one check xs)
- (All [a b]
- (-> (-> a (Maybe b)) (List a) (Maybe b)))
- (case xs
- #.Nil
- #.None
-
- (#.Cons x xs')
- (case (check x)
- (#.Some output)
- (#.Some output)
-
- #.None
- (one check xs'))))
-
-(def: #export (all check xs)
- (All [a b]
- (-> (-> a (Maybe b)) (List a) (List b)))
- (for {## TODO: Stop relying on this ASAP.
- @.js
- (fold (function (_ head tail)
- (case (check head)
- (#.Some head)
- (#.Cons head tail)
-
- #.None
- tail))
- #.Nil
- (reverse xs))}
- (case xs
- #.Nil
- #.Nil
-
- (#.Cons x xs')
- (case (check x)
- (#.Some output)
- (#.Cons output (all check xs'))
-
- #.None
- (all check xs')))))
-
-(def: #export (find predicate xs)
- {#.doc "Returns the first value in the list for which the predicate is #1."}
- (All [a]
- (-> (Predicate a) (List a) (Maybe a)))
- (..one (function (_ value)
- (if (predicate value)
- (#.Some value)
- #.None))
- xs))
-
-(def: #export (interpose sep xs)
- {#.doc "Puts a value between every two elements in the list."}
- (All [a]
- (-> a (List a) (List a)))
- (case xs
- #.Nil
- xs
-
- (#.Cons x #.Nil)
- xs
-
- (#.Cons x xs')
- (list& x sep (interpose sep xs'))))
-
-(def: #export (size list)
- (All [a] (-> (List a) Nat))
- (fold (function (_ _ acc) (n.+ 1 acc)) 0 list))
-
-(template [<name> <init> <op>]
- [(def: #export (<name> predicate xs)
- (All [a]
- (-> (Predicate a) (List a) Bit))
- (loop [xs xs]
- (case xs
- #.Nil
- <init>
-
- (#.Cons x xs')
- (case (predicate x)
- <init>
- (recur xs')
-
- output
- output))))]
-
- [every? #1 and]
- [any? #0 or]
- )
-
-(def: #export (nth i xs)
- {#.doc "Fetches the element at the specified index."}
- (All [a]
- (-> Nat (List a) (Maybe a)))
- (case xs
- #.Nil
- #.None
-
- (#.Cons x xs')
- (if (n.= 0 i)
- (#.Some x)
- (nth (dec i) xs'))))
-
-(implementation: #export (equivalence Equivalence<a>)
- (All [a] (-> (Equivalence a) (Equivalence (List a))))
-
- (def: (= xs ys)
- (case [xs ys]
- [#.Nil #.Nil]
- #1
-
- [(#.Cons x xs') (#.Cons y ys')]
- (and (\ Equivalence<a> = x y)
- (= xs' ys'))
-
- [_ _]
- #0
- )))
-
-(implementation: #export (hash super)
- (All [a] (-> (Hash a) (Hash (List a))))
-
- (def: &equivalence
- (..equivalence (\ super &equivalence)))
-
- (def: hash
- (\ ..fold fold
- (function (_ member hash)
- (n.+ (\ super hash member) hash))
- 0)))
-
-(implementation: #export monoid
- (All [a] (Monoid (List a)))
-
- (def: identity #.Nil)
- (def: (compose xs ys)
- (case xs
- #.Nil
- ys
-
- (#.Cons x xs')
- (#.Cons x (compose xs' ys)))))
-
-(open: "." ..monoid)
-
-(implementation: #export functor
- (Functor List)
-
- (def: (map f ma)
- (case ma
- #.Nil
- #.Nil
-
- (#.Cons a ma')
- (#.Cons (f a) (map f ma')))))
-
-(open: "." ..functor)
-
-(implementation: #export apply
- (Apply List)
-
- (def: &functor ..functor)
-
- (def: (apply ff fa)
- (case ff
- #.Nil
- #.Nil
-
- (#.Cons f ff')
- (compose (map f fa) (apply ff' fa)))))
-
-(implementation: #export monad
- (Monad List)
-
- (def: &functor ..functor)
-
- (def: (wrap a)
- (#.Cons a #.Nil))
-
- (def: join (|>> reverse (fold compose identity))))
-
-(def: #export (sort < xs)
- (All [a] (-> (-> a a Bit) (List a) (List a)))
- (case xs
- #.Nil
- (list)
-
- (#.Cons x xs')
- (let [[pre post] (fold (function (_ x' [pre post])
- (if (< x x')
- [(#.Cons x' pre) post]
- [pre (#.Cons x' post)]))
- [(list) (list)]
- xs')]
- ($_ compose (sort < pre) (list x) (sort < post)))))
-
-(def: #export (empty? xs)
- (All [a] (Predicate (List a)))
- (case xs
- #.Nil
- true
-
- _
- false))
-
-(def: #export (member? eq xs x)
- (All [a] (-> (Equivalence a) (List a) a Bit))
- (case xs
- #.Nil
- #0
-
- (#.Cons x' xs')
- (or (\ eq = x x')
- (member? eq xs' x))))
-
-(template [<name> <output> <side> <doc>]
- [(def: #export (<name> xs)
- {#.doc <doc>}
- (All [a] (-> (List a) (Maybe <output>)))
- (case xs
- #.Nil
- #.None
-
- (#.Cons x xs')
- (#.Some <side>)))]
-
- [head a x "Returns the first element of a list."]
- [tail (List a) xs' "For a list of size N, returns the N-1 elements after the first one."]
- )
-
-(def: #export (indices size)
- {#.doc "Produces all the valid indices for a given size."}
- (All [a] (-> Nat (List Nat)))
- (if (n.= 0 size)
- (list)
- (|> size dec (enum.range n.enum 0))))
-
-(def: (identifier$ name)
- (-> Text Code)
- [["" 0 0] (#.Identifier "" name)])
-
-(def: (nat@encode value)
- (-> Nat Text)
- (loop [input value
- output ""]
- (let [digit (case (n.% 10 input)
- 0 "0"
- 1 "1"
- 2 "2"
- 3 "3"
- 4 "4"
- 5 "5"
- 6 "6"
- 7 "7"
- 8 "8"
- 9 "9"
- _ (undefined))
- output' ("lux text concat" digit output)
- input' (n./ 10 input)]
- (if (n.= 0 input')
- output'
- (recur input' output')))))
-
-(macro: #export (zip tokens state)
- {#.doc (doc "Create list zippers with the specified number of input lists."
- (def: #export zip/2 (zip 2))
- (def: #export zip/3 (zip 3))
- ((zip 3) xs ys zs))}
- (case tokens
- (^ (list [_ (#.Nat num_lists)]))
- (if (n.> 0 num_lists)
- (let [(^open ".") ..functor
- indices (..indices num_lists)
- type_vars (: (List Code) (map (|>> nat@encode identifier$) indices))
- zip_type (` (All [(~+ type_vars)]
- (-> (~+ (map (: (-> Code Code) (function (_ var) (` (List (~ var)))))
- type_vars))
- (List [(~+ type_vars)]))))
- vars+lists (|> indices
- (map inc)
- (map (function (_ idx)
- (let [base (nat@encode idx)]
- [(identifier$ base)
- (identifier$ ("lux text concat" base "'"))]))))
- pattern (` [(~+ (map (function (_ [v vs]) (` (#.Cons (~ v) (~ vs))))
- vars+lists))])
- g!step (identifier$ "0step0")
- g!blank (identifier$ "0,0")
- list_vars (map product.right vars+lists)
- code (` (: (~ zip_type)
- (function ((~ g!step) (~+ list_vars))
- (case [(~+ list_vars)]
- (~ pattern)
- (#.Cons [(~+ (map product.left vars+lists))]
- ((~ g!step) (~+ list_vars)))
-
- (~ g!blank)
- #.Nil))))]
- (#.Right [state (list code)]))
- (#.Left "Cannot zip 0 lists."))
-
- _
- (#.Left "Wrong syntax for zip")))
-
-(def: #export zip/2 (zip 2))
-(def: #export zip/3 (zip 3))
-
-(macro: #export (zip_with tokens state)
- {#.doc (doc "Create list zippers with the specified number of input lists."
- (def: #export zip_with/2 (zip_with 2))
- (def: #export zip_with/3 (zip_with 3))
- ((zip_with 2) + xs ys))}
- (case tokens
- (^ (list [_ (#.Nat num_lists)]))
- (if (n.> 0 num_lists)
- (let [(^open ".") ..functor
- indices (..indices num_lists)
- g!return_type (identifier$ "0return_type0")
- g!func (identifier$ "0func0")
- type_vars (: (List Code) (map (|>> nat@encode identifier$) indices))
- zip_type (` (All [(~+ type_vars) (~ g!return_type)]
- (-> (-> (~+ type_vars) (~ g!return_type))
- (~+ (map (: (-> Code Code) (function (_ var) (` (List (~ var)))))
- type_vars))
- (List (~ g!return_type)))))
- vars+lists (|> indices
- (map inc)
- (map (function (_ idx)
- (let [base (nat@encode idx)]
- [(identifier$ base)
- (identifier$ ("lux text concat" base "'"))]))))
- pattern (` [(~+ (map (function (_ [v vs]) (` (#.Cons (~ v) (~ vs))))
- vars+lists))])
- g!step (identifier$ "0step0")
- g!blank (identifier$ "0,0")
- list_vars (map product.right vars+lists)
- code (` (: (~ zip_type)
- (function ((~ g!step) (~ g!func) (~+ list_vars))
- (case [(~+ list_vars)]
- (~ pattern)
- (#.Cons ((~ g!func) (~+ (map product.left vars+lists)))
- ((~ g!step) (~ g!func) (~+ list_vars)))
-
- (~ g!blank)
- #.Nil))))]
- (#.Right [state (list code)]))
- (#.Left "Cannot zip_with 0 lists."))
-
- _
- (#.Left "Wrong syntax for zip_with")))
-
-(def: #export zip_with/2 (zip_with 2))
-(def: #export zip_with/3 (zip_with 3))
-
-(def: #export (last xs)
- (All [a] (-> (List a) (Maybe a)))
- (case xs
- #.Nil
- #.None
-
- (#.Cons x #.Nil)
- (#.Some x)
-
- (#.Cons x xs')
- (last xs')))
-
-(def: #export (inits xs)
- {#.doc (doc "For a list of size N, returns the first N-1 elements."
- "Empty lists will result in a #.None value being returned instead.")}
- (All [a] (-> (List a) (Maybe (List a))))
- (case xs
- #.Nil
- #.None
-
- (#.Cons x #.Nil)
- (#.Some #.Nil)
-
- (#.Cons x xs')
- (case (inits xs')
- #.None
- (undefined)
-
- (#.Some tail)
- (#.Some (#.Cons x tail)))
- ))
-
-(def: #export (concat xss)
- (All [a] (-> (List (List a)) (List a)))
- (\ ..monad join xss))
-
-(implementation: #export (with monad)
- (All [M] (-> (Monad M) (Monad (All [a] (M (List a))))))
-
- (def: &functor (functor.compose (get@ #monad.&functor monad) ..functor))
-
- (def: wrap (|>> (\ ..monad wrap) (\ monad wrap)))
-
- (def: (join MlMla)
- (do {! monad}
- [lMla MlMla
- ## TODO: Remove this version ASAP and use one below.
- lla (for {@.old
- (: (($ 0) (List (List ($ 1))))
- (monad.seq ! lMla))}
- (monad.seq ! lMla))]
- (wrap (concat lla)))))
-
-(def: #export (lift monad)
- (All [M a] (-> (Monad M) (-> (M a) (M (List a)))))
- (\ monad map (\ ..monad wrap)))
-
-(def: #export (enumeration xs)
- {#.doc "Pairs every element in the list with its index, starting at 0."}
- (All [a] (-> (List a) (List [Nat a])))
- (loop [idx 0
- xs xs]
- (case xs
- #.Nil
- #.Nil
-
- (#.Cons x xs')
- (#.Cons [idx x] (recur (inc idx) xs')))))