]> gitweb.factorcode.org Git - factor.git/blob - core/combinators/short-circuit/short-circuit.factor
factor: trim using lists
[factor.git] / core / combinators / short-circuit / short-circuit.factor
1 USING: arrays combinators generalizations kernel macros
2 math sequences ;
3 IN: combinators.short-circuit
4
5 <PRIVATE
6
7 MACRO: keeping ( n quot -- quot' )
8     swap dup 1 + '[ _ _ nkeep _ nrot ] ;
9
10 PRIVATE>
11
12 MACRO: n&& ( quots n -- quot )
13     [
14         [ [ f ] ] 2dip swap [
15             [ '[ drop _ _ keeping dup not ] ]
16             [ drop '[ drop _ ndrop f ] ]
17             2bi 2array
18         ] with map
19     ] [ '[ _ nnip ] suffix 1array ] bi
20     [ cond ] 3append ;
21
22 : 0&& ( quots -- ? ) 0 n&& ;
23 : 1&& ( obj quots -- ? ) 1 n&& ;
24 : 2&& ( obj1 obj2 quots -- ? ) 2 n&& ;
25 : 3&& ( obj1 obj2 obj3 quots -- ? ) 3 n&& ;
26
27 MACRO: n|| ( quots n -- quot )
28     [
29         [ [ f ] ] 2dip swap [
30             [ '[ drop _ _ keeping dup ] ]
31             [ drop '[ _ nnip ] ]
32             2bi 2array
33         ] with map
34     ] [ '[ drop _ ndrop t ] [ f ] 2array suffix 1array ] bi
35     [ cond ] 3append ;
36
37 : 0|| ( quots -- ? ) 0 n|| ;
38 : 1|| ( obj quots -- ? ) 1 n|| ;
39 : 2|| ( obj1 obj2 quots -- ? ) 2 n|| ;
40 : 3|| ( obj1 obj2 obj3 quots -- ? ) 3 n|| ;