2009-01-08 19:56:49 -05:00
|
|
|
! Copyright (C) 2009 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-05-18 18:36:47 -04:00
|
|
|
USING: accessors fry generalizations sequences.generalizations
|
|
|
|
kernel macros math.order stack-checker math sequences ;
|
2009-01-08 19:56:49 -05:00
|
|
|
IN: combinators.smart
|
|
|
|
|
2009-03-28 23:26:49 -04:00
|
|
|
MACRO: drop-outputs ( quot -- quot' )
|
2010-01-14 10:10:13 -05:00
|
|
|
dup outputs '[ @ _ ndrop ] ;
|
2009-03-28 23:26:49 -04:00
|
|
|
|
2009-08-21 22:17:15 -04:00
|
|
|
MACRO: keep-inputs ( quot -- quot' )
|
2010-01-14 10:10:13 -05:00
|
|
|
dup inputs '[ _ _ nkeep ] ;
|
2009-08-21 22:17:15 -04:00
|
|
|
|
2009-01-08 19:56:49 -05:00
|
|
|
MACRO: output>sequence ( quot exemplar -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ dup outputs ] dip
|
2009-01-08 19:56:49 -05:00
|
|
|
'[ @ _ _ nsequence ] ;
|
|
|
|
|
2009-05-12 11:32:19 -04:00
|
|
|
MACRO: output>array ( quot -- newquot )
|
|
|
|
'[ _ { } output>sequence ] ;
|
2009-01-08 19:56:49 -05:00
|
|
|
|
|
|
|
MACRO: input<sequence ( quot -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ inputs ] keep
|
2009-01-08 19:56:49 -05:00
|
|
|
'[ _ firstn @ ] ;
|
|
|
|
|
2009-04-26 22:22:06 -04:00
|
|
|
MACRO: input<sequence-unsafe ( quot -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ inputs ] keep
|
2009-04-26 22:22:06 -04:00
|
|
|
'[ _ firstn-unsafe @ ] ;
|
|
|
|
|
2009-01-09 16:39:47 -05:00
|
|
|
MACRO: reduce-outputs ( quot operation -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ dup outputs 1 [-] ] dip n*quot compose ;
|
2009-01-08 19:56:49 -05:00
|
|
|
|
2009-05-12 11:32:19 -04:00
|
|
|
MACRO: sum-outputs ( quot -- n )
|
|
|
|
'[ _ [ + ] reduce-outputs ] ;
|
2009-01-18 22:18:52 -05:00
|
|
|
|
2009-02-12 03:19:41 -05:00
|
|
|
MACRO: map-reduce-outputs ( quot mapper reducer -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ dup outputs ] 2dip
|
2009-02-12 03:19:41 -05:00
|
|
|
[ swap '[ _ _ napply ] ]
|
|
|
|
[ [ 1 [-] ] dip n*quot ] bi-curry* bi
|
|
|
|
'[ @ @ @ ] ;
|
|
|
|
|
2009-01-18 22:18:52 -05:00
|
|
|
MACRO: append-outputs-as ( quot exemplar -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ dup outputs ] dip '[ @ _ _ nappend-as ] ;
|
2009-01-18 22:18:52 -05:00
|
|
|
|
2009-05-12 11:32:19 -04:00
|
|
|
MACRO: append-outputs ( quot -- seq )
|
|
|
|
'[ _ { } append-outputs-as ] ;
|
2009-08-19 05:38:59 -04:00
|
|
|
|
|
|
|
MACRO: preserving ( quot -- )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ inputs ] keep '[ _ ndup @ ] ;
|
2009-08-19 05:38:59 -04:00
|
|
|
|
2009-11-09 01:53:58 -05:00
|
|
|
MACRO: nullary ( quot -- quot' )
|
2010-01-14 10:10:13 -05:00
|
|
|
dup outputs '[ @ _ ndrop ] ;
|
2009-11-09 01:53:58 -05:00
|
|
|
|
2010-04-29 20:57:07 -04:00
|
|
|
MACRO: dropping ( quot -- quot' )
|
2010-04-29 21:21:53 -04:00
|
|
|
inputs '[ [ _ ndrop ] ] ;
|
2010-04-29 20:57:07 -04:00
|
|
|
|
|
|
|
MACRO: balancing ( quot -- quot' )
|
|
|
|
'[ _ [ preserving ] [ dropping ] bi ] ;
|
|
|
|
|
|
|
|
MACRO: smart-if ( pred true false -- quot )
|
2010-01-27 04:23:04 -05:00
|
|
|
'[ _ preserving _ _ if ] ;
|
2010-01-29 14:47:06 -05:00
|
|
|
|
2010-04-29 20:57:07 -04:00
|
|
|
MACRO: smart-when ( pred true -- quot )
|
|
|
|
'[ _ _ [ ] smart-if ] ;
|
|
|
|
|
|
|
|
MACRO: smart-unless ( pred false -- quot )
|
|
|
|
'[ _ [ ] _ smart-if ] ;
|
|
|
|
|
|
|
|
MACRO: smart-if* ( pred true false -- quot )
|
|
|
|
'[ _ balancing _ swap _ compose if ] ;
|
|
|
|
|
|
|
|
MACRO: smart-when* ( pred true -- quot )
|
|
|
|
'[ _ _ [ ] smart-if* ] ;
|
|
|
|
|
|
|
|
MACRO: smart-unless* ( pred false -- quot )
|
|
|
|
'[ _ [ ] _ smart-if* ] ;
|
|
|
|
|
|
|
|
MACRO: smart-apply ( quot n -- quot )
|
2010-01-31 15:46:46 -05:00
|
|
|
[ dup inputs ] dip '[ _ _ _ mnapply ] ;
|