Skip to content

Commit

Permalink
Adapting to new List type (part 15).
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Apr 12, 2024
1 parent a99fd57 commit 50e06c9
Show file tree
Hide file tree
Showing 53 changed files with 1,143 additions and 1,138 deletions.
38 changes: 19 additions & 19 deletions stdlib/source/library/lux/color/scheme.lux
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[lux (.except)
[data
[collection
["[0]" stack (.use "[1]#[0]" functor)]]]
["[0]" list (.use "[1]#[0]" functor)]]]
[math
[number
["i" integer]
Expand All @@ -22,13 +22,13 @@
(-> Decimal
Decimal)
(if (d.> +1.0 it)
(d.% +1.0 it)
(d.% +1.0 it)

(d.< +0.0 it)
(|> it (d.% +1.0) (d.+ +1.0))
(d.< +0.0 it)
(|> it (d.% +1.0) (d.+ +1.0))

... else
it))
... else
it))

(template.with [<name> <1> <2>]
[(`` (the .public (<name> it)
Expand Down Expand Up @@ -72,7 +72,7 @@
... https://en.wikipedia.org/wiki/Color_scheme
(every .public Scheme
(-> Spread Natural RGB
(Stack RGB)))
(List RGB)))

(the .public (analogous spread variations it)
Scheme
Expand All @@ -81,11 +81,11 @@
saturation (its hsl.#saturation it)
luminance (its hsl.#luminance it)
spread (..ratio spread)]
(stack#each (function (_ idx)
(hsl.rgb (hsl.hsl (|> idx ++ .integer i.decimal (d.* spread) (d.+ hue) ..ratio)
saturation
luminance)))
(stack.indices variations))))
(list#each (function (_ idx)
(hsl.rgb (hsl.hsl (|> idx ++ .integer i.decimal (d.* spread) (d.+ hue) ..ratio)
saturation
luminance)))
(list.indices variations))))

(the .public (monochromatic spread variations it)
Scheme
Expand All @@ -94,10 +94,10 @@
saturation (hsb.saturation it)
brightness (hsb.brightness it)
spread (..ratio spread)]
(|> (stack.indices variations)
(stack#each (|>> ++ .integer i.decimal
(d.* spread)
(d.+ brightness)
..ratio
(hsb.hsb hue saturation)
hsb.rgb)))))
(|> (list.indices variations)
(list#each (|>> ++ .integer i.decimal
(d.* spread)
(d.+ brightness)
..ratio
(hsb.hsb hue saturation)
hsb.rgb)))))
64 changes: 33 additions & 31 deletions stdlib/source/library/lux/concurrency/actor.lux
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
[predicate (.only Predicate)]]
[data
["[0]" bit]
["[0]" product]]
["[0]" product]
[collection
["[0]" list (.use "[1]#[0]" monoid)]]]
[macro
["[0]" local]
["[0]" template]]
Expand All @@ -34,7 +36,7 @@
[(-> state (Actor state)
(Future (Try state)))])
<Obituary> (template.macro (_ Actor state)
[[Text state (Stack (<Mail> Actor state))]])
[[Text state (List (<Mail> Actor state))]])
<Mailbox> (template.macro (_ Actor state)
[(Rec Mailbox
[(Future [(<Mail> Actor state) Mailbox])
Expand All @@ -44,16 +46,16 @@
(-> (Rec Mailbox
[(Future [it Mailbox])
(Resolver [it Mailbox])])
(IO (Stack it))))
(IO (List it))))
(do [! io.monad]
[current (future.value read)]
(when current
{.#Some [head tail]}
(by ! each (|>> {.#Top head})
(by ! each (list#composite (list head))
(pending tail))

{.#None}
(in {.#Empty}))))
(in (list)))))

(nominal.every .public (Actor state)
(Record
Expand Down Expand Up @@ -91,7 +93,7 @@
(io.value
(do io.monad
[pending (..pending tail)]
(resolve [error state {.#Top head pending}])))
(resolve [error state (list#composite (list head) pending)])))
(in [])))

{try.#Success state'}
Expand Down Expand Up @@ -135,25 +137,25 @@
(do [! io.monad]
[alive? (..alive? actor)]
(if alive?
(let [entry [mail (future.future [])]]
(do !
[|mailbox|&resolve (atom.read! (its #mailbox (nominal.reification actor)))]
(loop (again [[|mailbox| resolve] |mailbox|&resolve])
(do !
[|mailbox| (future.value |mailbox|)]
(when |mailbox|
{.#None}
(do !
[resolved? (resolve entry)]
(if resolved?
(do !
[_ (atom.write! (product.right entry) (its #mailbox (nominal.reification actor)))]
(in {try.#Success []}))
(again |mailbox|&resolve)))

{.#Some [_ |mailbox|']}
(again |mailbox|'))))))
(in (exception.except ..dead [])))))
(let [entry [mail (future.future [])]]
(do !
[|mailbox|&resolve (atom.read! (its #mailbox (nominal.reification actor)))]
(loop (again [[|mailbox| resolve] |mailbox|&resolve])
(do !
[|mailbox| (future.value |mailbox|)]
(when |mailbox|
{.#None}
(do !
[resolved? (resolve entry)]
(if resolved?
(do !
[_ (atom.write! (product.right entry) (its #mailbox (nominal.reification actor)))]
(in {try.#Success []}))
(again |mailbox|&resolve)))
{.#Some [_ |mailbox|']}
(again |mailbox|'))))))
(in (exception.except ..dead [])))))

(every .public (Message state it)
(-> state (Actor state)
Expand Down Expand Up @@ -231,10 +233,10 @@
(do [! io.monad]
[continue? (atom.read! signal)]
(if continue?
(|> actor
(..mail! (action event stop))
(by ! each try.maybe))
(in {.#None}))))
(|> actor
(..mail! (action event stop))
(by ! each try.maybe))
(in {.#None}))))
channel)))

... The following behavior and messages allow Lux's actors to behave like Clojure's agents.
Expand All @@ -250,8 +252,8 @@
(do (try.with future.monad)
[after (mail before actor)]
(if (? after)
(in after)
(future#in (exception.except ..invalid []))))))
(in after)
(future#in (exception.except ..invalid []))))))

(the .public state
(for_any (_ state)
Expand Down
64 changes: 32 additions & 32 deletions stdlib/source/library/lux/concurrency/event.lux
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[text
["%" \\injection]]
[collection
["[0]" stack]]]
["[0]" list (.use "[1]#[0]" monoid)]]]
[math
[number
["n" natural]]]
Expand Down Expand Up @@ -63,18 +63,18 @@

(the .public (loop name)
(-> Text [Scheduler Loop])
(let [state (is (Atom [Bit (Stack Event)])
(atom.atom [false (stack)]))]
(let [state (is (Atom [Bit (List Event)])
(atom.atom [false (list)]))]
[(is Scheduler
(function (schedule! milli_seconds action)
(do io.monad
[now instant.now
_ (atom.update! (function (_ [stated? events])
[stated?
(stack.partial [#when (instant.after (duration.of_millis (.integer milli_seconds))
now)
#what action]
events)])
(list#composite (list [#when (instant.after (duration.of_millis (.integer milli_seconds))
now)
#what action])
events)])
state)]
(in []))))
(is Loop
Expand All @@ -83,29 +83,29 @@
[started?,events (atom.read! state)
.let [[started? events] started?,events]]
(if started?
(in (exception.except ..already_started [name]))
(do !
[swapped? (atom.compare_and_swap! started?,events [true events] state)]
(if swapped?
(.loop (again [events_processed 0])
(do !
[started?,events (atom.read! state)
.let [[started? events] started?,events]]
(when events
... And... we're done!
{.#Empty}
(in {try.#Success events_processed})

_
(in (exception.except ..already_started [name]))
(do !
[swapped? (atom.compare_and_swap! started?,events [true events] state)]
(if swapped?
(.loop (again [events_processed 0])
(do !
[now instant.now
.let [[pending ready] (stack.partition (function (_ thread)
(instant#< (its #when thread) now))
events)]
swapped? (atom.compare_and_swap! started?,events [started? pending] state)]
(if swapped?
(do [! (try.with !)]
[_ (stack.each' ! (|>> (its #what) (..execute! name) io.io) ready)]
(again (n.+ (stack.size ready) events_processed)))
(again events_processed))))))
(retry! [])))))))]))
[started?,events (atom.read! state)
.let [[started? events] started?,events]]
(when events
... And... we're done!
(list)
(in {try.#Success events_processed})

_
(do !
[now instant.now
.let [[pending ready] (list.partition (function (_ thread)
(instant#< (its #when thread) now))
events)]
swapped? (atom.compare_and_swap! started?,events [started? pending] state)]
(if swapped?
(do [! (try.with !)]
[_ (list.each' ! (|>> (its #what) (..execute! name) io.io) ready)]
(again (n.+ (list.size ready) events_processed)))
(again events_processed))))))
(retry! [])))))))]))
Loading

0 comments on commit 50e06c9

Please sign in to comment.