From 4f4656b278c6f9dfbdd15d5d9bc86d63c5b44333 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 15 Sep 2021 20:22:07 -0400 Subject: "Sequence" => "Stream" --- .../library/lux/data/collection/sequence.lux | 140 --------------------- .../source/library/lux/data/collection/stream.lux | 140 +++++++++++++++++++++ stdlib/source/library/lux/documentation.lux | 14 +-- 3 files changed, 147 insertions(+), 147 deletions(-) delete mode 100644 stdlib/source/library/lux/data/collection/sequence.lux create mode 100644 stdlib/source/library/lux/data/collection/stream.lux (limited to 'stdlib/source/library') diff --git a/stdlib/source/library/lux/data/collection/sequence.lux b/stdlib/source/library/lux/data/collection/sequence.lux deleted file mode 100644 index fcfe2d0ed..000000000 --- a/stdlib/source/library/lux/data/collection/sequence.lux +++ /dev/null @@ -1,140 +0,0 @@ -(.module: - [library - [lux "*" - [abstract - [functor {"+" Functor}] - [comonad {"+" CoMonad}]] - [control - ["//" continuation {"+" Cont}] - ["<>" parser - ["<[0]>" code {"+" Parser}]]] - [macro {"+" with_symbols} - [syntax {"+" syntax:}] - ["[0]" code]] - [data - ["[0]" bit] - [collection - ["[0]" list ("[1]#[0]" monad)]]] - [math - [number - ["n" nat]]]]]) - -(type: .public (Sequence a) - (Cont [a (Sequence a)])) - -(def: .public (iterations step init) - (All (_ a b) - (-> (-> a [a b]) a (Sequence b))) - (let [[next x] (step init)] - (//.pending [x (iterations step next)]))) - -(def: .public (repeated x) - (All (_ a) - (-> a (Sequence a))) - (//.pending [x (repeated x)])) - -(def: .public (cycle [start next]) - (All (_ a) - (-> [a (List a)] (Sequence a))) - (loop [head start - tail next] - (//.pending [head (case tail - {.#End} - (again start next) - - {.#Item head' tail'} - (again head' tail'))]))) - -(template [ ] - [(def: .public ( sequence) - (All (_ a) (-> (Sequence a) )) - (let [[head tail] (//.result sequence)] - ))] - - [head a] - [tail (Sequence a)] - ) - -(def: .public (item idx sequence) - (All (_ a) (-> Nat (Sequence a) a)) - (let [[head tail] (//.result sequence)] - (case idx - 0 head - _ (item (-- idx) tail)))) - -(template [ ] - [(def: .public ( pred xs) - (All (_ a) - (-> (Sequence a) (List a))) - (let [[x xs'] (//.result xs)] - (if ( ) - (list& x ( xs')) - (list)))) - - (def: .public ( pred xs) - (All (_ a) - (-> (Sequence a) (Sequence a))) - (let [[x xs'] (//.result xs)] - (if ( ) - ( xs') - xs)))] - - [while until (-> a Bit) (pred x) pred |>] - [first after Nat (n.= 0 pred) (-- pred) not] - ) - -(template [ ] - [(def: .public ( pred xs) - (All (_ a) - (-> (Sequence a) [(List a) (Sequence a)])) - (let [[x xs'] (//.result xs)] - (if - [(list) xs] - (let [[tail next] ( xs')] - [{.#Item [x tail]} next]))))] - - [split_when (-> a Bit) (pred x) pred] - [split_at Nat (n.= 0 pred) (-- pred)] - ) - -(def: .public (only predicate sequence) - (All (_ a) (-> (-> a Bit) (Sequence a) (Sequence a))) - (let [[head tail] (//.result sequence)] - (if (predicate head) - (//.pending [head (only predicate tail)]) - (only predicate tail)))) - -(def: .public (partition left? xs) - (All (_ a) (-> (-> a Bit) (Sequence a) [(Sequence a) (Sequence a)])) - [(..only left? xs) - (..only (bit.complement left?) xs)]) - -(implementation: .public functor - (Functor Sequence) - - (def: (each f fa) - (let [[head tail] (//.result fa)] - (//.pending [(f head) (each f tail)])))) - -(implementation: .public comonad - (CoMonad Sequence) - - (def: &functor ..functor) - - (def: out head) - - (def: (disjoint wa) - (let [[head tail] (//.result wa)] - (//.pending [wa (disjoint tail)])))) - -(syntax: .public (^sequence& [patterns (.form (<>.many .any)) - body .any - branches (<>.some .any)]) - (with_symbols [g!sequence] - (let [body+ (` (let [(~+ (|> patterns - (list#each (function (_ pattern) - (list (` [(~ pattern) (~ g!sequence)]) - (` ((~! //.result) (~ g!sequence)))))) - list#conjoint))] - (~ body)))] - (in (list& g!sequence body+ branches))))) diff --git a/stdlib/source/library/lux/data/collection/stream.lux b/stdlib/source/library/lux/data/collection/stream.lux new file mode 100644 index 000000000..f09423882 --- /dev/null +++ b/stdlib/source/library/lux/data/collection/stream.lux @@ -0,0 +1,140 @@ +(.module: + [library + [lux "*" + [abstract + [functor {"+" Functor}] + [comonad {"+" CoMonad}]] + [control + ["//" continuation {"+" Cont}] + ["<>" parser + ["<[0]>" code {"+" Parser}]]] + [macro {"+" with_symbols} + [syntax {"+" syntax:}] + ["[0]" code]] + [data + ["[0]" bit] + [collection + ["[0]" list ("[1]#[0]" monad)]]] + [math + [number + ["n" nat]]]]]) + +(type: .public (Stream a) + (Cont [a (Stream a)])) + +(def: .public (iterations step init) + (All (_ a b) + (-> (-> a [a b]) a (Stream b))) + (let [[next x] (step init)] + (//.pending [x (iterations step next)]))) + +(def: .public (repeated x) + (All (_ a) + (-> a (Stream a))) + (//.pending [x (repeated x)])) + +(def: .public (cycle [start next]) + (All (_ a) + (-> [a (List a)] (Stream a))) + (loop [head start + tail next] + (//.pending [head (case tail + {.#End} + (again start next) + + {.#Item head' tail'} + (again head' tail'))]))) + +(template [ ] + [(def: .public ( stream) + (All (_ a) (-> (Stream a) )) + (let [[head tail] (//.result stream)] + ))] + + [head a] + [tail (Stream a)] + ) + +(def: .public (item idx stream) + (All (_ a) (-> Nat (Stream a) a)) + (let [[head tail] (//.result stream)] + (case idx + 0 head + _ (item (-- idx) tail)))) + +(template [ ] + [(def: .public ( pred xs) + (All (_ a) + (-> (Stream a) (List a))) + (let [[x xs'] (//.result xs)] + (if ( ) + (list& x ( xs')) + (list)))) + + (def: .public ( pred xs) + (All (_ a) + (-> (Stream a) (Stream a))) + (let [[x xs'] (//.result xs)] + (if ( ) + ( xs') + xs)))] + + [while until (-> a Bit) (pred x) pred |>] + [first after Nat (n.= 0 pred) (-- pred) not] + ) + +(template [ ] + [(def: .public ( pred xs) + (All (_ a) + (-> (Stream a) [(List a) (Stream a)])) + (let [[x xs'] (//.result xs)] + (if + [(list) xs] + (let [[tail next] ( xs')] + [{.#Item [x tail]} next]))))] + + [split_when (-> a Bit) (pred x) pred] + [split_at Nat (n.= 0 pred) (-- pred)] + ) + +(def: .public (only predicate stream) + (All (_ a) (-> (-> a Bit) (Stream a) (Stream a))) + (let [[head tail] (//.result stream)] + (if (predicate head) + (//.pending [head (only predicate tail)]) + (only predicate tail)))) + +(def: .public (partition left? xs) + (All (_ a) (-> (-> a Bit) (Stream a) [(Stream a) (Stream a)])) + [(..only left? xs) + (..only (bit.complement left?) xs)]) + +(implementation: .public functor + (Functor Stream) + + (def: (each f fa) + (let [[head tail] (//.result fa)] + (//.pending [(f head) (each f tail)])))) + +(implementation: .public comonad + (CoMonad Stream) + + (def: &functor ..functor) + + (def: out head) + + (def: (disjoint wa) + (let [[head tail] (//.result wa)] + (//.pending [wa (disjoint tail)])))) + +(syntax: .public (^stream& [patterns (.form (<>.many .any)) + body .any + branches (<>.some .any)]) + (with_symbols [g!stream] + (let [body+ (` (let [(~+ (|> patterns + (list#each (function (_ pattern) + (list (` [(~ pattern) (~ g!stream)]) + (` ((~! //.result) (~ g!stream)))))) + list#conjoint))] + (~ body)))] + (in (list& g!stream body+ branches))))) diff --git a/stdlib/source/library/lux/documentation.lux b/stdlib/source/library/lux/documentation.lux index 5bd6f7f30..ec99259f9 100644 --- a/stdlib/source/library/lux/documentation.lux +++ b/stdlib/source/library/lux/documentation.lux @@ -18,7 +18,7 @@ [collection ["[0]" list ("[1]#[0]" monad mix monoid)] ["[0]" set {"+" Set}] - ["[0]" sequence {"+" Sequence}]] + ["[0]" stream {"+" Stream}]] [format ["md" markdown {"+" Markdown Block}]]] ["[0]" macro @@ -165,9 +165,9 @@ ) (def: type_variable_names - (Sequence Text) - (sequence.iterations (product.forked ++ parameter_type_name) - 0)) + (Stream Text) + (stream.iterations (product.forked ++ parameter_type_name) + 0)) (template [ ] [(def: ( id) @@ -193,9 +193,9 @@ _ (let [parameter_id (n.- (list.size type_function_arguments) parameter_id)] (|> type_variable_names - (sequence.only (function (_ var_name) - (not (list.member? text.equivalence type_function_arguments var_name)))) - (sequence.item parameter_id))))) + (stream.only (function (_ var_name) + (not (list.member? text.equivalence type_function_arguments var_name)))) + (stream.item parameter_id))))) type_function_name)) (def: (level_parameters offset level) -- cgit v1.2.3