Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Last active July 29, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtbandes/0ccbf1662cf9668cbb61c5362f8933e7 to your computer and use it in GitHub Desktop.
Save jtbandes/0ccbf1662cf9668cbb61c5362f8933e7 to your computer and use it in GitHub Desktop.
func sequence<T>(_ initial: T, _ cond: (T) -> Bool, _ update: (inout T) -> ()) -> UnfoldSequence<T, T> {
return sequence(state: initial, next: { (state: inout T) -> T? in
guard cond(state) else { return nil }
update(&state)
return state
})
}
for x in sequence(4, { $0 < 10 }, { $0 = $0 + 1 }) {
print(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment