Erlang Central

Difference between revisions of "List Generators"

From ErlangCentral Wiki

Line 7:Line 7:
 
[[User:Adam|Adam Lindberg]]
 
[[User:Adam|Adam Lindberg]]
  
== Non-empty list ==
+
== Non-Empty List ==
  
 
If you want to generate lists which can never be empty, it's fairly simple to wrap the standard '''list()''' generator in QuickCheck:
 
If you want to generate lists which can never be empty, it's fairly simple to wrap the standard '''list()''' generator in QuickCheck:
Line 15:Line 15:
 
         [E|L]).
 
         [E|L]).
 
</code>
 
</code>
 +
This generator will generate one extra element and append it to the list to ensure that the list is never empty.
  
  

Revision as of 15:02, 9 September 2008


Authors

(This is a living page, please add your own list based generators here!)

Adam Lindberg

Non-Empty List

If you want to generate lists which can never be empty, it's fairly simple to wrap the standard list() generator in QuickCheck:

nelist(G) ->
    ?LET({E, L}, {G, list(G)},
         [E|L]).

This generator will generate one extra element and append it to the list to ensure that the list is never empty.


Regexp POSIX Character Class Strings

String generators for all the POSIX character classes in regexp. Note: some of them do not generate printable strings in Erlang.

-import(lists, [seq/2]).

%%% Alphanumeric characters ([a-zA-Z0-9]).
alnum() -> list(oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9))).

%%% Alphabetic characters ([a-zA-Z]).
alpha() -> list(oneof(seq($a, $z) ++ seq($A, $Z))).

%%% ASCII characters ([\x00-\x7F]).
ascii() -> list(oneof(seq(0, 127))).

%%% Space and tab ([ \t]).
blank() -> list(oneof([$ , $\t])).

%%% Control characters ([\x00-\x1F\x7F]).
cntrl() -> list(oneof(seq(0, 31) ++ [127])).

%%% Digits ([0-9]).
digit() -> list(oneof(seq($0, $9))).

%%% Visible characters (i.e. anything except spaces, control
%%% characters, etc.) ([\x21-\x7E]).
graph() -> list(oneof(seq(33, 126))).

%%% Lowercase letters ([a-z]).
lower() -> list(oneof(seq($a, $z))).

%%% Visible characters and spaces (i.e. anything except control
%%% characters, etc.) ([\x20-\x7E]).
print() -> list(oneof(seq(32, 126))).

%%% Punctuation and symbols ([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]).
punct() -> list(oneof("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~")).

%%% All whitespace characters, including line breaks ([ \t\r\n\v\f]).
space() -> list(oneof([$ , $\t, $\r, $\n, $\v, $\f])).

%%% Uppercase letters ([A-Z]).
upper() -> list(oneof(seq($A, $Z))).

%%% Word characters (letters, numbers and underscores) ([A-Za-z0-9_]).
word() ->  list(oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9) ++ "_")).

%%% Hexadecimal digits ([A-Fa-f0-9]).
xdigit() -> list(oneof(seq($A, $F) ++ seq($a, $f) ++ seq($0, $9))).