1module Control.Functor.HT where 2 3import qualified Data.Tuple.HT as Tuple 4import Data.Tuple.HT (fst3, snd3, thd3) 5 6import qualified Prelude as P 7import Prelude (Functor, fmap, flip, const, (.), ($), fst, snd) 8 9 10void :: Functor f => f a -> f () 11void = fmap (const ()) 12 13map :: Functor f => (a -> b) -> f a -> f b 14map = fmap 15 16for :: Functor f => f a -> (a -> b) -> f b 17for = flip fmap 18 19 20{- | 21Caution: 22Every pair member has a reference to the argument of 'unzip'. 23Depending on the consumption pattern this may cause a memory leak. 24For lists, I think, you should generally prefer 'List.unzip'. 25-} 26unzip :: Functor f => f (a, b) -> (f a, f b) 27unzip x = (fmap fst x, fmap snd x) 28 29{- | 30Caution: See 'unzip'. 31-} 32unzip3 :: Functor f => f (a, b, c) -> (f a, f b, f c) 33unzip3 x = (fmap fst3 x, fmap snd3 x, fmap thd3 x) 34 35 36{- | 37Caution: See 'unzip'. 38-} 39uncurry :: Functor f => (f a -> f b -> g) -> f (a, b) -> g 40uncurry f = P.uncurry f . unzip 41 42{- | 43Caution: See 'unzip'. 44-} 45uncurry3 :: Functor f => (f a -> f b -> f c -> g) -> f (a, b, c) -> g 46uncurry3 f = Tuple.uncurry3 f . unzip3 47 48 49mapFst :: Functor f => (a -> f c) -> (a, b) -> f (c, b) 50mapFst f ~(a,b) = fmap (flip (,) b) $ f a 51 52mapSnd :: Functor f => (b -> f c) -> (a, b) -> f (a, c) 53mapSnd f ~(a,b) = fmap ((,) a) $ f b 54 55 56mapFst3 :: Functor f => (a -> f d) -> (a,b,c) -> f (d,b,c) 57mapFst3 f ~(a,b,c) = fmap (\x -> (x,b,c)) $ f a 58 59mapSnd3 :: Functor f => (b -> f d) -> (a,b,c) -> f (a,d,c) 60mapSnd3 f ~(a,b,c) = fmap (\x -> (a,x,c)) $ f b 61 62mapThd3 :: Functor f => (c -> f d) -> (a,b,c) -> f (a,b,d) 63mapThd3 f ~(a,b,c) = fmap ((,,) a b) $ f c 64 65 66{- | 67Generalization of 'Data.List.HT.outerProduct'. 68-} 69outerProduct :: 70 (Functor f, Functor g) => 71 (a -> b -> c) -> f a -> g b -> f (g c) 72outerProduct f xs ys = fmap (flip fmap ys . f) xs 73