papa-0.7.0: src/Papa/Base/Data/Function.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Functions, with 'flip' generalised to any 'Functor', and '(&&&)' of
-- "Control.Arrow" (both functions applied to the same argument).
module Papa.Base.Data.Function (
module P,
(&&&),
flip,
) where
import Control.Arrow ((&&&))
import Data.Function as P (
fix,
on,
($),
(&),
)
import Data.Functor (Functor, (<&>))
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linear (-100) 100)
-- | Apply every function in a 'Functor' to the same argument. At the
-- function 'Functor' (@f = (->) c@) this is "Prelude"'s @flip@.
--
-- ==== __Examples__
--
-- >>> flip (-) 1 (10 :: Int)
-- 9
--
-- >>> flip [(+ 1), (* 2)] (10 :: Int)
-- [11,20]
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn (fn genInt)); a <- forAll genInt; b <- forAll genInt; flip (\x y -> apply (apply f x) y) a b === apply (apply f b) a
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
flip :: (Functor f) => f (a -> b) -> a -> f b
flip f a =
f <&> (\k -> k a)
{-# INLINE flip #-}