Category:Factor-numspec: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor-numspec library/vocabulary)
 
m (Chunes moved page Factor-numspec to Category:Factor-numspec: to make libheader template work correctly)
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
'''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.
'''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 [https://rosettacode.org/wiki/Category_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.
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:
Finally, in your program, make sure to add <code>numspec</code> to your <code>USING:</code> like so:
Line 9: Line 9:


=== Examples ===
=== 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.
It's important to remember that a number specification defines a named lazy list constant. Whether the list ends or not depends on whether you end the specification with <code>...</code>.


Get all three-digit numbers beginning and ending with <code>3</code>:
Get all three-digit numbers beginning and ending with <code>3</code>:
<lang factor>USING: lists numspec prettyprint ;
<lang factor>USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: 3_3 ;
NUMSPEC: a 3_3 ;
list>array .</lang>
a list>array .</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 22: Line 23:
Literals can be added where necessary. Let's make special cases for one- and two-digit numbers.
Literals can be added where necessary. Let's make special cases for one- and two-digit numbers.
<lang factor>USING: lists numspec prettyprint ;
<lang factor>USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: 3 33 3_3 ;
NUMSPEC: b 3 33 3_3 ;
list>array .</lang>
b list>array .</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 31: Line 33:
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.
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 ;
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: 3 33 3_3 ... ;
NUMSPEC: c 3 33 3_3 ... ;
[ 3030 < ] lwhile
c [ 3030 < ] lwhile
list>array .</lang>
list>array .</lang>
{{out}}
{{out}}
Line 40: Line 43:
Since this is an infinite list, we need to remember to limit it in some fashion (like with <code>lwhile</code>).
Since this is an infinite list, we need to remember to limit it in some fashion (like with <code>lwhile</code>).


Numbers with only prime digits:
<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 ;
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: P 2357
20 NUMSPEC: P PP ... ; ltake
NUMSPEC: d P PP ... ;
list>array .</lang>
20 d ltake list>array .</lang>
{{out}}
{{out}}
<pre>
<pre>
{ 2 3 5 7 22 23 25 27 32 33 35 37 52 53 55 57 72 73 75 77 }
{ 2 3 5 7 22 23 25 27 32 33 35 37 52 53 55 57 72 73 75 77 }
</pre>
</pre>
<code>2</code> and <code>5</code> surrounded by <code>9</code>s:
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 ;
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: V 25
CONSTANT: V L{ 2 5 }
NUMSPEC: 9 99 9V9 ... ;
NUMSPEC: e 9 99 9V9 ... ;
30 swap ltake list>array .</lang>
30 e ltake list>array .</lang>
{{out}}
{{out}}
<pre>
<pre>

Latest revision as of 22:02, 5 August 2021

Library
This is an example of a library. You may see a list of other libraries used on Rosetta Code at Category:Solutions by 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 talk page) to a text file called numspec.factor and then save it in a sub-directory called numspec inside your work directory. work is in the same directory as the Factor executable.

Finally, in your program, make sure to add numspec to your USING: like so:

<lang factor>USING: math numspec prettyprint ;</lang>

Examples

It's important to remember that a number specification defines a named lazy list constant. Whether the list ends or not depends on whether you end the specification with ....

Get all three-digit numbers beginning and ending with 3: <lang factor>USING: lists numspec prettyprint ; DIGIT: _ 0123456789 NUMSPEC: a 3_3 ; a list>array .</lang>

Output:
{ 303 313 323 333 343 353 363 373 383 393 }

Literals can be added where necessary. Let's make special cases for one- and two-digit numbers. <lang factor>USING: lists numspec prettyprint ; DIGIT: _ 0123456789 NUMSPEC: b 3 33 3_3 ; b list>array .</lang>

Output:
{ 3 33 303 313 323 333 343 353 363 373 383 393 }

But what if you don't want to make a case for every number of digits? Got you covered! ... rudimentarily infers the pattern. <lang factor>USING: lists lists.lazy numspec prettyprint ; DIGIT: _ 0123456789 NUMSPEC: c 3 33 3_3 ... ; c [ 3030 < ] lwhile list>array .</lang>

Output:
{ 3 33 303 313 323 333 343 353 363 373 383 393 3003 3013 3023 }

Since this is an infinite list, we need to remember to limit it in some fashion (like with lwhile).

Numbers with only prime digits: <lang factor>USING: lists lists.lazy numspec prettyprint ; DIGIT: P 2357 NUMSPEC: d P PP ... ; 20 d ltake list>array .</lang>

Output:
{ 2 3 5 7 22 23 25 27 32 33 35 37 52 53 55 57 72 73 75 77 }

2 and 5 surrounded by 9s: <lang factor>USING: lists lists.lazy numspec prettyprint ; DIGIT: V 25 NUMSPEC: e 9 99 9V9 ... ; 30 e ltake list>array .</lang>

Output:
{
    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
}

Pages in category "Factor-numspec"

The following 2 pages are in this category, out of 2 total.