Implement a take_while
method on Array
take_while
runs the block with each element of the Array
in turn until it returns false, then it stops and returns an Array
of the prior elements.
Example
Input >> [1,2,3,4].take_while { $0 < 3 }
Output >> R8: Array<Int> = 2 values {
[0] = 1
[1] = 2
}
Bonus
Implement take_while
as a global struct named TakeWhile
that implements SequenceType
(like Zip2
)
Example
Input >> Array(TakeWhile([1,2,3,4]) { $0 < 3 })
Output >> R8: Array<Int> = 2 values {
[0] = 1
[1] = 2
}
Thanks to @eridius for the suggestion this week!
Post your answers as replies; directly or with a paste-site link.