]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/models/combinators/combinators.factor
tools.test: Make the flag public. Finish porting tester changes to fuzzer.
[factor.git] / unmaintained / models / combinators / combinators.factor
1 USING: accessors arrays kernel models models.product monads
2 sequences sequences.extras shuffle ;
3 FROM: syntax => >> ;
4 IN: models.combinators
5
6 TUPLE: multi-model < model important? ;
7 GENERIC: (model-changed) ( model observer -- )
8 : <multi-model> ( models kind -- model ) f swap new-model [ [ add-dependency ] curry each ] keep ;
9 M: multi-model model-changed over value>> [ (model-changed) ] [ 2drop ] if ;
10 M: multi-model model-activated dup dependencies>> [ value>> ] find nip
11    [ swap model-changed ] [ drop ] if* ;
12
13 : #1 ( model -- model' ) t >>important? ;
14
15 IN: models
16 : notify-connections ( model -- )
17     dup connections>> dup [ dup multi-model? [ important?>> ] [ drop f ] if ] find-all
18     [ second tuck [ remove ] dip prefix ] each
19     [ model-changed ] with each ;
20 IN: models.combinators
21
22 TUPLE: basic-model < multi-model ;
23 M: basic-model (model-changed) [ value>> ] dip set-model ;
24 : merge ( models -- model ) basic-model <multi-model> ;
25 : 2merge ( model1 model2 -- model ) 2array merge ;
26 : <basic> ( value -- model ) basic-model new-model ;
27
28 TUPLE: filter-model < multi-model quot ;
29 M: filter-model (model-changed) [ value>> ] dip 2dup quot>> call( a -- ? )
30    [ set-model ] [ 2drop ] if ;
31 : filter-model ( model quot -- filter-model ) [ 1array \ filter-model <multi-model> ] dip >>quot ;
32
33 TUPLE: fold-model < multi-model quot base values ;
34 M: fold-model (model-changed) 2dup base>> =
35     [ [ [ value>> ] [ [ values>> ] [ quot>> ] bi ] bi* swapd reduce* ] keep set-model ]
36     [ [ [ value>> ] [ values>> ] bi* push ]
37       [ [ [ value>> ] [ [ value>> ] [ quot>> ] bi ] bi* call( val oldval -- newval ) ] keep set-model ] 2bi
38     ] if ;
39 M: fold-model model-activated drop ;
40 : new-fold-model ( deps -- model ) fold-model <multi-model> V{ } clone >>values ;
41 : fold ( model oldval quot -- model ) rot 1array new-fold-model swap >>quot
42    swap >>value ;
43 : fold* ( model oldmodel quot -- model ) over [ [ 2array new-fold-model ] dip >>quot ]
44     dip [ >>base ] [ value>> >>value ] bi ;
45
46 TUPLE: updater-model < multi-model values updates ;
47 M: updater-model (model-changed) [ tuck updates>> =
48    [ [ values>> value>> ] keep set-model ]
49    [ drop ] if ] keep f swap value<< ;
50 : updates ( values updates -- model ) [ 2array updater-model <multi-model> ] 2keep
51    [ >>values ] [ >>updates ] bi* ;
52
53 SYMBOL: switch
54 TUPLE: switch-model < multi-model original switcher on ;
55 M: switch-model (model-changed) 2dup switcher>> =
56    [ [ value>> ] dip over switch = [ nip [ original>> ] keep f >>on model-changed ] [ t >>on set-model ] if ]
57    [ dup on>> [ 2drop ] [ [ value>> ] dip over [ set-model ] [ 2drop ] if ] if ] if ;
58 : switch-models ( model1 model2 -- model' ) swap [ 2array switch-model <multi-model> ] 2keep
59    [ [ value>> >>value ] [ >>original ] bi ] [ >>switcher ] bi* ;
60 M: switch-model model-activated [ original>> ] keep model-changed ;
61 : >behavior ( event -- behavior ) t >>value ;
62
63 TUPLE: mapped-model < multi-model model quot ;
64 : new-mapped-model ( model quot class -- mapped-model ) [ over 1array ] dip
65    <multi-model> swap >>quot swap >>model ;
66 : <mapped> ( model quot -- model ) mapped-model new-mapped-model ;
67 M: mapped-model (model-changed)
68     [ [ value>> ] [ quot>> ] bi* call( old -- new ) ] [ nip ] 2bi
69     set-model ;
70
71 TUPLE: side-effect-model < mapped-model ;
72 M: side-effect-model (model-changed) [ value>> ] dip [ quot>> call( old -- ) ] 2keep set-model ;
73
74 TUPLE: quot-model < mapped-model ;
75 M: quot-model (model-changed) nip [ quot>> call( -- b ) ] keep set-model ;
76
77 TUPLE: action-value < basic-model parent ;
78 : <action-value> ( parent value -- model ) action-value new-model swap >>parent ;
79 M: action-value model-activated dup parent>> dup activate-model model-changed ; ! a fake dependency of sorts
80
81 TUPLE: action < multi-model quot ;
82 M: action (model-changed) [ [ value>> ] [ quot>> ] bi* call( a -- b ) ] keep value>>
83    [ swap add-connection ] 2keep model-changed ;
84 : <action> ( model quot -- action-model ) [ 1array action <multi-model> ] dip >>quot dup f <action-value> >>value value>> ;
85
86 TUPLE: collection < multi-model ;
87 : <collection> ( models -- product ) collection <multi-model> ;
88 M: collection (model-changed)
89     nip
90     dup dependencies>> [ value>> ] all?
91     [ dup [ value>> ] product-value swap set-model ]
92     [ drop ] if ;
93 M: collection model-activated dup (model-changed) ;
94
95 ! for side effects
96 TUPLE: (when-model) < multi-model quot cond ;
97 : when-model ( model quot cond -- model ) rot 1array (when-model) <multi-model> swap >>cond swap >>quot ;
98 M: (when-model) (model-changed) [ quot>> ] 2keep
99     [ value>> ] [ cond>> ] bi* call( a -- ? ) [ call( model -- ) ] [ 2drop ] if ;
100
101 ! only used in construction
102 : with-self ( quot: ( model -- model ) -- model ) [ f <basic> dup ] dip call swap [ add-dependency ] keep ; inline
103
104 USE: models.combinators.templates
105 << { "$>" "<$" "fmap" } [ fmaps ] each >>