Skip to content

Commit

Permalink
Swift 5.7 light touch adoption of some new features (#59)
Browse files Browse the repository at this point in the history
* packaging version upgrade

* if lets

* Add a primary associated type for Generator

* Simplify repeated

* Cleanup newlines

* Add an autoclosure type erased initializer (another way to create constants)
  • Loading branch information
nicorichard authored Jan 19, 2023
1 parent 5e5e3d9 commit ebc2021
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.6
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
7 changes: 7 additions & 0 deletions Sources/Genything/AnyGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public struct AnyGenerator<T>: Generator {
wrappedNext = next
}

/// Creates an `AnyGenerator` which generates elements via the provided autoclosure
///
/// - Parameter next: A block which returns the next element
public init(_ next: @autoclosure @escaping () -> T) {
wrappedNext = { _ in next() }
}

/// Produces the next element from this generator using the provided random source
///
/// - Parameter randomSource: A source of random to be used by upstream generators
Expand Down
8 changes: 2 additions & 6 deletions Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,9 @@ extension LazySequence: Arbitrary where Base: Arbitrary {
extension Repeated: Arbitrary where Element: Arbitrary {
/// A generator of `Repeated`s where `Element` conforms to `Arbitrary`
public static var arbitrary: AnyGenerator<Repeated> {
let constructor: (Element, Int) -> Repeated<Element> = { element, count in
repeatElement(element, count: count)
}

return Element.arbitrary
Element.arbitrary
.zip(Int.arbitrary)
.map { t in constructor(t.0, t.1) }
.map(repeatElement(_:count:))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Genything/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// A Generators typically creates values using a `RandomSource`. If the `RandomSource` is not shared between multiple generators, then they are said to be independent e.g. One generator does not affect the randomness of any other generators.
///
/// - Note: The generator protocol has no requirement to be random nor is there any requirement to be stateless. The generator might generate random elements or follow some predetermined pattern. Therefore, sharing a generator between unrelated contexts should be done with care.
public protocol Generator {
public protocol Generator<T> {
/// The type of element which this Generator produces
associatedtype T
/// Produces the next element from this generator using the provided random source
Expand Down
2 changes: 1 addition & 1 deletion Sources/Genything/Operators/Generator+distinct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private struct FIFODistinctMemory<T: Equatable> {

private mutating func append(_ item: T) {
defer {
if let maxSize = maxSize, memory.count > maxSize {
if let maxSize, memory.count > maxSize {
memory.removeFirst()
}
}
Expand Down
3 changes: 0 additions & 3 deletions Sources/Genything/Operators/Generator+map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ extension Generator {
public func map<R>(_ transform: @escaping (T) -> R) -> AnyGenerator<R> {
Generators.Map(source: self, transform: transform).eraseToAnyGenerator()
}



}

extension Generators {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Trickery/Fake+Emails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension Fake {

public static func business(_ name: String? = nil) -> AnyGenerator<String> {
let nameGen: AnyGenerator<String> = {
if let name = name {
if let name {
return Generators.constant(name).eraseToAnyGenerator()
}
return Fake.BusinessNames.any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Generator_DistinctTests: XCTestCase {
var last: Int? // Storage for the last bit we generated
testAllSatisfy(bitGenerator) { bit in
defer { last = bit } // Store the bit for the next comparison
if let last = last, last == bit {
if let last, last == bit {
// If this bit matches the last, the function does not work as expected
return false
}
Expand Down

0 comments on commit ebc2021

Please sign in to comment.