2008-02-11 14:50:29 -05:00
|
|
|
! Copyright (C) 2006, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-06-28 03:36:20 -04:00
|
|
|
USING: accessors arrays sequences sequences.private
|
2008-02-11 14:50:29 -05:00
|
|
|
kernel kernel.private math assocs quotations.private
|
|
|
|
slots.private ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: quotations
|
|
|
|
|
2008-07-20 02:15:58 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: uncurry ( curry -- obj quot )
|
2009-10-14 20:24:23 -04:00
|
|
|
{ curry } declare dup 2 slot swap 3 slot ; inline
|
2008-07-20 02:15:58 -04:00
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: uncompose ( compose -- quot quot2 )
|
2009-10-14 20:24:23 -04:00
|
|
|
{ compose } declare dup 2 slot swap 3 slot ; inline
|
2008-07-20 02:15:58 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2008-02-11 14:50:29 -05:00
|
|
|
M: quotation call (call) ;
|
|
|
|
|
2008-07-20 02:15:58 -04:00
|
|
|
M: curry call uncurry call ;
|
2008-02-11 14:50:29 -05:00
|
|
|
|
2009-05-10 17:39:17 -04:00
|
|
|
M: compose call uncompose [ call ] dip call ;
|
2008-02-11 14:50:29 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: wrapper equal?
|
2008-06-28 03:36:20 -04:00
|
|
|
over wrapper? [ [ wrapped>> ] bi@ = ] [ 2drop f ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-11 14:50:29 -05:00
|
|
|
UNION: callable quotation curry compose ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: callable equal?
|
|
|
|
over callable? [ sequence= ] [ 2drop f ] if ;
|
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
M: quotation length array>> length ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
M: quotation nth-unsafe array>> nth-unsafe ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: >quotation ( seq -- quot )
|
|
|
|
>array array>quotation ; inline
|
|
|
|
|
2008-02-11 14:50:29 -05:00
|
|
|
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> ;
|
|
|
|
|
2009-05-01 20:58:24 -04:00
|
|
|
M: curry length quot>> length 1 + ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: curry nth
|
2008-11-23 03:44:56 -05:00
|
|
|
over 0 =
|
|
|
|
[ nip obj>> literalize ]
|
2009-05-01 20:58:24 -04:00
|
|
|
[ [ 1 - ] dip quot>> nth ]
|
2008-11-23 03:44:56 -05:00
|
|
|
if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
INSTANCE: curry immutable-sequence
|
2008-02-11 14:50:29 -05:00
|
|
|
|
|
|
|
M: compose length
|
2008-06-28 03:36:20 -04:00
|
|
|
[ first>> length ] [ second>> length ] bi + ;
|
2008-02-11 14:50:29 -05:00
|
|
|
|
2009-11-06 18:06:26 -05:00
|
|
|
M: compose virtual-exemplar first>> ;
|
2008-06-09 03:14:14 -04:00
|
|
|
|
|
|
|
M: compose virtual@
|
2008-06-28 03:36:20 -04:00
|
|
|
2dup first>> length < [
|
|
|
|
first>>
|
2008-02-11 14:50:29 -05:00
|
|
|
] [
|
2008-06-28 03:36:20 -04:00
|
|
|
[ first>> length - ] [ second>> ] bi
|
2008-06-09 03:14:14 -04:00
|
|
|
] if ;
|
2008-02-11 14:50:29 -05:00
|
|
|
|
2008-06-09 03:14:14 -04:00
|
|
|
INSTANCE: compose virtual-sequence
|