Skip to content

Commit

Permalink
Added Stream effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Mar 6, 2024
1 parent a02af15 commit baf7ea1
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 3 deletions.
72 changes: 72 additions & 0 deletions stdlib/source/library/lux/control/stream.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.using
[library
[lux (.except)
[abstract
[functor (.only Functor)]
["[0]" monad (.only Monad)
["/" free]]]
["[0]" function]
[data
[collection
["[0]" list]
["[0]" sequence (.only Sequence)]]]
["[0]" type]]])

... https://en.wikipedia.org/wiki/Iterator
... https://en.wikipedia.org/wiki/Generator_(computer_programming)
... https://en.wikipedia.org/wiki/Stream_(computing)
(every (Yield it !)
[[it] (-> [] !)])

(the yield_functor
(for_any (_ it)
(Functor (Yield it)))
(implementation
(the (each on [input next])
[input (|>> next on)])))

(every .public (Stream yield it)
(/.Free (..Yield yield) it))

(the .public monad
(for_any (_ yield)
(Monad (Stream yield)))
(/.monad ..yield_functor))

(the .public functor
(for_any (_ yield)
(Functor (Stream yield)))
(its monad.functor ..monad))

(the .public (sequence it)
(for_any (_ yield it)
(-> (Stream yield it)
[(Sequence yield) it]))
(loop (next [yield (type.sharing [yield it]
(is (Stream yield it)
it)
(is (Sequence yield)
sequence.empty))
it it])
(when it
{/.#Pure it}
[yield it]

{/.#Impure [item after]}
(next (sequence.suffix item yield)
(after [])))))

(the .public (one it)
(for_any (_ it)
(-> it
(Stream it Any)))
{/.#Impure [[it] (function.constant {/.#Pure []})]})

(the .public many
(for_any (_ it)
(-> (List it)
(Stream it Any)))
(list.each' ..monad ..one))
7 changes: 7 additions & 0 deletions stdlib/source/library/lux/math/number/integer/08.lux
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
["[0]" equivalence]
["[0]" hash]
["[0]" order]]
[function
["[0]" predicate]]
[data
["[0]" binary
["[1]" \\injection]]]
Expand Down Expand Up @@ -76,6 +78,11 @@
(i64.or (nominal.reification minimum) it)
(i64.and (nominal.reification maximum) it)))

(the .public (valid? it)
(predicate.Predicate .Integer)
(not (or (//.> (nominal.reification maximum) it)
(//.< (nominal.reification minimum) it))))

(the .public of
(-> .Integer
Integer)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/source/test/lux/abstract/monad/free.lux
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

(the comparison
(Comparison (/.Free Stack))
(function (_ == left right)
(by (stack.equivalence ==) =
(function (_ = left right)
(by (stack.equivalence =) =
(..interpret left)
(..interpret right))))

Expand Down
4 changes: 3 additions & 1 deletion stdlib/source/test/lux/control.lux
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
["[1][0]" writer]
["[1][0]" pure]
["[1][0]" logic]
["[1][0]" pattern]])
["[1][0]" pattern]
["[1][0]" stream]])

(the .public test
Test
Expand All @@ -39,4 +40,5 @@
/pure.test
/logic.test
/pattern.test
/stream.test
))
77 changes: 77 additions & 0 deletions stdlib/source/test/lux/control/stream.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.using
[library
[lux (.except)
[abstract
[equivalence (.only Equivalence)]
[functor (.only Functor)]
["[0]" monad (.only Monad do)
["[1]T" \\test]]
["[0]" functor
["[1]T" \\test (.only Comparison)]]]
[data
["[0]" product]
[collection
["[0]" sequence (.only sequence)]]]
[math
["[0]" random]
[number
["[0]" natural]]]
[test
["_" property (.only Test)]]]]
[\\library
["[0]" /]])

(the (comparison yield_equivalence)
(for_any (_ yield)
(-> (Equivalence yield)
(Comparison (/.Stream yield))))
(function (_ value_equivalence reference it)
(let [[reference_yield reference_value] (/.sequence reference)
[it_yield it_value] (/.sequence it)]
(and (by (sequence.equivalence yield_equivalence) = reference_yield it_yield)
(by value_equivalence = reference_value reference_value)))))

(the .public test
Test
(<| (_.covering /._)
(do random.monad
[_0 random.natural
_1 random.natural
_2 random.natural])
(_.for [/.Stream])
(all _.and
(_.for [/.functor]
(functorT.spec (by /.monad in) (..comparison natural.equivalence) /.functor))
(_.for [/.monad]
(monadT.spec (by /.monad in) (..comparison natural.equivalence) /.monad))

(<| (_.for [/.sequence])
(all _.and
(_.coverage [/.one]
(|> (do /.monad
[_ (/.one _1)

_ (/.one _2)
_ (/.one _2)]
(in []))
/.sequence
product.left
(by (sequence.equivalence natural.equivalence) =
(sequence _1
_2 _2))))
(_.coverage [/.many]
(|> (do /.monad
[_ (/.many (list))
_ (/.many (list _1))
_ (/.many (list _2 _2))]
(in []))
/.sequence
product.left
(by (sequence.equivalence natural.equivalence) =
(sequence _1
_2 _2))))
))
)))

0 comments on commit baf7ea1

Please sign in to comment.