Skip to content

Commit a385de9

Browse files
joneshfpaf31
authored andcommitted
Add generalized flip (#83)
* Add generalized `flip` The name of this function along with its operator alias can change. I don't know a better name for either and have no strong feelings. * Rename `flip'` to `flap` and `??` to `<@>` * Increase precedence and rexport from prelude The new precedence lets the operator play nicely with others.
1 parent 69bc58a commit a385de9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Data/Functor.purs

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Data.Functor
44
, void
55
, voidRight, (<$)
66
, voidLeft, ($>)
7+
, flap, (<@>)
78
) where
89

910
import Data.Function (const, compose)
@@ -70,3 +71,26 @@ voidLeft :: forall f a b. Functor f => f a -> b -> f b
7071
voidLeft f x = const x <$> f
7172

7273
infixl 4 voidLeft as $>
74+
75+
-- | Apply a value in a computational context to a value in no context.
76+
-- |
77+
-- | Generalizes `flip`.
78+
-- |
79+
-- | ```purescript
80+
-- | longEnough :: String -> Bool
81+
-- | hasSymbol :: String -> Bool
82+
-- | hasDigit :: String -> Bool
83+
-- | password :: String
84+
-- |
85+
-- | validate :: String -> List Bool
86+
-- | validate = flap [longEnough, hasSymbol, hasDigit]
87+
-- | ```
88+
-- |
89+
-- | ```purescript
90+
-- | flap (-) 3 4 == 1
91+
-- | threeve <$> Just 1 <@> 'a' <*> Just true == Just (threeve 1 'a' true)
92+
-- | ```
93+
flap :: forall f a b. Functor f => f (a -> b) -> a -> f b
94+
flap ff x = map (\f -> f x) ff
95+
96+
infixl 4 flap as <@>

src/Prelude.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import Data.Eq (class Eq, eq, notEq, (/=), (==))
4141
import Data.EuclideanRing (class EuclideanRing, degree, div, mod, (/))
4242
import Data.Field (class Field)
4343
import Data.Function (const, flip, ($), (#))
44-
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
44+
import Data.Functor (class Functor, flap, map, void, ($>), (<#>), (<$), (<$>), (<@>))
4545
import Data.HeytingAlgebra (class HeytingAlgebra, conj, disj, not, (&&), (||))
4646
import Data.NaturalTransformation (type (~>))
4747
import Data.Ord (class Ord, compare, (<), (<=), (>), (>=), comparing, min, max, clamp, between)

0 commit comments

Comments
 (0)