Category:Factor-numspec: Difference between revisions
Content added Content deleted
m (fix link) |
(remove buggy library) |
||
Line 1: | Line 1: | ||
{{library}} |
|||
'''Factor-numspec''' is a vocabulary that aims to make tasks involving the restriction of digits within numbers easy. This vocabulary was made because there are a fair number of tasks on Rosetta Code involving this operation and more being added all the time. |
|||
To use it yourself, copy the source code (in the [http://rosettacode.org/wiki/Talk:Factor-numspec talk page]) to a text file called <code>numspec.factor</code> and then save it in a sub-directory called <code>numspec</code> inside your <code>work</code> directory. <code>work</code> is in the same directory as the Factor executable. |
|||
Finally, in your program, make sure to add <code>numspec</code> to your <code>USING:</code> like so: |
|||
<lang factor>USING: math numspec prettyprint ;</lang> |
|||
=== Examples === |
|||
It's important to remember that a number specification creates a lazy list of integers. Whether the list ends or not depends on whether you end the specification with <code>...</code> or not. |
|||
Get all three-digit numbers beginning and ending with <code>3</code>: |
|||
<lang factor>USING: lists numspec prettyprint ; |
|||
NUMSPEC: 3_3 ; |
|||
list>array .</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ 303 313 323 333 343 353 363 373 383 393 } |
|||
</pre> |
|||
Literals can be added where necessary. Let's make special cases for one- and two-digit numbers. |
|||
<lang factor>USING: lists numspec prettyprint ; |
|||
NUMSPEC: 3 33 3_3 ; |
|||
list>array .</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ 3 33 303 313 323 333 343 353 363 373 383 393 } |
|||
</pre> |
|||
But what if you don't want to make a case for every number of digits? Got you covered! <code>...</code> rudimentarily infers the pattern. |
|||
<lang factor>USING: lists lists.lazy numspec prettyprint ; |
|||
NUMSPEC: 3 33 3_3 ... ; |
|||
[ 3030 < ] lwhile |
|||
list>array .</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ 3 33 303 313 323 333 343 353 363 373 383 393 3003 3013 3023 } |
|||
</pre> |
|||
Since this is an infinite list, we need to remember to limit it in some fashion (like with <code>lwhile</code>). |
|||
<code>numspec</code> comes with symbols for common digit restrictions. Let's get a list of the numbers with prime digits. |
|||
<lang factor>USING: lists lists.lazy numspec prettyprint ; |
|||
20 NUMSPEC: P PP ... ; ltake |
|||
list>array .</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ 2 3 5 7 22 23 25 27 32 33 35 37 52 53 55 57 72 73 75 77 } |
|||
</pre> |
|||
Adding your own digit restrictions is easy. Just define a 1-character constant as a list with the possibilities. |
|||
<lang factor>USING: lists lists.lazy numspec prettyprint ; |
|||
CONSTANT: V L{ 2 5 } |
|||
NUMSPEC: 9 99 9V9 ... ; |
|||
30 swap ltake list>array .</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ |
|||
9 |
|||
99 |
|||
929 |
|||
959 |
|||
9229 |
|||
9259 |
|||
9529 |
|||
9559 |
|||
92229 |
|||
92259 |
|||
92529 |
|||
92559 |
|||
95229 |
|||
95259 |
|||
95529 |
|||
95559 |
|||
922229 |
|||
922259 |
|||
922529 |
|||
922559 |
|||
925229 |
|||
925259 |
|||
925529 |
|||
925559 |
|||
952229 |
|||
952259 |
|||
952529 |
|||
952559 |
|||
955229 |
|||
955259 |
|||
} |
|||
</pre> |
Revision as of 23:16, 24 July 2021
Pages in category "Factor-numspec"
The following 2 pages are in this category, out of 2 total.