67 lines
1.4 KiB
Haskell
67 lines
1.4 KiB
Haskell
module Toolbox
|
|
( -- Applicative
|
|
pure,
|
|
|
|
-- Monad
|
|
(>>=), (>=>),
|
|
|
|
-- local
|
|
(<>),
|
|
(<!>),(|?),
|
|
clo, rclo, mclo, mrclo,
|
|
just
|
|
)
|
|
where
|
|
|
|
|
|
import Control.Monad ((>>=),(>=>),MonadPlus(mplus,mzero),mfilter)
|
|
import Control.Applicative (pure)
|
|
|
|
import Data.Foldable
|
|
|
|
-- OR
|
|
|
|
infixr 6 <>
|
|
(<>) :: (MonadPlus m) => (a -> m b) -> (a -> m b) -> (a -> m b)
|
|
(f <> g) x = f x `mplus` g x
|
|
|
|
-- SHORT CIRCUIT OR
|
|
|
|
infixr 6 <!>
|
|
(<!>) :: (MonadPlus m, Foldable m) => (a -> m b) -> (a -> m b) -> (a -> m b)
|
|
(f <!> g) x = let fx = f x in if null fx then g x else fx
|
|
|
|
-- VALUE FILTER
|
|
|
|
infixl 4 |?
|
|
(|?) :: (MonadPlus m) => (a -> m b) -> (b -> Bool) -> (a -> m b)
|
|
f |? g = mfilter g . f
|
|
|
|
|
|
|
|
clo,mclo,rclo,mrclo:: (MonadPlus m, Foldable m) => (a -> m a) -> a -> m a
|
|
clo f = f >=> ( pure <> clo f )
|
|
rclo f = pure <> clo f
|
|
mrclo f = (f >=> mrclo f) <!> pure
|
|
mclo f = f >=> mrclo f
|
|
|
|
|
|
-- INPUT: Function f returning element of a list or fails if list empty
|
|
-- OUTPUT: Function returning the list containing the f value or empty list
|
|
-- ex: the maximum, the head, the last, e.t.c
|
|
|
|
-- the :: ([a] -> b) -> ([a] -> [b])
|
|
-- (the f) [] = []
|
|
-- (the f) xs = [f xs]
|
|
|
|
just :: ([a] -> b) -> ([a] -> [b])
|
|
(just f) [] = []
|
|
(just f) xs = [f xs]
|
|
|
|
its :: (a->b) -> ([a]->b)
|
|
its f = f.head
|
|
|
|
their :: (a->b) -> ([a]->[b])
|
|
their = map
|
|
|