Skip to content

Instantly share code, notes, and snippets.

@Schaltfehler
Schaltfehler / States-v3.md
Last active May 11, 2020 03:27 — forked from andymatuschak/States-v3.md
A composable pattern for pure state machines with effects (draft v3) #swift

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,

@Schaltfehler
Schaltfehler / MulticastDelegateV2.swift
Created May 25, 2018 05:39 — forked from klemenzagar91/MulticastDelegateV2.swift
Multicast Delegate with hash table
class MulticastDelegate <T> {
private let delegates: NSHashTable<AnyObject> = NSHashTable.weakObjects()
func add(delegate: T) {
delegates.add(delegate as AnyObject)
}
func remove(delegate: T) {
for oneDelegate in delegates.allObjects.reversed() {
if oneDelegate === delegate as AnyObject {