Author: Chris Lattner
import Dispatch | |
import Foundation | |
var x = 7 | |
let dd = withUnsafeBytes(of: &x, { DispatchData.init(bytes: $0) }) | |
print(dd as? Data) // Case 1: nil | |
print(dd as? NSData) // Case 2: nil | |
print(dd as Any as? Data) // Case 3: nil | |
print(dd as Any as? NSData) // Case 4: .some | |
print(dd as Any as? NSData as Data?) // Case 5: .some |
import Foundation | |
// Alternatives to `Scanner` (before: `NSScanner`). | |
// A scanner only needs a way to peek and to move to the next token. | |
protocol ScannerProtocol { | |
associatedtype Token: Equatable | |
var peek: Token? { get } | |
mutating func moveToNextToken() |
COMPACT WIDTH (stacked view) | |
- 320 x 568 pt | |
-> iPhone 5, 5s | |
- 320 x 768 pt | |
-> iPad 9.7" Split Landscape 2/3 right | |
- 320 x 834 pt | |
-> iPad 10.5" Split Landscape 2/3 right | |
- 320 x 1024 pt | |
-> iPad 9.7" Split Portrait right |
Our principal goal as programmers is to reduce complexity. At any point in a program, we can reason about the state of a constant trivially -- its state is the state upon assignment. By contrast, the state of a variable can change endlessly.
With judicious use, immutable objects can lead to quicker execution speed and lower memory usage. The hash value of a String
, the query parameters of an immutable URL, and the distance traced by an immutable sequence of points are all immutable as well. We can cache their values for later use instead of recompute them on every access. We can also share an immutable object with clients without requiring those clients to defensively copy it.
An immutable value is safe in many ways. For example, String
is immutable and so its hashCode
value is immutable. Consequently, it is safe to use as a String
as a key in a Map
: The lower order bits of that hash will never change, and so an inserted key will never reside in the wrong bucket. An immutab
Rx.Observable.create(o => { | |
console.log("subscribing"); | |
o.onError(new Error("always fails")); | |
}).retryWhen(attempts -> { | |
return attempts.zip(Rx.Observable.range(1, 3), (n, i) => i).flatMap(i => { | |
console.log("delay retry by " + i + " second(s)"); | |
return Rx.Observable.timer(i * 1000); | |
}); | |
}).subscribe(); | |
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 22 | |
buildToolsVersion "22.0.1" | |
defaultConfig { | |
applicationId "com.example.databaseexample" | |
minSdkVersion 14 | |
targetSdkVersion 22 |
/// Translation of [Peter Norvig's spell checker](http://norvig.com/spell-correct.html) into Swift. | |
/// Sample input corpus [here](http://norvig.com/big.txt) | |
import Foundation.NSString // purely for IO, most things done with Swift.String | |
// pythony slicing | |
postfix operator ..< { } | |
prefix operator ..< { } | |
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I> { return RangeStart(start: lhs) } | |
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I> { return RangeEnd(end: rhs) } |
/* | |
This is a demonstration of how to implement the Optional type in Swift. | |
The name 'Maybe' is taken from Haskell, but the two cases 'None' & 'Some' | |
are the same as Swift's Optional type (Haskell uses 'Nothing' & 'Just'). | |
The Maybe type conforms to NilLiteralConvertible, as does Swift's | |
Optional type. This allows a Maybe value to be constructed from 'nil'. | |
One aspect of Swift's Optional type which can't be reproduced is | |
'implicit Optional wrapping'. Here's an example: |
- Introduction to Functional Programming Johannes Weiß - https://vimeo.com/100786088
- ReactiveCocoa at MobiDevDay Andrew Sardone - https://vimeo.com/65637501
- The Future Of ReactiveCocoa Justin Spahr-Summers - https://www.youtube.com/watch?v=ICNjRS2X8WM
- Enemy of the State Justin Spahr-Summers - https://www.youtube.com/watch?v=7AqXBuJOJkY
- WWDC 2014 Session 229 - Advanced iOS Application Architecture and Patterns Andy Matuschak - https://developer.apple.com/videos/play/wwdc2014/229/
- Functioning as a Functionalist Andy Matuschak - https://www.youtube.com/watch?v=rJosPrqBqrA
- Controlling Complexity in Swift Andy Matuschak - https://realm.io/news/andy-matuschak-controlling-complexity/