Skip to content
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

Add a merge generator #42

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 31 additions & 0 deletions Sources/Genything/Generators/Combinations/Generators+merge.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// MARK: Combine

extension Generators {
/// Returns: A generator which produces results by looping over the provided generators in order
///
/// # Example
///
/// ```swift
/// let genA = Generators.constant("A")
/// let genB = Generators.constant("B")
///
/// Generators.merge(genA, genB) // Produces values ABABABABABAB...
Copy link
Contributor

Choose a reason for hiding this comment

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

little typo: Generators.merge([genA, genB])

/// ```
///
/// - Returns: The generator
public static func merge<G>(_ generators: [G]) -> AnyGenerator<G.T> where G: Generator {
Merge(generators).eraseToAnyGenerator()
}
}

class Merge<Source>: Generator where Source: Generator {
private let loop: Loop<[Source]>

init(_ sources: [Source]) {
self.loop = Loop(sources)
}

func next(_ randomSource: RandomSource) -> Source.T {
loop.next(randomSource).next(randomSource)
}
}
2 changes: 1 addition & 1 deletion Sources/Genything/Generators/Generators+iterate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Generators {

/// A Generator that generates a given `Sequence` of elements. When the generator exhausts the elements in the sequence, it will begin to return nil.
///
/// - Warning: This generator is stateful and cannot be restarted. Pay attention to how it to how the reference is being retained and do not share the generator to unsuspecting consumers.
/// - Warning: This generator is stateful and cannot be restarted. Pay attention to how the reference is being retained and do not share the generator to unsuspecting consumers.
private class Iterate<Elements>: Generator where Elements: Swift.Sequence {

/// Creates a Generator for a sequence of elements.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Genything/Generators/Generators+loop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extension Generators {
///
/// Since this generator will comprehensibly examine all elements of the collection it can be used to display all possible configurations or test all possible values when the problem space is small and known.
///
/// - Warning: This generator is stateful and cannot be restarted. Pay attention to how it to how the reference is being retained and do not share the generator to unsuspecting consumers.
private class Loop<Elements>: Generator where Elements: Swift.Collection {
/// - Warning: This generator is stateful and cannot be restarted. Pay attention to how the reference is being retained and do not share the generator to unsuspecting consumers.
class Loop<Elements>: Generator where Elements: Swift.Collection {

/// Creates a Generator that loops the provided collection of elements.
///
Expand Down
13 changes: 13 additions & 0 deletions Tests/GenythingTests/Generators/Generators_CollectTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import XCTest
@testable import Genything

final class Generators_CollectTests: XCTestCase {
func test_collecting_to_alternate_generators() {
testAllSatisfy(Generators.collect([
Generators.constant(0),
Generators.constant(1)
])) { values in
values[0] == 0 && values[1] == 1
}
}
}
17 changes: 17 additions & 0 deletions Tests/GenythingTests/Generators/Generators_MergeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import XCTest
@testable import Genything

final class Generators_MergeTests: XCTestCase {
func test_merging_generators() {
var curr = 0
testAllSatisfy(Generators.merge([
Generators.constant(0),
Generators.constant(1)
])) { value in
defer {
curr = (curr + 1) % 2
}
return value == curr
}
}
}