Skip to content

Commit

Permalink
Add convenience functions (#332)
Browse files Browse the repository at this point in the history
* Re-export (<&>) from Data.Functor

* Add `reduce` function
  • Loading branch information
devonhollowood authored and Gabriella439 committed Oct 16, 2018
1 parent 76224d9 commit f20d4e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Turtle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module Turtle (
, ExitCode(..)
, IsString(..)
, (&)
, (<&>)
) where

import Turtle.Format
Expand Down Expand Up @@ -163,3 +164,32 @@ infixl 1 &
(&) :: a -> (a -> b) -> b
x & f = f x
#endif

#if __GLASGOW_HASKELL__ >= 821
import Data.Functor ((<&>))
#else
-- | Flipped version of '<$>'.
--
-- @
-- ('<&>') = 'flip' 'fmap'
-- @
--
-- @since 4.11.0.0
--
-- ==== __Examples__
-- Apply @(+1)@ to a list, a 'Data.Maybe.Just' and a 'Data.Either.Right':
--
-- >>> Just 2 <&> (+1)
-- Just 3
--
-- >>> [1,2,3] <&> (+1)
-- [2,3,4]
--
-- >>> Right 3 <&> (+1)
-- Right 4
--
(<&>) :: Functor f => f a -> (a -> b) -> f b
as <&> f = f <$> as

infixl 1 <&>
#endif
11 changes: 11 additions & 0 deletions src/Turtle/Shell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module Turtle.Shell (
, foldIO
, foldShell
, fold
, reduce
, sh
, view

Expand Down Expand Up @@ -143,6 +144,16 @@ foldShell s f = liftIO (_foldShell s f)
fold :: MonadIO io => Shell a -> Fold a b -> io b
fold s f = foldIO s (Foldl.generalize f)

-- | Flipped version of 'fold'. Useful for reducing a stream of data
--
-- ==== __Example__
-- Sum a `Shell` of numbers:
--
-- >>> select [1, 2, 3] & reduce Fold.sum
-- 6
reduce :: MonadIO io => Fold a b -> Shell a -> io b
reduce = flip fold

-- | Run a `Shell` to completion, discarding any unused values
sh :: MonadIO io => Shell a -> io ()
sh s = fold s (pure ())
Expand Down

0 comments on commit f20d4e0

Please sign in to comment.