Skip to content

Instantly share code, notes, and snippets.

@roop
Last active August 29, 2015 14:25
Show Gist options
  • Save roop/5bb4713110093e96adb1 to your computer and use it in GitHub Desktop.
Save roop/5bb4713110093e96adb1 to your computer and use it in GitHub Desktop.
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