30-seconds-of-swift-code
30-seconds-of-swift-code copied to clipboard
dropRightWhile
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.
I got this :)
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})