Move firstn to generalizations, clean it up a bit
parent
7f32b1f93f
commit
34b0fad704
|
@ -1,9 +1,21 @@
|
||||||
! Copyright (C) 2007 Chris Double.
|
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
|
||||||
USING: help.syntax help.markup kernel sequences quotations
|
USING: help.syntax help.markup kernel sequences quotations
|
||||||
math ;
|
math arrays ;
|
||||||
IN: generalizations
|
IN: generalizations
|
||||||
|
|
||||||
|
HELP: narray
|
||||||
|
{ $values { "n" integer } }
|
||||||
|
{ $description "A generalization of " { $link 1array } ", "
|
||||||
|
{ $link 2array } ", " { $link 3array } " and " { $link 4array } " "
|
||||||
|
"that constructs an array from the top " { $snippet "n" } " elements of the stack."
|
||||||
|
} ;
|
||||||
|
|
||||||
|
HELP: firstn
|
||||||
|
{ $values { "n" integer } }
|
||||||
|
{ $description "A generalization of " { $link first } ", "
|
||||||
|
{ $link first2 } ", " { $link first3 } " and " { $link first4 } " "
|
||||||
|
"that pushes the first " { $snippet "n" } " elements of a sequence on the stack."
|
||||||
|
} ;
|
||||||
|
|
||||||
HELP: npick
|
HELP: npick
|
||||||
{ $values { "n" integer } }
|
{ $values { "n" integer } }
|
||||||
{ $description "A generalization of " { $link dup } ", "
|
{ $description "A generalization of " { $link dup } ", "
|
||||||
|
@ -119,6 +131,7 @@ ARTICLE: "generalizations" "Generalized shuffle words and combinators"
|
||||||
"macros where the arity of the input quotations depends on an "
|
"macros where the arity of the input quotations depends on an "
|
||||||
"input parameter."
|
"input parameter."
|
||||||
{ $subsection narray }
|
{ $subsection narray }
|
||||||
|
{ $subsection firstn }
|
||||||
{ $subsection ndup }
|
{ $subsection ndup }
|
||||||
{ $subsection npick }
|
{ $subsection npick }
|
||||||
{ $subsection nrot }
|
{ $subsection nrot }
|
||||||
|
|
|
@ -32,3 +32,7 @@ IN: generalizations.tests
|
||||||
[ [ dup 2^ 2array ] 5 napply ] must-infer
|
[ [ dup 2^ 2array ] 5 napply ] must-infer
|
||||||
|
|
||||||
[ { "xyc" "xyd" } ] [ "x" "y" { "c" "d" } [ 3append ] 2 nwith map ] unit-test
|
[ { "xyc" "xyd" } ] [ "x" "y" { "c" "d" } [ 3append ] 2 nwith map ] unit-test
|
||||||
|
|
||||||
|
[ 1 2 3 4 ] [ { 1 2 3 4 } 4 firstn ] unit-test
|
||||||
|
[ ] [ { } 0 firstn ] unit-test
|
||||||
|
[ "a" ] [ { "a" } 1 firstn ] unit-test
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
! Copyright (C) 2007, 2008 Chris Double, Doug Coleman.
|
! Copyright (C) 2007, 2008 Chris Double, Doug Coleman, Eduardo
|
||||||
|
! Cavazos, Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel sequences sequences.private namespaces math math.ranges
|
USING: kernel sequences sequences.private namespaces math math.ranges
|
||||||
combinators macros quotations fry locals arrays ;
|
combinators macros quotations fry locals arrays ;
|
||||||
IN: generalizations
|
IN: generalizations
|
||||||
|
|
||||||
MACRO: narray ( n -- quot )
|
MACRO: narray ( n -- quot )
|
||||||
dup [ f <array> ] curry
|
[ <reversed> ] [ '[ , f <array> ] ] bi
|
||||||
swap <reversed> [
|
[ '[ @ [ , swap set-nth-unsafe ] keep ] ] reduce ;
|
||||||
[ swap [ set-nth-unsafe ] keep ] curry
|
|
||||||
] map concat append ;
|
MACRO: firstn ( n -- )
|
||||||
|
dup zero? [ drop [ drop ] ] [
|
||||||
|
[ [ '[ , _ nth-unsafe ] ] map ]
|
||||||
|
[ 1- '[ , _ bounds-check 2drop ] ]
|
||||||
|
bi prefix '[ , cleave ]
|
||||||
|
] if ;
|
||||||
|
|
||||||
MACRO: npick ( n -- )
|
MACRO: npick ( n -- )
|
||||||
1- dup saver [ dup ] rot [ r> swap ] n*quot 3append ;
|
1- dup saver [ dup ] rot [ r> swap ] n*quot 3append ;
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
! Copyright (C) 2008 Slava Pestov.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel sequences io.files io.launcher io.encodings.ascii
|
USING: kernel sequences io.files io.launcher io.encodings.ascii
|
||||||
io.streams.string http.client sequences.lib combinators
|
io.streams.string http.client generalizations combinators
|
||||||
math.parser math.vectors math.intervals interval-maps memoize
|
math.parser math.vectors math.intervals interval-maps memoize
|
||||||
csv accessors assocs strings math splitting grouping arrays ;
|
csv accessors assocs strings math splitting grouping arrays ;
|
||||||
IN: geo-ip
|
IN: geo-ip
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
! USING: kernel quotations namespaces sequences assocs.lib ;
|
! USING: kernel quotations namespaces sequences assocs.lib ;
|
||||||
|
|
||||||
USING: kernel namespaces namespaces.private quotations sequences
|
USING: kernel namespaces namespaces.private quotations sequences
|
||||||
assocs.lib math.parser math sequences.lib locals mirrors ;
|
assocs.lib math.parser math generalizations locals mirrors ;
|
||||||
|
|
||||||
IN: namespaces.lib
|
IN: namespaces.lib
|
||||||
|
|
||||||
|
|
|
@ -67,11 +67,6 @@ IN: sequences.lib.tests
|
||||||
{ 13 } [ 1 2 { 3 4 } [ + + ] 2 each-withn + ] unit-test
|
{ 13 } [ 1 2 { 3 4 } [ + + ] 2 each-withn + ] unit-test
|
||||||
[ { 910 911 912 } ] [ 10 900 3 [ + + ] map-with2 ] unit-test
|
[ { 910 911 912 } ] [ 10 900 3 [ + + ] map-with2 ] unit-test
|
||||||
|
|
||||||
[ 1 2 3 4 ] [ { 1 2 3 4 } 4 firstn ] unit-test
|
|
||||||
|
|
||||||
[ ] [ { } 0 firstn ] unit-test
|
|
||||||
[ "a" ] [ { "a" } 1 firstn ] unit-test
|
|
||||||
|
|
||||||
[ "empty" ] [ { } [ "not empty" ] [ "empty" ] if-seq ] unit-test
|
[ "empty" ] [ { } [ "not empty" ] [ "empty" ] if-seq ] unit-test
|
||||||
[ { 1 } "not empty" ] [ { 1 } [ "not empty" ] [ "empty" ] if-seq ] unit-test
|
[ { 1 } "not empty" ] [ { 1 } [ "not empty" ] [ "empty" ] if-seq ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -20,11 +20,6 @@ IN: sequences.lib
|
||||||
|
|
||||||
: map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
|
: map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
|
||||||
|
|
||||||
MACRO: firstn ( n -- )
|
|
||||||
[ [ swap nth ] curry [ keep ] curry ] map
|
|
||||||
concat >quotation
|
|
||||||
[ drop ] compose ;
|
|
||||||
|
|
||||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
|
||||||
: each-percent ( seq quot -- )
|
: each-percent ( seq quot -- )
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
! 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.files io.encodings.ascii sequences sequences.lib
|
USING: io.files io.encodings.ascii sequences generalizations
|
||||||
math.parser combinators kernel memoize csv symbols summary
|
math.parser combinators kernel memoize csv symbols summary
|
||||||
words accessors math.order sorting ;
|
words accessors math.order sorting ;
|
||||||
IN: usa-cities
|
IN: usa-cities
|
||||||
|
|
Loading…
Reference in New Issue