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.
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,
NOTE (2022-07-09): Xcode finally added this functionality in Xcode 14, please see release notes here:
New Features in Xcode 14 Beta 3
When editing code, the Edit > Duplicate menu item and its corresponding keyboard shortcut now duplicate the selected text — or the line that currently contains the insertion point, if no text is selected. (8614499) (FB5618491)
import Foundation | |
/** | |
Coordinators are a design pattern that encourages decoupling view controllers such that they know as little as possible | |
about how they are presented, and don’t directly manipulate data or present other view controllers. Coordinators can be | |
“nested” such that child coordinators encapsulate different flows and present any from becoming too large. | |
- https://vimeo.com/144116310 | |
- http://khanlou.com/2015/10/coordinators-redux/ | |
- http://khanlou.com/2015/01/the-coordinator/ |
struct Coordinator { | |
let window: UIWindow | |
let navCtrl: UINavigationController? | |
func start() { | |
presentWelcomeScreen() | |
} | |
private func presentWelcomeScreen() { | |
let vc = WelcomeScreenViewController() // Instanciate from code, XIB, Storyboard, whatever your jam is |
// NOTE: This is now rolled up in a package and supports more scenarios: https://github.com/dsherret/using-statement | |
interface IDisposable { | |
dispose(); | |
} | |
function using<T extends IDisposable>(resource: T, func: (resource: T) => void) { | |
try { | |
func(resource); | |
} finally { |
import Random | |
random(0 ..< 10) // Int | |
random(1 ... 1000_000) // Int | |
random(-1000_000 ... 1000_000) // Int | |
random(Int.min ... Int.max) // Int | |
randomMax(UInt64.max) // UInt64 | |
random(0 ... UInt64.max) // UInt64 | |
random(1 ... UInt(10)) // UInt |
import Darwin | |
extension Int { | |
static func random() -> Int { | |
return Int(arc4random()) | |
} | |
static func random(range: Range<Int>) -> Int { | |
return Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex))) + range.startIndex | |
} |
// Thanks to http://www.labs.saachitech.com/2012/10/23/pdf-generation-using-uiprintpagerenderer | |
// Note: including images in the HTML won't work, see here: | |
// https://github.com/nyg/HTMLWithImagesToPDF | |
import UIKit | |
// 1. Create a print formatter | |
let html = "<b>Hello <i>World!</i></b>" | |
let fmt = UIMarkupTextPrintFormatter(markupText: html) |