Created
August 4, 2015 20:10
-
-
Save rnapier/03674b399e3bc517b9cd to your computer and use it in GitHub Desktop.
A Little Respect for AnySequence (http://robnapier.net/erasure)
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 Swift | |
/*: | |
A simple type-erased sequence | |
*/ | |
let seq = AnySequence([1,2,3]) | |
/*: | |
## Who Needs Types Like That? | |
*/ | |
/*: | |
A not-so-simple, not-type-erased sequence | |
*/ | |
let xs = [1,2,3] | |
let ys = ["A","B","C"] | |
let zs = zip(xs.reverse(), ys.reverse()) | |
func reverseZip<T,U>(xs: [T], _ ys: [U]) -> Zip2Sequence<ReverseRandomAccessCollection<[T]>, ReverseRandomAccessCollection<[U]>> { | |
return zip(xs.reverse(), ys.reverse()) | |
} | |
// Why do we need to change the signature, just to change the implementation? | |
func reverseZipRefactor<T,U>(xs: [T], _ ys: [U]) -> [(T,U)] { | |
return zip(xs, ys).reverse() | |
} | |
// This can handle either implementation | |
func reverseZipAny<T,U>(xs: [T], _ ys: [U]) -> AnySequence<(T,U)> { | |
return AnySequence(zip(xs, ys).reverse()) | |
} | |
/*: | |
## Chains of Association | |
*/ | |
protocol Animal { | |
typealias Food | |
func feed(food: Food) | |
} | |
// Kinds of Food | |
struct Grass {} | |
struct Worm {} | |
struct Cow: Animal { | |
func feed(food: Grass) { print("moo") } | |
} | |
struct Goat: Animal { | |
func feed(food: Grass) { print("bah") } | |
} | |
struct Bird: Animal { | |
func feed(food: Worm) { print("chirp") } | |
} | |
struct AnyAnimal<Food>: Animal { | |
private let _feed: (Food) -> Void | |
init<Base: Animal where Food == Base.Food>(_ base: Base) { | |
_feed = base.feed | |
} | |
func feed(food: Food) { _feed(food) } | |
} | |
let grassEaters = [AnyAnimal(Cow()), AnyAnimal(Goat())] | |
let grass = Grass() | |
for animal in grassEaters { | |
animal.feed(grass) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Rob! Thanks for the article, it was really helpful to me. I noticed some of the Swift syntax is outdated, so I updated it. You can check it out here: https://gist.github.com/mhmdatallaa/1839f4225553af56d0851d2c963dea8a.
Hope it helps!