Skip to content
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

Arbitrary set from array of constant values #127

Closed
benny-medflyt opened this issue Jun 25, 2018 · 6 comments
Closed

Arbitrary set from array of constant values #127

benny-medflyt opened this issue Jun 25, 2018 · 6 comments

Comments

@benny-medflyt
Copy link

If I have:

const pizzaToppings: string[] = [
    "Pepperoni",
    "Mushrooms",
    "Onions",
    "Sausage",
    "Bacon",
    "Extra cheese",
    "Black olives",
    "Green peppers"
]

I would like to create an Arbitrary<string[]> that will deliver values such as:

const toppings1 = ["Pepperoni", "Onions", "Sausage"];
const toppings2 = ["Black olives"];
const toppings3 = [];

(The min and max size should be customizeable)
(Ideally the order in the results should be the same as the order in the pizzaToppings array)

I can use use fc.set but I believe this is not ideal because:

  • It is inefficient, it has to choose values and then prune out duplicates, instead of directly generating unique results (using unique array indices)
  • We may not have (or feel like writing) an appropriate comparison function, if the array elements are complex objects.

Thank you

@dubzzz
Copy link
Owner

dubzzz commented Jun 26, 2018

Indeed the feature you suggest can be very useful and is not yet available out of the box.

The current way to do that would be fc.set or something more complex like:

const toppings = [/*your data*/];
const topArb = fc.array(fc.boolean(), toppings.length, toppings.length)
  .map(selection => toppings.filter((v, id) => selection[id] === true));

Or maybe something based on fc.genericTuple(..) instead of fc.array().

An implementation with min/max boundaries would require more work. In order to do that you can maybe use fc.nat with well selected boundaries instead of fc.boolean. And apply filter right after the map operation. The switch to fc.nat is just to limit the number of filtered values.

The filter would be something like:

const boundTopArb = topArb.filter(ts => min <= ts.length && ts.length <= max);

Sent from my mobile, I have not tried to run the code above but it should work fine if no typos :)

@dubzzz
Copy link
Owner

dubzzz commented Jun 26, 2018

For the naming I was thinking of something like: fc.subarrayOf() or fc.subarrayFrom(). I should also provide a shuffled version called fc.shuffledSubarrayFrom/Of().

What do you think of those names? Any other suggestions?

@benny-medflyt
Copy link
Author

Both of those names look good to me! 👍
For my use case I don't need the shuffled version, but looks like it could indeed come in handy sometime!
Thank you!

@dubzzz
Copy link
Owner

dubzzz commented Aug 23, 2018

@benny-medflyt

I just started to implement the subarray feature.
It should be part of the next release of fast-check - 1.5.0.
See PR #177.

Does it fit your needs?

dubzzz added a commit that referenced this issue Aug 23, 2018
@benny-medflyt
Copy link
Author

Yeah that looks great thanks! This can be closed now

@dubzzz
Copy link
Owner

dubzzz commented Aug 27, 2018

Great

@dubzzz dubzzz closed this as completed Aug 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants