diff --git a/ExSwift/Array.swift b/ExSwift/Array.swift index 5bdbeac..ea63b85 100644 --- a/ExSwift/Array.swift +++ b/ExSwift/Array.swift @@ -18,8 +18,8 @@ internal extension Array { - parameter items: Items to search for - returns: true if self contains all the items */ - func contains (items: T...) -> Bool { - return items.all { (item: T) -> Bool in self.indexOf(item) >= 0 } + func contains (items: Element...) -> Bool { + return items.all { (item: Element) -> Bool in self.indexOf(item) >= 0 } } /** @@ -28,12 +28,12 @@ internal extension Array { - parameter values: Arrays to subtract - returns: Difference of self and the input arrays */ - func difference (values: [T]...) -> [T] { + func difference (values: [Element]...) -> [Element] { - var result = [T]() + var result = [Element]() elements: for e in self { - if let element = e as? T { + if let element = e as? Element { for value in values { // if a value is in both self and one of the values arrays // jump to the next iteration of the outer loop @@ -131,7 +131,7 @@ internal extension Array { - parameter item: The item to search for - returns: Matched item or nil */ - func find (item: U) -> T? { + func find (item: U) -> Element? { if let index: Int = indexOf(item) { return self[index] } @@ -173,7 +173,7 @@ internal extension Array { */ func lastIndexOf (item: U) -> Int? { if item is Element { - for (index, value) in lazy(self).reverse().enumerate() { + for (index, value) in self.reverse().enumerate() { if value as! U == item { return count - 1 - index } @@ -205,7 +205,7 @@ internal extension Array { */ func get (range: Range) -> Array { - return self[rangeAsArray: range] + return Array(self[range]) } @@ -222,15 +222,16 @@ internal extension Array { // Gets the longest sequence let max = arrays.map { (element: Any) -> Int in - return reflect(element).count + return Int(Mirror(reflecting: element).children.count) }.max() as Int for i in 0.. Any? in - let (_, mirror) = reflect(element)[i] - return mirror.value + // let (_, mirror) = reflect(element)[i] + // return mirror.value + return Mirror(reflecting: element).children }) } @@ -246,7 +247,10 @@ internal extension Array { - parameter step: The number of elements to progress between each partition. Set to n if not supplied. - returns: Array partitioned into n element arrays, starting step elements apart. */ - func partition (var n: Int, var step: Int? = nil) -> [Array] { + func partition (n: Int, step: Int? = nil) -> [Array] { + var n = n + var step = step + var result = [Array]() // If no step is supplied move n each step. @@ -258,8 +262,8 @@ internal extension Array { if n < 1 { n = 0 } // Allow 0 if user wants [[],[],[]] for some reason. if n > count { return [[]] } - for i in stride(from: 0, through: count - n, by: step!) { - result += [self[i..<(i + n)]] + for i in 0.stride(through: count - n, by: step!) { + result += [Array(self[i..<(i + n)])] } return result @@ -275,8 +279,10 @@ internal extension Array { the last partition may less than n elements long. - returns: Array partitioned into n element arrays, starting step elements apart. */ - func partition (var n: Int, var step: Int? = nil, pad: Array?) -> [Array] { + func partition (n: Int, step: Int? = nil, pad: Array?) -> [Array] { var result = [Array]() + var n = n + var step = step // If no step is supplied move n each step. if step == nil { @@ -293,14 +299,14 @@ internal extension Array { n = 0 } - for i in stride(from: 0, to: count, by: step!) { + for i in 0.stride(through: count, by: step!) { var end = i + n if end > count { end = count } - - result += [self[i.. [Array] { + func partitionAll (n: Int, step: Int? = nil) -> [Array] { var result = [Array]() + var n = n + var step = step // If no step is supplied move n each step. if step == nil { @@ -333,8 +341,8 @@ internal extension Array { if step < 1 { step = 1 } // Less than 1 results in an infinite loop. if n < 1 { n = 0 } // Allow 0 if user wants [[],[],[]] for some reason. - for i in stride(from: 0, to: count, by: step!) { - result += [self[i..<(i + n)]] + for i in 0.stride(through: count, by: step!) { + result += [Array(self[i..<(i + n)])] } return result @@ -346,12 +354,12 @@ internal extension Array { - parameter cond: Function which takes an element and produces an equatable result. - returns: Array partitioned in order, splitting via results of cond. */ - func partitionBy (cond: (Element) -> T) -> [Array] { + func partitionBy (cond: (Element) -> Element) -> [Array] { var result = [Array]() - var lastValue: T? = nil + var lastValue: Element? = nil for item in self { - let value = cond(item) + let value = cond(item as! Element) if value == lastValue { let index: Int = result.count - 1 @@ -370,7 +378,8 @@ internal extension Array { */ mutating func shuffle () { - for var i = self.count - 1; i >= 1; i-- { + for i in (1..= 1; i -= 1 { let j = Int.random(max: i) swap(&self[i], &self[j]) } @@ -402,7 +411,7 @@ internal extension Array { } let index = Int.random(max: count - n) - return self[index..<(n + index)] + return Array(self[index..<(n + index)]) } /** @@ -439,7 +448,7 @@ internal extension Array { func maxBy (call: (Element) -> (U)) -> Element? { if let firstValue = self.first { - var maxElement: T = firstValue + var maxElement: Element = firstValue var maxValue: U = call(firstValue) for i in 1.. (call: (Element) -> (U)) -> Element? { if let firstValue = self.first { - var minElement: T = firstValue + var minElement: Element = firstValue var minValue: U = call(firstValue) for i in 1.. Array { - return self[0.. Array { - return self[(count - n).. Array { - return n > count ? [] : self[Int(n).. count ? [] : Array(self[Int(n).. () -> [T] { - var result = [T]() + func unique () -> [Element] { + var result = [Element]() for item in self { - if !result.contains(item as! T) { - result.append(item as! T) + if !result.contains(item as! Element) { + result.append(item as! Element) } } @@ -690,37 +699,37 @@ internal extension Array { - parameter call: The closure to use to determine uniqueness - returns: The set of elements for which call(element) is unique */ - func uniqueBy (call: (Element) -> (T)) -> [Element] { + func uniqueBy (call: (Element) -> (Element)) -> [Element] { var result: [Element] = [] - var uniqueItems: [T] = [] + var uniqueItems: [Element] = [] for item in self { - let callResult: T = call(item) + let callResult: Element = call(item as! Element) if !uniqueItems.contains(callResult) { uniqueItems.append(callResult) - result.append(item) + result.append(item as! Element) } } return result } - + /** Returns all permutations of a given length within an array - parameter length: The length of each permutation - returns: All permutations of a given length within an array */ - func permutation (length: Int) -> [[T]] { + func permutation (length: Int) -> [[Element]] { if length < 0 || length > self.count { return [] } else if length == 0 { return [[]] } else { - var permutations: [[T]] = [] + var permutations: [[Element]] = [] let combinations = combination(length) for combination in combinations { - var endArray: [[T]] = [] + var endArray: [[Element]] = [] var mutableCombination = combination permutations += self.permutationHelper(length, array: &mutableCombination, endArray: &endArray) } @@ -732,15 +741,15 @@ internal extension Array { Recursive helper method where all of the permutation-generating work is done This is Heap's algorithm */ - private func permutationHelper(n: Int, inout array: [T], inout endArray: [[T]]) -> [[T]] { + private func permutationHelper(n: Int, inout array: [Element], inout endArray: [[Element]]) -> [[Element]] { if n == 1 { endArray += [array] } - for var i = 0; i < n; i++ { + for i in 0 ..< n { permutationHelper(n - 1, array: &array, endArray: &endArray) let j = n % 2 == 0 ? i : 0; //(array[j], array[n - 1]) = (array[n - 1], array[j]) - let temp: T = array[j] + let temp: Element = array[j] array[j] = array[n - 1] array[n - 1] = temp } @@ -789,7 +798,7 @@ internal extension Array { let groupKey = group(item) if result.has(groupKey) { - result[groupKey]!++ + result[groupKey]! += 1 } else { result[groupKey] = 1 } @@ -824,12 +833,12 @@ internal extension Array { combinations.append(combination) var i = indexes.count - 1 while i >= 0 && indexes[i] == self.count - 1 { - i-- + i -= 1 } if i < 0 { break } - indexes[i]++ + indexes[i] += 1 (i+1).upTo(indexes.count - 1) { j in indexes[j] = indexes[i] } @@ -858,12 +867,12 @@ internal extension Array { combinations.append(combination) var i = indexes.count - 1 while i >= 0 && indexes[i] == i + offset { - i-- + i -= 1 } if i < 0 { break } - i++ + i += 1 let start = indexes[i-1] + 1 for j in (i-1).. [[T]] { + func repeatedPermutation(length: Int) -> [[Element]] { if length < 1 { return [] } @@ -911,7 +920,7 @@ internal extension Array { for item in self { if test(item) { - result++ + result += 1 } } @@ -929,9 +938,9 @@ internal extension Array { :return: A transposed version of the array, where the object at array[i][j] goes to array[j][i] */ - func transposition (array: [[T]]) -> [[T]] { // () -> [[U]] { + func transposition (array: [[Element]]) -> [[Element]] { // () -> [[U]] { let maxWidth: Int = array.map({ $0.count }).max() - var transposition = [[T]](count: maxWidth, repeatedValue: []) + var transposition = [[Element]](count: maxWidth, repeatedValue: []) (0.. () { + mutating func fill (object: Element) -> () { (0.. (separator: C) -> C? { + func implode (separator: C) -> C? { if Element.self is C.Type { - return Swift.join(separator, unsafeBitCast(self, [C].self)) + if let joined = unsafeBitCast(self, [C].self).joinWithSeparator(separator) as? C { + return joined + } else { + return nil + } } return nil @@ -1085,15 +1098,18 @@ internal extension Array { */ func flatten () -> [OutType] { var result = [OutType]() - let reflection = reflect(self) - - for i in 0.. Bool) -> [T] { + @available(*, unavailable, message="use 'sort' instead") func sortBy (isOrderedBefore: (Element, Element) -> Bool) -> [Element] { return sort(isOrderedBefore) } @@ -1129,7 +1145,7 @@ internal extension Array { - parameter n: the number of times to cycle through - parameter block: the block to run for each element in each cycle */ - func cycle (n: Int? = nil, block: (T) -> ()) { + func cycle (n: Int? = nil, block: (Element) -> ()) { var cyclesRun = 0 while true { if let n = n { @@ -1140,7 +1156,7 @@ internal extension Array { for item in self { block(item) } - cyclesRun++ + cyclesRun += 1 } } @@ -1154,7 +1170,7 @@ internal extension Array { - parameter block: the block to run each time - returns: the min element, or nil if there are no items for which the block returns true */ - func bSearch (block: (T) -> (Bool)) -> T? { + func bSearch (block: (Element) -> (Bool)) -> Element? { if count == 0 { return nil } @@ -1188,7 +1204,7 @@ internal extension Array { - parameter block: the block to run each time - returns: an item (there could be multiple matches) for which the block returns true */ - func bSearch (block: (T) -> (Int)) -> T? { + /*func bSearch (block: (Element) -> (Int)) -> Element? { let match = bSearch { item in block(item) >= 0 } @@ -1197,7 +1213,7 @@ internal extension Array { } else { return nil } - } + }*/ /** Sorts the array by the value returned from the block, in ascending order @@ -1205,7 +1221,7 @@ internal extension Array { - parameter block: the block to use to sort by - returns: an array sorted by that block, in ascending order */ - func sortUsing (block: ((T) -> U)) -> [T] { + func sortUsing (block: ((Element) -> U)) -> [Element] { return self.sort({ block($0.0) < block($0.1) }) } @@ -1291,7 +1307,7 @@ internal extension Array { - parameter range: - returns: Array of values */ - @available(*, unavailable, message="use the '[U](range)' constructor") static func range (range: Range) -> [U] { + @available(*, unavailable, message="use the '[U](range)' constructor") func range (range: Range) -> [U] { return [U](range) } @@ -1310,7 +1326,7 @@ internal extension Array { return [] } - return Array(self[Range(start: start, end: end)] as ArraySlice) + return Array(self[start ..< end] as ArraySlice) } /** @@ -1320,7 +1336,7 @@ internal extension Array { - returns: Subarray or nil if the index is out of bounds */ subscript (interval: HalfOpenInterval) -> Array { - return self[rangeAsArray: Range(start: interval.start, end: interval.end)] + return self[rangeAsArray: interval.start ..< interval.end] } /** @@ -1330,7 +1346,7 @@ internal extension Array { - returns: Subarray or nil if the index is out of bounds */ subscript (interval: ClosedInterval) -> Array { - return self[rangeAsArray: Range(start: interval.start, end: interval.end + 1)] + return self[rangeAsArray: interval.start ..< (interval.end + 1)] } /** @@ -1345,34 +1361,34 @@ internal extension Array { let indexes = [first, second] + rest return indexes.map { self[$0] } } - } + /** Remove an element from the array */ -public func - (first: [T], second: T) -> [T] { +public func - (first: [Element], second: Element) -> [Element] { return first - [second] } /** Difference operator */ -public func - (first: [T], second: [T]) -> [T] { +public func - (first: [Element], second: [Element]) -> [Element] { return first.difference(second) } /** Intersection operator */ -public func & (first: [T], second: [T]) -> [T] { +public func & (first: [Element], second: [Element]) -> [Element] { return first.intersection(second) } /** Union operator */ -public func | (first: [T], second: [T]) -> [T] { +public func | (first: [Element], second: [Element]) -> [Element] { return first.union(second) } /** diff --git a/ExSwift/Dictionary.swift b/ExSwift/Dictionary.swift index d6cc7da..feb823e 100644 --- a/ExSwift/Dictionary.swift +++ b/ExSwift/Dictionary.swift @@ -275,7 +275,7 @@ internal extension Dictionary { // If element has already been added to dictionary, append to it. If not, create one. if result.has(groupKey) { - result[groupKey]!++ + result[groupKey]! += 1 } else { result[groupKey] = 1 } @@ -333,7 +333,7 @@ internal extension Dictionary { for (key, value) in self { if test(key, value) { - result++ + result += 1 } } diff --git a/ExSwift/ExSwift.swift b/ExSwift/ExSwift.swift index 6060c7b..e770d7c 100644 --- a/ExSwift/ExSwift.swift +++ b/ExSwift/ExSwift.swift @@ -35,8 +35,8 @@ public class ExSwift { // Workaround for the now illegal (T...) type. let adaptedFunction = unsafeBitCast(function, Function.self) - - if times-- <= 0 { + times -= 1 + if times <= 0 { return adaptedFunction(params) } @@ -52,7 +52,7 @@ public class ExSwift { - parameter function: Function to wrap - returns: Wrapper function */ - public class func after (n: Int, function: Void -> T) -> (Void -> T?) { + /*public class func after (n: Int, function: Void -> T) -> (Void -> T?) { func callAfter (args: Any?...) -> T { return function() } @@ -60,7 +60,7 @@ public class ExSwift { let f = ExSwift.after(n, function: callAfter) return { f([nil]) } - } + }*/ /** Creates a wrapper function that invokes function once. @@ -97,14 +97,14 @@ public class ExSwift { - parameter function: Function to wrap - returns: Wrapper function */ - public class func once (function: Void -> T) -> (Void -> T) { + /*public class func once (function: Void -> T) -> (Void -> T) { let f = ExSwift.once { (params: Any?...) -> T in return function() } return { f([nil]) } - } + }*/ /** Creates a wrapper that, when called, invokes function with any additional @@ -245,29 +245,28 @@ extension ExSwift { */ internal class func bridgeObjCObject (object: S) -> [T] { var result = [T]() - let reflection = reflect(object) + let reflection = Mirror(reflecting: object) + let mirrorChildrenCollection = AnyRandomAccessCollection(reflection.children) // object has an Objective-C type if let obj = object as? T { // object has type T result.append(obj) - } else if reflection.disposition == .ObjCObject { + } else if reflection.subjectType == NSArray.self { // If it is an NSArray, flattening will produce the expected result if let array = object as? NSArray { result += array.flatten() - } else if let bridged = reflection.value as? T { + } else if let bridged = mirrorChildrenCollection as? T { result.append(bridged) } - } else if reflection.disposition == .IndexContainer { + } else if object is Array { // object is a native Swift array // recursively convert each item - (0.. () -> [OutType] { var result = [OutType]() - let reflection = reflect(self) - - for i in 0.. Self { - return self.dynamicType(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate + n) + return self.dynamicType.init(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate + n) } } // MARK: Arithmetic diff --git a/ExSwift/Range.swift b/ExSwift/Range.swift index af4d568..a0a3683 100644 --- a/ExSwift/Range.swift +++ b/ExSwift/Range.swift @@ -16,7 +16,7 @@ internal extension Range { - parameter function: Function to call */ func times (function: () -> ()) { - each { (current: T) -> () in + each { (current: Element) -> () in function() } } @@ -26,7 +26,7 @@ internal extension Range { - parameter function: Function to invoke */ - func times (function: (T) -> ()) { + func times (function: (Element) -> ()) { each (function) } @@ -35,7 +35,7 @@ internal extension Range { - parameter function: Function to invoke */ - func each (function: (T) -> ()) { + func each (function: (Element) -> ()) { for i in self { function(i) } @@ -46,8 +46,8 @@ internal extension Range { - returns: Each element of the range in an array */ - func toArray () -> [T] { - var result: [T] = [] + func toArray () -> [Element] { + var result: [Element] = [] for i in self { result.append(i) } diff --git a/ExSwift/Sequence.swift b/ExSwift/Sequence.swift index 1b2c043..e249928 100644 --- a/ExSwift/Sequence.swift +++ b/ExSwift/Sequence.swift @@ -15,7 +15,7 @@ internal extension AnySequence { - returns: First element of the sequence if present */ - var first: T? { + var first: Element? { let generator = self.generate() return generator.next() } @@ -26,7 +26,7 @@ internal extension AnySequence { - parameter call: Function to call for each element - returns: True if call returns true for any element of self */ - func any (call: (T) -> Bool) -> Bool { + func any (call: (Element) -> Bool) -> Bool { let generator = self.generate() while let nextItem = generator.next() { if call(nextItem) { @@ -42,7 +42,7 @@ internal extension AnySequence { - parameter index: - returns: Object at index in sequence, nil if index is out of bounds */ - func get (index: Int) -> T? { + func get (index: Int) -> Element? { let generator = self.generate() for _ in 0..<(index - 1) { generator.next() @@ -56,7 +56,7 @@ internal extension AnySequence { - parameter range: - returns: Subsequence in range */ - func get (range: Range) -> AnySequence { + func get (range: Range) -> AnySequence { return self.skip(range.startIndex).take(range.endIndex - range.startIndex) } @@ -74,7 +74,7 @@ internal extension AnySequence { return index } } - index++ + index += 1 } return nil } @@ -85,7 +85,7 @@ internal extension AnySequence { - parameter n: Number of elements to skip - returns: Sequence from n to the end */ - func skip (n: Int) -> AnySequence { + func skip (n: Int) -> AnySequence { let generator = self.generate() for _ in 0.. Bool) -> AnySequence { - return AnySequence(lazy(self).filter(include)) + func filter(include: (Element) -> Bool) -> AnySequence { + return AnySequence(self.filter(include)) } /** @@ -109,7 +109,7 @@ internal extension AnySequence { - parameter exclude: Function invoked to test elements for exlcusion from the sequence - returns: Filtered sequence */ - func reject (exclude: (T -> Bool)) -> AnySequence { + func reject (exclude: (Element -> Bool)) -> AnySequence { return self.filter { return !exclude($0) } @@ -121,7 +121,7 @@ internal extension AnySequence { - parameter condition: A function which returns a boolean if an element satisfies a given condition or not - returns: Elements of the sequence starting with the element which does not meet the condition */ - func skipWhile(condition:(T) -> Bool) -> AnySequence { + func skipWhile(condition:(Element) -> Bool) -> AnySequence { let generator = self.generate() let checkingGenerator = self.generate() @@ -144,10 +144,10 @@ internal extension AnySequence { - parameter item: The item to search for - returns: true if self contains item */ - func contains (item: T) -> Bool { + func contains (item: Element) -> Bool { let generator = self.generate() while let nextItem = generator.next() { - if nextItem as! T == item { + if nextItem as! Element == item { return true } } @@ -160,7 +160,7 @@ internal extension AnySequence { - parameter n: Number of elements to take - returns: First n elements */ - func take (n: Int) -> AnySequence { + func take (n: Int) -> AnySequence { return AnySequence(TakeSequence(self, n)) } @@ -170,7 +170,7 @@ internal extension AnySequence { - parameter condition: A function which returns a boolean if an element satisfies a given condition or not. - returns: Elements of the sequence up until an element does not meet the condition */ - func takeWhile (condition:(T?) -> Bool) -> AnySequence { + func takeWhile (condition:(Element?) -> Bool) -> AnySequence { return AnySequence(TakeWhileSequence(self, condition)) } @@ -179,8 +179,8 @@ internal extension AnySequence { - returns: Each element of the sequence in an array */ - func toArray () -> [T] { - var result: [T] = [] + func toArray () -> [Element] { + var result: [Element] = [] for item in self { result.append(item) } @@ -203,8 +203,8 @@ public struct TakeSequence: SequenceType { public func generate() -> AnyGenerator { var count = 0 var generator = self.sequence.generate() - return anyGenerator { - count++ + return AnyGenerator { + count += 1 if count > self.n { return nil } else { @@ -229,7 +229,7 @@ public struct TakeWhileSequence: SequenceType { public func generate() -> AnyGenerator { var generator = self.sequence.generate() var endConditionMet = false - return anyGenerator { + return AnyGenerator { let next: S.Generator.Element? = generator.next() if !endConditionMet { endConditionMet = !self.condition(next) diff --git a/ExSwift/String.swift b/ExSwift/String.swift index 38bea6b..bc6e060 100644 --- a/ExSwift/String.swift +++ b/ExSwift/String.swift @@ -31,7 +31,7 @@ public extension String { return nil } - let range = Range(start: advance(startIndex, range.startIndex), end: advance(startIndex, range.endIndex)) + let range = startIndex.advancedBy(range.startIndex) ..< startIndex.advancedBy(range.endIndex) return self[range] } @@ -91,9 +91,7 @@ public extension String { - returns: Array of substrings */ func explode (separator: Character) -> [String] { - return split(self.characters, isSeparator: { (element: Character) -> Bool in - return element == separator - }).map { String($0) } + return self.characters.split { $0 == separator }.map { String($0) } } /** @@ -215,7 +213,8 @@ public extension String { - parameter charset: Chars to use in the random string - returns: Random string */ - static func random (var length len: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String { + static func random (length: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String { + var len = length if len < 1 { len = Int.random(max: 16)