diff options
author | H.J <thechairman@thechairman.info> | 2024-06-17 15:39:21 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-17 15:39:21 -0400 |
commit | 32bc75cae2a0fceafea5c48c550888bc1d7c6016 (patch) | |
tree | 8cefbff840d1ce58ffcdf1957e4b52f7f77cc162 /aoc2017-gleam/src/helpers/state.gleam | |
parent | 413891726756aed407a2cfbceb4cf0e14bbcc982 (diff) | |
download | gleam_aoc-32bc75cae2a0fceafea5c48c550888bc1d7c6016.tar.gz gleam_aoc-32bc75cae2a0fceafea5c48c550888bc1d7c6016.zip |
gleam 2017 day 14
Diffstat (limited to 'aoc2017-gleam/src/helpers/state.gleam')
-rw-r--r-- | aoc2017-gleam/src/helpers/state.gleam | 55 |
1 files changed, 0 insertions, 55 deletions
diff --git a/aoc2017-gleam/src/helpers/state.gleam b/aoc2017-gleam/src/helpers/state.gleam deleted file mode 100644 index cbbad81..0000000 --- a/aoc2017-gleam/src/helpers/state.gleam +++ /dev/null @@ -1,55 +0,0 @@ -import gleam/erlang/process.{type Subject, Normal} -import gleam/option.{None} -import gleam/otp/actor.{type Next, Continue, Stop} -import gleam/set.{type Set} - -const timeout = 1000 - -pub type Message(k) { - Shutdown - Check(key: k, client: Subject(Bool)) - Add(key: k) - Drop(key: k) - Pop(client: Subject(Result(k, Nil))) -} - -fn handle_message(message: Message(k), set: Set(k)) -> Next(Message(k), Set(k)) { - case message { - Shutdown -> Stop(Normal) - Check(key, client) -> { - process.send(client, set.contains(set, key)) - Continue(set, None) - } - Add(key) -> Continue(set.insert(set, key), None) - Drop(key) -> Continue(set.delete(set, key), None) - Pop(client) -> { - case set.to_list(set) { - [next, ..] -> { - process.send(client, Ok(next)) - Continue(set.delete(set, next), None) - } - [] -> { - process.send(client, Error(Nil)) - Stop(Normal) - } - } - } - } -} - -pub fn start_actor(with: Set(a)) { - let assert Ok(actor) = actor.start(with, handle_message) - actor -} - -pub fn pop(actor) { - process.call(actor, Pop, timeout) -} - -pub fn check(actor, value) { - process.call(actor, Check(value, _), timeout) -} - -pub fn drop(actor, value) { - process.send(actor, Drop(value)) -} |