Category talk:Factor-numspec: Difference between revisions

remove buggy code
(Add Factor-numspec source code)
 
(remove buggy code)
Line 1:
== Source code ==
<lang factor>USING: grouping kernel lexer lists lists.lazy math
math.functions parser sequences ;
IN: numspec
 
<PRIVATE
 
: digits>number ( {5,3,1} -- 531 )
<reversed> 0 [ 10^ * + ] reduce-index ; flushable
 
: (numspec) ( list-of-lists-of-lists -- list )
[ lcartesian-product* ] lmap-lazy lconcat
[ digits>number ] lmap-lazy ;
 
: (extrapolate) ( seq -- newseq ) [ second 1 ] keep insert-nth ;
 
: extrapolate ( seq -- list )
but-last [ but-last >list ] [ last ] bi
[ (extrapolate) ] lfrom-by lappend-lazy ;
 
: ?extrapolate ( seq -- list )
dup last "..." = [ extrapolate ] [ >list ] if ;
 
: expand ( token -- list )
parse-datum dup integer? [ 1list ] [ execute( -- x ) ] if ;
 
: prep-cartesian ( seq -- list )
dup length 1 = [ L{ 0 } prefix ] when >list ;
 
: <numspec> ( list -- list' )
[ 1 group [ expand ] map prep-cartesian ] lmap-lazy ;
 
PRIVATE>
 
! Some common digit restrictions. Custom ones are easily definable.
CONSTANT: _ L{ 0 1 2 3 4 5 6 7 8 9 } ! any
CONSTANT: S L{ 1 2 3 4 5 6 7 8 9 } ! no zero (for leading digit)
CONSTANT: E L{ 0 2 4 6 8 } ! even
CONSTANT: O L{ 1 3 5 7 9 } ! odd
CONSTANT: P L{ 2 3 5 7 } ! prime
CONSTANT: Q L{ 1 3 7 9 } ! number can be prime if this is last digit
 
: numspec ( seq -- list ) ?extrapolate <numspec> (numspec) ;
 
SYNTAX: NUMSPEC: ";" parse-tokens numspec suffix! ;</lang>
1,808

edits