factor/extra/tuple-arrays/tuple-arrays.factor

36 lines
931 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-12 17:56:51 -04:00
TUPLE: tuple-array seq class ;
2007-09-20 18:09:08 -04:00
: <tuple-array> ( length example -- tuple-array )
2008-07-12 17:56:51 -04:00
[ tuple>array length 1- [ * { } new-sequence ] keep <sliced-groups> ]
[ class ] 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
2008-07-12 17:56:51 -04:00
: deconstruct ( tuple -- seq )
tuple>array 1 tail ;
2007-09-20 18:09:08 -04:00
M: tuple-array set-nth ( elt n seq -- )
2008-07-12 17:56:51 -04:00
>r >r deconstruct r> r> seq>> set-nth ;
2007-09-20 18:09:08 -04:00
2008-07-12 17:56:51 -04:00
M: tuple-array new-sequence
class>> new <tuple-array> ;
2007-09-20 18:09:08 -04:00
: >tuple-array ( seq -- tuple-array/seq )
dup empty? [
0 over first <tuple-array> clone-like
] 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