         }
         return result
     }
-
-    public func flatMap<S: SequenceType>(transform: (Element) throws -> S) rethrows -> [S.Generator.Element] {
-        var result: [S.Generator.Element] = []
+    
+    public func flatMap<S: Sequence>(transform: (Element) throws -> S) rethrows -> [S.Iterator.Element] {
+        var result: [S.Iterator.Element] = []
         try self.forEach {
-            result.appendContentsOf(try transform($0))
+            result.append(contentsOf: try transform($0))
         }
         return result
     }
-
+    
     public func filter(@noescape includeElement: (Element) throws -> Bool) rethrows -> [Element] {
         var result: [Element] = []
         try self.forEach {

        makeUniqueWithCapacity(grow(count + 1))
        buffer.insert(newElement, at: i)
    }

    /// Insert the contents of `newElements` into this deque, starting at index `i`.
    ///
    /// - Complexity: O(`count`). Note though that complexity is O(1) if `i` is of a constant distance from the front or end of the deque.
    public mutating func insertContentsOf<C: CollectionType where C.Generator.Element == Element>(newElements: C, at i: Int) {
        makeUniqueWithCapacity(grow(count + numericCast(newElements.count)))
        buffer.insertContentsOf(newElements, at: i)
    }

    /// Remove the element at index `i` from this deque.
    ///
    /// - Complexity: O(`count`). Note though that complexity is O(1) if `i` is of a constant distance from the front or end of the deque.

            buffer = new
        }
    }
    
    internal var isUnique: Bool { mutating get { return isUniquelyReferenced(&buffer) } }
    
    private mutating func makeUnique() {
        self.makeUniqueWithCapacity(buffer.capacity)
    }
    
    private mutating func makeUniqueWithCapacity(capacity: Int) {
        guard !isUnique || buffer.capacity < capacity else { return }
        let copy = DequeBuffer<Element>(capacity: capacity)
