Last active
August 9, 2018 08:15
-
-
Save mortenbekditlevsen/7d00736a7e830fa02f1a75d54f192cb2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// So from the Objc.io talk, we learn about a way of representing filesystem paths that can point to either files or directories. | |
// Internally, these are represented as an array of path elements. Let's do that too: | |
public struct Path<Element> { | |
public struct Collection { | |
private var components: [String] | |
public func child(_ key: String) -> Path<Element> { | |
return append(key) | |
} | |
fileprivate func append<T>(_ args: String ...) -> Path<T> { | |
return Path<T>(components + args) | |
} | |
fileprivate init(_ components: [String]) { | |
self.components = components | |
} | |
public var rendered: String { | |
return components.joined(separator: "/") | |
} | |
} | |
private var components: [String] | |
fileprivate func append<T>(_ args: String ...) -> Path<T> { | |
return Path<T>(components + args) | |
} | |
fileprivate func append<T>(_ args: String ...) -> Path<T>.Collection { | |
return Path<T>.Collection(components + args) | |
} | |
fileprivate init(_ components: [String]) { | |
self.components = components | |
} | |
public var rendered: String { | |
return components.joined(separator: "/") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment