on develop help sign in
Reduce duplication with `alignWithMaybe`
commit59ca0334b13aff3cf3282e29f6049b7d59fd85a5 authorLennart Spitzner <lsp@informatik.uni-kiel.de> dateMon 17 Oct 2016, 00:06 (10 years ago) parent1f8e1cb
1 file changed+9−8
src/Reflex/Class.hs+9−8
src/Reflex/Class.hs+9−8
@@ -641,7 +641,7 @@ switchPromptly ea0 eea = do
641641
642642instance Reflex t => Align (Event t) where
643643 nil = never
644− align ea eb = fmapMaybe dmapToThese $ merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
644+ align = alignWithMaybe Just
645645
646646-- | Create a new 'Event' that only occurs if the supplied 'Event' occurs and
647647-- the 'Behavior' is true at the time of occurence.
@@ -708,14 +708,15 @@ distributeListOverDynWith f = fmap (f . map (\(Const2 _ :=> Identity v) -> v) .
708708-- | Create a new 'Event' that occurs when the first supplied 'Event' occurs
709709-- unless the second supplied 'Event' occurs simultaneously.
710710unlessE :: Reflex t => Event t a -> Event t b -> Event t a
711−unlessE ea eb =
712− fmapMaybe dmapper
711+unlessE = alignWithMaybe $ \case { This a -> Just a ; _ -> Nothing }
712+
713+-- (intentially not exported, for now)
714+alignWithMaybe
715+ :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
716+alignWithMaybe f ea eb =
717+ fmapMaybe (f <=< dmapToThese)
713718 $ merge
714− $ DMap.fromList [LeftTag DMap.:=> ea, RightTag DMap.:=> eb]
715− where
716− dmapper m = case (DMap.lookup LeftTag m, DMap.lookup RightTag m) of
717− (Just (Identity a), Nothing) -> Just a
718− _ -> Nothing
719+ $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
719720
720721
721722--------------------------------------------------------------------------------