Skip to content

Commit

Permalink
Add a merge generator (#42)
Browse files Browse the repository at this point in the history
* add a merge generator

* fix some comments

* Fix comment

* change collect to a struct rather than an anonymous generator func
  • Loading branch information
nicorichard authored Apr 29, 2022
1 parent 59987b5 commit f7e35a8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
17 changes: 12 additions & 5 deletions Sources/Genything/Generators/Combinations/Generators+collect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ extension Generators {
/// - Returns: The generator of Arrays containing values selected in order from the provided generators
public static func collect<G, R>(_ generators: [G], _ transform: @escaping (G.T) -> R) -> AnyGenerator<[R]>
where G: Generator {
AnyGenerator<[R]> { ctx in
generators.map {
$0.next(ctx)
}.map(transform)
}
Collect(sources: generators, transform: transform).eraseToAnyGenerator()
}
}

struct Collect<Source, T>: Generator where Source: Generator {
let sources: [Source]
let transform: (Source.T) -> T

func next(_ randomSource: RandomSource) -> [T] {
sources.map {
$0.next(randomSource)
}.map(transform)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ extension Generators {
/// - Returns: The generator
public static func join<G>(_ generators: [G], separator: String = "") -> AnyGenerator<String> where G: Generator,
G.T == String {
Generators
.collect(generators)
collect(generators)
.map { $0.joined(separator: separator) }
}
}
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...
/// ```
///
/// - 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
}
}
}

0 comments on commit f7e35a8

Please sign in to comment.