Last active
August 29, 2015 14:12
-
-
Save kazk/6ace4280bed93488f317 to your computer and use it in GitHub Desktop.
This file contains 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
// Creates a sequence that consists of a single element repeated n times, | |
// or an infinite sequence repeating that element indefinitely. | |
// [D std.range.repeat]: http://dlang.org/phobos/std_range.html#repeat | |
public func repeat<T>(x: T) -> SequenceOf<T> { | |
return SequenceOf {_ -> GeneratorOf<T> in GeneratorOf { x }} | |
} | |
public func repeat<T>(x: T, count: Int) -> SequenceOf<T> { | |
return SequenceOf {_ -> GeneratorOf<T> in | |
var i = 0 | |
return GeneratorOf { (i++ < count) ? x : nil } | |
} | |
} | |
// extension SequenceOf { var array:[T] { return Array(self) } } | |
// repeat(1, 10).array.count //=> 10 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment