-
-
Save obadz/9f322df8ba6c8a9767683d2f86af8589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS_GHC -Wall #-} | |
module Control.DirectionalOps where | |
import qualified Control.Monad as Monad | |
import Data.Maybe | |
infixl 1 |>> | |
infixr 1 <<| | |
infixl 2 >>> | |
infixr 2 <<< | |
infixl 1 |$> | |
infixr 1 <$| | |
infixl 2 >$> | |
infixr 2 <$< | |
infixl 1 |*> | |
infixr 1 <*| | |
infixl 2 >*> | |
infixr 2 <*< | |
{-# INLINE (|>>) #-} | |
(|>>) :: a -> (a -> b) -> b | |
(|>>) = flip ($) | |
{-# INLINE (>>>) #-} | |
(>>>) :: (a -> b) -> (b -> c) -> a -> c | |
(>>>) = flip (.) | |
{-# INLINE (|$>) #-} | |
(|$>) :: Functor f => f a -> (a -> b) -> f b | |
(|$>) = flip (<$>) | |
{-# INLINE (>$>) #-} | |
(>$>) :: Functor f => (a -> f b) -> (b -> c) -> a -> f c | |
(>$>) = \ f g x -> x |>> f |>> fmap g | |
{-# INLINE (|*>) #-} | |
(|*>) :: Applicative f => f a -> f (a -> b) -> f b | |
(|*>) = flip (<*>) | |
{-# INLINE (>*>) #-} | |
(>*>) :: Applicative f => (a -> f b) -> f (b -> c) -> a -> f c | |
(>*>) = \ f g x -> x |>> f |*> g | |
{-# INLINE (<<|) #-} | |
(<<|) :: (a -> b) -> a -> b | |
(<<|) = ($) | |
{-# INLINE (<<<) #-} | |
(<<<) :: (b -> c) -> (a -> b) -> a -> c | |
(<<<) = (.) | |
{-# INLINE (<$|) #-} | |
(<$|) :: Functor f => (a -> b) -> f a -> f b | |
(<$|) = (<$>) | |
{-# INLINE (<$<) #-} | |
(<$<) :: Functor f => (b -> c) -> (a -> f b) -> a -> f c | |
(<$<) = flip (>$>) | |
{-# INLINE (<*|) #-} | |
(<*|) :: Applicative f => f (a -> b) -> f a -> f b | |
(<*|) = (<*>) | |
{-# INLINE (<*<) #-} | |
(<*<) :: Applicative f => f (b -> c) -> (a -> f b) -> a -> f c | |
(<*<) = flip (>*>) | |
-- Just redefining the fixity of this fucker because somehow it's infixr somehow | |
infixl 2 >=> | |
{-# INLINE (>=>) #-} | |
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c | |
(>=>) = (Monad.>=>) | |
example :: Either String Int | |
example = | |
"abc" | |
|>> repeat | |
|>> zipWith (,) [1..] | |
|>> filter (fst >>> even) | |
|>> take 3 | |
|>> listToMaybe | |
|$> fst | |
|$> (+ 5) | |
|*> Just (+ 18) | |
>>= (\ x -> if x > 3 then Just x else Nothing) | |
|>> maybe (Left "Something went wrong") Right | |
|$> (* 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment