factor/core/quotations/quotations.factor

74 lines
1.5 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: accessors arrays sequences sequences.private
kernel kernel.private math assocs quotations.private
slots.private ;
2007-09-20 18:09:08 -04:00
IN: quotations
<PRIVATE
: uncurry ( curry -- obj quot )
{ curry } declare dup 2 slot swap 3 slot ; inline
: uncompose ( compose -- quot quot2 )
{ compose } declare dup 2 slot swap 3 slot ; inline
PRIVATE>
M: quotation call (call) ;
M: curry call uncurry call ;
2009-05-10 17:39:17 -04:00
M: compose call uncompose [ call ] dip call ;
2007-09-20 18:09:08 -04:00
M: wrapper equal?
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 array>> length ;
2007-09-20 18:09:08 -04:00
M: quotation nth-unsafe array>> nth-unsafe ;
2007-09-20 18:09:08 -04:00
: >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 quot>> length 1 + ;
2007-09-20 18:09:08 -04:00
M: curry nth
over 0 =
[ nip obj>> literalize ]
[ [ 1 - ] dip quot>> nth ]
if ;
2007-09-20 18:09:08 -04:00
INSTANCE: curry immutable-sequence
M: compose length
[ first>> length ] [ second>> length ] bi + ;
M: compose virtual-exemplar first>> ;
M: compose virtual@
2dup first>> length < [
first>>
] [
[ first>> length - ] [ second>> ] bi
] if ;
INSTANCE: compose virtual-sequence