Take elements from an array until a condition is met