Releases: 0xLet/E.num
Releases · 0xLet/E.num
Variable.array([...]).flatten
Unflattened Variable Output
(lldb) po output
▿ Variable
▿ array : 2 elements
▿ 0 : Variable
- string : "First"
▿ 1 : Variable
▿ array : 2 elements
- 0 : E.Variable.void
▿ 1 : Variable
▿ array : 2 elements
▿ 0 : Variable
▿ array : 3 elements
▿ 0 : Variable
▿ array : 1 element
- 0 : E.Variable.void
▿ 1 : Variable
▿ array : 1 element
- 0 : E.Variable.void
▿ 2 : Variable
▿ array : 1 element
- 0 : E.Variable.void
▿ 1 : Variable
▿ array : 2 elements
▿ 0 : Variable
- string : "Link"
▿ 1 : Variable
▿ array : 2 elements
▿ 0 : Variable
- string : "Last"
▿ 1 : Variable
▿ array : 1 element
▿ 0 : Variable
- string : "Last !!!"
Flattened Variable Output
(lldb) po output.flatten
▿ Variable
▿ array : 8 elements
▿ 0 : Variable
- string : "First"
- 1 : E.Variable.void
- 2 : E.Variable.void
- 3 : E.Variable.void
- 4 : E.Variable.void
▿ 5 : Variable
- string : "Link"
▿ 6 : Variable
- string : "Last"
▿ 7 : Variable
- string : "Last !!!"
Simple State
Simple State
enum State
public indirect enum State {
case cyclic(action: StateAction, to: State, while: () -> Bool)
case some(with: StateResult)
case transition(to: State, with: StateResult)
}
enum StateResult
public indirect enum StateResult {
case none
case transition(State)
case some(action: StateAction)
case condition(true: StateResult, false: StateResult, statement: () -> Bool)
}
enum StateAction
public enum StateAction {
case none
case some(Function)
}
Examples
Retry Login
let loggedOut = State.some(with: .some(action: .some(.void({
print("Logged Out")
XCTAssert(false)
}))))
let loggedIn = State.some(with: .some(action: .some(.void({
print("Logged In")
XCTAssert(true)
}))))
let attemptLogin = State.some(with: .condition(true: .transition(loggedIn),
false: .transition(loggedOut),
statement: { () -> Bool in
print("Attempting to login without retry...")
sleep(3)
return true
}))
let loginFailed = State.transition(to: attemptLogin, with: .some(action: .some(.void({
print("Login Failed!")
}))))
let attemptRetryLogin = State.some(with: .condition(true: .transition(loggedIn),
false: .transition(loginFailed),
statement: { () -> Bool in
print("Attempting to login with retry...")
sleep(3)
return false
}))
attemptRetryLogin.run()
Cyclic
var value: Variable = .int(0)
let maxValue: Variable = .int(256)
let incValue: Function = .void {
value = value.update { .int($0 + 1) }
}
let boolFunc: Function = .out {
.bool(value.value() ?? 0 < maxValue.value() ?? 0)
}
let finState = State.some(with: .some(action: .some(.void({
print("DONE!")
XCTAssert(true)
}))))
let cyclicState = State.cyclic(action: .some(incValue), to: finState) {
boolFunc()?.value() ?? false
}
XCTAssertEqual(value.value(), 0)
cyclicState.run()
XCTAssertEqual(value.value(), 256)
import E
Control
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)
ExpressibleByLiteral
Literals
extension Variable: ExpressibleByBooleanLiteral { ... }
extension Variable: ExpressibleByIntegerLiteral { ... }
extension Variable: ExpressibleByFloatLiteral { ... }
extension Variable: ExpressibleByStringLiteral { ... }
extension Variable: ExpressibleByArrayLiteral { ... }
extension Variable: ExpressibleByDictionaryLiteral { ... }
Example
let text: Variable = "Hello, World!"
XCTAssertEqual(text, "Hello, World!")
Variables and Functions
Variables
case void
case bool(Bool)
case int(Int)
case float(Float)
case double(Double)
case string(String)
case set(Set<Variable>)
case array([Variable])
case dictionary([Variable: Variable])
Variable Example
let text: Variable = .string("Hello, World!")
let list: Variable = .array(
[
.bool(false),
.string("False"),
.int(27)
]
)
// ...
if case .string(let value) = text {
print("String: \(value)")
}
if case .array(let value) = list,
let lastValue = value.last,
case .int(let number) = lastValue {
print(number * 99)
}
Functions
case void(() -> ())
case `in`((Variable) -> ())
case out(() -> Variable)
case `inout`((Variable) -> Variable)
Function Example
let printString = Function.in { stringValue in
guard case .string(let value) = stringValue else {
return
}
print(value)
}
// ...
printString(.string("Hello, World..."))