Skip to content

Instantly share code, notes, and snippets.

@Koshub
Koshub / States-v3.md
Created May 9, 2020 — forked from andymatuschak/States-v3.md
A composable pattern for pure state machines with effects (draft v3)
View States-v3.md

A composable pattern for pure state machines with effects

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,

View NSTimer+Closure.swift
extension NSTimer {
/**
Creates and schedules a one-time `NSTimer` instance.
:param: delay The delay before execution.
:param: handler A closure to execute after `delay`.
:returns: The newly-created `NSTimer` instance.
*/
class func schedule(#delay: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
View .gitignore
#########################
# .gitignore file for Xcode4 / OS X Source projects
#
# Version 2.0
# For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects
#
# 2013 updates:
# - fixed the broken "save personal Schemes"
#
# NB: if you are storing "built" products, this WILL NOT WORK,