1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays sequences sequences.private
4 kernel kernel.private math assocs quotations.private
10 : uncurry ( curry -- obj quot )
11 dup 2 slot swap 3 slot ; inline
13 : uncompose ( compose -- quot quot2 )
14 dup 2 slot swap 3 slot ; inline
18 M: quotation call (call) ;
20 M: curry call uncurry call ;
22 M: compose call uncompose slip call ;
25 over wrapper? [ [ wrapped>> ] bi@ = ] [ 2drop f ] if ;
27 UNION: callable quotation curry compose ;
30 over callable? [ sequence= ] [ 2drop f ] if ;
32 M: quotation length array>> length ;
34 M: quotation nth-unsafe array>> nth-unsafe ;
36 : >quotation ( seq -- quot )
37 >array array>quotation ; inline
39 M: callable like drop dup quotation? [ >quotation ] unless ;
41 INSTANCE: quotation immutable-sequence
43 : 1quotation ( obj -- quot ) 1array >quotation ;
45 GENERIC: literalize ( obj -- wrapped )
47 M: object literalize ;
49 M: wrapper literalize <wrapper> ;
51 M: curry length quot>> length 1+ ;
55 [ nip obj>> literalize ]
56 [ [ 1- ] dip quot>> nth ]
59 INSTANCE: curry immutable-sequence
62 [ first>> length ] [ second>> length ] bi + ;
64 M: compose virtual-seq first>> ;
67 2dup first>> length < [
70 [ first>> length - ] [ second>> ] bi
73 INSTANCE: compose virtual-sequence