Skip to content

Instantly share code, notes, and snippets.

@artemnovichkov
Last active June 20, 2019 06:44
Show Gist options
  • Save artemnovichkov/302cc644186e0cafadbeb127fc027259 to your computer and use it in GitHub Desktop.
Save artemnovichkov/302cc644186e0cafadbeb127fc027259 to your computer and use it in GitHub Desktop.
// Create an interator class
class ElementIterator<T>: IteratorProtocol {
typealias Element = T
let element: T
init(element: T) {
self.element = element
}
func next() -> T? {
return element
}
}
// Create a protocol with default implementation to simplify using for custom classes
protocol Iterable {
func makeIterator() -> ElementIterator<Self>
}
extension Iterable {
public func makeIterator() -> ElementIterator<Self> {
return ElementIterator(element: self)
}
}
typealias ElementSequence = Sequence & Iterable
// Extend Double
extension Double: ElementSequence {}
struct Man {
let name: String
let age: Double
}
// Create a names and init men with default age
let names = ["Anton", "Artem", "Jeka"]
print(zip(names, 25).map(Man.init))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment