Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state package #862

Open
emil14 opened this issue Feb 3, 2025 · 0 comments
Open

state package #862

emil14 opened this issue Feb 3, 2025 · 0 comments
Labels

Comments

@emil14
Copy link
Collaborator

emil14 commented Feb 3, 2025

Implement state package with 3 public components:

  1. state.State - main component, must be implemented as runtime-function
  2. state.Bool - little helper that wraps State and implements boolean routing
  3. state.Accumulator - this one needs to be moved from builtin/streams.neva (and should be imported from there), it's used by Reduce (in the future it will be moved to streams.Reduce)
// State maintains the value of type T.
// It waits for message from `init` and `get`, then sends initial value to `res`.
// After that loop starts - wait for `set` and `get`, send latest value to `res`, repeat.
// It only receives `init` once, subsequent messages to that port will be blocked.
#extern(state)
pub def State<T>(init T, set T, get T) (res T)

// Bool is a helper-component for routing based on a boolean state.
// It wraps `State<bool>` and has the same semantics except conditional output.
pub def Bool(init bool, set bool, get bool) (then bool, else bool) {
    state State<bool>
    ---
    :init -> state:init
    :set -> state:set
    :get -> state:get
    state:res -> switch {
        true -> :then
        _ -> :else
    }
}

// Accumulator maintains the current state of the reduction.
// It updates its value with each new input and outputs the final result when last is true.
#extern(accumulator)
pub def Accumulator<T>(init T, upd T, last bool) (cur T, res T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

1 participant