Author: Chris Lattner
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,
// Taken from the commercial iOS PDF framework http://pspdfkit.com. | |
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved. | |
// Licensed under MIT (http://opensource.org/licenses/MIT) | |
// | |
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it. | |
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit. | |
#import <objc/runtime.h> | |
#import <objc/message.h> |
- (UIImage *)dynamicImage | |
{ | |
UITraitCollection *const baseTraitCollection = /* an existing trait collection */; | |
UITraitCollection *const lightTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]]]; | |
UITraitCollection *const purelyDarkTraitCollection = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]; | |
UITraitCollection *const darkTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, purelyDarkTraitCollection]]; | |
__block UIImage *lightImage; | |
[lightTraitCollection performAsCurrentTraitCollection:^{ | |
lightImage = /* draw image */; |
//Swift 4 modifications for this gist: https://gist.github.com/krooked/9c4c81557fc85bc61e51c0b4f3301e6e | |
import Foundation | |
import UIKit | |
extension UIImage { | |
func cropImageByAlpha() -> UIImage { | |
let cgImage = self.cgImage | |
let context = createARGBBitmapContextFromImage(inImage: cgImage!) | |
let height = cgImage!.height | |
let width = cgImage!.width |
// Swift Playgrounds Beta 1.0 | |
import AVFoundation | |
import AVKit | |
import Accelerate | |
import Accounts | |
import AudioToolbox | |
import AudioUnit | |
import CFNetwork | |
import CoreAudio | |
import CoreAudioKit |
postfix operator ..< { } | |
prefix operator ..< { } | |
struct RangeStart<I: ForwardIndexType> { let start: I } | |
struct RangeEnd<I: ForwardIndexType> { let end: I } | |
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I> | |
{ return RangeStart(start: lhs) } | |
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I> |
#!/usr/bin/env xcrun swift | |
// $ chmod +x script.swift | |
// $ ./script.swift | |
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app | |
import Foundation | |
@noreturn private func failWithError(message: String) { | |
print("🚫 \(message)") |
extension Sequence { | |
var headAndTail: (head: Element, tail: SubSequence)? { | |
var head: Element? | |
let tail = drop { | |
if head == nil { | |
head = $0 | |
return true | |
} else { |