Skip to content

Instantly share code, notes, and snippets.

@Forkk
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Forkk/fd2517217d6c962ec7ad to your computer and use it in GitHub Desktop.
Save Forkk/fd2517217d6c962ec7ad to your computer and use it in GitHub Desktop.
-- | Sample an auto's output when a blip stream outputs something.
--
-- Takes two autos, one which outputs a some value, and another which outputs a
-- blip stream. Produces an auto which outputs a blip with the contents of the
-- first auto whenever the blip stream outputs.
sample :: forall m a b c. (Monad m) =>
Auto m a c
-> Auto m a (Blip b)
-> Auto m a (Blip (b, c))
sample valueAuto blipAuto = onJusts <<< arr extractMaybe <<< ((asMaybes <<< blipAuto) &&& valueAuto)
where
extractMaybe :: (Maybe b, c) -> Maybe (b, c)
extractMaybe (Just b, c) = Just (b, c)
extractMaybe _ = Nothing
@mstksg
Copy link

mstksg commented Jun 10, 2015

sample = liftA2 $ \x -> fmap (,x)

Functor instance for Blip is usually handy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment