Docs (85% documented)

Reference Reference

TraverSwift

Build Status

A thin framework for swift’s Collection effected by Scala’s Traversable & Haskell’s Data.List

Table of Contents

Gallery

General SequenceType

any & all function

// 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 function

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

subsequence function

// 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 function

// 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 function

// 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 function

// 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 function

// 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 function

// 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 function

// 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 function (for SequenceType wrapped in Implicitly unwrapped optional)

// 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

Equatable element SequenceType

group function

// 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

stripPrefix function

// 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 function

// 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 function

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

intersect function

// 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 operator

// 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

Special element SequenceType

sum & product function for IntegerArithmeticType or FloatingArithmeticType(Custom protocol) element

// 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 function for BooleanType element

// 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

General CollectionType

findIndex function

// 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 function

// 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

Array

existsAny & existsAll function (for array which has element of Optional<T>)

currently, function with argument of SequenceType can’t be implemented with compiler crash.

// 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 function for array of array

currently, function with argument of SequenceType(flatten) can’t be implemented with compiler crash.

// 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

Documentation

Document generated by jazzy is here

Licence

MIT