30-seconds-of-swift-code icon indicating copy to clipboard operation
30-seconds-of-swift-code copied to clipboard

dropRightWhile

Open elizabethsiegle opened this issue 4 years ago • 2 comments

Make a function that removes elements from the end of an array until the passed function returns true and returns the remaining elements in the array.

elizabethsiegle avatar Oct 08 '19 07:10 elizabethsiegle

I got this :)

abdulajet avatar Oct 08 '19 20:10 abdulajet

I am not sure thing can be made generic without it being an extension on Collection. Otherwise the call site sorta will have to look like this:

drop(arr: [1, 2, 3, 4, 5]) { element in
    guard let element = element as? Int else { return false }
    return element > 3
}

vs doing something cool like:

drop(arr: [1, 2, 3, 4, 5], while: {$0 > 3}) 

abdulajet avatar Oct 08 '19 21:10 abdulajet