Skip to content

Instantly share code, notes, and snippets.

@callionica
Last active December 12, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callionica/b258843483924daf3cab to your computer and use it in GitHub Desktop.
Save callionica/b258843483924daf3cab to your computer and use it in GitHub Desktop.
// Protocol
protocol Feed {
var url : String { get }
}
// Concrete classes
class LocalFeed : Feed {
var url : String = "file://Local"
}
class RemoteFeed : Feed {
var url : String = "http://Remote"
}
// Hash/equality policy - don't pollute protocols or classes with this nonsense
// We do things this way because we're going to use Set and Set is an intrusive container - requires the contained objects to have special members
class Containable : Hashable {
var element: Feed
var hashValue: Int { return element.url.hashValue }
init(_ element : Feed) {
self.element = element
}
}
func ==(lhs: Containable, rhs: Containable) -> Bool {
return lhs.element.url == rhs.element.url
}
// Container
typealias Container = Set<Containable>
// Made Bucket a SequenceType so we can loop over it directly
class Bucket : SequenceType {
//typealias Generator = LazyMapGenerator<Container.Generator, Feed>
typealias Generator = AnyGenerator<Feed>
var _feeds : Container = Container()
func add(feed : Feed) {
_feeds.insert(Containable(feed))
}
var feeds : [Feed] {
return _feeds.map { $0.element };
}
func generate()->Generator {
return AnyGenerator(_feeds.lazy.map { $0.element }.generate());
}
}
// Use the code
var bucket = Bucket();
bucket.add(LocalFeed())
bucket.add(RemoteFeed())
for feed in bucket {
print(feed.url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment