factor/library/collections/vectors-epilogue.factor

35 lines
1.0 KiB
Factor
Raw Normal View History

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
: >vector ( list -- vector )
dup length <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
: 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 ;
M: general-list thaw >vector ;
M: general-list freeze drop >list ;