]> gitweb.factorcode.org Git - factor.git/blob - extra/models/combinators/combinators.factor
3ff4d98fee7d279195c7989981acd01ac73f9d64
[factor.git] / extra / 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 <PRIVATE
34 ! Quot must have static stack effect, unlike "reduce"
35 :: reduce* ( seq identity quot: ( prev elt -- next ) -- result )
36     seq [ identity ] [
37         unclip identity swap quot call( prev elt -- next )
38         quot reduce*
39     ] if-empty ; inline recursive
40 PRIVATE>
41
42 TUPLE: fold-model < multi-model quot base values ;
43 M: fold-model (model-changed) 2dup base>> =
44     [ [ [ value>> ] [ [ values>> ] [ quot>> ] bi ] bi* swapd reduce* ] keep set-model ]
45     [ [ [ value>> ] [ values>> ] bi* push ]
46       [ [ [ value>> ] [ [ value>> ] [ quot>> ] bi ] bi* call( val oldval -- newval ) ] keep set-model ] 2bi
47     ] if ;
48 M: fold-model model-activated drop ;
49 : new-fold-model ( deps -- model ) fold-model <multi-model> V{ } clone >>values ;
50 : fold ( model oldval quot -- model ) rot 1array new-fold-model swap >>quot
51    swap >>value ;
52 : fold* ( model oldmodel quot -- model ) over [ [ 2array new-fold-model ] dip >>quot ]
53     dip [ >>base ] [ value>> >>value ] bi ;
54
55 TUPLE: updater-model < multi-model values updates ;
56 M: updater-model (model-changed) [ tuck updates>> =
57    [ [ values>> value>> ] keep set-model ]
58    [ drop ] if ] keep f swap value<< ;
59 : updates ( values updates -- model ) [ 2array updater-model <multi-model> ] 2keep
60    [ >>values ] [ >>updates ] bi* ;
61
62 SYMBOL: switch
63 TUPLE: switch-model < multi-model original switcher on ;
64 M: switch-model (model-changed) 2dup switcher>> =
65    [ [ value>> ] dip over switch = [ nip [ original>> ] keep f >>on model-changed ] [ t >>on set-model ] if ]
66    [ dup on>> [ 2drop ] [ [ value>> ] dip over [ set-model ] [ 2drop ] if ] if ] if ;
67 : switch-models ( model1 model2 -- model' ) swap [ 2array switch-model <multi-model> ] 2keep
68    [ [ value>> >>value ] [ >>original ] bi ] [ >>switcher ] bi* ;
69 M: switch-model model-activated [ original>> ] keep model-changed ;
70 : >behavior ( event -- behavior ) t >>value ;
71
72 TUPLE: mapped-model < multi-model model quot ;
73 : new-mapped-model ( model quot class -- mapped-model ) [ over 1array ] dip
74    <multi-model> swap >>quot swap >>model ;
75 : <mapped> ( model quot -- model ) mapped-model new-mapped-model ;
76 M: mapped-model (model-changed)
77     [ [ value>> ] [ quot>> ] bi* call( old -- new ) ] [ nip ] 2bi
78     set-model ;
79
80 TUPLE: side-effect-model < mapped-model ;
81 M: side-effect-model (model-changed) [ value>> ] dip [ quot>> call( old -- ) ] 2keep set-model ;
82
83 TUPLE: quot-model < mapped-model ;
84 M: quot-model (model-changed) nip [ quot>> call( -- b ) ] keep set-model ;
85
86 TUPLE: action-value < basic-model parent ;
87 : <action-value> ( parent value -- model ) action-value new-model swap >>parent ;
88 M: action-value model-activated dup parent>> dup activate-model model-changed ; ! a fake dependency of sorts
89
90 TUPLE: action < multi-model quot ;
91 M: action (model-changed) [ [ value>> ] [ quot>> ] bi* call( a -- b ) ] keep value>>
92    [ swap add-connection ] 2keep model-changed ;
93 : <action> ( model quot -- action-model ) [ 1array action <multi-model> ] dip >>quot dup f <action-value> >>value value>> ;
94
95 TUPLE: collection < multi-model ;
96 : <collection> ( models -- product ) collection <multi-model> ;
97 M: collection (model-changed)
98     nip
99     dup dependencies>> [ value>> ] all?
100     [ dup [ value>> ] product-value swap set-model ]
101     [ drop ] if ;
102 M: collection model-activated dup (model-changed) ;
103
104 ! for side effects
105 TUPLE: (when-model) < multi-model quot cond ;
106 : when-model ( model quot cond -- model ) rot 1array (when-model) <multi-model> swap >>cond swap >>quot ;
107 M: (when-model) (model-changed) [ quot>> ] 2keep
108     [ value>> ] [ cond>> ] bi* call( a -- ? ) [ call( model -- ) ] [ 2drop ] if ;
109
110 ! only used in construction
111 : with-self ( quot: ( model -- model ) -- model ) [ f <basic> dup ] dip call swap [ add-dependency ] keep ; inline
112
113 USE: models.combinators.templates
114 << { "$>" "<$" "fmap" } [ fmaps ] each >>