switch

Builder for a boolean flag. Note: Because this parser will never fail, it can not be used with combinators such as some or many, as these combinators continue until a failure occurs. See flag'.
switch = flag False True
Swap the order of functors in a sum of functors.
>>> S.toList $ S.print $ separate $ maps S.switch $ maps (S.distinguish (=='a')) $ S.each "banana"
'a'
'a'
'a'
"bnn" :> ()

>>> S.toList $ S.print $ separate $ maps (S.distinguish (=='a')) $ S.each "banana"
'b'
'n'
'n'
"aaa" :> ()
This parser returns True if the given flag is set and False if the flag is absent
Counterpart to either for Either.
Basic switch. By default, the first signal function is applied. Whenever the second value in the pair actually is an event, the value carried by the event is used to obtain a new signal function to be applied *at that time and at future times*. Until that happens, the first value in the pair is produced in the output signal. Important note: at the time of switching, the second signal function is applied immediately. If that second SF can also switch at time zero, then a double (nested) switch might take place. If the second SF refers to the first one, the switch might take place infinitely many times and never be resolved. Remember: The continuation is evaluated strictly at the time of switching!
A Var and a Rec can combine if their rows line up properly. Given a Variant along with a Record of functions from each possible value of the variant to a single output type, apply the correct function to the value in the variant.
Combinator for the <switch> element.
A simple boolean flag Note: this parser never fails.
switch p a b uses the memo table a whenever p gives true and the memo table b whenever p gives false.
This is a template function which makes it possible to branch on a collection of string literals in an efficient way. By using switch, such branching is compiled to a trie of primitive parsing operations, which has optimized control flow, vectorized reads and grouped checking for needed input bytes. The syntax is slightly magical, it overloads the usual case expression. An example:
$(switch [| case _ of
"foo" -> pure True
"bar" -> pure False |])
The underscore is mandatory in case _ of. Each branch must be a string literal, but optionally we may have a default case, like in
$(switch [| case _ of
"foo" -> pure 10
"bar" -> pure 20
_     -> pure 30 |])
All case right hand sides must be parsers with the same type. That type is also the type of the whole switch expression. A switch has longest match semantics, and the order of cases does not matter, except for the default case, which may only appear as the last case. If a switch does not have a default case, and no case matches the input, then it returns with failure, without having consumed any input. A fallthrough to the default case also does not consume any input.
Intrinsic switch: Start with the given wire. As soon as its event occurs, switch to the wire in the event's value.
  • Inhibits: like argument wire until switch, then like the new wire.
  • Switch: once, now, restart state.
A switch/case statement for Which. This is equivalent to flip which Use Case instances like Cases to apply a Which of functions to a variant of values.
let y = pick (5 :: Int) :: Which '[Int, Bool]
switch y (
cases (show @Bool
./ show @Int
./ nil)) `shouldBe` "5"
Or CaseFunc @Typeable to apply a polymorphic function that work on all Typeables.
let y = pick (5 :: Int) :: Which '[Int, Bool]
switch y (CaseFunc @Typeable (show . typeRep . (pure @Proxy))) `shouldBe` Int
Or you may use your own custom instance of Case.
Convert between Left and Right.
Run the first Automaton until the second value in the output tuple is Just c, then start the second automaton, discarding the current output b. This is analogous to Yampa's switch, with Maybe instead of Event.
Run first MSF until the second value in the output tuple is Just c (for some c), then start the second MSF. Analog to Yampa's switch, with Maybe instead of Event.
Try to parse a switch, activate the given value when succesful You'll also need to add at least one long or short. Multiple switchs override eachother.
Higher-order switching. Use an event stream of value streams and produces event values of the latest produced value stream. Switches to a new value stream each time one is produced. The currently used value stream maintains local state until the outer event stream produces a new value stream. In this example we're sequencing the value streams we'd like to use and then switching them when the outer event stream fires.
>>> import Control.Varying.Spline

>>> :{
let v :: VarT IO () (Event Int)
v = switch $ flip outputStream Nothing $ do
step $ Just $ 1 >>> accumulate (+) 0
step Nothing
step Nothing
step $ Just 5
step Nothing
in testVarOver v [(), (), (), (), ()] -- testing over five frames

>>> :}
Just 1
Just 2
Just 3
Just 5
Just 5