1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel kernel.private math sequences
4 sequences.private slots.private ;
8 { array array read-only initial: { } }
14 : uncurry ( curry -- obj quot )
15 { curry } declare dup 2 slot swap 3 slot ; inline
17 : uncompose ( compose -- quot quot2 )
18 { compose } declare dup 2 slot swap 3 slot ; inline
22 M: quotation call (call) ;
24 M: curry call uncurry call ;
26 M: compose call uncompose [ call ] dip call ;
29 over wrapper? [ [ wrapped>> ] same? ] [ 2drop f ] if ;
31 UNION: callable quotation curry compose ;
34 over callable? [ sequence= ] [ 2drop f ] if ;
36 M: quotation length array>> length ;
38 M: quotation nth-unsafe array>> nth-unsafe ;
40 : >quotation ( seq -- quot )
41 >array array>quotation ; inline
43 M: callable like drop dup quotation? [ >quotation ] unless ;
45 INSTANCE: quotation immutable-sequence
47 : 1quotation ( obj -- quot ) 1array >quotation ;
49 GENERIC: literalize ( obj -- wrapped )
51 M: object literalize ;
53 M: wrapper literalize <wrapper> ;
55 M: curry length quot>> length 1 + ;
59 [ nip obj>> literalize ]
60 [ [ 1 - ] dip quot>> nth ]
63 INSTANCE: curry immutable-sequence
66 [ first>> length ] [ second>> length ] bi + ;
68 M: compose virtual-exemplar first>> ;
71 2dup first>> length < [
74 [ first>> length - ] [ second>> ] bi
77 INSTANCE: compose virtual-sequence