Last active
June 20, 2019 06:44
-
-
Save artemnovichkov/302cc644186e0cafadbeb127fc027259 to your computer and use it in GitHub Desktop.
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
// 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