Category:Factor-numspec: Difference between revisions

m
Chunes moved page Factor-numspec to Category:Factor-numspec: to make libheader template work correctly
m (fix link)
m (Chunes moved page Factor-numspec to Category:Factor-numspec: to make libheader template work correctly)
 
(2 intermediate revisions by the same user not shown)
Line 9:
 
=== Examples ===
It's important to remember that a number specification createsdefines a named lazy list of integersconstant. 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 ;
DIGIT: _ 0123456789
NUMSPEC: a 3_3 ;
a list>array .</lang>
{{out}}
<pre>
Line 22 ⟶ 23:
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>
{{out}}
<pre>
Line 31 ⟶ 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.
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: c 3 33 3_3 ... ;
c [ 3030 < ] lwhile
list>array .</lang>
{{out}}
Line 40 ⟶ 43:
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 ;
DIGIT: P 2357
20 NUMSPEC: d P PP ... ; ltake
20 d 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>
<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 ;
DIGIT: V 25
CONSTANT: V L{ 2 5 }
NUMSPEC: e 9 99 9V9 ... ;
30 swape ltake list>array .</lang>
{{out}}
<pre>
1,808

edits