This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const promiseMachine = Machine({ | |
id: 'promise', | |
initial: 'pending', | |
states: { | |
pending: { | |
on: { | |
// state transition (shorthand) | |
// this is equivalent to { target: 'resolved' } | |
RESOLVE: 'resolved', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lightMachine = Machine({ | |
key: 'light', | |
initial: 'green', | |
states: { | |
green: { | |
on: { NEXT: "yellow" } | |
}, | |
yellow: { on: { NEXT: "red" }}, | |
red: { | |
initial: 'green', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const allData = new Array(25).fill(0).map((_valu, i) => i + 1); | |
const perPage = 10; | |
const dataMachine = new Machine({ | |
id: 'dataMachine', | |
initial: 'loading', | |
context: { | |
data: [] | |
}, | |
states: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A very cool visualisation machine | |
// Allows you to see what the state machine does | |
// And how it transitions from one state to the next | |
const toggleMachine = new Machine({ | |
id: 'toggleMachine', | |
initial: 'inactive', | |
states: { | |
inactive: { | |
// Tell it which events the state should listen for. | |
// And you do that by defining an on property. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
/// 1. Capture the scene | |
guard let windowScene = (scene as? UIWindowScene) else { return } | |
/// 2. Create a new UIWindow using the windowScene constructor which takes in a window scene. | |
let window = UIWindow(windowScene: windowScene) | |
/// 3. Create a view hierarchy programmatically | |
let viewController = ArticleListViewController() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let balanceToConvert: String? | |
... | |
if let inputEntry = balance, inputEntry.sign == .negative { | |
balanceToConvert = Int(inputEntry.input) // Can return nil | |
.flatMap(Int.init) // Convert to Int if not nil | |
.flatMap { $0 * -1 } // flip sign if not nil | |
.flatMap(String.init) // convert back to String if not nil | |
} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the flatmap method it would look something like this | |
switch self { | |
case .some(let unwrapped): | |
return transform(unwrappedValue) | |
case .none: | |
return nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func processInput(_ stringNumber: String?) -> Int? { | |
return stringNumber.flatMap { Int($0) } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func processInput(_ stringNumber: String?) -> Int? { | |
guard let unwrappedStringNumber = stringNumber { | |
return nil | |
} | |
return Int(unwrappedStringNumber) | |
} |