Move replicate into core; move selection sort into its own vocab; remove usages of and? and or? which are redundant now

db4
Slava Pestov 2008-06-13 01:51:46 -05:00
parent 90dbac97f8
commit 045b657474
29 changed files with 74 additions and 57 deletions

View File

@ -38,7 +38,7 @@ IN: bit-arrays.tests
[ t ] [ [ t ] [
100 [ 100 [
drop 100 [ drop 2 random zero? ] map drop 100 [ 2 random zero? ] replicate
dup >bit-array >array = dup >bit-array >array =
] all? ] all?
] unit-test ] unit-test

View File

@ -204,7 +204,7 @@ UNION: z1 b1 c1 ;
10 [ 10 [
[ ] [ [ ] [
20 [ drop random-op ] map >quotation 20 [ random-op ] [ ] replicate-as
[ infer effect-in [ random-class ] times ] keep [ infer effect-in [ random-class ] times ] keep
call call
drop drop
@ -238,8 +238,8 @@ UNION: z1 b1 c1 ;
20 [ 20 [
[ t ] [ [ t ] [
20 [ drop random-boolean-op ] [ ] map-as dup . 20 [ random-boolean-op ] [ ] replicate-as dup .
[ infer effect-in [ drop random-boolean ] map dup . ] keep [ infer effect-in [ random-boolean ] replicate dup . ] keep
[ >r [ ] each r> call ] 2keep [ >r [ ] each r> call ] 2keep

View File

@ -80,7 +80,7 @@ M: object value-literal \ literal-expected inference-warning ;
1 #drop node, 1 #drop node,
pop-d dup value-literal >r value-recursion r> ; pop-d dup value-literal >r value-recursion r> ;
: value-vector ( n -- vector ) [ drop <computed> ] V{ } map-as ; : value-vector ( n -- vector ) [ <computed> ] V{ } replicate-as ;
: add-inputs ( seq stack -- n stack ) : add-inputs ( seq stack -- n stack )
tuck [ length ] bi@ - dup 0 > tuck [ length ] bi@ - dup 0 >
@ -162,7 +162,7 @@ TUPLE: too-many-r> ;
dup ensure-values dup ensure-values
#>r #>r
over 0 pick node-inputs over 0 pick node-inputs
over [ drop pop-d ] map reverse [ push-r ] each over [ pop-d ] replicate reverse [ push-r ] each
0 pick pick node-outputs 0 pick pick node-outputs
node, node,
drop ; drop ;
@ -171,7 +171,7 @@ TUPLE: too-many-r> ;
dup check-r> dup check-r>
#r> #r>
0 pick pick node-inputs 0 pick pick node-inputs
over [ drop pop-r ] map reverse [ push-d ] each over [ pop-r ] replicate reverse [ push-d ] each
over 0 pick node-outputs over 0 pick node-outputs
node, node,
drop ; drop ;

View File

@ -361,6 +361,12 @@ PRIVATE>
: map ( seq quot -- newseq ) : map ( seq quot -- newseq )
over map-as ; inline over map-as ; inline
: replicate ( seq quot -- newseq )
[ drop ] prepose map ; inline
: replicate-as ( seq quot exemplar -- newseq )
>r [ drop ] prepose r> map-as ; inline
: change-each ( seq quot -- ) : change-each ( seq quot -- )
over map-into ; inline over map-into ; inline

View File

@ -11,7 +11,7 @@ unit-test
[ t ] [ [ t ] [
100 [ 100 [
drop drop
100 [ drop 20 random [ drop 1000 random ] map ] map natural-sort [ before=? ] monotonic? 100 [ 20 random [ 1000 random ] replicate ] replicate natural-sort [ before=? ] monotonic?
] all? ] all?
] unit-test ] unit-test

View File

@ -98,7 +98,7 @@ unit-test
[ ] [ [ ] [
[ [
4 [ 4 [
100 [ drop "obdurak" clone ] map 100 [ "obdurak" clone ] replicate
gc gc
dup [ dup [
1234 0 rot set-string-nth 1234 0 rot set-string-nth

View File

@ -26,7 +26,7 @@ IN: vectors.tests
[ V{ 1 2 } ] [ [ 1 2 ] >vector ] unit-test [ V{ 1 2 } ] [ [ 1 2 ] >vector ] unit-test
[ t ] [ [ t ] [
100 [ drop 100 random ] map >vector 100 [ 100 random ] V{ } map-as
dup >array >vector = dup >array >vector =
] unit-test ] unit-test

View File

@ -24,7 +24,7 @@ M: color-preview model-changed
[ [ 256 /f ] map 1 suffix <solid> ] <filter> ; [ [ 256 /f ] map 1 suffix <solid> ] <filter> ;
: <color-sliders> ( -- model gadget ) : <color-sliders> ( -- model gadget )
3 [ drop 0 0 0 255 <range> ] map 3 [ 0 0 0 255 <range> ] replicate
dup [ range-model ] map <compose> dup [ range-model ] map <compose>
swap [ [ <color-slider> gadget, ] each ] make-filled-pile ; swap [ [ <color-slider> gadget, ] each ] make-filled-pile ;

View File

@ -1,8 +1,8 @@
! Copyright (C) 2007 Daniel Ehrenberg ! Copyright (C) 2007 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: parser generic kernel classes words slots assocs USING: parser generic kernel classes words slots assocs
sequences arrays vectors definitions prettyprint combinators.lib sequences arrays vectors definitions prettyprint
math hashtables sets ; math hashtables sets macros namespaces ;
IN: delegate IN: delegate
: protocol-words ( protocol -- words ) : protocol-words ( protocol -- words )
@ -23,7 +23,15 @@ M: tuple-class group-words
: consult-method ( word class quot -- ) : consult-method ( word class quot -- )
[ drop swap first create-method ] [ drop swap first create-method ]
[ nip swap first2 swapd [ ndip ] 2curry swap suffix ] 3bi [
nip
[
over second saver %
%
dup second restorer %
first ,
] [ ] make
] 3bi
define ; define ;
: change-word-prop ( word prop quot -- ) : change-word-prop ( word prop quot -- )

View File

@ -15,7 +15,7 @@ IN: io.files.unique
[ 10 random CHAR: 0 + ] [ random-letter ] if ; [ 10 random CHAR: 0 + ] [ random-letter ] if ;
: random-name ( n -- string ) : random-name ( n -- string )
[ drop random-ch ] "" map-as ; [ random-ch ] "" replicate-as ;
: unique-length ( -- n ) 10 ; inline : unique-length ( -- n ) 10 ; inline
: unique-retries ( -- n ) 10 ; inline : unique-retries ( -- n ) 10 ; inline

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: io.encodings io.backend io.ports io.streams.duplex USING: io.encodings io.backend io.ports io.streams.duplex
io splitting grouping sequences sequences.lib namespaces kernel io splitting grouping sequences namespaces kernel
destructors math concurrency.combinators accessors destructors math concurrency.combinators accessors
arrays continuations quotations ; arrays continuations quotations ;
IN: io.pipes IN: io.pipes

View File

@ -142,7 +142,7 @@ DEFER: (d)
! Computing a basis ! Computing a basis
: graded ( seq -- seq ) : graded ( seq -- seq )
dup 0 [ length max ] reduce 1+ [ drop V{ } clone ] map dup 0 [ length max ] reduce 1+ [ V{ } clone ] replicate
[ dup length pick nth push ] reduce ; [ dup length pick nth push ] reduce ;
: nth-basis-elt ( generators n -- elt ) : nth-basis-elt ( generators n -- elt )

View File

@ -2,6 +2,10 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: tools.test lcs ; USING: tools.test lcs ;
\ lcs must-infer
\ diff must-infer
\ levenshtein must-infer
[ 3 ] [ "sitting" "kitten" levenshtein ] unit-test [ 3 ] [ "sitting" "kitten" levenshtein ] unit-test
[ 3 ] [ "kitten" "sitting" levenshtein ] unit-test [ 3 ] [ "kitten" "sitting" levenshtein ] unit-test
[ 1 ] [ "freshpak" "freshpack" levenshtein ] unit-test [ 1 ] [ "freshpak" "freshpack" levenshtein ] unit-test

View File

@ -63,15 +63,19 @@ TUPLE: trace-state old new table i j ;
[ 1- ] change-i [ 1- ] change-j ; [ 1- ] change-i [ 1- ] change-j ;
: inserted? ( state -- ? ) : inserted? ( state -- ? )
{
[ j>> 0 > ] [ j>> 0 > ]
[ [ i>> zero? ] [ top-beats-side? ] or? ] and? ; [ { [ i>> zero? ] [ top-beats-side? ] } 1|| ]
} 1&& ;
: do-insert ( state -- state ) : do-insert ( state -- state )
dup new-nth insert boa , [ 1- ] change-j ; dup new-nth insert boa , [ 1- ] change-j ;
: deleted? ( state -- ? ) : deleted? ( state -- ? )
{
[ i>> 0 > ] [ i>> 0 > ]
[ [ j>> zero? ] [ top-beats-side? not ] or? ] and? ; [ { [ j>> zero? ] [ top-beats-side? not ] } 1|| ]
} 1&& ;
: do-delete ( state -- state ) : do-delete ( state -- state )
dup old-nth delete boa , [ 1- ] change-i ; dup old-nth delete boa , [ 1- ] change-i ;

View File

@ -17,9 +17,6 @@ IN: project-euler.150
: partial-sum-infimum ( seq -- seq ) : partial-sum-infimum ( seq -- seq )
0 0 rot [ (partial-sum-infimum) ] each drop ; inline 0 0 rot [ (partial-sum-infimum) ] each drop ; inline
: generate ( n quot -- seq )
[ drop ] prepose map ; inline
: map-infimum ( seq quot -- min ) : map-infimum ( seq quot -- min )
[ min ] compose 0 swap reduce ; inline [ min ] compose 0 swap reduce ; inline
@ -30,7 +27,7 @@ IN: project-euler.150
615949 * 797807 + 20 2^ rem dup 19 2^ - ; inline 615949 * 797807 + 20 2^ rem dup 19 2^ - ; inline
: sums-triangle ( -- seq ) : sums-triangle ( -- seq )
0 1000 [ 1+ [ next ] generate partial-sums ] map nip ; 0 1000 [ 1+ [ next ] replicate partial-sums ] map nip ;
PRIVATE> PRIVATE>

View File

@ -131,10 +131,6 @@ MACRO: firstn ( n -- )
[ find drop [ head-slice ] when* ] curry [ find drop [ head-slice ] when* ] curry
[ dup ] prepose keep like ; [ dup ] prepose keep like ;
: replicate ( seq quot -- newseq )
#! quot: ( -- obj )
[ drop ] prepose map ; inline
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<PRIVATE <PRIVATE
@ -244,20 +240,6 @@ PRIVATE>
: short ( seq n -- seq n' ) : short ( seq n -- seq n' )
over length min ; inline over length min ; inline
<PRIVATE
:: insert ( seq quot n -- )
n zero? [
n n 1- [ seq nth quot call ] bi@ >= [
n n 1- seq exchange
seq quot n 1- insert
] unless
] unless ; inline
PRIVATE>
: insertion-sort ( seq quot -- )
! quot is a transformation on elements
over length [ insert ] 2with each ; inline
: if-seq ( seq quot1 quot2 -- ) : if-seq ( seq quot1 quot2 -- )
[ f like ] 2dip if* ; inline [ f like ] 2dip if* ; inline

View File

@ -0,0 +1 @@
Daniel Ehrenberg

View File

@ -0,0 +1,16 @@
USING: locals sequences kernel math ;
IN: sorting.insertion
<PRIVATE
:: insert ( seq quot n -- )
n zero? [
n n 1- [ seq nth quot call ] bi@ >= [
n n 1- seq exchange
seq quot n 1- insert
] unless
] unless ; inline
PRIVATE>
: insertion-sort ( seq quot -- )
! quot is a transformation on elements
over length [ insert ] with with each ; inline

View File

@ -0,0 +1 @@
Insertion sort

View File

@ -0,0 +1 @@
collections

View File

@ -144,7 +144,7 @@ M: not-enough-characters summary ( obj -- str )
] if next ; ] if next ;
: expect-string ( string -- ) : expect-string ( string -- )
dup [ drop get-char next ] map 2dup = dup [ get-char next ] replicate 2dup =
[ 2drop ] [ expected ] if ; [ 2drop ] [ expected ] if ;
: init-parser ( -- ) : init-parser ( -- )

View File

@ -5,4 +5,4 @@ IN: temporary
[ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ] [ upper-alpha-chars "" like ] unit-test [ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ] [ upper-alpha-chars "" like ] unit-test
[ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ] [ alpha-chars "" like ] unit-test [ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ] [ alpha-chars "" like ] unit-test
[ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ] [ alphanumeric-chars "" like ] unit-test [ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ] [ alphanumeric-chars "" like ] unit-test
[ t ] [ 100 [ drop random-alphanumeric-char ] map alphanumeric-chars [ member? ] curry all? ] unit-test [ t ] [ 100 [ random-alphanumeric-char ] replicate alphanumeric-chars [ member? ] curry all? ] unit-test

View File

@ -30,5 +30,4 @@ IN: strings.lib
alphanumeric-chars random ; alphanumeric-chars random ;
: random-alphanumeric-string ( length -- str ) : random-alphanumeric-string ( length -- str )
[ drop random-alphanumeric-char ] map "" like ; [ random-alphanumeric-char ] "" replicate-as ;

View File

@ -8,7 +8,7 @@ IN: ui.gadgets.frames
! gadgets gets left-over space. ! gadgets gets left-over space.
TUPLE: frame ; TUPLE: frame ;
: <frame-grid> ( -- grid ) 9 [ drop <gadget> ] map 3 group ; : <frame-grid> ( -- grid ) 9 [ <gadget> ] replicate 3 group ;
: @center 1 1 ; : @center 1 1 ;
: @left 0 1 ; : @left 0 1 ;

View File

@ -23,8 +23,7 @@ CATEGORY: grapheme-control Zl Zp Cc Cf ;
CATEGORY: (extend) Me Mn ; CATEGORY: (extend) Me Mn ;
: extend? ( ch -- ? ) : extend? ( ch -- ? )
[ (extend)? ] { [ (extend)? ] [ "Other_Grapheme_Extend" property? ] } 1|| ;
[ "Other_Grapheme_Extend" property? ] or? ;
: grapheme-class ( ch -- class ) : grapheme-class ( ch -- class )
{ {
@ -35,7 +34,7 @@ CATEGORY: (extend) Me Mn ;
} cond ; } cond ;
: init-grapheme-table ( -- table ) : init-grapheme-table ( -- table )
graphemes [ drop graphemes f <array> ] map ; graphemes [ graphemes f <array> ] replicate ;
SYMBOL: table SYMBOL: table

View File

@ -58,8 +58,7 @@ ducet insert-helpers
HEX: 7FFF bitand HEX: 8000 bitor 0 0 f weight boa ; HEX: 7FFF bitand HEX: 8000 bitor 0 0 f weight boa ;
: illegal? ( char -- ? ) : illegal? ( char -- ? )
[ "Noncharacter_Code_Point" property? ] { [ "Noncharacter_Code_Point" property? ] [ category "Cs" = ] } 1|| ;
[ category "Cs" = ] or? ;
: derive-weight ( char -- weights ) : derive-weight ( char -- weights )
first dup illegal? first dup illegal?

View File

@ -62,7 +62,7 @@ VALUE: properties
dup [ swap (chain-decomposed) ] curry assoc-map ; dup [ swap (chain-decomposed) ] curry assoc-map ;
: first* ( seq -- ? ) : first* ( seq -- ? )
second [ empty? ] [ first ] or? ; second { [ empty? ] [ first ] } 1|| ;
: (process-decomposed) ( data -- alist ) : (process-decomposed) ( data -- alist )
5 swap (process-data) 5 swap (process-data)
@ -107,7 +107,7 @@ VALUE: properties
:: fill-ranges ( table -- table ) :: fill-ranges ( table -- table )
name-map >alist sort-values keys name-map >alist sort-values keys
[ [ "first>" tail? ] [ "last>" tail? ] or? ] filter [ { [ "first>" tail? ] [ "last>" tail? ] } 1|| ] filter
2 group [ 2 group [
[ name>char ] bi@ [ [a,b] ] [ table ?nth ] bi [ name>char ] bi@ [ [a,b] ] [ table ?nth ] bi
[ swap table ?set-nth ] curry each [ swap table ?set-nth ] curry each

View File

@ -1,5 +1,5 @@
USING: sequences namespaces unicode.data kernel math arrays USING: sequences namespaces unicode.data kernel math arrays
locals combinators.lib sequences.lib combinators.lib ; locals combinators.lib sorting.insertion combinators.lib ;
IN: unicode.normalize IN: unicode.normalize
! Conjoining Jamo behavior ! Conjoining Jamo behavior

View File

@ -26,7 +26,7 @@ short-url "SHORT_URLS" {
3append ; foldable 3append ; foldable
: random-url ( -- string ) : random-url ( -- string )
1 6 [a,b] random [ drop letter-bank random ] "" map-as ; 1 6 [a,b] random [ letter-bank random ] "" replicate-as ;
: insert-short-url ( short-url -- short-url ) : insert-short-url ( short-url -- short-url )
'[ , dup random-url >>short insert-tuple ] 10 retry ; '[ , dup random-url >>short insert-tuple ] 10 retry ;