From c6afd90485fe19e6b5b3f78df173aae819e2624b Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 16 Aug 2024 09:01:34 -0700 Subject: More convenient switch and multiSwitch. --- Spear/Step.hs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Spear/Step.hs b/Spear/Step.hs index e767166..a860247 100644 --- a/Spear/Step.hs +++ b/Spear/Step.hs @@ -112,9 +112,11 @@ swhen expectedEvent step = Step $ \elapsed dt state maybeEvent a -> in (a', swhen expectedEvent step') else (a, swhen expectedEvent step) --- | Construct a step that switches between two steps based on input. +-- | Construct a step that switches between two steps based on input events. -- --- The initial step is the first one. +-- The current step runs with every 'runStep' even when there are no new events. +-- +-- The initial step is the identity, 'sid'. switch :: Eq e => e -> @@ -122,16 +124,8 @@ switch :: e -> Step s (Maybe e) a a -> Step s (Maybe e) a a -switch flag1 s1 flag2 s2 = switch' s1 flag1 s1 flag2 s2 +switch = switch' sid -switch' :: - Eq e => - Step s (Maybe e) a a -> - e -> - Step s (Maybe e) a a -> - e -> - Step s (Maybe e) a a -> - Step s (Maybe e) a a switch' cur flag1 s1 flag2 s2 = Step $ \elapsed dt g e a -> case e of Nothing -> @@ -141,11 +135,15 @@ switch' cur flag1 s1 flag2 s2 = Step $ \elapsed dt g e a -> let next | e' == flag1 = s1 | e' == flag2 = s2 - | otherwise = cur + | otherwise = cur (a', s') = runStep next elapsed dt g e a in (a', switch' s' flag1 s1 flag2 s2) --- | Construct a step that switches among multiple steps based on input. +-- | Construct a step that switches among multiple steps based on input events. +-- +-- The current step runs with every 'runStep' even when there are no new events. +-- +-- The initial step is the identity, 'sid'. multiSwitch :: (Eq e, Ord e) => [(e, Step s (Maybe e) a a)] -> Step s (Maybe e) a a multiSwitch xs = multiSwitch' Nothing sid (Map.fromList xs) -- cgit v1.2.3