How to implement Haskell's splitEvery in Swift?
问题 PROBLEM let x = (0..<10).splitEvery( 3 ) XCTAssertEqual( x, [(0...2),(3...5),(6...8),(9)], "implementation broken" ) COMMENTS I am running into problems calculating number of elements in the Range, etc... extension Range { func splitEvery( nInEach: Int ) -> [Range] { let n = self.endIndex - self.startIndex // ERROR - cannot invoke '-' with an argument list of type (T,T) } } 回答1: The values in a range are of ForwardIndexType , so you can only advance() them, or compute the distance() , but the