Comments (5)
BTW, I think the lazy extension could be as simple as:
extension Gen {
public static func lazy(_ gen: @autoclosure () -> Gen<A>) -> Gen<A> {
gen()
}
}
from swiftcheck.
Sorry, I probably didn't explain correctly. The problem is that all of the elements of the array passed to one(...)
are evaluated strictly before the function is called. That's what causes the endless recursion. That wouldn't be a problem in a lazily evaluated language like Haskell.
I'm currently testing this workaround, which seems to be able to do the job at least for the time being:
extension Gen {
public static func lazy(_ gen: @escaping () -> Gen<A>) -> Gen<A> {
return Gen<Bool>.pure(true).flatMap { _ in
return gen()
}
}
}
// then call with:
(Gen<List>.lazy { List.arbitrary }).map ...
from swiftcheck.
Try running it. Since Gen<T>.one(...)
picks the given options at random, the probability of giving you a Stack Overflow is 0. Unless you give it weights such that it's forced to always pick the recursive step.
Also, I'd recommend giving it a weight dependent on how deep you want the average List to be:
- 50% recursion prob: 1/1000 prob that you'll get a length 10+ List.
- 90% recursion prob: 1/3 prob that you'll get a length 10+ List.
from swiftcheck.
Ah! Of course. I see, the compiler is trying to unroll the recursion right away.
Thanks for explaining it again ^^
Yes, making a lazy extension to it like you're doing is probably the best approach.
Maybe SwiftCheck needs a LazyGen? 🤔 I don't know enough about its design, so I couldn't say.
from swiftcheck.
I'd love to see such a functionality included in SwiftCheck. Swift in fact includes laziness to some extent with the use of autoclosures.
Unfortunately this seems to be available only with parameters, not as a general modifier, so I guess you can't have something like an [@autoclosure ()-> Gen<A>]
so you could change the signature of one(of:)
from:
public static func one<S : BidirectionalCollection>(of gs : S) -> Gen<A>
where S.Iterator.Element == Gen<A>, S.Index : RandomType
to:
public static func one<S : BidirectionalCollection>(of gs : S) -> Gen<A>
where S.Iterator.Element == @autoclosure () -> Gen<A>, S.Index : RandomType
Maybe they could add a one(of:)
method with a variable number of @autoclosure arguments the same way zip methods are generated?
from swiftcheck.
Related Issues (20)
- Preferred idiom for testing exception-throwing code HOT 1
- Generator is not used during shrinking. HOT 2
- no Gen.let or heterogeneous sequence/tuple? HOT 7
- `forAll` doesn't support explicit generator being passed HOT 9
- Gen.frequency seems to fixate on a single generator for a single run HOT 1
- passing any checkerargs results in only a single test case running HOT 8
- allow specifying seed so that `generate` can be repeatable. HOT 14
- How exactly to rerun failed examples? HOT 5
- Doesn't compile with Xcode 10.2 HOT 1
- String generator breaks if test not always true HOT 2
- StdGen.genRange seems to have wrong value HOT 2
- Xcode canvas for SwiftUI always fail HOT 1
- Code from README doesn't compile
- Asynchronous Testing
- Integer partition generator
- Xcode 12 or swift 5.3 incompatibility
- SwiftCheck
- Unable to run Playground HOT 1
- (U)Int `arbitrary` implementation provides limited values with `.proliferate(withSize:)`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftcheck.