This file contains 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
/* | |
This gist contains an analysis of the design space for abstractions in a type system like Swift's. | |
It focuses on Monoid as an example but the same set of tradeoffs apply to any abstraction. | |
As such, it focuses on a single abstraction and does not explore issues that arise when designing | |
an abstraction hierarchy in depth. | |
The matrix below describes some of the design important design tradeoffs for various approaches including: | |
- OO style protocols such as `Monoid { var identity: Self ... } | |
(Haskell also adopts this approach for many abstractions, including for Monoid) | |
- ML signature style static protocols with empty enum conformances analagous with ML structures |
This file contains 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
public protocol KeypathUpdatable { | |
func update<T>(_ keyPath: WritableKeyPath<Self, T>, to value: T) -> Self | |
} | |
public extension KeypathUpdatable { | |
public func update<T>(_ keyPath: WritableKeyPath<Self, T>, to value: T) -> Self { | |
var copy = self | |
copy[keyPath: keyPath] = value | |
return copy | |
} |
This file contains 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
// This example shows how closed protocols can be emulated in Swift today. | |
// The technique leverages Swift's support for public (but not open) classes. | |
// First, it's worth observing that there is an almost trivial technique that can be used when | |
// it is possible to specify a (possibly abstract) superclass for all conforiming types. | |
// Simply declare a public (not open) base class and a protocol with a Self inheritance constraint. | |
// Swift does not support open subclasses of a public superclass so no classes outside the module | |
// will be able to meet the self inheritance constraint. | |
public class FooBase {} | |
public protocol Foo where Self: FooBase {} |
This file contains 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
// This example shows how higher-kinded types can be emulated in Swift today. | |
// It acheives correct typing at the cost of some boilerplate, manual lifting and an existential representation. | |
// The technique below was directly inspired by the paper Lightweight Higher-Kinded Polymorphism | |
// by Jeremy Yallop and Leo White found at http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf | |
/// `ConstructorTag` represents a type constructor. | |
/// `Argument` represents an argument to the type constructor. | |
struct Apply<ConstructorTag, Argument> { | |
/// An existential containing a value of `Constructor<Argument>` | |
/// Where `Constructor` is the type constructor represented by `ConstructorTag` |
This file contains 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
1. Create a new xcode project | |
- You can skip this if you're adding the playground to an existing project | |
2. File > Save as workspace - save workspace in project folder | |
- You can skip this if you're adding the playground to an existing workspace | |
3. File > Playground - create new playground in project folder | |
4. Drag playground in at the _workspace_ level (root item, i.e. _not_ under the project) | |
5. Add a new Framework to your project (I usually name mine something like 'PlaygroundKit') | |
- You don't need to include tests | |
- Choose 'None' for Enbed in Application | |
6. Make all project files you want to be accessible to the playground are members of the new target |
This file contains 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
import QuartzCore | |
extension CGFloat { | |
func map(from from: ClosedInterval<CGFloat>, to: ClosedInterval<CGFloat>) -> CGFloat { | |
let result = ((self - from.start) / (from.end - from.start)) * (to.end - to.start) + to.start | |
return result | |
} | |
} | |
extension Double { |
This file contains 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
// NSScanner+Swift.swift | |
// A set of Swift-idiomatic methods for NSScanner | |
// | |
// (c) 2015 Nate Cook, licensed under the MIT license | |
import Foundation | |
extension NSScanner { | |
// MARK: Strings |
This file contains 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
Latency Comparison Numbers (~2012) | |
---------------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |