Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added folding1, ifolding1, and itoNonEmptyOf (fixes #1015) #1057

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Control/Lens/Fold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Control.Lens.Fold
-- ** Building Folds
, folding, ifolding
, foldring, ifoldring
, folding1, ifolding1
, folded
, folded64
, unfolded
Expand Down Expand Up @@ -120,7 +121,7 @@ module Control.Lens.Fold
, ifoldlOf'
, ifoldrMOf
, ifoldlMOf
, itoListOf
, itoListOf, itoNonEmptyOf
, elemIndexOf
, elemIndicesOf
, findIndexOf
Expand Down Expand Up @@ -159,6 +160,7 @@ import Control.Monad as Monad
import Control.Monad.Reader
import Control.Monad.State
import Data.CallStack
import Data.Semigroup.Foldable(Foldable1, traverse1_)
import Data.Functor.Apply hiding ((<.))
import Data.Int (Int64)
import Data.List (intercalate)
Expand All @@ -175,6 +177,7 @@ import qualified Data.Semigroup as Semi
-- >>> import Data.Function
-- >>> import Data.List.Lens
-- >>> import Data.List.NonEmpty (NonEmpty (..))
-- >>> import qualified Data.List.NonEmpty as NonEmpty
-- >>> import Debug.SimpleReflect.Expr
-- >>> import Debug.SimpleReflect.Vars as Vars hiding (f,g)
-- >>> import Control.DeepSeq (NFData (..), force)
Expand Down Expand Up @@ -203,10 +206,24 @@ folding :: Foldable f => (s -> f a) -> Fold s a
folding sfa agb = phantom . traverse_ agb . sfa
{-# INLINE folding #-}

-- | construct a Fold1 from a function that produces a Foldable1
--
-- >>> toNonEmptyOf folding1 reverse (1 :| [2,3,4])
-- (4 :| [3,2,1])
folding1 :: Foldable1 f => (s -> f a) -> Fold1 s a
folding1 sfa agb = phantom . traverse1_ agb . sfa
{-# INLINE folding1 #-}

ifolding :: (Foldable f, Indexable i p, Contravariant g, Applicative g) => (s -> f (i, a)) -> Over p g s t a b
ifolding sfa f = phantom . traverse_ (phantom . uncurry (indexed f)) . sfa
{-# INLINE ifolding #-}

-- | Version of ifolding to build an 'IndexedFold1'
ifolding1 :: (Foldable1 f, Indexable i p, Contravariant g, Apply g)
=> (s -> f (i, a)) -> Over p g s t a b
ifolding1 sfa f = phantom . traverse1_ (phantom . uncurry (indexed f)) . sfa
{-# INLINE ifolding1 #-}

-- | Obtain a 'Fold' by lifting 'foldr' like function.
--
-- >>> [1,2,3,4]^..foldring foldr
Expand Down Expand Up @@ -643,6 +660,14 @@ toListOf l = foldrOf l (:) []
toNonEmptyOf :: Getting (NonEmptyDList a) s a -> s -> NonEmpty a
toNonEmptyOf l = flip getNonEmptyDList [] . foldMapOf l (NonEmptyDList #. (:|))

-- | indexed version of 'toNonEmptyOf'
--
-- >>> itoNonEmptyOf traverse1 $ "hello" :| ["world"]
-- (0,"hello") :| [(1,"world")]
itoNonEmptyOf :: IndexedGetting i (NonEmptyDList (i,a)) s a -> s -> NonEmpty (i,a)
itoNonEmptyOf l = flip getNonEmptyDList [] . ifoldMapOf l (\i a -> NonEmptyDList $ ((i,a) :|))
{-# INLINE itoNonEmptyOf #-}

-- | Calls 'pure' on the target of a 'Lens', 'Getter', or 'Iso'.
--
-- Calls 'pure' on the targets of a 'Traversal', 'Fold', or 'Prism', and
Expand Down