rapid icon indicating copy to clipboard operation
rapid copied to clipboard

Allow configuring number of attempts to produce a value with `Filter()`

Open swist opened this issue 7 months ago • 2 comments

I understand this is a pretty rare use-case, but it would be great to dial the tradeoff between taking a long time to Filter() a valid value and number of values we get. At the moment if we don't produce a value that passes Filter() within small attempts (currently set to 5), the generator fails. Would it make sense to make it an optional argument to Filter()?

swist avatar Jul 01 '25 15:07 swist

Can you provide some use cases from your experience where this would be valueable?

flyingmutant avatar Jul 02 '25 09:07 flyingmutant

We have written a generic schema based generator (take a protobuf schema, generate a protobuf out). The high level idea looks a bit like so:

generators.MakeProto(&SomeProtoObject{}).
    Filter(func (p *SomeProtoObject) {return p.Valid()})

(you can ignore the non generic syntax here, there's other reasons for it, namely we don't know the concrete protobuf message type at compile time)

in practice this works if I crank up the value of retries to something larger O(100) - O(1000) then it's actually viable and we've used that with decent success

You can think of it as a dial for Make[T]

I've raised #86 to suggest an API for it, happy to iterate on this if you think it's a good idea

swist avatar Jul 29 '25 16:07 swist