Move remaining sequence operations from generalizations to sequences.generalizations where they belong

Slava Pestov 2010-05-18 18:36:47 -04:00
parent 0bc97cce00
commit c0f39aa99d
39 changed files with 245 additions and 247 deletions

View File

@ -1,11 +1,12 @@
! (c) 2009 Joe Groff, see BSD license ! (c) 2009 Joe Groff, see BSD license
USING: accessors alien alien.c-types alien.complex alien.data alien.parser USING: accessors alien alien.c-types alien.complex alien.data
grouping alien.strings alien.syntax arrays ascii assocs alien.parser grouping alien.strings alien.syntax arrays ascii
byte-arrays combinators combinators.short-circuit fry generalizations assocs byte-arrays combinators combinators.short-circuit fry
kernel lexer macros math math.parser namespaces parser sequences generalizations kernel lexer macros math math.parser namespaces
splitting stack-checker vectors vocabs.parser words locals parser sequences sequences.generalizations splitting
io.encodings.ascii io.encodings.string shuffle effects math.ranges stack-checker vectors vocabs.parser words locals
math.order sorting strings system alien.libraries ; io.encodings.ascii io.encodings.string shuffle effects
math.ranges math.order sorting strings system alien.libraries ;
QUALIFIED-WITH: alien.c-types c QUALIFIED-WITH: alien.c-types c
IN: alien.fortran IN: alien.fortran

View File

@ -3,11 +3,11 @@
USING: alien alien.strings arrays byte-arrays generic hashtables USING: alien alien.strings arrays byte-arrays generic hashtables
hashtables.private io io.binary io.files io.encodings.binary hashtables.private io io.binary io.files io.encodings.binary
io.pathnames kernel kernel.private math namespaces make parser io.pathnames kernel kernel.private math namespaces make parser
prettyprint sequences strings sbufs vectors words quotations prettyprint sequences sequences.generalizations strings sbufs
assocs system layouts splitting grouping growable classes vectors words quotations assocs system layouts splitting
classes.private classes.builtin classes.tuple grouping growable classes classes.private classes.builtin
classes.tuple.private vocabs vocabs.loader source-files classes.tuple classes.tuple.private vocabs vocabs.loader
definitions debugger quotations.private combinators source-files definitions debugger quotations.private combinators
combinators.short-circuit math.order math.private accessors combinators.short-circuit math.order math.private accessors
slots.private generic.single.private compiler.units slots.private generic.single.private compiler.units
compiler.constants fry locals bootstrap.image.syntax compiler.constants fry locals bootstrap.image.syntax

View File

@ -4,7 +4,7 @@ USING: accessors checksums checksums.common checksums.stream
combinators combinators.smart fry generalizations grouping combinators combinators.smart fry generalizations grouping
io.binary kernel literals locals make math math.bitwise io.binary kernel literals locals make math math.bitwise
math.ranges multiline namespaces sbufs sequences math.ranges multiline namespaces sbufs sequences
sequences.private splitting strings ; sequences.generalizations sequences.private splitting strings ;
IN: checksums.sha IN: checksums.sha
SINGLETON: sha1 SINGLETON: sha1

View File

@ -1,7 +1,7 @@
! Copyright (C) 2009 Doug Coleman. ! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors fry generalizations kernel macros math.order USING: accessors fry generalizations sequences.generalizations
stack-checker math sequences ; kernel macros math.order stack-checker math sequences ;
IN: combinators.smart IN: combinators.smart
MACRO: drop-outputs ( quot -- quot' ) MACRO: drop-outputs ( quot -- quot' )

View File

@ -1,9 +1,9 @@
! Copyright (C) 2008, 2010 Slava Pestov, Daniel Ehrenberg. ! Copyright (C) 2008, 2010 Slava Pestov, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs arrays classes combinators USING: accessors assocs arrays classes combinators
compiler.units fry generalizations generic kernel locals compiler.units fry generalizations sequences.generalizations
namespaces quotations sequences sets slots words generic kernel locals namespaces quotations sequences sets slots
compiler.cfg.instructions compiler.cfg.instructions.syntax words compiler.cfg.instructions compiler.cfg.instructions.syntax
compiler.cfg.rpo ; compiler.cfg.rpo ;
FROM: namespaces => set ; FROM: namespaces => set ;
FROM: sets => members ; FROM: sets => members ;

View File

@ -6,7 +6,7 @@ compiler.cfg.stacks.local compiler.tree.propagation.info
compiler.cfg.instructions compiler.cfg.instructions
cpu.architecture effects fry generalizations cpu.architecture effects fry generalizations
kernel locals macros make math namespaces quotations sequences kernel locals macros make math namespaces quotations sequences
splitting stack-checker words ; sequences.generalizations splitting stack-checker words ;
IN: compiler.cfg.intrinsics.simd.backend IN: compiler.cfg.intrinsics.simd.backend
! Selection of implementation based on available CPU instructions ! Selection of implementation based on available CPU instructions

View File

@ -1,9 +1,10 @@
! Copyright (C) 2009 Slava Pestov. ! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel accessors sequences arrays fry namespaces generic USING: kernel accessors sequences arrays fry namespaces generic
words sets combinators generalizations cpu.architecture compiler.units words sets combinators generalizations sequences.generalizations
compiler.cfg.utilities compiler.cfg compiler.cfg.rpo cpu.architecture compiler.units compiler.cfg.utilities
compiler.cfg.instructions compiler.cfg.def-use ; compiler.cfg compiler.cfg.rpo compiler.cfg.instructions
compiler.cfg.def-use ;
FROM: compiler.cfg.instructions.syntax => insn-def-slot insn-use-slots insn-temp-slots scalar-rep ; FROM: compiler.cfg.instructions.syntax => insn-def-slot insn-use-slots insn-temp-slots scalar-rep ;
FROM: namespaces => set ; FROM: namespaces => set ;
IN: compiler.cfg.representations.preferred IN: compiler.cfg.representations.preferred

View File

@ -3,7 +3,7 @@
USING: arrays byte-arrays byte-vectors generic assocs hashtables USING: arrays byte-arrays byte-vectors generic assocs hashtables
io.binary kernel kernel.private math namespaces make sequences io.binary kernel kernel.private math namespaces make sequences
words quotations strings alien.accessors alien.strings layouts words quotations strings alien.accessors alien.strings layouts
system combinators math.bitwise math.order generalizations system combinators math.bitwise math.order combinators.smart
accessors growable fry compiler.constants memoize ; accessors growable fry compiler.constants memoize ;
IN: compiler.codegen.fixup IN: compiler.codegen.fixup
@ -138,12 +138,14 @@ MEMO: cached-string>symbol ( symbol -- obj ) string>symbol ;
: with-fixup ( quot -- code ) : with-fixup ( quot -- code )
'[ '[
init-fixup [
@ init-fixup
emit-binary-literals @
label-table [ compute-labels ] change emit-binary-literals
parameter-table get >array label-table [ compute-labels ] change
literal-table get >array parameter-table get >array
relocation-table get >byte-array literal-table get >array
label-table get relocation-table get >byte-array
] B{ } make 5 narray ; inline label-table get
] B{ } make
] output>array ; inline

View File

@ -6,9 +6,10 @@ definitions stack-checker.dependencies quotations
classes.tuple.private math math.partial-dispatch math.private classes.tuple.private math math.partial-dispatch math.private
math.intervals sets.private math.floats.private math.intervals sets.private math.floats.private
math.integers.private layouts math.order vectors hashtables math.integers.private layouts math.order vectors hashtables
combinators effects generalizations assocs sets combinators effects generalizations sequences.generalizations
combinators.short-circuit sequences.private locals growable assocs sets combinators.short-circuit sequences.private locals
stack-checker namespaces compiler.tree.propagation.info ; growable stack-checker namespaces compiler.tree.propagation.info
;
FROM: math => float ; FROM: math => float ;
FROM: sets => set ; FROM: sets => set ;
IN: compiler.tree.propagation.transforms IN: compiler.tree.propagation.transforms

View File

@ -2,59 +2,10 @@ USING: help.syntax help.markup kernel sequences quotations
math arrays combinators ; math arrays combinators ;
IN: generalizations IN: generalizations
HELP: nsequence
{ $values { "n" integer } { "seq" "an exemplar" } }
{ $description "A generalization of " { $link 2sequence } ", "
{ $link 3sequence } ", and " { $link 4sequence } " "
"that constructs a sequence from the top " { $snippet "n" } " elements of the stack."
}
{ $examples
{ $example "USING: generalizations prettyprint ;" "CHAR: f CHAR: i CHAR: s CHAR: h 4 \"\" nsequence ." "\"fish\"" }
} ;
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."
}
{ $examples
"Some core words expressed in terms of " { $link narray } ":"
{ $table
{ { $link 1array } { $snippet "1 narray" } }
{ { $link 2array } { $snippet "2 narray" } }
{ { $link 3array } { $snippet "3 narray" } }
{ { $link 4array } { $snippet "4 narray" } }
}
} ;
{ nsequence narray } related-words
HELP: nsum HELP: nsum
{ $values { "n" integer } } { $values { "n" integer } }
{ $description "Adds the top " { $snippet "n" } " stack values." } ; { $description "Adds the top " { $snippet "n" } " stack values." } ;
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."
}
{ $examples
"Some core words expressed in terms of " { $link firstn } ":"
{ $table
{ { $link first } { $snippet "1 firstn" } }
{ { $link first2 } { $snippet "2 firstn" } }
{ { $link first3 } { $snippet "3 firstn" } }
{ { $link first4 } { $snippet "4 firstn" } }
}
} ;
HELP: set-firstn
{ $values { "n" integer } }
{ $description "A generalization of " { $link set-first } " "
"that sets the first " { $snippet "n" } " elements of a sequence from the top " { $snippet "n" } " elements of the stack." } ;
HELP: npick HELP: npick
{ $values { "n" integer } } { $values { "n" integer } }
{ $description "A generalization of " { $link dup } ", " { $description "A generalization of " { $link dup } ", "
@ -302,46 +253,6 @@ HELP: n*quot
} }
{ $description "Construct a quotation containing the contents of " { $snippet "seq" } " repeated " { $snippet "n"} " times." } ; { $description "Construct a quotation containing the contents of " { $snippet "seq" } " repeated " { $snippet "n"} " times." } ;
HELP: nappend
{ $values
{ "n" integer }
{ "seq" sequence }
}
{ $description "Outputs a new sequence consisting of the elements of the top " { $snippet "n" } " sequences from the datastack in turn." }
{ $errors "Throws an error if any of the sequences contain elements that are not permitted in the sequence type of the first sequence." }
{ $examples
{ $example "USING: generalizations prettyprint math ;"
"{ 1 2 } { 3 4 } { 5 6 } { 7 8 } 4 nappend ."
"{ 1 2 3 4 5 6 7 8 }"
}
} ;
HELP: nappend-as
{ $values
{ "n" integer } { "exemplar" sequence }
{ "seq" sequence }
}
{ $description "Outputs a new sequence of type " { $snippet "exemplar" } " consisting of the elements of the top " { $snippet "n" } " sequences from the datastack in turn." }
{ $errors "Throws an error if any of the sequences contain elements that are not permitted in the sequence type of the first sequence." }
{ $examples
{ $example "USING: generalizations prettyprint math ;"
"{ 1 2 } { 3 4 } { 5 6 } { 7 8 } 4 V{ } nappend-as ."
"V{ 1 2 3 4 5 6 7 8 }"
}
} ;
{ nappend nappend-as } related-words
ARTICLE: "sequence-generalizations" "Generalized sequence operations"
{ $subsections
narray
nsequence
firstn
set-firstn
nappend
nappend-as
} ;
ARTICLE: "shuffle-generalizations" "Generalized shuffle words" ARTICLE: "shuffle-generalizations" "Generalized shuffle words"
{ $subsections { $subsections
ndup ndup
@ -381,11 +292,10 @@ 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."
{ $subsections { $subsections
"sequence-generalizations"
"shuffle-generalizations" "shuffle-generalizations"
"combinator-generalizations" "combinator-generalizations"
"other-generalizations" "other-generalizations"
} }
"Also see the " { $vocab-link "sequences.generalizations" } " vocabulary for generalized sequence iteration combinators." ; "Also see the " { $vocab-link "sequences.generalizations" } " vocabulary for generalized sequence operations." ;
ABOUT: "generalizations" ABOUT: "generalizations"

View File

@ -39,24 +39,10 @@ IN: generalizations.tests
[ { "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
[ { 1 2 3 4 } ] [ 1 2 3 4 { f f f f } [ 4 set-firstn ] keep ] unit-test
[ 1 2 3 4 { f f f } [ 4 set-firstn ] keep ] must-fail
[ ] [ { } 0 firstn ] unit-test
[ "a" ] [ { "a" } 1 firstn ] unit-test
[ [ 1 2 ] ] [ 1 2 2 [ ] nsequence ] unit-test
[ 4 5 1 2 3 ] [ 1 2 3 4 5 2 3 mnswap ] unit-test [ 4 5 1 2 3 ] [ 1 2 3 4 5 2 3 mnswap ] unit-test
[ 1 2 3 4 5 6 ] [ 1 2 3 4 5 6 2 4 mnswap 4 2 mnswap ] unit-test [ 1 2 3 4 5 6 ] [ 1 2 3 4 5 6 2 4 mnswap 4 2 mnswap ] unit-test
[ { 1 2 3 4 } ] [ { 1 } { 2 } { 3 } { 4 } 4 nappend ] unit-test
[ V{ 1 2 3 4 } ] [ { 1 } { 2 } { 3 } { 4 } 4 V{ } nappend-as ] unit-test
[ 4 nappend ] must-infer
[ 4 { } nappend-as ] must-infer
[ 17 ] [ 3 1 3 3 7 5 nsum ] unit-test [ 17 ] [ 3 1 3 3 7 5 nsum ] unit-test
{ 4 1 } [ 4 nsum ] must-infer-as { 4 1 } [ 4 nsum ] must-infer-as

View File

@ -14,26 +14,9 @@ ALIAS: n*quot (n*quot)
>> >>
MACRO: nsequence ( n seq -- )
[ [nsequence] ] keep
'[ @ _ like ] ;
MACRO: narray ( n -- )
'[ _ { } nsequence ] ;
MACRO: nsum ( n -- ) MACRO: nsum ( n -- )
1 - [ + ] n*quot ; 1 - [ + ] n*quot ;
MACRO: firstn-unsafe ( n -- )
[firstn] ;
MACRO: firstn ( n -- )
dup zero? [ drop [ drop ] ] [
[ 1 - swap bounds-check 2drop ]
[ firstn-unsafe ]
bi-curry '[ _ _ bi ]
] if ;
MACRO: npick ( n -- ) MACRO: npick ( n -- )
1 - [ dup ] [ '[ _ dip swap ] ] repeat ; 1 - [ dup ] [ '[ _ dip swap ] ] repeat ;
@ -53,18 +36,6 @@ MACRO: nrot ( n -- )
MACRO: -nrot ( n -- ) MACRO: -nrot ( n -- )
1 - [ ] [ '[ swap _ dip ] ] repeat ; 1 - [ ] [ '[ swap _ dip ] ] repeat ;
MACRO: set-firstn-unsafe ( n -- )
[ 1 + ]
[ iota [ '[ _ rot [ set-nth-unsafe ] keep ] ] map ] bi
'[ _ -nrot _ spread drop ] ;
MACRO: set-firstn ( n -- )
dup zero? [ drop [ drop ] ] [
[ 1 - swap bounds-check 2drop ]
[ set-firstn-unsafe ]
bi-curry '[ _ _ bi ]
] if ;
MACRO: ndrop ( n -- ) MACRO: ndrop ( n -- )
[ drop ] n*quot ; [ drop ] n*quot ;
@ -143,9 +114,3 @@ MACRO: nweave ( n -- )
MACRO: nbi-curry ( n -- ) MACRO: nbi-curry ( n -- )
[ bi-curry ] n*quot ; [ bi-curry ] n*quot ;
: nappend-as ( n exemplar -- seq )
[ narray concat ] dip like ; inline
: nappend ( n -- seq ) narray concat ; inline

View File

@ -6,7 +6,7 @@ classes.tuple namespaces make vectors bit-arrays byte-arrays
strings sbufs math.functions macros sequences.private strings sbufs math.functions macros sequences.private
combinators mirrors splitting combinators.smart combinators mirrors splitting combinators.smart
combinators.short-circuit fry words.symbol generalizations combinators.short-circuit fry words.symbol generalizations
classes ; sequences.generalizations classes ;
IN: inverse IN: inverse
ERROR: fail ; ERROR: fail ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2007, 2008 Slava Pestov, Eduardo Cavazos. ! Copyright (C) 2007, 2008 Slava Pestov, Eduardo Cavazos.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs classes classes.tuple fry USING: accessors arrays assocs classes classes.tuple fry
generalizations hashtables kernel locals locals.backend sequences.generalizations hashtables kernel locals locals.backend
locals.errors locals.types make quotations sequences vectors locals.errors locals.types make quotations sequences vectors
words ; words ;
IN: locals.rewrite.sugar IN: locals.rewrite.sugar

View File

@ -4,7 +4,8 @@ USING: logging.server sequences namespaces concurrency.messaging
words kernel arrays shuffle tools.annotations words kernel arrays shuffle tools.annotations
prettyprint.config prettyprint debugger io.streams.string prettyprint.config prettyprint debugger io.streams.string
splitting continuations effects generalizations parser strings splitting continuations effects generalizations parser strings
quotations fry accessors math assocs math.order ; quotations fry accessors math assocs math.order
sequences.generalizations ;
IN: logging IN: logging
SYMBOLS: DEBUG NOTICE WARNING ERROR CRITICAL ; SYMBOLS: DEBUG NOTICE WARNING ERROR CRITICAL ;

View File

@ -2,8 +2,8 @@
USING: accessors alien alien.c-types alien.data combinators USING: accessors alien alien.c-types alien.data combinators
sequences.cords cpu.architecture fry generalizations grouping sequences.cords cpu.architecture fry generalizations grouping
kernel libc locals math math.libm math.order math.ranges kernel libc locals math math.libm math.order math.ranges
math.vectors sequences sequences.private specialized-arrays math.vectors sequences sequences.generalizations
vocabs.loader ; sequences.private specialized-arrays vocabs.loader ;
QUALIFIED-WITH: alien.c-types c QUALIFIED-WITH: alien.c-types c
SPECIALIZED-ARRAYS: SPECIALIZED-ARRAYS:
c:char c:short c:int c:longlong c:char c:short c:int c:longlong

View File

@ -3,7 +3,8 @@ cpu.architecture effects fry functors generalizations generic
generic.parser kernel lexer literals locals macros math math.functions generic.parser kernel lexer literals locals macros math math.functions
math.vectors math.vectors.private math.vectors.simd.intrinsics math.vectors math.vectors.private math.vectors.simd.intrinsics
namespaces parser prettyprint.custom quotations sequences namespaces parser prettyprint.custom quotations sequences
sequences.private vocabs vocabs.loader words ; sequences.generalizations sequences.private vocabs vocabs.loader
words ;
QUALIFIED-WITH: alien.c-types c QUALIFIED-WITH: alien.c-types c
IN: math.vectors.simd IN: math.vectors.simd

View File

@ -1,7 +1,8 @@
! Copyright (C) 2009, 2010 Slava Pestov. ! Copyright (C) 2009, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: models.arrow models.product stack-checker accessors fry USING: models.arrow models.product stack-checker accessors fry
generalizations combinators.smart macros kernel ; generalizations sequences.generalizations combinators.smart
macros kernel ;
IN: models.arrow.smart IN: models.arrow.smart
MACRO: <smart-arrow> ( quot -- quot' ) MACRO: <smart-arrow> ( quot -- quot' )

View File

@ -1,7 +1,8 @@
! 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: accessors kernel models arrays sequences math math.order USING: accessors kernel models arrays sequences math math.order
models.product generalizations math.functions ; models.product generalizations sequences.generalizations
math.functions ;
FROM: models.product => product ; FROM: models.product => product ;
IN: models.range IN: models.range

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Doug Coleman. ! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: namespaces sequences math.parser kernel macros USING: namespaces sequences math.parser kernel macros
generalizations locals ; generalizations sequences.generalizations locals ;
IN: nmake IN: nmake
SYMBOL: building-seq SYMBOL: building-seq

View File

@ -6,8 +6,8 @@ USING: alien alien.c-types ascii calendar combinators.short-circuit
continuations kernel libc math macros namespaces math.vectors continuations kernel libc math macros namespaces math.vectors
math.parser opengl.gl combinators combinators.smart arrays math.parser opengl.gl combinators combinators.smart arrays
sequences splitting words byte-arrays assocs vocabs sequences splitting words byte-arrays assocs vocabs
colors colors.constants accessors generalizations locals fry colors colors.constants accessors generalizations
specialized-arrays ; sequences.generalizations locals fry specialized-arrays ;
FROM: alien.c-types => float ; FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: float
SPECIALIZED-ARRAY: uint SPECIALIZED-ARRAY: uint

View File

@ -3,6 +3,85 @@ USING: help.syntax help.markup kernel sequences quotations
math arrays combinators ; math arrays combinators ;
IN: sequences.generalizations IN: sequences.generalizations
HELP: nsequence
{ $values { "n" integer } { "seq" "an exemplar" } }
{ $description "A generalization of " { $link 2sequence } ", "
{ $link 3sequence } ", and " { $link 4sequence } " "
"that constructs a sequence from the top " { $snippet "n" } " elements of the stack."
}
{ $examples
{ $example "USING: generalizations prettyprint ;" "CHAR: f CHAR: i CHAR: s CHAR: h 4 \"\" nsequence ." "\"fish\"" }
} ;
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."
}
{ $examples
"Some core words expressed in terms of " { $link narray } ":"
{ $table
{ { $link 1array } { $snippet "1 narray" } }
{ { $link 2array } { $snippet "2 narray" } }
{ { $link 3array } { $snippet "3 narray" } }
{ { $link 4array } { $snippet "4 narray" } }
}
} ;
{ nsequence narray } related-words
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."
}
{ $examples
"Some core words expressed in terms of " { $link firstn } ":"
{ $table
{ { $link first } { $snippet "1 firstn" } }
{ { $link first2 } { $snippet "2 firstn" } }
{ { $link first3 } { $snippet "3 firstn" } }
{ { $link first4 } { $snippet "4 firstn" } }
}
} ;
HELP: set-firstn
{ $values { "n" integer } }
{ $description "A generalization of " { $link set-first } " "
"that sets the first " { $snippet "n" } " elements of a sequence from the top " { $snippet "n" } " elements of the stack." } ;
HELP: nappend
{ $values
{ "n" integer }
{ "seq" sequence }
}
{ $description "Outputs a new sequence consisting of the elements of the top " { $snippet "n" } " sequences from the datastack in turn." }
{ $errors "Throws an error if any of the sequences contain elements that are not permitted in the sequence type of the first sequence." }
{ $examples
{ $example "USING: generalizations prettyprint math ;"
"{ 1 2 } { 3 4 } { 5 6 } { 7 8 } 4 nappend ."
"{ 1 2 3 4 5 6 7 8 }"
}
} ;
HELP: nappend-as
{ $values
{ "n" integer } { "exemplar" sequence }
{ "seq" sequence }
}
{ $description "Outputs a new sequence of type " { $snippet "exemplar" } " consisting of the elements of the top " { $snippet "n" } " sequences from the datastack in turn." }
{ $errors "Throws an error if any of the sequences contain elements that are not permitted in the sequence type of the first sequence." }
{ $examples
{ $example "USING: generalizations prettyprint math ;"
"{ 1 2 } { 3 4 } { 5 6 } { 7 8 } 4 V{ } nappend-as ."
"V{ 1 2 3 4 5 6 7 8 }"
}
} ;
{ nappend nappend-as } related-words
HELP: neach HELP: neach
{ $values { "seq..." { $snippet "n" } " sequences on the datastack" } { "quot" "a quotation with stack effect " { $snippet "( element... -- )" } } { "n" integer } } { $values { "seq..." { $snippet "n" } " sequences on the datastack" } { "quot" "a quotation with stack effect " { $snippet "( element... -- )" } } { "n" integer } }
{ $description "A generalization of " { $link each } ", " { $link 2each } ", and " { $link 3each } " that can iterate over any number of sequences in parallel." } ; { $description "A generalization of " { $link each } ", " { $link 2each } ", and " { $link 3each } " that can iterate over any number of sequences in parallel." } ;
@ -31,8 +110,17 @@ HELP: nproduce-as
{ $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj1 obj2 ... objn )" } } { "exemplar..." { $snippet "n" } " sequences on the datastack" } { "n" integer } { "seq..." { $snippet "n" } " sequences on the datastack of the same types as the " { $snippet "exemplar" } "s" } } { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj1 obj2 ... objn )" } } { "exemplar..." { $snippet "n" } " sequences on the datastack" } { "n" integer } { "seq..." { $snippet "n" } " sequences on the datastack of the same types as the " { $snippet "exemplar" } "s" } }
{ $description "A generalization of " { $link produce-as } " that generates " { $snippet "n" } " sequences in parallel by calling " { $snippet "quot" } " repeatedly until " { $snippet "pred" } " outputs false." } ; { $description "A generalization of " { $link produce-as } " that generates " { $snippet "n" } " sequences in parallel by calling " { $snippet "quot" } " repeatedly until " { $snippet "pred" } " outputs false." } ;
ARTICLE: "sequences.generalizations" "Generalized sequence iteration combinators" ARTICLE: "sequences.generalizations" "Generalized sequence words"
"The " { $vocab-link "sequences.generalizations" } " vocabulary defines generalized versions of the iteration " { $link "sequences-combinators" } "." "The " { $vocab-link "sequences.generalizations" } " vocabulary defines generalized versions of various sequence operations."
{ $subsections
narray
nsequence
firstn
set-firstn
nappend
nappend-as
}
"Generalized " { $link "sequences-combinators" } ":"
{ $subsections { $subsections
neach neach
nmap nmap

View File

@ -3,6 +3,20 @@ USING: tools.test generalizations kernel math arrays sequences
sequences.generalizations ascii fry math.parser io io.streams.string ; sequences.generalizations ascii fry math.parser io io.streams.string ;
IN: sequences.generalizations.tests IN: sequences.generalizations.tests
[ 1 2 3 4 ] [ { 1 2 3 4 } 4 firstn ] unit-test
[ { 1 2 3 4 } ] [ 1 2 3 4 { f f f f } [ 4 set-firstn ] keep ] unit-test
[ 1 2 3 4 { f f f } [ 4 set-firstn ] keep ] must-fail
[ ] [ { } 0 firstn ] unit-test
[ "a" ] [ { "a" } 1 firstn ] unit-test
[ [ 1 2 ] ] [ 1 2 2 [ ] nsequence ] unit-test
[ { 1 2 3 4 } ] [ { 1 } { 2 } { 3 } { 4 } 4 nappend ] unit-test
[ V{ 1 2 3 4 } ] [ { 1 } { 2 } { 3 } { 4 } 4 V{ } nappend-as ] unit-test
[ 4 nappend ] must-infer
[ 4 { } nappend-as ] must-infer
: neach-test ( a b c d -- ) : neach-test ( a b c d -- )
[ 4 nappend print ] 4 neach ; [ 4 nappend print ] 4 neach ;
: nmap-test ( a b c d -- e ) : nmap-test ( a b c d -- e )

View File

@ -4,12 +4,47 @@ combinators macros math.order math.ranges quotations fry effects
memoize.private generalizations ; memoize.private generalizations ;
IN: sequences.generalizations IN: sequences.generalizations
MACRO: nsequence ( n seq -- )
[ [nsequence] ] keep
'[ @ _ like ] ;
MACRO: narray ( n -- )
'[ _ { } nsequence ] ;
MACRO: firstn-unsafe ( n -- )
[firstn] ;
MACRO: firstn ( n -- )
dup zero? [ drop [ drop ] ] [
[ 1 - swap bounds-check 2drop ]
[ firstn-unsafe ]
bi-curry '[ _ _ bi ]
] if ;
MACRO: set-firstn-unsafe ( n -- )
[ 1 + ]
[ iota [ '[ _ rot [ set-nth-unsafe ] keep ] ] map ] bi
'[ _ -nrot _ spread drop ] ;
MACRO: set-firstn ( n -- )
dup zero? [ drop [ drop ] ] [
[ 1 - swap bounds-check 2drop ]
[ set-firstn-unsafe ]
bi-curry '[ _ _ bi ]
] if ;
: nappend-as ( n exemplar -- seq )
[ narray concat ] dip like ; inline
: nappend ( n -- seq ) narray concat ; inline
MACRO: nmin-length ( n -- ) MACRO: nmin-length ( n -- )
dup 1 - [ min ] n*quot dup 1 - [ min ] n*quot
'[ [ length ] _ napply @ ] ; '[ [ length ] _ napply @ ] ;
: nnth-unsafe ( n seq... n -- ) : nnth-unsafe ( n seq... n -- )
[ nth-unsafe ] swap [ apply-curry ] [ cleave* ] bi ; inline [ nth-unsafe ] swap [ apply-curry ] [ cleave* ] bi ; inline
MACRO: nset-nth-unsafe ( n -- ) MACRO: nset-nth-unsafe ( n -- )
[ [ drop ] ] [ [ drop ] ]
[ '[ [ set-nth-unsafe ] _ [ apply-curry ] [ cleave-curry ] [ spread* ] tri ] ] [ '[ [ set-nth-unsafe ] _ [ apply-curry ] [ cleave-curry ] [ spread* ] tri ] ]

View File

@ -1,8 +1,8 @@
! Copyright (C) 2007 Chris Double, Doug Coleman. ! Copyright (C) 2007 Chris Double, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs combinators effects.parser generalizations USING: accessors assocs combinators effects.parser
hashtables kernel locals locals.backend macros make math generalizations sequences.generalizations hashtables kernel
parser sequences ; locals locals.backend macros make math parser sequences ;
IN: shuffle IN: shuffle
<PRIVATE <PRIVATE

View File

@ -1,9 +1,10 @@
! Copyright (C) 2005, 2010 Slava Pestov. ! Copyright (C) 2005, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel math sorting words parser io summary USING: accessors kernel math sorting words parser io summary
quotations sequences prettyprint continuations effects quotations sequences sequences.generalizations prettyprint
definitions compiler.units namespaces assocs tools.time generic continuations effects definitions compiler.units namespaces
inspector fry locals generalizations macros ; assocs tools.time generic inspector fry locals generalizations
macros ;
IN: tools.annotations IN: tools.annotations
<PRIVATE <PRIVATE

View File

@ -4,8 +4,9 @@ USING: accessors arrays assocs binary-search classes
classes.struct combinators combinators.smart continuations fry classes.struct combinators combinators.smart continuations fry
generalizations generic grouping io io.styles kernel make math generalizations generic grouping io io.styles kernel make math
math.order math.parser math.statistics memory memory.private math.order math.parser math.statistics memory memory.private
layouts namespaces parser prettyprint sequences sorting layouts namespaces parser prettyprint sequences
splitting strings system vm words hints hashtables ; sequences.generalizations sorting splitting strings system vm
words hints hashtables ;
IN: tools.memory IN: tools.memory
<PRIVATE <PRIVATE

View File

@ -1,13 +1,13 @@
! Copyright (C) 2003, 2010 Slava Pestov. ! Copyright (C) 2003, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs combinators compiler.units USING: accessors arrays assocs combinators compiler.units
continuations debugger effects fry generalizations io io.files continuations debugger effects fry generalizations
io.styles kernel lexer locals macros math.parser namespaces sequences.generalizations io io.files io.styles kernel lexer
parser vocabs.parser prettyprint quotations sequences locals macros math.parser namespaces parser vocabs.parser
source-files splitting stack-checker summary unicode.case prettyprint quotations sequences source-files splitting
vectors vocabs vocabs.loader vocabs.files vocabs.metadata words stack-checker summary unicode.case vectors vocabs vocabs.loader
tools.errors source-files.errors io.streams.string make vocabs.files vocabs.metadata words tools.errors
compiler.errors ; source-files.errors io.streams.string make compiler.errors ;
IN: tools.test IN: tools.test
TUPLE: test-failure < source-file-error continuation ; TUPLE: test-failure < source-file-error continuation ;

View File

@ -5,8 +5,8 @@ USING: accessors alien alien.c-types alien.libraries
alien.syntax byte-arrays classes.struct combinators alien.syntax byte-arrays classes.struct combinators
combinators.short-circuit combinators.smart continuations combinators.short-circuit combinators.smart continuations
generalizations io kernel libc locals macros math namespaces generalizations io kernel libc locals macros math namespaces
sequences stack-checker strings system unix.time unix.types sequences sequences.generalizations stack-checker strings system
vocabs vocabs.loader unix.ffi ; unix.time unix.types vocabs vocabs.loader unix.ffi ;
IN: unix IN: unix
ERROR: unix-error errno message ; ERROR: unix-error errno message ;

View File

@ -2,8 +2,8 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs combinators USING: accessors arrays assocs combinators
combinators.short-circuit fry generalizations inverse kernel combinators.short-circuit fry generalizations inverse kernel
namespaces sequences sorting strings unicode.categories namespaces sequences sequences.generalizations sorting strings
xml.data xml.syntax xml.syntax.private ; unicode.categories xml.data xml.syntax xml.syntax.private ;
IN: xml.syntax.inverse IN: xml.syntax.inverse
: remove-blanks ( seq -- newseq ) : remove-blanks ( seq -- newseq )

View File

@ -1,11 +1,11 @@
! Copyright (C) 2005, 2009 Daniel Ehrenberg ! Copyright (C) 2005, 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: words assocs kernel accessors parser vocabs.parser effects.parser USING: words assocs kernel accessors parser vocabs.parser
sequences summary lexer splitting combinators locals effects.parser sequences summary lexer splitting combinators
memoize sequences.deep xml.data xml.state xml namespaces present locals memoize sequences.deep xml.data xml.state xml namespaces
arrays generalizations strings make math macros multiline present arrays generalizations sequences.generalizations strings
combinators.short-circuit sorting fry unicode.categories make math macros multiline combinators.short-circuit sorting fry
effects ; unicode.categories effects ;
IN: xml.syntax IN: xml.syntax
<PRIVATE <PRIVATE

View File

@ -1,7 +1,8 @@
! (c)2009 Joe Groff bsd license ! (c)2009 Joe Groff bsd license
USING: accessors alien audio classes.struct fry calendar alarms USING: accessors alien audio classes.struct fry calendar alarms
combinators combinators.short-circuit destructors generalizations combinators combinators.short-circuit destructors generalizations
kernel literals locals math openal sequences specialized-arrays strings ; kernel literals locals math openal sequences
sequences.generalizations specialized-arrays strings ;
QUALIFIED-WITH: alien.c-types c QUALIFIED-WITH: alien.c-types c
SPECIALIZED-ARRAYS: c:float c:uchar c:uint ; SPECIALIZED-ARRAYS: c:float c:uchar c:uint ;
IN: audio.engine IN: audio.engine

View File

@ -2,8 +2,8 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors combinators combinators.short-circuit USING: accessors combinators combinators.short-circuit
generalizations kernel locals math.order math.ranges generalizations kernel locals math.order math.ranges
sequences.parser sequences sorting.functor sorting.slots sequences.parser sequences sequences.generalizations
unicode.categories ; sorting.functor sorting.slots unicode.categories ;
IN: c.lexer IN: c.lexer
: take-c-comment ( sequence-parser -- seq/f ) : take-c-comment ( sequence-parser -- seq/f )

View File

@ -1,8 +1,9 @@
! Copyright (C) 2009 Slava Pestov, Doug Coleman. ! Copyright (C) 2009 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs classes classes.tuple USING: accessors arrays assocs classes classes.tuple
effects.parser fry generalizations generic.standard kernel effects.parser fry generalizations sequences.generalizations
lexer locals macros parser sequences sets slots vocabs words ; generic.standard kernel lexer locals macros parser sequences
sets slots vocabs words ;
IN: constructors IN: constructors
! An experiment ! An experiment

View File

@ -1,15 +0,0 @@
USING: accessors sequences generalizations io.encodings.utf8 db.postgresql parser combinators vocabs.parser db.sqlite
io.files ;
IN: db.info
! having sensative (and likely to change) information directly in source code seems a bad idea
: get-info ( -- lines ) current-vocab name>> "vocab:" "/dbinfo.txt" surround utf8 file-lines ;
SYNTAX: get-psql-info <postgresql-db> get-info 5 firstn
{
[ >>host ]
[ >>port ]
[ >>username ]
[ [ f ] [ ] if-empty >>password ]
[ >>database ]
} spread suffix! ;
SYNTAX: get-sqlite-info get-info first <sqlite-db> suffix! ;

View File

@ -1,9 +1,9 @@
! Copyright (c) 2008 Daniel Ehrenberg. ! Copyright (c) 2008 Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: words kernel sequences locals locals.parser fry USING: words kernel sequences sequences.generalizations locals
locals.definitions accessors parser namespaces continuations locals.parser fry locals.definitions accessors parser namespaces
summary definitions generalizations arrays prettyprint debugger io continuations summary definitions generalizations arrays
effects tools.annotations effects.parser ; prettyprint debugger io effects tools.annotations effects.parser ;
IN: descriptive IN: descriptive
ERROR: descriptive-error args underlying word ; ERROR: descriptive-error args underlying word ;

View File

@ -1,7 +1,8 @@
! (c)Joe Groff bsd license ! (c)Joe Groff bsd license
USING: accessors classes.struct fry generalizations kernel locals USING: accessors classes.struct fry generalizations kernel
math math.combinatorics math.functions math.matrices.simd math.vectors locals math math.combinatorics math.functions math.matrices.simd
math.vectors.simd math.quaternions sequences sequences.private specialized-arrays math.vectors math.vectors.simd math.quaternions sequences
sequences.generalizations sequences.private specialized-arrays
typed ; typed ;
FROM: sequences.private => nth-unsafe ; FROM: sequences.private => nth-unsafe ;
FROM: math.quaternions.private => (q*sign) ; FROM: math.quaternions.private => (q*sign) ;

View File

@ -3,9 +3,9 @@
USING: kernel math sequences vectors classes classes.algebra USING: kernel math sequences vectors classes classes.algebra
combinators arrays words assocs parser namespaces make combinators arrays words assocs parser namespaces make
definitions prettyprint prettyprint.backend prettyprint.custom definitions prettyprint prettyprint.backend prettyprint.custom
quotations generalizations debugger io compiler.units quotations generalizations sequences.generalizations debugger io
kernel.private effects accessors hashtables sorting shuffle compiler.units kernel.private effects accessors hashtables
math.order sets see effects.parser ; sorting shuffle math.order sets see effects.parser ;
FROM: namespaces => set ; FROM: namespaces => set ;
IN: multi-methods IN: multi-methods

View File

@ -2,10 +2,11 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs combinators.short-circuit USING: accessors arrays assocs combinators.short-circuit
continuations fry kernel namespaces quotations sequences sets continuations fry kernel namespaces quotations sequences sets
generalizations slots locals.types splitting math generalizations sequences.generalizations slots locals.types
locals.rewrite.closures generic words combinators locals smalltalk.ast splitting math locals.rewrite.closures generic words combinators
smalltalk.compiler.lexenv smalltalk.compiler.assignment locals smalltalk.ast smalltalk.compiler.lexenv
smalltalk.compiler.return smalltalk.selectors smalltalk.classes ; smalltalk.compiler.assignment smalltalk.compiler.return
smalltalk.selectors smalltalk.classes ;
IN: smalltalk.compiler IN: smalltalk.compiler
GENERIC: compile-ast ( lexenv ast -- quot ) GENERIC: compile-ast ( lexenv ast -- quot )