0001    //
0002    //  Array+SourceKitten.swift
0003    //  SourceKitten
0004    //
0005    //  Created by JP Simard on 4/4/15.
0006    //  Copyright (c) 2015 SourceKitten. All rights reserved.
0007    //
0008    
0009    /**
0010    Returns an array containing the last contiguous group of elements matching the filter.
0011    
0012    - parameter array:  Array to filter.
0013    - parameter filter: Closure to filter elements.
0014    */
0015    public func filterLastContiguous<T>(array: [T], filter: T -> Bool) -> [T] {
0016        // remove trailing elements until the last one matches the filter
0017        var arrayWithTrailingNonMatchesRemoved = array
0018        while let last = arrayWithTrailingNonMatchesRemoved.last where !filter(last) {
0019            arrayWithTrailingNonMatchesRemoved.removeLast()
0020        }
0021        var lastContiguousArray = [T]()
0022        // keep trailing elements until the first one matches the filter
0023        while let last = arrayWithTrailingNonMatchesRemoved.last where filter(last) {
0024            lastContiguousArray.insert(arrayWithTrailingNonMatchesRemoved.removeLast(),
0025                atIndex: 0)
0026        }
0027        return lastContiguousArray
0028    }
0029