官网地址:https://github.com/ankurp/Dollar
Dollar 是一个第三方扩展。
包括了。Array,Dictionary,Object,Function,Chaining
导入Dollar 直接使用即可
数组扩展操作
func dollarArrayTest(){
//取对应数组 组成新的数组
print(Dollar.at(["ant","bat","cat","dog","egg"], indexes: [0,2,4]))
//分成多个数组
print(Dollar.chunk([1,2,3,4],size: 2))
print(Dollar.chunk([1,2,3,4],size: 3))
//找出第二个数组中。第一个数组没有的值
print(Dollar.difference([1,2,3,4,5],[5,2,10]))
//根据条件和array分组
print(Dollar.groupBy([1,2,3,4,5], callback: {$0 % 2 }))
//得到数组出现的次数
print(Dollar.frequencies(["a","d","c","a","d","v"]))
//得到数组最大公约数
print(Dollar.gcd(3,9))
//计算交集
print(Dollar.intersection([1,2,3],[5,2,1,4],[2,1]))
//计算并集
print(Dollar.union([1,2,3],[1,23,3]))
//得到数组最小公倍数
print(Dollar.lcm(3, 9))
//配对组合新 出租
print(Dollar.transpose([[1,2,3],[4,5,6]]))
}
输出结果
2020-09-11 22:45:37.001952+0800 安果视频壁纸[6488:367125] [Storyboard] Unknown class Ma in Interface Builder file.
["ant", "cat", "egg"]
[[1, 2], [3, 4]]
[[1, 2, 3], [4]]
[1, 3, 4]
[0: [2, 4], 1: [1, 3, 5]]
["a": 2, "v": 1, "d": 2, "c": 1]
3
[2, 1]
[1, 2, 3, 23]
9
[[1, 4], [2, 5], [3, 6]]
字典扩展操作。
func dollarDictTest(){
let dict:Dictionary<String,Int> = ["Dog":1,"Cat":2]
let dict2:Dictionary<String,Int> = ["Cow":3]
let dict3:Dictionary<String,Int> = ["Sheep":4]
//合并
print(Dollar.merge(dict,dict2,dict3))
// 选择
print(Dollar.pick(["Dog":1,"Cat":2,"Cow":3], keys: "Dog","Cow"))
//省略
print(Dollar.omit(["Dog":1,"Cat":2,"Cow":3], keys: "Cat","Dog"))
}
结果
字典扩展
["Dog": 1, "Cat": 2, "Cow": 3, "Sheep": 4]
["Dog": 1, "Cow": 3]
["Cow": 3]
科里化 curry
func dollarFunctest(){
let curriedAdder = Dollar.curry(adder)
let addTenAnd = curriedAdder(10)
let addThirtyAnd = addTenAnd(20)
print(addThirtyAnd(1))
print(addThirtyAnd(50))
print(addTenAnd(10)(10))
}
结果
curry
31
80
30