TraverSwift 0.5

TraverSwift 0.5

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2015年1月
SPM支持 SPM

yad 维护。



  • UsrNameu1

一个轻量级框架,用于 Swift 的集合,受 Scala 的 Traversable 和 Haskell 的 Data.List 影响

目录

画廊

通用 SequenceType

any & all 函数

// at least one of the elements holds condition
let seq = [1,2,3,4,5,6,7]
any (seq) { a in a > 6 } // true

// all of the elements hold condition
all (seq) { a in a > 0 } // true

intersperse 函数

// returns interspersed array
let str = "abcde"
let result = intersperse(str, ",")
result == ["a",",","b",",","c",",","d",",","e"] // true

subsequence 函数

// returns subsequence array
let seq = [1,2,3]
let result = subsequences(seq)
result == [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] // true

scan 函数

// returns array of reduce result
let seq1 = SequenceOf([4, 2, 4])
scan(seq1, 64, /) == [64, 16, 8, 2] // true
scan(seq2, 4) { x, y in 2 * x + y } == [4, 9, 20, 43] // true

flatMap 函数

// returns flattened array of sequences transformed from sequence's elements 
let seq1 = SequenceOf([4, 2, 3])
let result = flatMap(seq1) { elem in SequenceOf([elem, elem * 10 + elem]) }
result == [4,44,2,22,3,33] // true

groupBy 函数

// returns array of array for seqence grouped by second condition
let seq1 = SequenceOf([1,2,3,4,5,6,7,8,9])
let result1 = groupBy(seq1, <)
result1 == [[1,2,3,4,5,6,7,8,9]] // true
let result2 = groupBy(seq1, >)
result2 == [[1],[2],[3],[4],[5],[6],[7],[8],[9]] // true

let seq2 = SequenceOf([5,6,1,3,4,5,6,7,8,9,10])
let result3 = groupBy(seq2, <)
result3 == [[5,6], [1,3,4,5,6,7,8,9,10]] // true
let result4 = groupBy(seq2, >)
result4 == [[5],[6,1],[3],[4],[5],[6],[7],[8],[9],[10]] // true

tail & rtail 函数

// get elements except first
let seq1 = SequenceOf([1, 2, 3, 4, 5, 6])
tail(seq1) == [2, 3, 4, 5, 6] // true

// get elements except last
rtail(seq1) == [1, 2, 3, 4, 5] // true

tails & rtails 函数

// get all final segments 
let seq1 = SequenceOf([1, 2, 3, 4])
tails(seq1) == [[1,2,3,4],[2,3,4],[3,4],[4],[]] // true

// get all initial segments
rtails(seq1) == [[],[1],[1,2],[1,2,3],[1,2,3,4]] // true

takeWhile & dropWhile & span 函数

// get the longest prefix in which elements all hold given condition
let seq1 = [1,2,3,4,1,2,3,4]
takeWhile(seq1) { elem in elem < 3 } == [1,2] // true

// get the remaining of takeWhile
dropWhile(seq1) { elem in elem < 3 } == [3,4,1,2,3,4] // true

// get pair of takeWhile & dropWhile function
let result1 = span(seq1) { elem in elem < 3 }
result1.0 == [1, 2] // true
result1.1 == [3,4,1,2,3,4] // true

cast 函数 (对于 包装在隐式解包的可选 SequenceType)

// cast fail for compounded collection
let objs: [NSObject]! = [NSString(string: "aaa"), NSNumber(int: 123), NSString(string: "ag")]
cast(objs, NSString.self) == nil // true

// cast succceeds for pure collection
let strs: [NSObject]! = [NSString(string: "aaa"), NSString(), NSString(string: "ag")]
cast(strs, NSString.self) != nil // true

等价元素 SequenceType

分组函数

// returns array of array for seqence grouped by equal operator
let seq1 = SequenceOf([1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7])
let result1 = group(seq1)
result1 == [[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[7]] // true

移除前缀函数

// returns .Some after the prefix if the sequence start with prefix given
stripPrefix("foobar", "foo")! == ["b","a","r"] // true
stripPrefix("foo", "foo")! == [] // true

// returns .None if the sequence don't start with prefix given
stripPrefix("barfoo", "foo") == nil // true
stripPrefix("barfoobaz", "foo") == nil // true

distinct函数

// returns array without duplicated elements
distinct([1,2,3,4,5,4,3,2,3,1,0]) == [1,2,3,4,5,0] // true
distinct("bannana") == ["b", "a", "n"] // true

union函数

// returns union without duplicated elements
union([1,2,3], [2, 3, 4]) == [1,2,3,4] // true

intersect函数

// returns intersection for two sequences
intersect([1,2,3,4], [2,4,6,8]) == [2,4] // true

// returns result which contains duplicate if first sequence does
intersect([1,2,2,3,4], [6,4,4,2]) == [2,2,4] // true

equel运算符

// check whether all elements equal in the same order
let seq1 = SequenceOf([1,2,3,4,5,6])
let seq2 = SequenceOf([1,2,3,4,5,6])  
seq1 == seq2 // true

特殊元素 SequenceType

整数算术类型或浮点算术类型求和 & 乘积函数(Custom协议)元素

// sum for all elements
let seq1 = SequenceOf([1,2,3,4,5,6])
sum(seq1) == 21 // true
let seq2 = SequenceOf([1.1,2.2,3.3,4.0,5.2,6.1])
sum(seq2) == 21.9 // true

// product for all elements
let seq3 = SequenceOf([1.0,2.2,3.0,4.1,5.0,6.0])
product(seq1) == 720 // true
product(seq3) == 1.0 * 2.2 * 3.0 * 4.1 * 5.0 * 6.0 // true

布尔类型元素的and & or函数

// check whether all elements are true
let seq1 = SequenceOf([true, true, true, true])
and(seq1) // true

// check whether at least one element is true
let seq2 = SequenceOf([false, true, false, false])
or(seq2) // true

通用集合类型

findIndex函数

// index for element of collection which satisfies a given condition first
let col1 = [1,2,3,4,4,6]
findIndex(col1) { elem in elem > 3 }! == 3 // true

// nil when no element satisfies condition
findIndex(col1) { elem in elem > 6 } == nil // true

take & drop & split函数

// get prefix elements indexed before given index
let col1 = [1,2,3,4,5,6,7]
take(col1, 3) == [1, 2, 3] // true
take(col1, 7) == [1, 2, 3, 4, 5, 6, 7] // true

// get suffix elements indexed after and equal to given index
drop(col1, 3) == [4, 5, 6, 7] // true
drop(col1, 0) == [1, 2, 3, 4, 5, 6, 7] // true

// get pair of take & drop function
let result1 = splitAt(col1, 3)
result1.0 == [1, 2, 3] // true
result1.1 == [4, 5, 6, 7] // true

数组

existsAny & existsAll函数(对于包含Optional元素的数组)

当前,具有SequenceType参数的函数无法通过编译器崩溃实现。

// one of the elements is .Some
let arr1 = ["1e3","123","rf3","rf3"].map{ str in str.toInt() }
existsAny(arr1) // true
// all of elements are .Some
let arr2 = ["13","123","3","312"].map{ str in str.toInt() }
existsAll(arr2) // true

concat函数(数组为数组)

当前,具有SequenceType参数(flatten)的函数无法通过编译器崩溃实现。

// array of concatenation
let arr1 = [[1,2,3],[4,5,6],[7,8,9]]
concat(arr1) == [1,2,3,4,5,6,7,8,9] // true

文档

jazzy生成的文档在这里

许可证

MIT