graphula icon indicating copy to clipboard operation
graphula copied to clipboard

Add function to edit with arbitrary

Open pbrisbin opened this issue 8 months ago • 1 comments

ensure can be wasteful, it may generate many values before the condition is met. We currently use it often to ensure a field is Just:

... $ ensure $ isJust . someField

In some entities, there are a collection of related fields that will all be just, or all nothing. So we might end up ensuring more than one field this way. The waste then compounds. If, for example, there's a 50/50 chance of just-vs-nothing, and you have 3 fields that you're doing this for, you'll only get something you want 12.5% of the time.

Using edit to just set the fields is one option, but then you lose out of the benefits of arbitrary, and you have to ensure you don't violate uniqueness constraints.

We'll often resort to something like this,

x <- generate arbitrary
... $ edit $ persistFieldLens SomeField ?~ x

Graphula could supply a function to help with this pattern:

editGen :: (a -> record -> record) -> Gen a -> NodeOptions record

Usage would look like,

... $ ensureGen setSomeField $ Just <$> arbitrary

pbrisbin avatar May 23 '25 12:05 pbrisbin

/cc @StevenXL

pbrisbin avatar May 23 '25 12:05 pbrisbin