Hone logo
Hone
Problems

Implementing a Reusable Boolean Toggle Mechanism

Many applications frequently need to manage simple on/off, visible/hidden, or active/inactive states. Instead of manually creating and updating a boolean state each time, a reusable mechanism can simplify development and improve consistency across your codebase. This challenge asks you to design and implement such a mechanism, abstracting the common logic for managing a boolean state.

Problem Description

Your task is to create a function (let's call it createToggle for a language-agnostic context) that encapsulates the logic for managing a single boolean state. This function should be highly reusable and provide a clear interface for interacting with the boolean value.

Specifically, your createToggle function should:

  1. Manage a Boolean State: Internally, it must hold and update a single boolean value (true or false).
  2. Accept an Initial Value: It should accept an optional argument, initialValue, which will set the starting state of the toggle.
  3. Default to False: If no initialValue is provided, the toggle's state should default to false.
  4. Provide Current State: It must expose a way to retrieve the current boolean value at any time.
  5. Provide a Toggle Function: It must expose a function that, when called, inverts the current boolean state (if true, it becomes false; if false, it becomes true).
  6. Provide Explicit Setters: It should also expose two additional functions: one to explicitly set the state to true and another to explicitly set the state to false, regardless of the current state.

Expected Behavior:

  • Calling the toggle function repeatedly should reliably alternate the state between true and false.
  • Setting an initial value should correctly reflect the starting state.
  • Explicitly setting the state to true or false should override any previous state and immediately update the value.
  • The mechanism should maintain its state independently between different instances created by createToggle.

Edge Cases:

  • What happens if createToggle is called without any arguments? The state should begin as false.
  • What if the initial value is explicitly true? The state should begin as true.
  • What if a toggle operation is performed immediately after an explicit setTrue or setFalse? The toggle should correctly invert the newly set value.

Examples

Example 1: Basic usage with default initial value

Input:
// Initialize the toggle mechanism
toggleMechanism = createToggle()

// Read initial state
state1 = toggleMechanism.getValue()

// Toggle the state
toggleMechanism.toggle()

// Read new state
state2 = toggleMechanism.getValue()

// Toggle again
toggleMechanism.toggle()

// Read final state
state3 = toggleMechanism.getValue()
Output:
state1: false
state2: true
state3: false

Explanation: The createToggle function is called without an initial value, so it defaults to false. The first toggle() call flips it to true, and the second toggle() call flips it back to false.

Example 2: Usage with an explicit initial value and explicit setters

Input:
// Initialize the toggle mechanism with an initial value of true
toggleMechanism = createToggle(true)

// Read initial state
state1 = toggleMechanism.getValue()

// Set state to false explicitly
toggleMechanism.setFalse()

// Read new state
state2 = toggleMechanism.getValue()

// Toggle the state
toggleMechanism.toggle()

// Read new state
state3 = toggleMechanism.getValue()

// Set state to true explicitly
toggleMechanism.setTrue()

// Read final state
state4 = toggleMechanism.getValue()
Output:
state1: true
state2: false
state3: true
state4: true

Explanation: The toggle starts as true. setFalse() changes it to false. toggle() then flips it to true. Finally, setTrue() keeps it true.

Constraints

  • The state managed by createToggle must always be a strict boolean (true or false).
  • The createToggle function should encapsulate its state; external code should not directly modify the internal state variable.
  • The operations (getting the value, toggling, setting true, setting false) should complete in constant time (O(1)).
  • The solution should be designed for reusability, meaning it should be possible to create multiple independent toggle mechanisms.

Notes

  • Consider how to return the current state and the control functions. Depending on your chosen language, this could be an object, a tuple/array, or a custom data structure.
  • The essence of this challenge lies in managing mutable state within a functional pattern, similar to how React's useState or useReducer hooks operate, but simplified for a single boolean.
  • Focus on clarity, correctness, and reusability of the logic rather than a specific UI framework implementation. Your pseudocode should clearly define the inputs and outputs of createToggle and its returned functions.
Loading editor...
plaintext