factor/core/quotations/quotations.factor

66 lines
1.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays sequences sequences.private
kernel kernel.private math assocs quotations.private
slots.private ;
2007-09-20 18:09:08 -04:00
IN: quotations
M: quotation call (call) ;
2008-03-26 04:57:48 -04:00
M: curry call dup 3 slot swap 4 slot call ;
2008-03-26 04:57:48 -04:00
M: compose call dup 3 slot swap 4 slot slip call ;
2007-09-20 18:09:08 -04:00
M: wrapper equal?
2008-03-29 21:36:58 -04:00
over wrapper? [ [ wrapped ] bi@ = ] [ 2drop f ] if ;
2007-09-20 18:09:08 -04:00
UNION: callable quotation curry compose ;
2007-09-20 18:09:08 -04:00
M: callable equal?
over callable? [ sequence= ] [ 2drop f ] if ;
M: quotation length quotation-array length ;
M: quotation nth-unsafe quotation-array nth-unsafe ;
: >quotation ( seq -- quot )
>array array>quotation ; inline
M: callable like drop dup quotation? [ >quotation ] unless ;
2007-09-20 18:09:08 -04:00
INSTANCE: quotation immutable-sequence
: 1quotation ( obj -- quot ) 1array >quotation ;
GENERIC: literalize ( obj -- wrapped )
M: object literalize ;
M: wrapper literalize <wrapper> ;
M: curry length curry-quot length 1+ ;
M: curry nth
over zero? [
nip curry-obj literalize
] [
>r 1- r> curry-quot nth
] if ;
INSTANCE: curry immutable-sequence
M: compose length
[ compose-first length ]
[ compose-second length ] bi + ;
M: compose virtual-seq compose-first ;
M: compose virtual@
2dup compose-first length < [
compose-first
] [
[ compose-first length - ] [ compose-second ] bi
] if ;
INSTANCE: compose virtual-sequence