🔍
react with match at the end
Creates a reaction to a set of reagents, which may not pope the contents of the reagents, but only peek.
If the branch of match rеturns a lambda, then the reagents content is popped; otherwise, not.
Syntax: react {[<x>:<r1>〔; .. 〕] <...>; match {<...>} }
; react[{[<x>:<r1>〔; .. 〕] <...>; match {<...>} }]
Whеre:
- x, .. are arguments of a lambda to be evaluated on reaction triggering;
- r1, .. are reagents involved into reaction.
o)r1: reagent[`async];
o)r2: reagent[`async];
o)react {[x:r1] match [x] {1 -> 0N!"peek in r1: one"; _ -> 0N!"peek in r1: not one"} };
o)r1 1
"peek in r1: one"
o)r1 2
o)// There is no reaction because the first value from the reagent is not was popped
o)react {[x:r1; y:r2] 0N!format["sum r1 and r2: %";x+y]};
o)r2 10
"sum r1 and r2: 11"
"peek in r1: not one"
o)r2 10
o)"sum r1 and r2: 12"
o)
You can use many
react
with match
at the end with identical arguments. o)r: reagent[`async];
o)react {[x:r] match [x] {1 -> 0N!"peek: one"; _ -> 0N!"peek: not one"} };
o)react {[x:r] match [x] {1 -> 0N!"peek: 1"; _ -> 0N!"peek: not 1"} };
o)r 1
"peek: one"
"peek: 1"
o)r 42
o)extract: reagent[`async];
o)react {[x:r; y:extract] 0N!format["popped % from r and % from extract";x;y]};
o)extract[]
"popped 1 from r and 0N0 from extract"
"peek: not one"
"peek: not 1"
o)react {[x:r] match [x] {1 -> {0N!"popped: one"}; _ -> {0N!"popped: not one"}} };
o)r 3
o)extract[]
"popped 42 from r and 0N0 from extract"
"peek: not one"
"peek: not 1"
"popped: not one"
o)r 1
"peek: one"
"peek: 1"
"popped: one"
o)
If you use
react
with match
at the end, you must take care of extraction peeked elements.