-
Notifications
You must be signed in to change notification settings - Fork 405
Introduce Gen.zipWith
multi-arity method
#1062
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
Conversation
Is this intended to behave similarly to mapN, but without the Cats dependency? |
Pretty much. To be more accurate, it is supposed to resemble functions case class Foo(pos: Int, ascii: String, hex: String)
val gen1 = (Gen.posNum[Int], Gen.asciiStr, Gen.hexStr).mapN { Foo.apply }
val gen2 = Apply[Gen].map3(Gen.posNum[Int], Gen.asciiStr, Gen.hexStr) { Foo.apply }
val gen3 = Gen.zipWith(Gen.posNum[Int], Gen.asciiStr, Gen.hexStr) { Foo.apply } However, there's one catch with the Cats functions: they both use a chain of |
e206580
to
cad9980
Compare
3a3f79f
to
f48b7aa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request introduces a multi-arity generator method, Gen.zipWith, which lets users combine multiple generators and apply a mapping function directly without creating an intermediary tuple.
- Adds a new zipWith method implementation in codegen.scala that constructs a chain of flatMap/map calls based on the supplied arity.
- Updates tests in GenSpecification.scala to validate zipWith for various arities, from 2 to 22.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
project/codegen.scala | Introduces the zipWith generator, constructing composed generator expressions via flatMap and map calls. |
core/jvm/src/test/scala/org/scalacheck/GenSpecification.scala | Adds property tests for zipWith with multiple arities to ensure correctness. |
Comments suppressed due to low confidence (1)
project/codegen.scala:120
- [nitpick] Consider adding an inline comment to clarify the purpose and structure of destructuring the last element of tTtgs; this can help others understand the foldRight initializer logic.
val ((_, ti), gi) = tTtgs.last
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I lost track of this one in a GitHub bankruptcy. I'm sorry it got so desperately lonely you had to talk to a bot.
@rossabaker , thank you for the review!
No worries! Actually, I just wanted to check out that "copilot review" feature – to see how it works, what the output looks like, etc. So I thought – what could be better than some my own PR for that? |
Adds a code generator for
Gen.zipWith
functions.Example:
Without
Gen.zipWith
the same was possible to accomplish withGen.zip
, however it would required an additionalmap
over an intermediary tuple instance:Therefore
Gen.zipWith
allows to avoid the intermediate conversion which can come in handy in come cases.Note that
Gen.zipWith
starts with arity 2 whereasGen.zip
starts with arity 1.However, I think the latter was an oversight –
Gen.zip
for arity 1 is a no-op function.