Skip to content

Instantly share code, notes, and snippets.

@allending
Created July 16, 2015 07:16
Show Gist options
  • Save allending/6cdbc304568480190fd1 to your computer and use it in GitHub Desktop.
Save allending/6cdbc304568480190fd1 to your computer and use it in GitHub Desktop.
import Foundation
extension SequenceType {
func each(@noescape block: (element: Self.Generator.Element) -> Void) {
for item in self {
block(element: item)
}
}
func eachWithIndex(@noescape block: (Int, Self.Generator.Element) -> Void) {
for (index, element) in self.enumerate() {
block(index, element)
}
}
}
let arr = ["1 goat", 2, "3"]
arr.each { print($0) }
arr.eachWithIndex { print("index:\($0), value: \($1)") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment