1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors fry generalizations kernel macros math.order
4 stack-checker math sequences ;
7 MACRO: drop-outputs ( quot -- quot' )
8 dup outputs '[ @ _ ndrop ] ;
10 MACRO: keep-inputs ( quot -- quot' )
11 dup inputs '[ _ _ nkeep ] ;
13 MACRO: output>sequence ( quot exemplar -- newquot )
15 '[ @ _ _ nsequence ] ;
17 MACRO: output>array ( quot -- newquot )
18 '[ _ { } output>sequence ] ;
20 MACRO: input<sequence ( quot -- newquot )
24 MACRO: input<sequence-unsafe ( quot -- newquot )
26 '[ _ firstn-unsafe @ ] ;
28 MACRO: reduce-outputs ( quot operation -- newquot )
29 [ dup outputs 1 [-] ] dip n*quot compose ;
31 MACRO: sum-outputs ( quot -- n )
32 '[ _ [ + ] reduce-outputs ] ;
34 MACRO: map-reduce-outputs ( quot mapper reducer -- newquot )
36 [ swap '[ _ _ napply ] ]
37 [ [ 1 [-] ] dip n*quot ] bi-curry* bi
40 MACRO: append-outputs-as ( quot exemplar -- newquot )
41 [ dup outputs ] dip '[ @ _ _ nappend-as ] ;
43 MACRO: append-outputs ( quot -- seq )
44 '[ _ { } append-outputs-as ] ;
46 MACRO: preserving ( quot -- )
47 [ inputs ] keep '[ _ ndup @ ] ;
49 MACRO: nullary ( quot -- quot' )
50 dup outputs '[ @ _ ndrop ] ;
52 MACRO: smart-if ( pred true false -- )
53 '[ _ preserving _ _ if ] ; inline