Last active
July 29, 2016 21:17
-
-
Save jtbandes/0ccbf1662cf9668cbb61c5362f8933e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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