Last active
August 29, 2015 14:25
-
-
Save quells/812394c1d94baf79f269 to your computer and use it in GitHub Desktop.
Potential solution to http://inessential.com/2015/07/19/secret_projects_diary_2_swift_2_0_prot
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
//: # Brent's "Secret Projects Diary #2" | |
import Foundation | |
//: ## Feeds | |
protocol Feed: Equatable { | |
var url: String {get} | |
} | |
class LocalFeed: Feed { | |
var url: String | |
init(url: String) { | |
self.url = url | |
} | |
init<T: Feed>(feed: T) { | |
self.url = feed.url | |
} | |
} | |
func ==(lhs: LocalFeed, rhs: LocalFeed) -> Bool { return lhs.url == rhs.url } | |
class FeverFeed: Feed { | |
enum Kind { | |
case Kindling | |
case Spark | |
} | |
var url: String | |
var kind: FeverFeed.Kind | |
init(url: String, kind: FeverFeed.Kind) { | |
self.url = url | |
self.kind = kind | |
} | |
init<T: Feed>(feed: T) { | |
self.url = feed.url | |
self.kind = .Spark // Must choose some default, or add to the init signature | |
} | |
} | |
func ==(lhs: FeverFeed, rhs: FeverFeed) -> Bool { return lhs.url == rhs.url && lhs.kind == rhs.kind } | |
//: ## Folders | |
class Folder<T: Feed> { | |
var feeds = [T]() | |
func addFeeds(feedsToAdd: [T]) { | |
for oneFeed in feedsToAdd { | |
if !feeds.contains(oneFeed) { | |
feeds += [oneFeed] | |
} | |
} | |
} | |
} | |
//: ## Example Implementation | |
extension Folder { | |
func domains() -> [String] { | |
let urls = feeds.map { NSURL(string: $0.url)! } | |
let domains = urls.map { String($0.host!) } | |
return domains | |
} | |
} | |
class LocalFolderGeneric<T: LocalFeed>: Folder<T> { | |
func localFunction() -> String { | |
return "Just Local Things" | |
} | |
} | |
class FeverFolderGeneric<T: FeverFeed>: Folder<T> { | |
func hotListKinds() -> [FeverFeed.Kind] { | |
return feeds.map { $0.kind } | |
} | |
} | |
typealias LocalFolder = LocalFolderGeneric<LocalFeed> | |
typealias FeverFolder = FeverFolderGeneric<FeverFeed> | |
let IN = "http://inessential.com/xml/rss.xml" | |
let BE = "https://www.youtube.com/user/BetterElevation" | |
let DF = "http://daringfireball.net/feeds/main" | |
let QBranch = [IN, BE, DF] | |
let localFeeds = QBranch.map { LocalFeed(url: $0) } | |
let localFolder = LocalFolder() | |
localFolder.addFeeds(localFeeds) | |
localFolder.localFunction() | |
localFolder.domains() | |
let MS = FeverFeed(url: "http://www.macstories.net/feed/", kind: .Spark) | |
let feverFeeds = QBranch.map { FeverFeed(url: $0, kind: .Kindling) } + [MS] | |
let feverFolder = FeverFolder() | |
feverFolder.addFeeds(feverFeeds) | |
feverFolder.hotListKinds() | |
feverFolder.domains() |
Added typealias
es for LocalFolder
and FeverFolder
for some syntactic sugar.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added initializations of specialized
Feed
classes from genericFeed
s.