Skip to content

Instantly share code, notes, and snippets.

@tjeerdintveen
Created October 18, 2018 13:24
Show Gist options
  • Save tjeerdintveen/c76b03ec9526df59d8247873bd18c436 to your computer and use it in GitHub Desktop.
Save tjeerdintveen/c76b03ec9526df59d8247873bd18c436 to your computer and use it in GitHub Desktop.
inspect as an extension on Sequence
extension Sequence {
public func inspect(
_ body: (Element) throws -> Void
) rethrows -> Self {
for element in self {
try body(element)
}
return self
}
}
@tjeerdintveen
Copy link
Author

You can add inspection in a transformation pipeline to see the values.
It's like forEach. Except that you can keep on going.

E.g.

someArray.filter. {  }.inspect { print($0) }.map {  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment