Cluster
is a Swift library that provides a type for grouping an element and its associated values in an efficient and flexible way. The primary element of a cluster is stored separately from its associated secondary elements, making it easy to access the primary value without iterating through the entire cluster.
Think of it as being a subversion of the Zip2Sequence
but where Seqence1
is an always available variable of same value type as Seqence2
.
open class Cluster<Element> {
public var primary: Element
public var secondaries: [Element]
}
To install Cluster
using the Swift Package Manager, add the following line to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/Iliasnolsson/Cluster", from: "1.0.0"..<"2.0.0")
]
Alternatively, you can manually add the Cluster.swift
file to your project.
Here are some examples of how to use Cluster
.
To create a cluster, you can use one of the provided initializers. For example, to create a cluster with a primary element and no secondary elements:
let cluster = Cluster("A")
To create a cluster with a primary element and an array of secondary elements:
let cluster = Cluster(primary: "A", secondaries: ["B", "C"])
To create a cluster from an array of elements:
let array = ["A", "B", "C"]
let cluster = Cluster(array: array)
To access the primary element of a cluster:
let cluster = Cluster(primary: "A", secondaries: ["B", "C"])
let primary = cluster.primary // "A"
To access the secondary elements of a cluster:
let cluster = Cluster(primary: "A", secondaries: ["B", "C"])
let secondaries = cluster.secondaries // ["B", "C"]
To access all elements of a cluster:
let cluster = Cluster(primary: "A", secondaries: ["B", "C"])
let array = cluster.array() // ["A", "B", "C"]
To map the elements of a cluster using a closure:
let cluster = Cluster(primary: "Hello", secondaries: ["World"])
let mapped = cluster.map({ $0.count }) // Cluster<Int>(primary: 5, secondaries: [5])
To map the elements of a cluster using another cluster and a closure:
let cluster1 = Cluster(primary: 2, secondaries: [4, 6, 8])
let cluster2 = Cluster(primary: 1, secondaries: [3, 5, 7])
let mapped = try cluster1.map(with: cluster2, { $0 * $1 }) // Cluster<Int>(primary: 2, secondaries: [12, 30, 56])
To compact map the elements of a cluster using a closure:
let cluster = Cluster(primary: "Hello", secondaries: ["World", ""])
let compactMapped = try cluster.compactMap({ $0.isEmpty ? nil : $0.count }) // Cluster<Int>(primary: 5, secondaries: [5])
To check if all elements of a cluster satisfy a given condition:
let cluster = Cluster(primary: "Hello", secondaries: ["World"])
let allSatisfy = cluster.allSatisfy({ $0 is String }) // true
To check if all secondary elements of a cluster have the same value as the primary element for a given property:
let cluster = Cluster(primary: "Hello", secondaries: ["Hello", "Hello"])
let allEquals
You stopped typing at the title "Testing Elements" in the readme. Can you continue
Sure, sorry about that! Here's the complete Testing Elements
section:
To check if all elements of a cluster satisfy a given condition:
let cluster = Cluster(primary: "Hello", secondaries: ["World"])
let allSatisfy = cluster.allSatisfy({ $0 is String }) // true
To check if all secondary elements of a cluster have the same value as the primary element for a given property:
let cluster = Cluster(primary: "Hello", secondaries: ["Hello", "Hello"])
let allEquals = cluster.allEqualsSameValue({ $0.count }) // true
To find the minimum element of a cluster:
let cluster = Cluster(primary: "Hello", secondaries: ["World", ""])
let minElement = try cluster.min(by: { $0.count < $1.count }) // ""
To find the maximum element of a cluster:
let cluster = Cluster(primary: "Hello", secondaries: ["World", ""])
let maxElement = try cluster.max(by: { $0.count < $1.count }) // "Hello"
Cluster
also conforms to the Codable
protocol, making it easy to encode and decode instances of the type using JSONEncoder
and JSONDecoder
. For example:
let cluster = Cluster(primary: "A", secondaries: ["B", "C"])
let encoder = JSONEncoder()
let data = try encoder.encode(cluster)
let decoder = JSONDecoder()
let decodedCluster = try decoder.decode(Cluster<String>.self, from: data)
Contributions to Cluster
are welcome and encouraged! If you have an idea for a new feature or improvement, please open an issue or pull request.
Cluster
is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 0 |
Last commit: 2 weeks ago |
Smaller patch to now make cluster primary & secondaries mutable
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics