2005-04-07 18:54:02 -04:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
|
USING: errors generic kernel kernel-internals lists math
|
|
|
|
|
math-internals sequences ;
|
|
|
|
|
|
|
|
|
|
IN: vectors
|
|
|
|
|
|
2005-04-19 20:28:01 -04:00
|
|
|
: >vector ( list -- vector ) 0 <vector> [ swap nappend ] keep ;
|
2005-04-07 18:54:02 -04:00
|
|
|
|
|
|
|
|
: vector-project ( n quot -- vector )
|
|
|
|
|
#! Execute the quotation n times, passing the loop counter
|
|
|
|
|
#! the quotation as it ranges from 0..n-1. Collect results
|
|
|
|
|
#! in a new vector.
|
2005-04-17 21:59:11 -04:00
|
|
|
project >vector ; inline
|
2005-04-07 18:54:02 -04:00
|
|
|
|
2005-04-12 13:35:27 -04:00
|
|
|
: zero-vector ( n -- vector )
|
|
|
|
|
[ drop 0 ] vector-project ;
|
|
|
|
|
|
2005-04-07 18:54:02 -04:00
|
|
|
: vector-tail ( n vector -- list )
|
|
|
|
|
#! Return a new list with all elements from the nth
|
|
|
|
|
#! index upwards.
|
|
|
|
|
2dup length swap - [
|
|
|
|
|
pick + over nth
|
|
|
|
|
] project 2nip ;
|
|
|
|
|
|
|
|
|
|
: vector-tail* ( n vector -- list )
|
|
|
|
|
#! Unlike vector-tail, n is an index from the end of the
|
|
|
|
|
#! vector. For example, if n=1, this returns a vector of
|
|
|
|
|
#! one element.
|
|
|
|
|
[ length swap - ] keep vector-tail ;
|