2

Using the java library QuickTheories, is there a builtin way to create a generator from a list of values?

Something like:

public Gen<A> fromList(List<A> xs) {
    ....
}

Rolling your own is not too bad, but seems like reinventing the wheel:

List<TimeZone> timeZones = Arrays.stream(TimeZone.getAvailableIDs()).map(id -> TimeZone.getTimeZone(id));
return integers().allPositive().map(i -> timeZones.get(i % timeZones.size());
eMMe
  • 569
  • 5
  • 16
Justin Blank
  • 1,768
  • 1
  • 15
  • 32

1 Answers1

4

pick seems exactly what you need.

From the javadoc:

public <T> Gen<T> pick(java.util.List<T> ts)

Generates a value by randomly picking one from the supplied. When shrinking, values earlier in the list will be considered "smaller".

eMMe
  • 569
  • 5
  • 16