Hookah是一个用于Swift的功能库。它受到Lo-Dash项目的启发。
Hookah.each
-> Hookah.forEach
Hookah.each<T where T:CollectionType>(collection: T,@noescape iteratee: T.Generator.Element throws -> ()) rethrows
Hookah.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`
遍历集合中的元素,对每个元素调用迭代器函数。
Hookah.eachRight
-> Hookah.forEachRight
Hookah.eachRight<T where T:CollectionType, T.Index == Int>(collection: T,@noescape iteratee: T.Generator.Element throws -> ()) rethrows
Hookah.eachRight([1,2]){ print($0) }
// → log `2` then `1`
这就像Hookah.each,但它是从右到左遍历集合的元素。
Hookah.every
Hookah.every<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> Bool
Hookah.every([0, 10, 28]){ $0 % 2 == 0 }
// -> true
let scores = ["khoi" : 82, "quan" : 40, "toan": 90]
Hookah.every(scores){ $0.1 > 50 }
// -> false
检查谓词是否对所有集合元素返回true。一旦谓词返回false,迭代将停止。
如果没有元素未通过谓词检查,则返回true,否则为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)]
遍历集合中的元素,返回一个数组,其中包含谓词返回true的所有元素。
返回新的过滤数组。
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))
遍历集合中的元素,返回第一个使谓词返回 true 的元素。
返回匹配的元素,否则返回 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 }
// -> 4
与 Hookah.find 类似,但它从右向左遍历集合的元素
返回匹配的元素,否则返回 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]]
创建一个字典,其键是由运行迭代器通过元素得到的字符串,其值是由获得该键的元素组成的数组
返回 [String: [T]] 字典。
Hookah.includes
Hookah.includes<T where T: CollectionType, T.Generator.Element: Equatable>(collection: T, value: T.Generator.Element) -> Bool
Hookah.includes([1,2,3,4,5], value: 5)
// -> true
如果值在集合中,则返回 true。
布尔值,确定值是否出现。
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]
通过运行转换函数对集合中的每个元素进行操作来创建一个值数组。
返回新映射的数组。
Hookah.reduce
Hookah.reduce<T,E where T:CollectionType>(collection: T,initial: E, @noescape combine: (E, T.Generator.Element) throws -> E) rethrows -> E
Hookah.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: +)
// -> 3
将集合减少到通过运行迭代器通过集合中的每个元素得到的累积结果的价值。
返回累积值。
Hookah.reduceRight
Hookah.reduceRight<T,E where T:CollectionType>(collection: T,initial: E, @noescape combine: (E, T.Generator.Element) throws -> E) rethrows -> E
Hookah.reduceRight(["foo","bar","baz"], initial: "") {return "\($0)\($1)" }
// -> "bazbarfoo"
此方法与 Hookah.reduce 类似,但它从右向左遍历集合的元素。
返回累积值。
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)]
与 Hookah.filter 的相反操作;此方法返回对于预测条件不返回 true 的集合元素。
返回新的过滤数组。
Hookah.sample
Hookah.sample<T where T:CollectionType, T.Index == Int>(collection: T) -> T.Generator.Element
Hookah.sample([1,2,3,4])
// -> 2
从集合中获取一个随机元素。
返回随机元素。
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]
从集合中获取 n 个随机元素。
随机元素数组
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]
创建一个洗牌值数组。
返回洗牌后的数组。
Hookah.size
Hookah.size<T where T:CollectionType>(collection: T) -> Int
Hookah.size([1,2,3,4])
// -> 4
Hookah.size(["khoi":1,"toan":2])
// -> 2
返回集合的大小。
复杂度:在大多数情况下为 O(1)。在最坏的情况下为 O(n)。
集合大小。
Hookah.some
Hookah.some<T where T:CollectionType>(collection: T,@noescape predicate: T.Generator.Element throws -> Bool) rethrows -> Bool
Hookah.some([11, 10, 22]){ $0 % 2 != 0 }
// -> true
检查预测条件是否对集合中任何元素返回 true。一旦预测条件返回 true,迭代将停止。
如果任何元素通过预测检查返回 true,则返回 true,否则返回 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]]
创建一个元素数组,根据 size 的长度分割成组。如果数组不能平均分割,则最后一个块包含所有剩余元素。
新数组包含块
Hookah.compact
Hookah.compact<T>(array: [T?]) -> [T]
Hookah.compact([2,3,4,nil,6,7])
// -> [2,3,4,6,7]
创建一个移除所有nil值的数组。
新的过滤数组。
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]
创建一个新的数组,将额外值连接到数组中。
新的连接数组。
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]
创建一个新的数组,将额外的数组连接到数组中。
新的连接数组。
Hookah.difference
Hookah.difference<T where T:Equatable>(array:[T], values:[T])-> [T]
Hookah.difference([3,2,1], values:[4,2])
// -> [3,1]
创建一个不包含在其他提供的数组中的唯一数组值的数组。
返回新的过滤值数组。
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]
此方法与Hookah.difference类似,但它接受iteratee,该iteratee对数组的每个元素以及值进行调用,以生成计算唯一性的标准。
返回新的过滤值数组。
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]]
此方法与Hookah.difference类似,不同之处在于它接受一个比较器,用于调用比较数组元素到值的比较。
返回新的过滤值数组。
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]
创建一个从数组开头删除n个元素的数组片段。
1
。返回数组的片段。
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]
从数组的末尾删除n个元素,创建一个切片。
1
。返回数组的片段。
Hookah.dropRightWhile
Hookah.dropRightWhile<T>(array: [T], predicate: T -> Bool) -> [T]
Hookah.dropRightWhile([1, 2, 3, 4, 5]){$0 > 3}
// -> [1,2,3]
创建一个切片,排除从末尾删除的元素。直到断言函数返回false,元素将被删除。
返回数组的片段。
Hookah.dropWhile
Hookah.dropWhile<T>(array: [T], predicate: T -> Bool) -> [T]
Hookah.dropWhile([1, 2, 3, 4, 5]){$0 < 3}
// -> [3,4,5]
创建一个切片,排除从数组开始处删除的元素。直到断言函数返回false,元素将被删除。
返回数组的片段。
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]
通过遍历数组中的每个元素,通过迭代函数运行它们的值并将结果连接到其他映射值,创建一个扁平化的值数组。
返回新的数组。
Hookah.flatten
Hookah.flatten<T>(array: [T]) -> [T]
Hookah.flatten([1, [2, 3, [4]]] as [NSObject])
// -> [1, 2, 3, [4]
展开数组一层。
返回新的展开数组。
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]
此方法类似于Hookah.flatten,不同之处在于它会递归地展开数组。
返回新的展开数组。
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]
用值填充数组的指定索引的元素。
注意:此方法会修改数组。
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]
从开始位置到但不包括结束位置用值填充数组的元素。
注意:此方法会修改数组。
0
。nil
。Hookah.findIndex
Hookah.findIndex<T>(array: [T], predicate: T -> Bool) -> Int
Hookah.findIndex([1,2,3,4]) { $0 % 2 == 0 }
// -> 1 // index of 2
这种方法类似于Hookah.find,但返回的是第一个通过谓词返回true的元素的索引,而不是元素本身。
返回找到的元素的索引,否则返回-1。
Hookah.findLastIndex
Hookah.findLastIndex<T>(array: [T], predicate: T -> Bool) -> Int
Hookah.findLastIndex([1,2,3,4]) { $0 % 2 == 0 }
// -> 3 // index of 4
这种方法类似于Hookah.findIndex,但它以从右到左的顺序遍历数组的元素。
返回找到的元素的索引,否则返回-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`
获取在数组中找到第一个值值的位置。
nil
。返回找到的元素的索引,否则返回-1。
Hookah.initial
Hookah.initial<T>(array:[T]) -> [T]
Hookah.initial([1,2,3])
// -> [1,2]
获取数组中除了最后一个元素之外的所有元素。
返回数组的片段。
Hookah.intersection
Hookah.intersection<T where T:Equatable>(arrays:[T]...) -> [T]
Hookah.intersection([2,1], [4,2], [1,2])
// -> [2]
创建一个数组,该数组包含所有提供的数组中包含的唯一值。
返回包含共享值的新的数组。
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]
此方法类似于Hookah.intersection,但它接受一个迭代器,该迭代器被用于每个数组的每个元素,以生成计算唯一性的标准。
返回包含共享值的新的数组。
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]]
此方法类似于Hookah.intersection,但它接受比较器来比较数组元素。
返回包含共享值的新的数组。
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]
通过将数组从起始位置开始切片,但不包括结束位置来创建一个数组。
nil
。切割后的数组。
Hookah.xor
Hookah.xor<T where T:Equatable>(arrays:[T]...) -> [T]
Hookah.xor([2,1], [4,2])
// -> [1,4]
创建一个新数组,其值为所提供数组的对称差集。
返回新的值数组。
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]
此方法类似于 Hookah.xor,但它接受迭代器,该迭代器对每个数组的每个元素进行调用,生成计算唯一性所依据的标准。
返回新的值数组。
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]]
此方法类似于 Hookah.xor,但它接受比较器来比较数组的元素。
返回新的值数组。
Hookah 非常欢迎所有的贡献。查看 CONTRIBUTING.md