File tree 2 files changed +19
-2
lines changed
2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,15 @@ module Data.Function
3
3
, const
4
4
, apply , ($)
5
5
, applyFlipped , (#)
6
+ , applyN
6
7
, on
7
8
, module Control.Category
8
9
) where
9
10
10
11
import Control.Category (id , compose , (<<<), (>>>))
12
+ import Data.Boolean (otherwise )
13
+ import Data.Ord ((<=))
14
+ import Data.Ring ((-))
11
15
12
16
-- | Flips the order of the arguments to a function of two arguments.
13
17
-- |
@@ -77,6 +81,20 @@ applyFlipped x f = f x
77
81
-- | ```
78
82
infixl 1 applyFlipped as #
79
83
84
+ -- | `applyN f n` applies the function `f` to its argument `n` times.
85
+ -- |
86
+ -- | If n is less than or equal to 0, the function is not applied.
87
+ -- |
88
+ -- | ```purescript
89
+ -- | applyN (_ + 1) 10 0 == 10
90
+ -- | ```
91
+ applyN :: forall a . (a -> a ) -> Int -> a -> a
92
+ applyN f = go
93
+ where
94
+ go n acc
95
+ | n <= 0 = acc
96
+ | otherwise = go (n - 1 ) (f acc)
97
+
80
98
-- | The `on` function is used to change the domain of a binary operator.
81
99
-- |
82
100
-- | For example, we can create a function which compares two records based on the values of their `x` properties:
Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ module Data.Ord
15
15
) where
16
16
17
17
import Data.Eq (class Eq , class Eq1 )
18
- import Data.Function (on )
19
18
import Data.Ord.Unsafe (unsafeCompare )
20
19
import Data.Ordering (Ordering (..))
21
20
import Data.Ring (class Ring , zero , one , negate )
@@ -105,7 +104,7 @@ infixl 4 greaterThanOrEq as >=
105
104
106
105
-- | Compares two values by mapping them to a type with an `Ord` instance.
107
106
comparing :: forall a b . Ord b => (a -> b ) -> (a -> a -> Ordering )
108
- comparing f = compare `on` f
107
+ comparing f x y = compare (f x) (f y)
109
108
110
109
-- | Take the minimum of two values. If they are considered equal, the first
111
110
-- | argument is chosen.
You can’t perform that action at this time.
0 commit comments