1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays sequences sequences.private
4 kernel kernel.private math assocs quotations.private
8 M: quotation call (call) ;
10 M: curry call dup 3 slot swap 4 slot call ;
12 M: compose call dup 3 slot swap 4 slot slip call ;
15 over wrapper? [ [ wrapped ] bi@ = ] [ 2drop f ] if ;
17 UNION: callable quotation curry compose ;
20 over callable? [ sequence= ] [ 2drop f ] if ;
22 M: quotation length quotation-array length ;
24 M: quotation nth-unsafe quotation-array nth-unsafe ;
26 : >quotation ( seq -- quot )
27 >array array>quotation ; inline
29 M: callable like drop dup quotation? [ >quotation ] unless ;
31 INSTANCE: quotation immutable-sequence
33 : 1quotation ( obj -- quot ) 1array >quotation ;
35 GENERIC: literalize ( obj -- wrapped )
37 M: object literalize ;
39 M: wrapper literalize <wrapper> ;
41 M: curry length curry-quot length 1+ ;
45 nip curry-obj literalize
47 >r 1- r> curry-quot nth
50 INSTANCE: curry immutable-sequence
53 [ compose-first length ]
54 [ compose-second length ] bi + ;
56 M: compose virtual-seq compose-first ;
59 2dup compose-first length < [
62 [ compose-first length - ] [ compose-second ] bi
65 INSTANCE: compose virtual-sequence