Created
June 15, 2018 15:05
-
-
Save richy486/9acdc66e7063f138d058d402f73b0167 to your computer and use it in GitHub Desktop.
Absolute Trivial State Derivative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Action: Int { | |
case buttonDown | |
} | |
struct State { | |
var counter: Int = 0 | |
} | |
struct View { | |
var height: Float | |
var width: Float | |
} | |
// s = f(s,a) = s + (a * 1) | |
func updateState(state: State, action: Action) -> State { | |
return State(counter: state.counter + (action == .buttonDown ? 1 : 0)) | |
} | |
// v = f(s) = s * 10 | |
func updateView(state: State) -> View { | |
return View(height: 10.0 * Float(state.counter), width: 100.0) | |
} | |
var state = State() | |
state = updateState(state: state, action: .buttonDown) | |
var view = updateView(state: state) | |
print("1) initial") | |
print("state: \(state)") | |
print("view: \(view)") | |
// Dirivitives | |
struct State_d { | |
var counter: Int = 0 | |
} | |
struct View_d { | |
var height: Float | |
var width: Float | |
} | |
// s' = f'(s,a) = 1 | |
func updateState_d(state: State, action: Action) -> State_d { | |
return State_d(counter: 1) | |
} | |
// v' = f'(s) = 10 | |
func updateView_d(state: State_d) -> View_d { | |
return View_d(height: 10.0, width: 0.0) | |
} | |
let state_d = updateState_d(state: state, action: .buttonDown) | |
let view_d = updateView_d(state: state_d) | |
view.height += view_d.height | |
view.width += view_d.width | |
print("2) after differential") | |
print("view: \(view)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment