Skip to content

Commit

Permalink
Added Filter effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Mar 10, 2024
1 parent cd40bb6 commit 8084df9
Show file tree
Hide file tree
Showing 23 changed files with 511 additions and 478 deletions.
24 changes: 24 additions & 0 deletions stdlib/source/library/lux/abstract/functor/effect.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
... 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 Or or)
["[0]" algorithm]]]
["[0]" // (.only Functor)])

(every .public (Effect input output !)
[input (-> output !)])

(the .public functor
(for_any (_ input output)
(Functor (Effect input output)))
(implementation
(the (each on [input next])
[input (|>> next on)])))

(the .public Or
algorithm.Sum)

(the .public or
//.sum)
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
["[0]" constant (.only)
[pool (.only Resource)]]
[encoding
["[0]" unsigned]
[name
["[0]" external]
["[0]" internal]]]]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
[target
[jvm
["_" bytecode (.only Bytecode)]
["[0]" type]
[encoding
["[0]" unsigned]]]]]]
["[0]" type]]]]]
["[0]" //
["[1][0]" runtime (.only Operation)]
["[1][0]" value]
Expand Down
107 changes: 107 additions & 0 deletions stdlib/source/library/lux/control/filter.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
... 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 only)
[abstract
[functor (.only Functor)
["//" effect]]
["[0]" monad (.only Monad do)
["/" free]]]
["[0]" function (.only)
[predicate (.only Predicate)]]
[data
[collection
["[0]" list]
["[0]" sequence (.only Sequence)]]]
[macro
["[0]" expansion]]]]
[//
["[0]" stream (.only Stream)]])

(the Yield stream.Effect)

(every (Read it)
(//.Effect [] it))

... https://en.wikipedia.org/wiki/Filter_(software)
(every .public (Effect read yield)
(all //.Or
(Read read)
(Yield yield)
))

(every .public (Filter read yield)
(/.Free (Effect read yield)))

(the .public monad
(for_any (_ read yield)
(Monad (Filter read yield)))
(/.monad (all //.or
//.functor
//.functor
)))

(expansion.let [#Read (these 0 #0)
#Yield (these 0 #1)]
(these (the .public read
(for_any (_ read yield)
(Filter read yield read))
{/.#Impure {#Read [[] (|>> {/.#Pure})]}})

(the .public (yield it)
(for_any (_ read yield)
(-> yield
(Filter read yield Any)))
{/.#Impure {#Yield [[it] (function.constant {/.#Pure []})]}})

(the .public (stream filter it)
(for_any (_ read yield it)
(-> (Filter read yield it) (Stream read it)
(Stream yield it)))
(when filter
{/.#Impure {#Read [_ after_read]}}
(when it
{/.#Impure [yield after_stream]}
(stream (after_read yield) (after_stream []))

{/.#Pure it}
{/.#Pure it})

{/.#Impure {#Yield [item after]}}
{/.#Impure [item (function (_ input)
(stream (after input) it))]}

{/.#Pure it}
{/.#Pure it})))
)

(the .public identity
(for_any (_ it)
(Filter it it Any))
(loop (next [_ []])
(do ..monad
[it ..read
_ (..yield it)]
(next []))))

(the .public (each on)
(for_any (_ read yield)
(-> (-> read yield)
(Filter read yield Any)))
(do ..monad
[it ..read
_ (..yield (on it))]
(each on)))

(the .public (only ?)
(for_any (_ it)
(-> (Predicate it)
(Filter it it Any)))
(do ..monad
[it ..read
_ (if (? it)
(..yield it)
(in []))]
(only ?)))
83 changes: 48 additions & 35 deletions stdlib/source/library/lux/control/stream.lux
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,74 @@

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

... 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] (-> [] !)])
(every .public (Effect it)
(//.Effect [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))
(every .public (Stream yield)
(/.Free (..Effect yield)))

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

(the .public functor
(for_any (_ yield)
(Functor (Stream yield)))
(its monad.functor ..monad))
(the .public (each on it)
(for_any (_ _0 _1 state it)
(-> (-> _0 _1) (Stream _0 it)
(Stream _1 it)))
(when it
{/.#Impure [item after]}
{/.#Impure [(on item) (|>> after (each on))]}

{/.#Pure it}
{/.#Pure it}))

(the .public (only ? it)
(for_any (_ yield it)
(-> (Predicate yield) (Stream yield it)
(Stream yield it)))
(when it
{/.#Impure [item after]}
(if (? item)
{/.#Impure [item (|>> after (only ?))]}
(only ? (after [])))

{/.#Pure it}
{/.#Pure it}))

(the .public (mix step state it)
(for_any (_ yield state it)
(-> (-> yield state state) state (Stream yield it)
[state it]))
(when it
{/.#Impure [item after]}
(mix step (step item state) (after []))

{/.#Pure it}
[state it]))

(the .public (sequence it)
(the .public sequence
(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 [])))))
(mix sequence.suffix
sequence.empty))

(the .public (one it)
(for_any (_ it)
Expand Down
7 changes: 7 additions & 0 deletions stdlib/source/library/lux/data/collection/sequence.lux
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
[data
["[0]" product]
[collection
["[0]" list (.use "[1]#[0]" mix functor monoid)]
["[0]" stack (.use "[1]#[0]" mix functor monoid)]
["[0]" array
["[1]" \\unsafe (.only Array)]]]]
Expand Down Expand Up @@ -405,6 +406,12 @@
(Sequence it)))
(stack#mix ..suffix ..empty))

(the .public of_list
(for_any (_ it)
(-> (List it)
(Sequence it)))
(list#mix ..suffix ..empty))

(the .public (member? equivalence sequence val)
(for_any (_ it)
(-> (Equivalence it) (Sequence it) it
Expand Down
7 changes: 7 additions & 0 deletions stdlib/source/library/lux/math/number/natural/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 @@ -60,6 +62,11 @@
i64.mask
nominal.abstraction))

(the .public valid?
(predicate.Predicate .Natural)
(|>> (//.> (nominal.reification maximum))
not))

(the .public of
(-> .Natural
Natural)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
[number
["n" natural]
[natural
["[0]" /16]]]]]]
[///
[encoding
["[0]" unsigned]]])
["[0]" /16]]]]]])

(every .public Entry
(Record
Expand Down
Loading

0 comments on commit 8084df9

Please sign in to comment.