factor/basis/tuple-arrays/tuple-arrays.factor

35 lines
923 B
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2007 Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
2008-06-09 06:22:21 -04:00
USING: splitting grouping classes.tuple classes math kernel
2008-07-12 17:56:51 -04:00
sequences arrays accessors ;
2007-09-20 18:09:08 -04:00
IN: tuple-arrays
2008-07-14 04:30:33 -04:00
TUPLE: tuple-array { seq read-only } { class read-only } ;
2007-09-20 18:09:08 -04:00
2008-07-14 04:30:33 -04:00
: <tuple-array> ( length class -- tuple-array )
[
new tuple>array 1 tail
[ <repetition> concat ] [ length ] bi <sliced-groups>
] [ ] bi tuple-array boa ;
2007-09-20 18:09:08 -04:00
M: tuple-array nth
2008-07-12 17:56:51 -04:00
[ seq>> nth ] [ class>> ] bi prefix >tuple ;
2007-09-20 18:09:08 -04:00
M: tuple-array set-nth ( elt n seq -- )
2008-12-17 20:17:37 -05:00
[ tuple>array 1 tail ] 2dip seq>> set-nth ;
2007-09-20 18:09:08 -04:00
2008-07-12 17:56:51 -04:00
M: tuple-array new-sequence
2008-07-14 04:30:33 -04:00
class>> <tuple-array> ;
2007-09-20 18:09:08 -04:00
2008-07-14 04:30:33 -04:00
: >tuple-array ( seq -- tuple-array )
2007-09-20 18:09:08 -04:00
dup empty? [
2008-07-14 04:30:33 -04:00
0 over first class <tuple-array> clone-like
2007-09-20 18:09:08 -04:00
] unless ;
M: tuple-array like
drop dup tuple-array? [ >tuple-array ] unless ;
2008-07-12 17:56:51 -04:00
M: tuple-array length seq>> length ;
2007-09-20 18:09:08 -04:00
INSTANCE: tuple-array sequence