CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Feb 2016 |
| SPMSupports SPM | ✗ |
Maintained by Khoi.Geeky.
Hookah is a functional library for Swift. It’s inspired by Lo-Dash project.
Hookah.each -> Hookah.forEach Hookah.each<T where T:CollectionType>(collection: T,@noescape iteratee: T.Generator.Element throws -> ()) rethrowsHookah.each([1,2]){ print($0) }
// → log `1` then `2`
let scores = ["khoi" : 82, "quan" : 40, "toan": 90]
Hookah.each(scores){ print($0.0) }
// -> log `khoi` then `quan` then `toan`Iterates over elements of collection invoking iteratee function on each element.
Hookah.eachRight -> Hookah.forEachRight Hookah.eachRight<T where T:CollectionType, T.Index == Int>(collection: T,@noescape iteratee: T.Generator.Element throws -> ()) rethrowsHookah.eachRight([1,2]){ print($0) }
// → log `2` then `1`This is like Hookah.each except that it iterates over elements of collection from right to left.
Hookah.every Hookah.every<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> BoolHookah.every([0, 10, 28]){ $0 % 2 == 0 }
// -> true
let scores = ["khoi" : 82, "quan" : 40, "toan": 90]
Hookah.every(scores){ $0.1 > 50 }
// -> falseChecks if predicate returns true for all elements of collection. Iteration is stopped once predicate returns false.
Returns true if all elements pass the predicate check, else false.
Hookah.filter Hookah.filter<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> [T.Generator.Element]Hookah.filter([1, 2, 4]){ $0 % 2 == 0 }
// -> [2,4]
let scores = ["khoi" : 82, "quan" : 40, "toan": 90]
Hookah.filter(scores){ $0.1 > 50 }
// -> [("khoi", 82), ("toan", 90)]Iterates over elements of collection, returning an array of all elements predicate returns true for.
Returns the new filtered array.
Hookah.find Hookah.find<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> T.Generator.Element?Hookah.find([1, 2, 4]){ $0 % 2 == 0 }
// -> Optional(2)
let scores = ["khoi" : 82, "quan" : 40, "toan": 90]
Hookah.find(scores){ $0.0 == "khoi" }
// -> Optional(("khoi", 82))Iterates over elements of collection, returning the first element predicate returns true for.
Returns the matched element, else nil.
Hookah.findLast Hookah.findLast<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> T.Generator.Element?Hookah.findLast([1, 2, 4]){ $0 % 2 == 0 }
// -> 4This is like Hookah.find except it iterates over the elements of the collection from right to left
Returns the matched element, else nil.
Hookah.groupBy Hookah.groupBy<T where T:CollectionType>(collection: T, @noescape iteratee: T.Generator.Element throws -> String) rethrows -> [String: [T.Generator.Element]]Hookah.groupBy([1,2,3,4,5]){ $0 % 2 == 0 ? "even" : "odd" }
// -> ["odd": [1, 3, 5], "even": [2, 4]]Create a dictionary where the key is a string got by run iteratee through the element, and the value is the arrays of the elements responsible for getting that key
Returns the dictionary [String: [T]]
Hookah.includes Hookah.includes<T where T: CollectionType, T.Generator.Element: Equatable>(collection: T, value: T.Generator.Element) -> BoolHookah.includes([1,2,3,4,5], value: 5)
// -> trueReturn true if value is presented in the collection.
Boolean determined whether the value is presented.
Hookah.map Hookah.map<T: CollectionType, E>(collection: T,@noescape transform: T.Generator.Element throws -> E ) rethrows -> [E]func double(a: Int) -> Int{
return a * 2
}
Hookah.map([1,2,3,4], transform: double)
// -> [2,4,6,8]Creates an array of values by running each element in collection through a transform function.
The new mapped array.
Hookah.reduce Hookah.reduce<T,E where T:CollectionType>(collection: T,initial: E, @noescape combine: (E, T.Generator.Element) throws -> E) rethrows -> EHookah.reduce([1,2,3], initial: 0) { $0 + $1 }
// -> 6
// Thanks for Swift Operator we can do this as well
Hookah.reduce([1,2], initial: 0, combine: +)
// -> 3Reduces collection to a value which is the accumulated result of running each element in collection through iteratee, where each successive invocation is supplied the return value of the previous.
Returns the accumulated value.
Hookah.reduceRight Hookah.reduceRight<T,E where T:CollectionType>(collection: T,initial: E, @noescape combine: (E, T.Generator.Element) throws -> E) rethrows -> EHookah.reduceRight(["foo","bar","baz"], initial: "") {return "\($0)\($1)" }
// -> "bazbarfoo"This method is like Hookah.reduce except that it iterates over elements of collection from right to left.
Returns the accumulated value.
Hookah.reject Hookah.reject<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> [T.Generator.Element]Hookah.reject([1,2,3,4,5]){ $0 % 2 == 0 }
// -> [1,3,5]
let scores = ["khoi" : 82, "quan" : 40, "toan": 90]
Hookah.reject(scores) {$0.1 < 50}
// -> [("khoi", 82), ("toan", 90)]The opposite of Hookah.filter; this method returns the elements of collection that predicate does not return true for.
Returns the new filtered array.
Hookah.sample Hookah.sample<T where T:CollectionType, T.Index == Int>(collection: T) -> T.Generator.ElementHookah.sample([1,2,3,4])
// -> 2Gets a random element from collection.
Return the random element.
Hookah.sampleSize Hookah.sampleSize<T where T:CollectionType, T.Index == Int>(collection: T, n: Int) -> [T.Generator.Element]Hookah.sampleSize([1,2,3,4],n: 2)
// -> [2,4]Gets n random elements from collection.
Using Fisher-Yates shuffle
Array of random elements
Hookah.shuffle Hookah.shuffle<T where T:CollectionType, T.Index == Int>(collection: T) -> [T.Generator.Element]Hookah.shuffle([1,2,3,4])
// -> [2,4,1,3]Creates an array of shuffled values.
Using Fisher-Yates shuffle.
Returns the shuffled array.
Hookah.size Hookah.size<T where T:CollectionType>(collection: T) -> IntHookah.size([1,2,3,4])
// -> 4
Hookah.size(["khoi":1,"toan":2])
// -> 2Return the size of collection.
Complexity: O(1) in most cases. O(n) in worst cases.
The collection size.
Hookah.some Hookah.some<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> BoolHookah.some([11, 10, 22]){ $0 % 2 != 0 }
// -> trueChecks if predicate returns true for ANY element of collection. Iteration is stopped once predicate returns true.
Returns true if any element passes the predicate check, else false.
Hookah.chunk Hookah.chunk<T>(array: [T], size: Int = 0) -> [[T]]Hookah.chunk([1,2,3,4,5],size: 2)
// -> [[1, 2], [3, 4], [5]]Create an array of elements split in to groups by the length of size. If array can’t be split evenly, the final chunk contains all the remain elements.
The new array contains chunks
Hookah.compact Hookah.compact<T>(array: [T?]) -> [T]Hookah.compact([2,3,4,nil,6,7])
// -> [2,3,4,6,7]Create an array with all nil values removed.
The new filtered array.
Hookah.concat (values)Hookah.concat<T>(array: [T], values: T...) -> [T]Hookah.concat([1,2,3], values: 2, 3, 4)
// -> [1,2,3,2,3,4]Creates a new array concatenating additional values.
The new concatenated array.
Hookah.concat (arrays)Hookah.concat<T>(array: [T], arrays: [T]...) -> [T]Hookah.concat(array, arrays: [1,2],[3,4],[0])
// -> [1,1,2,3,4,0]Creates a new array concatenating additional arrays.
The new concatenated array.
Hookah.difference Hookah.difference<T where T:Equatable>(array:[T], values:[T])-> [T]Hookah.difference([3,2,1], values:[4,2])
// -> [3,1]Creates an array of unique array values not included in the other provided arrays.
Returns the new array of filtered values.
Hookah.differenceBy Hookah.differenceBy<T where T:Equatable>(array:[T], values:[T], iteratee:(T->T)) -> [T]Hookah.differenceBy([3.1, 2.2, 1.3], values: [4.4, 2.5], iteratee: floor)
// -> [3.1, 1.3]This method is like Hookah.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which uniqueness is computed.
Returns the new array of filtered values.
Hookah.differenceWith Hookah.differenceWith<T>(array:[T], values:[T], comparator:((T,T)->Bool)) -> [T]func compare(obj1:[String:Int], obj2:[String:Int]) -> Bool {
if obj1["x"] == obj2["x"] && obj1["y"] == obj2["y"] {
return true
}
return false;
}
Hookah.differenceWith([["x":1,"y":2], ["x":2, "y":1]], values: [["x":1, "y":2]], comparator: compare)
// -> [["x":2, "y":1]]This method is like Hookah.difference except that it accepts comparator which is invoked to compare elements of array to values.
Returns the new array of filtered values.
Hookah.drop Hookah.drop<T>(array: [T], n: Int = 1) -> [T]Hookah.drop([1, 2, 3])
// -> [2,3]
Hookah.drop([1, 2, 3], n: 2)
// -> [3]Creates a slice of array with n elements dropped from the beginning.
1 by default.Returns the slice of array.
Hookah.dropRight Hookah.dropRight<T>(array: [T], n: Int = 1) -> [T]Hookah.dropRight([1, 2, 3])
// -> [1,2]
Hookah.dropRight([1, 2, 3], n: 2)
// -> [1]Creates a slice of array with n elements dropped from the end.
1 by default.Returns the slice of array.
Hookah.dropRightWhile Hookah.dropRightWhile<T>(array: [T], predicate: T -> Bool) -> [T]Hookah.dropRightWhile([1, 2, 3, 4, 5]){$0 > 3}
// -> [1,2,3]Creates a slice of array excluding elements dropped from the end. Elements are dropped until predicate returns false.
Returns the slice of array.
Hookah.dropWhile Hookah.dropWhile<T>(array: [T], predicate: T -> Bool) -> [T]Hookah.dropWhile([1, 2, 3, 4, 5]){$0 < 3}
// -> [3,4,5]Creates a slice of array excluding elements dropped from the beginning. Elements are dropped until predicate returns false.
Returns the slice of array.
Hookah.flatMap Hookah.flatMap<T>(array:[T], iteratee:T->[T]) -> [T]func duplicate(num:Int) -> [Int] {
return [num, num]
}
Hookah.flatMap([1,2], iteratee:duplicate)
// -> [1,1,2,2]Creates an array of flattened values by running each element in array through iteratee and concating its result to the other mapped values.
Returns the new array.
Hookah.flatten Hookah.flatten<T>(array: [T]) -> [T]Hookah.flatten([1, [2, 3, [4]]] as [NSObject])
// -> [1, 2, 3, [4]Flatten array one level.
The new flattened array.
Hookah.flattenDeep Hookah.flattenDeep<T>(array: [T]) -> [T]Hookah.flattenDeep([[1],2,[3,[[4]],5],[[6,7],8],[[9]]])
// -> [1,2,3,4,5,6,7,8,9]This method is like Hookah.flatten except that it recursively flattens array.
The new flattened array.
Hookah.fill (value, indexes)Hookah.fill<T>(inout array: [T], value: T, indexes: [Int])var array = [1,2,3,4]
Hookah.fill(&array, value: 0, indexes: [1,3])
print(array)
-> logs [1,0,3,0]Fill elements of array in indexes with value.
NOTE: This method mutates array.
Hookah.fill (value, start, end)Hookah.fill<T>(inout array: [T], value: T, start: Int = 0, end: Int? = nil)var array = [1,2,3,4]
Hookah.fill(&array, value: 0, start: 0, end: 2)
print(array)
-> logs [0,0,3,4]Fill elements of array with value from start upto, but not including end.
NOTE: This method mutates array.
0 by default.nil by default.Hookah.findIndex Hookah.findIndex<T>(array: [T], predicate: T -> Bool) -> IntHookah.findIndex([1,2,3,4]) { $0 % 2 == 0 }
// -> 1 // index of 2This method is like Hookah.find except that it returns the index of the first element predicate returns true for instead of the element itself.
Returns the index of the found element, else -1.
Hookah.findLastIndex Hookah.findLastIndex<T>(array: [T], predicate: T -> Bool) -> IntHookah.findLastIndex([1,2,3,4]) { $0 % 2 == 0 }
// -> 3 // index of 4This method is like Hookah.findIndex except that it iterates over elements of array from right to left.
Returns the index of the found element, else -1.
Hookah.indexOf Hookah.indexOf<T where T:Equatable>(array:[T], value:T, fromIndex:UInt?=nil) -> Int?Hookah.indexOf([1,2,1,2], value:2)
// -> 1 // index of first `2`Gets the index at which the first occurrence of value is found in array.
nil by defaultReturns the index of the found element, else -1.
Hookah.initial Hookah.initial<T>(array:[T]) -> [T]Hookah.initial([1,2,3])
// -> [1,2]Gets all but the last element of array.
Returns the slice of array.
Hookah.intersection Hookah.intersection<T where T:Equatable>(arrays:[T]...) -> [T]Hookah.intersection([2,1], [4,2], [1,2])
// -> [2]Creates an array of unique values that are included in all of the provided arrays.
Returns the new array of shared values.
Hookah.intersectionBy Hookah.intersectionBy<T where T:Equatable>(arrays:[T]..., iteratee:T->T) -> [T]Hookah.intersectionBy([2.1, 1.2], [4.3, 2.4], iteratee:floor)
// -> [2.1]This method is like Hookah.intersection except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed.
Returns the new array of shared values.
Hookah.intersectionWith Hookah.intersectionWith<T>(arrays:[T]..., comparator:(T,T)->Bool) -> [T] func compare(obj1:[String:Int], obj2:[String:Int]) -> Bool {
if obj1["x"] == obj2["x"] && obj1["y"] == obj2["y"] {
return true
}
return false;
}
let a1 = [["x":1, "y":2], ["x":2, "y":1]]
let a2 = [["x":1, "y":1], ["x":1, "y":2]]
Hookah.intersectionWith(a1, a2, comparator: compare)
// -> [["x":1, "y":2]]This method is like Hookah.intersection except that it accepts comparator which is invoked to compare elements of arrays.
Returns the new array of shared values.
Hookah.slice Hookah.slice<T>(array: [T], start: Int, end: Int? = nil) -> [T]Hookah.slice([1,2,3,4,5], start: 0, end: 2)
// -> [1,2]
Hookah.slice([1,2,3,4,5], start: 3)
// -> [4, 5]Create an array by slicing the array from start up to, but not including, end.
nil by default.The sliced array.
Hookah.xor Hookah.xor<T where T:Equatable>(arrays:[T]...) -> [T]Hookah.xor([2,1], [4,2])
// -> [1,4]Creates an array of unique values that is the symmetric difference of the provided arrays.
Returns the new array of values.
Hookah.xorBy Hookah.xorBy<T where T:Equatable>(arrays:[T]..., iteratee:(T->T)) -> [T]Hookah.xorBy([2.1, 1.2], [4.3, 2.4], iteratee: floor)
// -> [1.2, 4.3]This method is like Hookah.xor except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed.
Returns the new array of values.
Hookah.xorWith Hookah.xorWith<T>(arrays:[T]..., comparator:(T,T)->Bool) -> [T]func compare(obj1:[String:Int], obj2:[String:Int]) -> Bool {
if obj1["x"] == obj2["x"] && obj1["y"] == obj2["y"] {
return true
}
return false;
}
let a1 = [["x":1, "y":2], ["x":2, "y":1]]
let a2 = [["x":1, "y":1], ["x":1, "y":2]]
Hookah.xorWith(a1, a2, comparator: compare)
// -> [["x":2,"y":1],["x":1,"y":1]]This method is like Hookah.xor except that it accepts comparator which is invoked to compare elements of arrays.
Returns the new array of values.
All contributions Hookah are extremely welcome. Checkout CONTRIBUTING.md