This shows the execution order given JavaScript's Call Stack, Event Loop, and any asynchronous APIs provided in the JS execution environment (in this example; Web APIs in a Browser environment)
Given the code
| /* | |
| erica sadun ericasadun.com | |
| Math Types | |
| Swift2 | |
| */ | |
| import Foundation | |
| import QuartzCore |
State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?
There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.
Here I present a composable pattern for pure state machiness with effects,
Shortened URL for this gist: https://git.io/v68XM
GitHub repository: ES6 in Motion
Arrow Functions
It's now here, in The Programmer's Compendium. The content is the same as before, but being part of the compendium means that it's actively maintained.
| public struct Event { | |
| public internal(set) var timeStamp: Date | |
| public internal(set) var eventTag: String | |
| public init(timeStamp: Date, tag: String) { | |
| self.timeStamp = timeStamp | |
| self.eventTag = tag | |
| } | |
| } |
| extension UIImageView { | |
| func topAlignmentAndAspectFit(to view: UIView) { | |
| self.contentMode = .scaleAspectFill | |
| self.translatesAutoresizingMaskIntoConstraints = false | |
| view.addSubview(self) | |
| self.addConstraints( | |
| [NSLayoutConstraint(item: self, | |
| attribute: .height, | |
| relatedBy: .equal, |