Skip to content

Commit 66ec968

Browse files
Added documentation and where clause for API uniqued() (#32)
1 parent 009393d commit 66ec968

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Sources/Algorithms/Unique.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515

1616
extension Sequence where Element: Hashable {
1717
/// Returns an array with only the unique elements of this sequence, in the
18-
/// order of the first occurence of each unique element.
18+
/// order of the first occurrence of each unique element.
19+
///
20+
/// let animals = ["dog", "pig", "cat", "ox", "dog", "cat"]
21+
/// let uniqued = animals.uniqued()
22+
/// print(uniqued)
23+
/// // Prints '["dog", "pig", "cat", "ox"]'
24+
///
25+
/// - Returns: An array with only the unique elements of this sequence.
26+
/// .
27+
/// - Complexity: O(*n*), where *n* is the length of the sequence.
1928
@inlinable
2029
public func uniqued() -> [Element] {
2130
uniqued(on: { $0 })
@@ -24,8 +33,26 @@ extension Sequence where Element: Hashable {
2433

2534
extension Sequence {
2635
/// Returns an array with the unique elements of this sequence (as determined
27-
/// by the given projection), in the order of the first occurence of each
36+
/// by the given projection), in the order of the first occurrence of each
2837
/// unique element.
38+
///
39+
/// This example finds the elements of the `animals` array with unique
40+
/// first characters:
41+
///
42+
/// let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]
43+
/// let uniqued = animals.uniqued(on: {$0.first})
44+
/// print(uniqued)
45+
/// // Prints '["dog", "pig", "cat", "ox"]'
46+
///
47+
/// - Parameter projection: A closure that transforms an element into the
48+
/// value to use for uniqueness. If `projection` returns the same value
49+
/// for two different elements, the second element will be excluded
50+
/// from the resulting array.
51+
///
52+
/// - Returns: An array with only the unique elements of this sequence, as
53+
/// determined by the result of `projection` for each element.
54+
///
55+
/// - Complexity: O(*n*), where *n* is the length of the sequence.
2956
@inlinable
3057
public func uniqued<Subject: Hashable>(
3158
on projection: (Element) throws -> Subject

0 commit comments

Comments
 (0)