monadic binding for models (frp)
parent
40fc26556d
commit
d439c61ed5
|
@ -43,26 +43,22 @@ M: switch-model (model-changed) 2dup switcher>> =
|
||||||
[ >>original ] [ >>switcher ] bi* ;
|
[ >>original ] [ >>switcher ] bi* ;
|
||||||
M: switch-model model-activated [ original>> ] keep model-changed ;
|
M: switch-model model-activated [ original>> ] keep model-changed ;
|
||||||
|
|
||||||
|
|
||||||
TUPLE: mapped-model < multi-model model quot ;
|
TUPLE: mapped-model < multi-model model quot ;
|
||||||
|
: new-mapped-model ( model quot class -- const-model ) [ over 1array ] dip
|
||||||
: <mapped> ( model quot -- mapped )
|
<multi-model> swap >>quot swap >>model ;
|
||||||
f mapped-model new-model
|
: <mapped> ( model quot -- mapped ) mapped-model new-mapped-model ;
|
||||||
swap >>quot
|
|
||||||
over >>model
|
|
||||||
[ add-dependency ] keep ;
|
|
||||||
M: mapped-model (model-changed)
|
M: mapped-model (model-changed)
|
||||||
[ [ value>> ] [ quot>> ] bi* call( old -- new ) ] [ nip ] 2bi
|
[ [ value>> ] [ quot>> ] bi* call( old -- new ) ] [ nip ] 2bi
|
||||||
set-model ;
|
set-model ;
|
||||||
M: mapped-model model-activated [ model>> ] keep model-changed ;
|
M: mapped-model model-activated [ model>> ] keep model-changed ;
|
||||||
|
|
||||||
TUPLE: side-effect-model < mapped-model ;
|
TUPLE: side-effect-model < mapped-model ;
|
||||||
M: side-effect-model (model-changed) [ value>> ] [ quot>> ] bi* call( old -- ) ;
|
M: side-effect-model (model-changed) [ [ value>> ] [ quot>> ] bi* call( old -- ) ] keep t swap set-model ;
|
||||||
: <$ ( model quot -- side-effect-model )
|
: $> ( model quot -- side-effect-model ) side-effect-model new-mapped-model ;
|
||||||
f side-effect-model new-model
|
|
||||||
swap >>quot
|
TUPLE: quot-model < mapped-model ;
|
||||||
over >>model
|
M: quot-model (model-changed) nip [ quot>> call( -- b ) ] keep set-model ;
|
||||||
[ add-dependency ] keep ;
|
: <$ ( model quot -- quot-model ) quot-model new-mapped-model ;
|
||||||
|
|
||||||
TUPLE: frp-product < multi-model ;
|
TUPLE: frp-product < multi-model ;
|
||||||
: <frp-product> ( models -- product ) frp-product <multi-model> ;
|
: <frp-product> ( models -- product ) frp-product <multi-model> ;
|
||||||
|
@ -75,6 +71,15 @@ M: frp-product update-model
|
||||||
dup value>> swap [ set-model ] set-product-value ;
|
dup value>> swap [ set-model ] set-product-value ;
|
||||||
M: frp-product model-activated dup model-changed ;
|
M: frp-product model-activated dup model-changed ;
|
||||||
|
|
||||||
|
TUPLE: action-value < basic-model parent ;
|
||||||
|
: <action-value> ( parent value -- model ) action-value new-model swap >>parent ;
|
||||||
|
M: action-value model-activated parent>> activate-model ; ! a fake dependency of sorts
|
||||||
|
|
||||||
|
TUPLE: action < multi-model quot ;
|
||||||
|
M: action (model-changed) [ [ value>> ] [ quot>> ] bi* call( a -- b ) ] keep value>>
|
||||||
|
swap add-connection ;
|
||||||
|
: <action> ( model quot -- action ) [ 1array action <multi-model> ] dip >>quot dup f <action-value> >>value value>> ;
|
||||||
|
|
||||||
! Gadgets
|
! Gadgets
|
||||||
TUPLE: frp-button < button hook ;
|
TUPLE: frp-button < button hook ;
|
||||||
: <frp-button> ( text -- button ) [
|
: <frp-button> ( text -- button ) [
|
||||||
|
@ -131,6 +136,7 @@ M: model -> dup , ;
|
||||||
|
|
||||||
! Instances
|
! Instances
|
||||||
M: model fmap <mapped> ;
|
M: model fmap <mapped> ;
|
||||||
|
M: model >>= [ swap <action> ] curry ;
|
||||||
|
|
||||||
SINGLETON: gadget-monad
|
SINGLETON: gadget-monad
|
||||||
INSTANCE: gadget-monad monad
|
INSTANCE: gadget-monad monad
|
||||||
|
@ -140,15 +146,20 @@ M: gadget-monad return drop <gadget> swap >>model ;
|
||||||
M: gadget >>= output-model [ swap call( x -- y ) ] curry ;
|
M: gadget >>= output-model [ swap call( x -- y ) ] curry ;
|
||||||
|
|
||||||
! Macros
|
! Macros
|
||||||
|
: lift ( int -- quot ) dup
|
||||||
|
'[ [ _ narray <frp-product> ] dip [ _ firstn ] prepend ] ; inline
|
||||||
|
|
||||||
MACRO: liftA-n ( int -- quot ) dup
|
MACRO: liftA-n ( int -- quot ) lift [ <mapped> ] append ;
|
||||||
'[ [ _ narray <frp-product> ] dip [ _ firstn ] prepend <mapped> ] ;
|
|
||||||
|
|
||||||
MACRO: <$-n ( int -- quot ) dup
|
MACRO: $>-n ( int -- quot ) lift [ $> ] append ;
|
||||||
'[ [ _ narray <frp-product> ] dip [ _ firstn ] prepend <$ ] ;
|
|
||||||
|
MACRO: <$-n ( int -- quot ) lift [ <$ ] append ;
|
||||||
|
|
||||||
: liftA2 ( a b quot -- arrow ) 2 liftA-n ; inline
|
: liftA2 ( a b quot -- arrow ) 2 liftA-n ; inline
|
||||||
: liftA3 ( a b c quot -- arrow ) 3 liftA-n ; inline
|
: liftA3 ( a b c quot -- arrow ) 3 liftA-n ; inline
|
||||||
|
|
||||||
|
: $>2 ( a b quot -- arrow ) 2 $>-n ; inline
|
||||||
|
: $>3 ( a b c quot -- arrow ) 3 $>-n ; inline
|
||||||
|
|
||||||
: <$2 ( a b quot -- arrow ) 2 <$-n ; inline
|
: <$2 ( a b quot -- arrow ) 2 <$-n ; inline
|
||||||
: <$3 ( a b c quot -- arrow ) 3 <$-n ; inline
|
: <$3 ( a b c quot -- arrow ) 3 <$-n ; inline
|
|
@ -12,7 +12,7 @@ IN: ui.gadgets.alerts
|
||||||
fldm [ <frp-field> ->% 1 ]
|
fldm [ <frp-field> ->% 1 ]
|
||||||
btn [ "okay" <frp-button> model >>model ] |
|
btn [ "okay" <frp-button> model >>model ] |
|
||||||
btn -> [ fldm swap <updates> ]
|
btn -> [ fldm swap <updates> ]
|
||||||
[ [ drop lbl close-window ] <$ , ] bi
|
[ [ drop lbl close-window ] $> , ] bi
|
||||||
] ] <vbox> { 161 86 } >>pref-dim "" open-window ;
|
] ] <vbox> { 161 86 } >>pref-dim "" open-window ;
|
||||||
|
|
||||||
: ask-user ( string -- model ) f <model> swap ask-user* ;
|
: ask-user ( string -- model ) f <model> swap ask-user* ;
|
Loading…
Reference in New Issue