-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add a merge generator * fix some comments * Fix comment * change collect to a struct rather than an anonymous generator func
- Loading branch information
1 parent
59987b5
commit f7e35a8
Showing
7 changed files
with
77 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Sources/Genything/Generators/Combinations/Generators+merge.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Tests/GenythingTests/Generators/Generators_CollectTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
Tests/GenythingTests/Generators/Generators_MergeTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |