Matrices with friendly semantics and a familiar interface
var x: Matrix = [[1, 2, 3], [5, 6, 7]]
x.columns.append([4, 8])
let y = -x * x.transpose
assert(y.determinant == 320)
Lambda calculus functions supporting intensional equality
let truth = Lambda { t in Lambda { f in t } }
let falsity = Lambda { t in Lambda { f in f } }
let not = Lambda { condition in condition[falsity][truth] }
assert(not[falsity] == truth)
Fractional numbers providing precise representations of rationals
let x: Fraction = (5 + 1) / (8 * 2) - 3
let y = x * x + 1/2
let z = x / 0
assert(y == 473/64 && z.isInfinite)
Default-value dictionary for representing collections with many repeated values
let fruitCount = Sparse(["apple": 12, "pear" : 2, "orange" : 5], defaultValue: 0)
assert(fruitCount["apple"] == 12 && fruitCount["bannana"] == 0)
let slice = Sparse<Int, Int>([1 : 5], defaultValue: 0).slice(0..<Int.max)
assert(Array(slice.prefix(3)) == [0, 5, 0])
Generators enabling concatenation, dropping, taking, iterating, cycling, unwrapping, and more!
for x in (1...3) + (7...9) { } // concatenating
for x in ["1.2", "4.6", "blah", "2.9"].map({ Float($0) }).unwrap() { } // unwrapping
for x in (1...10).dropWhile({ $0 <= 3 }) // dropping
// and much, much more!
Value-semantic graph modeling values connected by bidirectional edges
var graph = Graph(nodes: ["Swift", "Haskell", "Rust", "Ruby", "Python"])
codeGraph.addEdge("Swift", "Haskell")
codeGraph.addEdge("Swift", "Rust")
assert(graph.connectedComponents.count == 3)
Type for determining the ordering of two Comparable
values
switch Ordering(a, b) {
case .Ascending: print("It's going up!")
case .Same: print("They're no different.")
case .Descending: print("Down they go!")
}
Bidirectional mapping providing O(1) lookup between values
var map: BidirectionalMap = ["seven" : 7, "three" : 3, "twenty" : 20]
map.associateValues("five", 5)
map.disassociateValues(right: 20)
assert(map["seven"] == 7 && map[7] == "seven")
Generators that can be copied while maintaining independent state
let referenceSemanticGenerator = anyGenerator([1, 2, 3].generate())
let forkableGenerator = BufferingGenerator(bridgedFromGenerator: referenceSemanticGenerator)
let fork = forkableGenerator.fork()
assert(forkableGenerator.next() == 1 && fork.next() == 1)
Counted set for storing an unordered collection of non-unique elements
let bagA: Bag = ["hi", "hello", "hello", "hello", "world"]
let bagB: Bag = ["hello", "hello", "world", "world", "earth"]
let bagC = bagA.intersect(bagB)
assert(bagC.count == 3)
Permutation collection type with mutation support
var array = [1, 2, 3, 4, 5].permute(SwapPermutation(swaps: (0, 1), (2, 3)))
print(array) // -> [2, 1, 4, 3, 5]
array.replaceRange(1...3, with: [10])
print(array) // -> [2, 10, 5]
Lazily shuffled collection for fast random index access
var array = [1, 2, 3, 4, 5].lazy.shuffle()
print(array) // -> [5, 3, 1, 2, 4]
array.shuffle()
print(array) // -> [5, 3, 2, 4, 1]
Sorted array for optimized min, max, and contains operations
var queue: SortedArray = [1, 2, 6, 18, 20]
queue.insert(3)
queue.insert(12)
print(queue) // -> [1, 2, 3, 6, 12, 18, 20]