Skip to content

Instantly share code, notes, and snippets.

@rehno-lindeque
Created March 21, 2014 11:46
Show Gist options
  • Save rehno-lindeque/9684480 to your computer and use it in GitHub Desktop.
Save rehno-lindeque/9684480 to your computer and use it in GitHub Desktop.
Elm Functor/Applicative operator shorthands
-- Shorthand map over List-like
f <$ lx = map f lx -- <$> in Haskell
lx $> f = map f lx
infixr 2 <$
infixr 2 $>
-- Shorthand sequential application
lf <$$ lx = zipWith (\f x -> f x) lf lx -- <*> in Haskell
lx $$> lf = zipWith (\f x -> f x) lf lx -- <**> in Haskell
infixr 1 <$$
infixr 1 $$>
--Shorthand map over Maybe
f <? mx = case mx of -- <$> in Haskell
(Just x) -> Just (f x)
Nothing -> Nothing
mx ?> f = case mx of
Just x -> Just (f x)
Nothing -> Nothing
infixr 2 <?
infixr 2 ?>
-- Shorthand sequential application
mf <?? mx = if isJust mx && isJust mf -- <*> in Haskell
then Just <| (\(Just f) (Just x) -> f x) mf mx
else Nothing
mx ??> mf = if isJust mx && isJust mf -- <**> in Haskell
then Just <| (\(Just f) (Just x) -> f x) mf mx
else Nothing
infixr 1 <??
infixr 1 ??>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment