Skip to content

Parameter packs for zip functions #31

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 0 additions & 13 deletions Sources/Gen/Gen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,6 @@ extension Gen {
}
}

/// Combines two generators into a single one.
///
/// - Parameters:
/// - a: A generator of `A`s.
/// - b: A generator of `B`s.
/// - Returns: A generator of `(A, B)` pairs.
@inlinable
public func zip<A, B>(_ a: Gen<A>, _ b: Gen<B>) -> Gen<(A, B)> {
return Gen<(A, B)> { rng in
(a._run(&rng), b._run(&rng))
}
}

extension Gen {
/// Transforms a generator of `Value`s into a generator of `NewValue`s by transforming a value into a generator of `NewValue`s.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/Gen/UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

extension Gen where Value == UIColor {
public static let color = zip(.float(in: 0...1), .float(in: 0...1), .float(in: 0...1))
.map { UIColor(red: $0, green: $1, blue: $2, alpha: 1) }
.map { (rgb: (Double, Double, Double)) in UIColor(red: rgb.0, green: rgb.1, blue: rgb.2, alpha: 1) }
}
#endif
8 changes: 4 additions & 4 deletions Sources/Gen/Xoshiro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public struct Xoshiro: RandomNumberGenerator {
@inlinable
public init() {
self.state = zip(
.int(in: .min ... .max),
.int(in: .min ... .max),
.int(in: .min ... .max),
.int(in: .min ... .max)
Gen<UInt64>.int(in: .min ... .max),
Gen<UInt64>.int(in: .min ... .max),
Gen<UInt64>.int(in: .min ... .max),
Gen<UInt64>.int(in: .min ... .max)
)
.run()
}
Expand Down
26 changes: 26 additions & 0 deletions Sources/Gen/Zip.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
#if swift(>=5.9)
/// Combines multiple generators into a single one that produces a tuple containing values from each input generator.
///
/// - Parameter gens: A variadic sequence of generators, each producing a value of a distinct type.
/// - Returns: A generator that produces a tuple of values generated by each input generator in the order they were provided.
@inlinable
public func zip<each Value>(_ gens: repeat Gen<each Value>) -> Gen<(repeat each Value)> {
return Gen<(repeat each Value)> { rng in
return (repeat (each gens)._run(&rng))
}
}
#else
/// Combines two generators into a single one.
///
/// - Parameters:
/// - a: A generator of `A`s.
/// - b: A generator of `B`s.
/// - Returns: A generator of `(A, B)` pairs.
@inlinable
public func zip<A, B>(_ a: Gen<A>, _ b: Gen<B>) -> Gen<(A, B)> {
return Gen<(A, B)> { rng in
(a._run(&rng), b._run(&rng))
}
}

@inlinable
public func zip<A, B, C>(
_ a: Gen<A>,
Expand Down Expand Up @@ -113,3 +138,4 @@ public func zip<A, B, C, D, E, F, G, H, I, J>(
{
return zip(zip(a, b), c, d, e, f, g, h, i, j).map { ($0.0, $0.1, $1, $2, $3, $4, $5, $6, $7, $8) }
}
#endif