]> gitweb.factorcode.org Git - factor.git/blob - core/quotations/quotations.factor
7e7a77d4e1f1abe7873b24a0626869cc3625ee39
[factor.git] / core / quotations / quotations.factor
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 ;
5 IN: quotations
6
7 <PRIVATE
8
9 : uncurry ( curry -- obj quot )
10     { curry } declare dup 2 slot swap 3 slot ; inline
11
12 : uncompose ( compose -- quot quot2 )
13     { compose } declare dup 2 slot swap 3 slot ; inline
14
15 PRIVATE>
16
17 M: quotation call (call) ;
18
19 M: curry call uncurry call ;
20
21 M: compose call uncompose [ call ] dip call ;
22
23 M: wrapper equal?
24     over wrapper? [ [ wrapped>> ] same? ] [ 2drop f ] if ;
25
26 UNION: callable quotation curry compose ;
27
28 M: callable equal?
29     over callable? [ sequence= ] [ 2drop f ] if ;
30
31 M: quotation length array>> length ;
32
33 M: quotation nth-unsafe array>> nth-unsafe ;
34
35 : >quotation ( seq -- quot )
36     >array array>quotation ; inline
37
38 M: callable like drop dup quotation? [ >quotation ] unless ;
39
40 INSTANCE: quotation immutable-sequence
41
42 : 1quotation ( obj -- quot ) 1array >quotation ;
43
44 GENERIC: literalize ( obj -- wrapped )
45
46 M: object literalize ;
47
48 M: wrapper literalize <wrapper> ;
49
50 M: curry length quot>> length 1 + ;
51
52 M: curry nth
53     over 0 =
54     [ nip obj>> literalize ]
55     [ [ 1 - ] dip quot>> nth ]
56     if ;
57
58 INSTANCE: curry immutable-sequence
59
60 M: compose length
61     [ first>> length ] [ second>> length ] bi + ;
62
63 M: compose virtual-exemplar first>> ;
64
65 M: compose virtual@
66     2dup first>> length < [
67         first>>
68     ] [
69         [ first>> length - ] [ second>> ] bi
70     ] if ;
71
72 INSTANCE: compose virtual-sequence