-
Notifications
You must be signed in to change notification settings - Fork 407
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
Add Gen.fix to make recursive generators cleaner #616
Conversation
What's the example do? It's building lists that terminate with numbers higher than 5? val fixGen =
Gen.fix[List[Int]] { recurse =>
Gen.choose(0, 10).flatMap { idx =>
if (idx < 5) recurse.map(idx :: _)
else Gen.const(idx :: Nil)
}
} |
Yes. I didn’t want to make a longer example such as a tree or JSON or something in the comment and I wanted to test the same thing that I put in the comment. It’s probably not a great example because in this case there are ways to do this without recursion. |
Yeah, I was also wondering if there was a better example to use. I do like this example for one reason: Writing with existing combinators is clunky, because I tried: val fixGen = {
Gen.nonEmptyListOf(Gen.choose(0, 9)).map { ns =>
val idx = ns.indexWhere(_ >= 5)
ns.take(if (idx == -1) ns.size - 1 else idx + 1)
}
} Maybe there is a built-in generator that could be rewritten using this syntax as an example? |
This construct seems pretty useful. Should we elevate it by giving it a better name for visibility sake? Could we rename to something like val listGen = Gen.of { lists: Gen[List[Int]] =>
...
} The best name I came up with is |
Fix is the name for this traditionally: fix(f) = f(fix(f)). I think the name should explain something about recursion. If you aren’t doing recursion you don’t need this. What about |
I agree. I guess I prefer a verb or an adverb, so even just PS. I'm familiar with fixed point since I used to Scheme occasionally. I won't suggest |
After some time, I haven't had any strong feelings about a better name, so should we go with |
fast-check calls it |
That would seem to give evidence for naming it |
same idea as typelevel/cats#3208 but particular to Gen.
Gen and Parsers are what motivated the 3208 PR.