From 82337056dbce0e1b97c90a01dc819d6ab1a0f66d Mon Sep 17 00:00:00 2001 From: Marlin Forbes Date: Sat, 27 Apr 2019 08:11:01 +0200 Subject: [PATCH] fixes and readme --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++- src/generators.php | 16 ++++++---------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ee9448f..d264e3a 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,52 @@ Below is the list of generators that are currently available: * `variant(int $seed, Generator $gen)` * `vectorOf(int $n, Generator $gen)` -See [GeneratorCheck.php](checks/GeneratorCheck.php) and [GeneratorTest](tests/GeneratorTest.php) for examples of how these are used. +To generate a value from one of the above, use `generate`: + + >>> use function Datashaman\PHPCheck\{ + ... choose, + ... generate + ... }; + >>> + >>> var_dump(generate(choose(0, 10))); + int(2) + +To generate a sample of values with increasing size, use `sample`: + + >>> use function Datashaman\PHPCheck\{ + ... ascii, + ... strings, + ... sample + ... }; + >>> + >>> var_dump(sample(strings(ascii()))); + array(11) { + [0]=> + string(0) "" + [1]=> + string(2) "0]" + [2]=> + string(2) "^|" + [3]=> + string(3) "P@N" + [4]=> + string(5) "G1KPu" + [5]=> + string(5) "q-e1y" + [6]=> + string(4) "NcdL" + [7]=> + string(7) "hS:{_>@" + [8]=> + string(10) "wjv1X"Zm$V" + [9]=> + string(16) "aX-6*s0-WX>#cf~T" + [10]=> + string(12) ";g<&8*b&Q0=)" + } + => null + +See [GeneratorCheck](checks/GeneratorCheck.php) and [GeneratorTest](tests/GeneratorTest.php) for examples of how these are used. The `characters` generator accepts either characters or integer codepoints for `minChar` and `maxChar`. Characters are generated from the complete range of Unicode characters excluding control characters, private ranges and surrogates. diff --git a/src/generators.php b/src/generators.php index 40cf14c..bc9bf5c 100644 --- a/src/generators.php +++ b/src/generators.php @@ -559,21 +559,17 @@ function (Random $r) use ($n, $gen) { ); } -function sample(Generator $gen): Generator +function sample(Generator $gen): array { logExecution('mkGen', 'sample', $gen); $sizes = \range(0, 20, 2); - return makeGen( - function (Random $r) use ($gen, $sizes) { - return \array_map( - function ($n) use ($r, $gen) { - return generate($gen, $r, $n); - }, - $sizes - ); - } + return \array_map( + function ($n) use ($gen) { + return generate($gen, null, $n); + }, + $sizes ); }