-1

In a boolean system, say you have a while loop going on. Then you might have this:

if (just_did_x) {
  do_something()
}

To prevent it calling do_something() more than once, you might do:

if (just_did_x) {
  do_something()
  just_did_x = false
}

But the problem with that is, you might reference that boolean in multiple conditions:

if (just_did_x) {
  do_something()
}

if (just_did_x && just_did_y) {
  do_something2()
}

if (just_did_x && just_did_z) {
  do_something3()
}

...

So you can set just_did_x = false in each of those if blocks, but that is extra processing / duplication. It also would make the code error in this case:

if (just_did_x) {
  do_something()
  just_did_x = false
}

// just_did_x == false now, so this doesn't evaluate.
if (just_did_x && just_did_y) {
  do_something2()
  just_did_x = false
}

// same here.
if (just_did_x && just_did_z) {
  do_something3()
  just_did_x = false
}

...

This means you need to queue setting the boolean values until after all conditions have been evaluated. This just adds complexity. In addition, you can't set the boolean just_did_x to false until just_did_y and just_did_z have become true (assuming they work like a queue). So now we need even more boolean variables.

if (just_did_x) {
  do_something()
  just_did_x_condition = true
}

if (just_did_x && just_did_y) {
  do_something2()
  just_did_x_just_did_y_condition = true
}

if (just_did_x && just_did_z) {
  do_something3()
  just_did_x_just_did_z_condition = true
}

if (just_did_x_condition
  && just_did_x_just_did_y_condition
  && just_did_x_just_did_z_condition) {
    just_did_x = false
    just_did_y = false
    just_did_z = false
  }

But that isn't quite right because you want to run do_something() after every "event" of just_did_x. So I'm starting to get the sense the boolean system can't replicate the event system. But maybe it's still possible!

With events, you don't need to set and unset any values, the event lives while it is being handled, and then goes away. (There is probably some equivalent to the boolean thing going on in the background / with garbage collection, but I don't know about how that works). With events, you just handle the event:

handleEvent(just_did_x, function(event){
  do_something()
})

With listening to multiple events, that might look like the following:

handleEvent(just_did_x, just_did_y, function(event){
  do_something2()
})

handleEvent(just_did_x, just_did_z, function(event){
  do_something3()
})

...

Basically what I'm wondering is if there is a system for working with boolean variables that mimics the event system. Given that something triggers the while loop to evaluate the conditions in the boolean system (either through an infinite while loop or an actual system event), how to structure the boolean system so it works just like events.

By "boolean system" I just mean a set of boolean variables used to track all the state mimicking events.

  • 1
    Unclear. But read about continuation passing style – Basile Starynkevitch Jul 1 '18 at 19:20
  • Wondering how I could clarify it, I tried providing example code snippets to demonstrate. – Lance Pollard Jul 1 '18 at 19:24
  • With "boolean system", do you have something like a finite automata / state machine in mind? – Doc Brown Jul 1 '18 at 19:47
  • Just added about boolean systems, I just mean a set of boolean variables used to track all the state mimicking events. I am trying to create a nondeterministic finite automata (NFA) by converting everything into boolean variables, but I think what I've outlined above is probably pretty different from an automaton, not sure. – Lance Pollard Jul 1 '18 at 19:48
0

It is unclear what this “boolean system” is. But you can avoid those problems by deferring to update these variables. For example, we might have an outer loop

previous_state = initial_state
while true:
  new_state = handle_events(previous_state)
  previous_state = new_state

where handle_events() does not mutate the previous state it was given. If the states can be merged (e.g. because they are just booleans that indicate something happend once or more times) then you could also loop over multiple event handlers:

previous_state = initial_state
while true:
  new_state = nothing_happened
  for handle_event in all_event_handlers:
     new_state |= handle_event(previous_state)
  previous_state = new_state

It is not necessary to extract a handle_event() function, but separating the deferred update from all your conditions can make it easier to write clear code, and can make it easier to use type system features like C++ const references that prevent accidental changes to the previous state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.