[latex3-commits] [git/LaTeX3-latex3-latex3] master: Implement candidate \intarray_rand_item:N (d6ecdf1)

Bruno Le Floch bruno at le-floch.fr
Sun May 6 16:13:51 CEST 2018


Repository : https://github.com/latex3/latex3
On branch  : master
Link       : https://github.com/latex3/latex3/commit/d6ecdf1e0c9d23fb6b854ebd9019d31f1e5aaff6

>---------------------------------------------------------------

commit d6ecdf1e0c9d23fb6b854ebd9019d31f1e5aaff6
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Sat May 5 19:53:10 2018 -0400

    Implement candidate \intarray_rand_item:N
    
    This allows a poor-man's weighted dice: just make a constant integer
    array with each entry repeated a number of times equal to the desired
    weight.


>---------------------------------------------------------------

d6ecdf1e0c9d23fb6b854ebd9019d31f1e5aaff6
 l3kernel/l3candidates.dtx               |    9 +++++++++
 l3kernel/l3intarray.dtx                 |    8 ++++++++
 l3kernel/testfiles/m3expl001.luatex.tlg |    1 +
 l3kernel/testfiles/m3expl001.ptex.tlg   |    1 +
 l3kernel/testfiles/m3expl001.tlg        |    1 +
 l3kernel/testfiles/m3expl001.uptex.tlg  |    1 +
 l3kernel/testfiles/m3expl001.xetex.tlg  |    1 +
 l3kernel/testfiles/m3expl003.luatex.tlg |    1 +
 l3kernel/testfiles/m3expl003.ptex.tlg   |    1 +
 l3kernel/testfiles/m3expl003.tlg        |    1 +
 l3kernel/testfiles/m3expl003.uptex.tlg  |    1 +
 l3kernel/testfiles/m3expl003.xetex.tlg  |    1 +
 l3kernel/testfiles/m3rand001.lvt        |   11 +++++++++++
 l3kernel/testfiles/m3rand001.tlg        |   24 ++++++++++++++++++++++
 l3kernel/testfiles/m3rand001.xetex.tlg  |   33 +++++++++++++++++++++++++++++++
 15 files changed, 95 insertions(+)

diff --git a/l3kernel/l3candidates.dtx b/l3kernel/l3candidates.dtx
index 24e62b9..4734cd3 100644
--- a/l3kernel/l3candidates.dtx
+++ b/l3kernel/l3candidates.dtx
@@ -436,6 +436,15 @@
 %
 % \section{Additions to \pkg{l3intarray}}
 %
+% \begin{function}[EXP, added = 2018-05-05]{\intarray_rand_item:N}
+%   \begin{syntax}
+%     \cs{intarray_rand_item:N} \meta{intarray~var}
+%   \end{syntax}
+%   Selects a pseudo-random item of the \meta{integer array}.  If the
+%   \meta{integer array} is empty, produce an error.  This is not yet
+%   available in \XeTeX{}.
+% \end{function}
+%
 % \subsection{Working with contents of integer arrays}
 %
 % \begin{function}[added = 2018-05-04, rEXP]{\intarray_const_from_clist:Nn}
diff --git a/l3kernel/l3intarray.dtx b/l3kernel/l3intarray.dtx
index e09dc7d..23fefb1 100644
--- a/l3kernel/l3intarray.dtx
+++ b/l3kernel/l3intarray.dtx
@@ -347,6 +347,14 @@
 % \end{macro}
 % \end{macro}
 %
+% \begin{macro}{\intarray_rand_item:N}
+%   Importantly, \cs{intarray_item:Nn} only evaluates its argument once.
+%    \begin{macrocode}
+\cs_new:Npn \intarray_rand_item:N #1
+  { \intarray_item:Nn #1 { \int_rand:n { \intarray_count:N #1 } } }
+%    \end{macrocode}
+% \end{macro}
+%
 % \subsection{Working with contents of integer arrays}
 %
 % At the time of writing these are candidates, but we need at least
diff --git a/l3kernel/testfiles/m3expl001.luatex.tlg b/l3kernel/testfiles/m3expl001.luatex.tlg
index 41ec364..f6b12e7 100644
--- a/l3kernel/testfiles/m3expl001.luatex.tlg
+++ b/l3kernel/testfiles/m3expl001.luatex.tlg
@@ -3334,6 +3334,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl001.ptex.tlg b/l3kernel/testfiles/m3expl001.ptex.tlg
index 875492a..d587461 100644
--- a/l3kernel/testfiles/m3expl001.ptex.tlg
+++ b/l3kernel/testfiles/m3expl001.ptex.tlg
@@ -3588,6 +3588,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl001.tlg b/l3kernel/testfiles/m3expl001.tlg
index a3e2c94..eb00310 100644
--- a/l3kernel/testfiles/m3expl001.tlg
+++ b/l3kernel/testfiles/m3expl001.tlg
@@ -3588,6 +3588,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl001.uptex.tlg b/l3kernel/testfiles/m3expl001.uptex.tlg
index 992f14a..1194d42 100644
--- a/l3kernel/testfiles/m3expl001.uptex.tlg
+++ b/l3kernel/testfiles/m3expl001.uptex.tlg
@@ -3588,6 +3588,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl001.xetex.tlg b/l3kernel/testfiles/m3expl001.xetex.tlg
index 5d50ab1..e38b05b 100644
--- a/l3kernel/testfiles/m3expl001.xetex.tlg
+++ b/l3kernel/testfiles/m3expl001.xetex.tlg
@@ -3333,6 +3333,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl003.luatex.tlg b/l3kernel/testfiles/m3expl003.luatex.tlg
index 41ec364..f6b12e7 100644
--- a/l3kernel/testfiles/m3expl003.luatex.tlg
+++ b/l3kernel/testfiles/m3expl003.luatex.tlg
@@ -3334,6 +3334,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl003.ptex.tlg b/l3kernel/testfiles/m3expl003.ptex.tlg
index 875492a..d587461 100644
--- a/l3kernel/testfiles/m3expl003.ptex.tlg
+++ b/l3kernel/testfiles/m3expl003.ptex.tlg
@@ -3588,6 +3588,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl003.tlg b/l3kernel/testfiles/m3expl003.tlg
index a3e2c94..eb00310 100644
--- a/l3kernel/testfiles/m3expl003.tlg
+++ b/l3kernel/testfiles/m3expl003.tlg
@@ -3588,6 +3588,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl003.uptex.tlg b/l3kernel/testfiles/m3expl003.uptex.tlg
index 992f14a..1194d42 100644
--- a/l3kernel/testfiles/m3expl003.uptex.tlg
+++ b/l3kernel/testfiles/m3expl003.uptex.tlg
@@ -3588,6 +3588,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3expl003.xetex.tlg b/l3kernel/testfiles/m3expl003.xetex.tlg
index 5d50ab1..e38b05b 100644
--- a/l3kernel/testfiles/m3expl003.xetex.tlg
+++ b/l3kernel/testfiles/m3expl003.xetex.tlg
@@ -3333,6 +3333,7 @@ Defining \intarray_gzero:N on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
 Defining \__intarray_item:Nw on line ...
+Defining \intarray_rand_item:N on line ...
 Defining \intarray_const_from_clist:Nn on line ...
 Defining \__intarray_const_from_clist:nN on line ...
 Defining \intarray_to_clist:N on line ...
diff --git a/l3kernel/testfiles/m3rand001.lvt b/l3kernel/testfiles/m3rand001.lvt
index ac7dcdb..8008722 100644
--- a/l3kernel/testfiles/m3rand001.lvt
+++ b/l3kernel/testfiles/m3rand001.lvt
@@ -160,4 +160,15 @@
   }
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\OMIT
+\intarray_const_from_clist:Nn \c_empty_intarray { }
+\intarray_const_from_clist:Nn \c_test_intarray { 3 , 3 , 3 , 1 }
+\TIMO
+\TESTEXP { Random~prop~key/value }
+  {
+    \intarray_rand_item:N \c_empty_intarray \NEWLINE
+    \test_ii:n { \intarray_rand_item:N \c_test_intarray }
+  }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \END
diff --git a/l3kernel/testfiles/m3rand001.tlg b/l3kernel/testfiles/m3rand001.tlg
index 39cae1f..8d01500 100644
--- a/l3kernel/testfiles/m3rand001.tlg
+++ b/l3kernel/testfiles/m3rand001.tlg
@@ -292,3 +292,27 @@ and I'll forget about whatever was undefined.
 |1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|
 |2|10|7|9|6|11|10|6|6|2|4|1|3|1|2|
 ============================================================
+============================================================
+TEST 11: Random prop key/value
+============================================================
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Bounds ordered backwards in \int_rand:nn {1} {0}.
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Access to an entry beyond an array's bounds.
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+0
+|3|1|3|1|3|3|3|1|3|1|3|3|1|3|1|
+============================================================
diff --git a/l3kernel/testfiles/m3rand001.xetex.tlg b/l3kernel/testfiles/m3rand001.xetex.tlg
index 04140fc..12de33f 100644
--- a/l3kernel/testfiles/m3rand001.xetex.tlg
+++ b/l3kernel/testfiles/m3rand001.xetex.tlg
@@ -476,3 +476,36 @@ and I'll forget about whatever was undefined.
 1
 (1) (1) (1) 
 ============================================================
+============================================================
+TEST 11: Random prop key/value
+============================================================
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Random numbers unavailable for \int_rand:n {\inta...
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Access to an entry beyond an array's bounds.
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Random numbers unavailable for \int_rand:n {\inta...
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+0
+(3) 
+============================================================





More information about the latex3-commits mailing list