 
     func map<T>(_ transform: @escaping (Element) -> T) -> MappedGenerator<Self, T> {
         return MappedGenerator(base: self, transformer: transform)
+    }
+
+    mutating func reduce<T>(initial: T, _ reducer: (T, Element) -> T) -> T {
+        var result = initial
+        self.forEach { (item) in
+            result = reducer(result, item)
+        }
+        return result
     }
 
     func filter(_ shouldInclude: @escaping (Element) -> Bool) -> FilteredGenerator<Self> {

    mutating func next() -> Element?

    mutating func forEach(_ body: (Element) -> Void)
    func map<T>(_ transform: @escaping (Element) -> T) -> MappedGenerator<Self, T>
    func filter(_ shouldInclude: @escaping (Element) -> Bool) -> FilteredGenerator<Self>
    func enumerated() -> EnumeratedGenerator<Self>


}



print("\n----\n")



struct FileChunksReader: Generator {
    let path: String
    let fileHandle: FileHandle
    let chunkSize: Int

    init?(path: String, chunkSize: Int = 4096) {
        guard chunkSize > 0 else { return nil }
        guard let fileHandle = FileHandle(forReadingAtPath: path) else {
            return nil
        }
        self.path = path
        self.fileHandle = fileHandle
        self.chunkSize = chunkSize
    }

    mutating func next() -> Data? {
        let data = self.fileHandle.readData(ofLength: Int(self.chunkSize))
        guard !data.isEmpty else { return nil }
        return data
    }
}

var hostFileReader = FileChunksReader(path: "/etc/hosts", chunkSize: 128)!
let hostFileSize = hostFileReader.reduce(initial: 0) { (accumulatedSize, data) in
    return accumulatedSize + data.count
}
"It takes \(hostFileSize) bytes for '/etc/hosts'"

//: ---
//:
//: [<- Previous](@previous) | [Next ->](@next)
