typelift / SwiftCheck

QuickCheck for Swift
MIT License
1.42k stars 106 forks source link

no Gen.let or heterogeneous sequence/tuple? #276

Closed johanatan closed 5 years ago

johanatan commented 6 years ago

i.e., https://github.com/clojure/test.check/blob/master/src/main/clojure/clojure/test/check/generators.cljc#L1585-L1588

and https://github.com/clojure/test.check/blob/master/src/main/clojure/clojure/test/check/generators.cljc#L82-L87

Any workarounds approaching these other than nesting flatMaps (which would become rather unwieldy)?

johanatan commented 6 years ago

Ah, I found Gen<DEST_TYPE>.map(G0, G1, ... GN) (now called zipWith). That should suffice for this.

johanatan commented 6 years ago

Also, Gen.compose.

CodaFi commented 6 years ago

Gen.compose is definitely your friend here for tuples of large arity.

johanatan commented 6 years ago

Does that mean I have to define classes to hold them or is there a way to actually produce a Tuple directly from Gen.compose?

CodaFi commented 6 years ago

Nope, it's polymorphic in the return type of the builder closure. All you've gotta do is provide a block that acts as a stencil for your tuple

let generator = Gen.compose { (builder) -> (Int, Float, Array<Int>) in
  return (builder.generate(), builder.generate(), builder.generate())
}
johanatan commented 6 years ago

Awesome. Thanks!

CodaFi commented 5 years ago

Given that I think you've got your answer, I'm going to close this out. Please feel free to reopen it if I'm wrong about that part.