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