File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ module Turtle (
87
87
, ExitCode (.. )
88
88
, IsString (.. )
89
89
, (&)
90
+ , (<&>)
90
91
) where
91
92
92
93
import Turtle.Format
@@ -163,3 +164,32 @@ infixl 1 &
163
164
(&) :: a -> (a -> b ) -> b
164
165
x & f = f x
165
166
#endif
167
+
168
+ #if __GLASGOW_HASKELL__ >= 821
169
+ import Data.Functor ((<&>) )
170
+ #else
171
+ -- | Flipped version of '<$>'.
172
+ --
173
+ -- @
174
+ -- ('<&>') = 'flip' 'fmap'
175
+ -- @
176
+ --
177
+ -- @since 4.11.0.0
178
+ --
179
+ -- ==== __Examples__
180
+ -- Apply @(+1)@ to a list, a 'Data.Maybe.Just' and a 'Data.Either.Right':
181
+ --
182
+ -- >>> Just 2 <&> (+1)
183
+ -- Just 3
184
+ --
185
+ -- >>> [1,2,3] <&> (+1)
186
+ -- [2,3,4]
187
+ --
188
+ -- >>> Right 3 <&> (+1)
189
+ -- Right 4
190
+ --
191
+ (<&>) :: Functor f => f a -> (a -> b ) -> f b
192
+ as <&> f = f <$> as
193
+
194
+ infixl 1 <&>
195
+ #endif
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ module Turtle.Shell (
66
66
, foldIO
67
67
, foldShell
68
68
, fold
69
+ , reduce
69
70
, sh
70
71
, view
71
72
@@ -143,6 +144,16 @@ foldShell s f = liftIO (_foldShell s f)
143
144
fold :: MonadIO io => Shell a -> Fold a b -> io b
144
145
fold s f = foldIO s (Foldl. generalize f)
145
146
147
+ -- | Flipped version of 'fold'. Useful for reducing a stream of data
148
+ --
149
+ -- ==== __Example__
150
+ -- Sum a `Shell` of numbers:
151
+ --
152
+ -- >>> select [1, 2, 3] & reduce Fold.sum
153
+ -- 6
154
+ reduce :: MonadIO io => Fold a b -> Shell a -> io b
155
+ reduce = flip fold
156
+
146
157
-- | Run a `Shell` to completion, discarding any unused values
147
158
sh :: MonadIO io => Shell a -> io ()
148
159
sh s = fold s (pure () )
You can’t perform that action at this time.
0 commit comments