- A = [xA, yA] is a point on the 2D plane. Same for B, C, ...
- lengths are in any unit (ex: pixels)
- code snippets are in JavaScript
angleRad = angleDeg * Math.PI / 180;
| # simple navigation componenent like on iOS | |
| # put at the top of your file | |
| class Paging | |
| pages = Array() | |
| constructor: (@bg_layer) -> | |
| # push a new page on top |
| import UIKit | |
| import GenericViewKit | |
| public class StateView: GenericView { | |
| public enum State { | |
| case Loading | |
| case Loaded | |
| case Error | |
| } |
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,
| protocol State {} | |
| struct Transition<From: State, To: State> { | |
| let transition: () -> To | |
| } | |
| struct Machine<CurrentState: State> { | |
| var state: CurrentState | |
| //: | |
| //: UIView Animation Syntax Sugar | |
| //: | |
| //: Created by Andyy Hope on 18/08/2016. | |
| //: Twitter: @andyyhope | |
| //: Medium: Andyy Hope, https://medium.com/@AndyyHope | |
| import UIKit | |
| extension UIView { |
| // | |
| // debounce-throttle.swift | |
| // | |
| // Created by Simon Ljungberg on 19/12/16. | |
| // License: MIT | |
| // | |
| import Foundation | |
| extension TimeInterval { |
| import Foundation | |
| import UIKit | |
| import ReactiveSwift | |
| import Result | |
| extension UIViewController { | |
| typealias KeyboardStatusInfo = (height: CGFloat , duration: TimeInterval, animationOptions: UIViewAnimationOptions) | |
| var keyboardUpdatesSignal: Signal<KeyboardStatusInfo, NoError> { |
| import AppKit | |
| import AVFoundation | |
| class MovieWriter: NSObject { | |
| func writeImagesAsMovie(_ allImages: [NSImage], videoPath: String, videoSize: CGSize, videoFPS: Int32) { | |
| // Create AVAssetWriter to write video | |
| guard let assetWriter = createAssetWriter(videoPath, size: videoSize) else { | |
| print("Error converting images to video: AVAssetWriter not created") | |
| return | |
| } |