Skip to content

Control

Compare
Choose a tag to compare
@0xLeif 0xLeif released this 29 Sep 19:31
· 16 commits to main since this release

Controls

case `if`(Bool, Function)
case `else`(Bool, Function)
case ifElse(Bool, Function, Function)
case loop(ClosedRange<Int>, Function)
case forEach([Variable], Function)
case forever(Function)

Variables

/// Update the Variable's Value
/// - Returns: A new Variable with the type of T
func update<T>(_ closure: (T) -> Variable) -> Self

/// Modify the Variable to be any type of Variable
/// - Returns: A new Variable of any type
func modify<T>(_ closure: (T?) -> Variable) -> Self

func value<T>(as type: T.Type? = nil) -> T?

Example

var count: Variable = .int(0)

let stillIntValue = count
    .update { .string($0 ?? "Hello, World!") } // returns .int(0)
    .update { .int($0 + 27) }

let defaultedStringValue = count.modify { value in
    .string(value ?? "Hello, World!")
}

XCTAssertEqual(count, 0)
XCTAssertEqual(stillIntValue, 27)
XCTAssertEqual(defaultedStringValue, "Hello, World!")

Control.loop(0 ... 5,
             .in { index in
                count = count.update { value in
                    .int(value + (index.value() ?? 0))
                }
             })
    .run()

XCTAssertEqual(count, 15)