Skip to content

Correct Chunked documentation #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Guides/Chunked.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ collection conforms.

### Complexity

The eager methods are O(_n_), the lazy methods are O(_1_).
The eager methods are O(_n_) where _n_ is the number of elements in the
collection. The lazy methods are O(_n_) because the start index is pre-computed.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There definitely is a difference in complexity between the eager and lazy functions, but I’m finding it difficult to describe succinctly.

The eager functions are O(n) where n is the number of elements in the collection.

The lazy functions on the order of O(n) since the start index could require iterating to the very end of the collection. Each iteration of the receiving lazy sequence is also on the order of O(n), but total n where n is the number of elements in the collection—not n2.


### Naming

Expand Down
19 changes: 17 additions & 2 deletions Sources/Algorithms/Chunked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ extension LazySequenceProtocol where Self: Collection, Elements: Collection {
/// Returns a lazy collection of subsequences of this collection, chunked by
/// the given predicate.
///
/// - Parameter belongInSameGroup: A closure that takes two adjacent elements
/// of the sequence and returns whether or not they belong in the same group.
///
/// - Complexity: O(*n*), because the start index is pre-computed.
@inlinable
public func chunked(
Expand All @@ -218,7 +221,11 @@ extension LazySequenceProtocol where Self: Collection, Elements: Collection {
}

/// Returns a lazy collection of subsequences of this collection, chunked by
/// grouping elements that project to the same value.
/// grouping elements that project to the equal values.
///
/// - Parameter projection: A closure that takes an element in the sequence
/// and returns an `Equatable` value that can be used to determine if adjacent
/// elements belong in the same group.
///
/// - Complexity: O(*n*), because the start index is pre-computed.
@inlinable
Expand All @@ -239,6 +246,10 @@ extension Collection {
/// Returns a collection of subsequences of this collection, chunked by the
/// given predicate.
///
/// - Parameter belongInSameGroup: A closure that takes two adjacent elements
/// of the collection and returns whether or not they belong in the same
/// group.
///
/// - Complexity: O(*n*), where *n* is the length of this collection.
@inlinable
public func chunked(
Expand Down Expand Up @@ -266,7 +277,11 @@ extension Collection {
}

/// Returns a collection of subsequences of this collection, chunked by
/// grouping elements that project to the same value.
/// grouping elements that project to equal values.
///
/// - Parameter projection: A closure that takes an element in the collection
/// and returns an `Equatable` value that can be used to determine if adjacent
/// elements belong in the same group.
///
/// - Complexity: O(*n*), where *n* is the length of this collection.
@inlinable
Expand Down