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 ;
9 : uncurry ( curry -- obj quot )
10 { curry } declare dup 2 slot swap 3 slot ; inline
12 : uncompose ( compose -- quot quot2 )
13 { compose } declare dup 2 slot swap 3 slot ; inline
17 M: quotation call (call) ;
19 M: curry call uncurry call ;
21 M: compose call uncompose [ call ] dip call ;
24 over wrapper? [ [ wrapped>> ] same? ] [ 2drop f ] if ;
26 UNION: callable quotation curry compose ;
29 over callable? [ sequence= ] [ 2drop f ] if ;
31 M: quotation length array>> length ;
33 M: quotation nth-unsafe array>> nth-unsafe ;
35 : >quotation ( seq -- quot )
36 >array array>quotation ; inline
38 M: callable like drop dup quotation? [ >quotation ] unless ;
40 INSTANCE: quotation immutable-sequence
42 : 1quotation ( obj -- quot ) 1array >quotation ;
44 GENERIC: literalize ( obj -- wrapped )
46 M: object literalize ;
48 M: wrapper literalize <wrapper> ;
50 M: curry length quot>> length 1 + ;
54 [ nip obj>> literalize ]
55 [ [ 1 - ] dip quot>> nth ]
58 INSTANCE: curry immutable-sequence
61 [ first>> length ] [ second>> length ] bi + ;
63 M: compose virtual-exemplar first>> ;
66 2dup first>> length < [
69 [ first>> length - ] [ second>> ] bi
72 INSTANCE: compose virtual-sequence