Create a gist now

Instantly share code, notes, and snippets.

Brent's Feeds and Folders problem. See: http://roopc.net/posts/2015/brents-feeds-and-folders/
protocol Feed: Equatable {
var url: String { get }
}
func ==<T: Feed>(lhs: T, rhs: T) -> Bool {
return (lhs.url == rhs.url)
}
protocol Folder {
typealias FeedType: Feed
var feeds: [FeedType] { get }
func addFeeds(feedsToAdd: [FeedType])
}
struct LocalFeed: Feed {
var url: String
init(url: String) {
self.url = url
}
}
class LocalFolder: Folder {
var feeds: [LocalFeed] = []
func addFeeds(feedsToAdd: [LocalFeed]) {
for oneFeed in feedsToAdd {
if !feeds.contains(oneFeed) {
feeds += [oneFeed]
}
}
}
}
let folder = LocalFolder()
folder.addFeeds([ LocalFeed(url: "1"), LocalFeed(url: "2") ])
folder.addFeeds([ LocalFeed(url: "2"), LocalFeed(url: "3") ])
print(folder.feeds.count) // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment