Skip to content

Instantly share code, notes, and snippets.

@lilyball
Last active August 29, 2015 14:04
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 lilyball/a9a5607582c363ec0238 to your computer and use it in GitHub Desktop.
Save lilyball/a9a5607582c363ec0238 to your computer and use it in GitHub Desktop.

Challenge #2

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment