Skip to content

Commit f61fd48

Browse files
committed
Merge pull request #27 from zrho/master
Convert functions to `Forget`, including `to`.
2 parents d7f198c + 4517870 commit f61fd48

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/Data/Lens/Fold.purs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Data.Lens.Fold
55
, preview, foldOf, foldMapOf, foldrOf, foldlOf, toListOf, firstOf, lastOf
66
, maximumOf, minimumOf, allOf, anyOf, andOf, orOf, elemOf, notElemOf, sumOf
77
, productOf, lengthOf, findOf, sequenceOf_, has, hasn't, replicated, filtered
8+
, folded, unfolded
89
, ifoldMapOf, ifoldrOf, ifoldlOf, iallOf, ianyOf, itoListOf, itraverseOf_
910
, module ExportTypes
1011
) where
@@ -14,12 +15,12 @@ import Prelude
1415
import Control.Apply ((*>))
1516

1617
import Data.Either (Either(..), either)
17-
import Data.Foldable (Foldable, foldr)
18-
import Data.Functor.Contravariant (Contravariant)
18+
import Data.Foldable (Foldable, foldMap)
1919
import Data.List (List(..), (:))
2020
import Data.Maybe (Maybe(..), maybe)
2121
import Data.Maybe.First (First(..), runFirst)
2222
import Data.Maybe.Last (Last(..), runLast)
23+
import Data.Monoid (Monoid, mempty)
2324
import Data.Monoid.Additive (Additive(..), runAdditive)
2425
import Data.Monoid.Conj (Conj(..), runConj)
2526
import Data.Monoid.Disj (Disj(..), runDisj)
@@ -28,7 +29,6 @@ import Data.Monoid.Endo (Endo(..), runEndo)
2829
import Data.Monoid.Multiplicative (Multiplicative(..), runMultiplicative)
2930
import Data.Profunctor (dimap)
3031
import Data.Profunctor.Choice (Choice, right)
31-
import Data.Profunctor.Star (Star(..), runStar)
3232
import Data.Tuple (Tuple(..), uncurry)
3333

3434
import Data.Lens.Internal.Void (coerce)
@@ -152,25 +152,19 @@ filtered :: forall p a. (Choice p) => (a -> Boolean) -> OpticP p a a
152152
filtered f = dimap (\x -> if f x then Right x else Left x) (either id id) <<< right
153153

154154
-- | Replicates the elements of a fold.
155-
replicated
156-
:: forall a b t f. (Applicative f, Contravariant f)
157-
=> Int -> Optic (Star f) a b a t
158-
replicated n p = Star (flip go n <<< runStar p) where
159-
go x 0 = coerce (pure unit)
160-
go x n = x *> go x (n - 1)
155+
replicated :: forall a b t r. (Monoid r) => Int -> Fold r a b a t
156+
replicated n = Forget <<< go n <<< runForget where
157+
go 0 x = mempty
158+
go n x = x <> go (n - 1) x
161159

162160
-- | Folds over a `Foldable` container.
163-
folded
164-
:: forall f g a b t. (Applicative f, Contravariant f, Foldable g)
165-
=> Optic (Star f) (g a) b a t
166-
folded p = Star $ foldr (\a r -> runStar p a *> r) (coerce $ pure unit)
161+
folded :: forall g a b t r. (Monoid r, Foldable g) => Fold r (g a) b a t
162+
folded = Forget <<< foldMap <<< runForget
167163

168164
-- | Builds a `Fold` using an unfold.
169-
unfolded
170-
:: forall f s t a b. (Applicative f, Contravariant f)
171-
=> (s -> Maybe (Tuple a s)) -> Optic (Star f) s t a b
172-
unfolded f p = Star go where
173-
go = maybe (coerce $ pure unit) (\(Tuple a s) -> runStar p a *> go s) <<< f
165+
unfolded :: forall r s t a b. (Monoid r) => (s -> Maybe (Tuple a s)) -> Fold r s t a b
166+
unfolded f p = Forget go where
167+
go = maybe mempty (\(Tuple a sn) -> runForget p a <> go sn) <<< f
174168

175169
-- | Fold map over an `IndexedFold`.
176170
ifoldMapOf :: forall r i s t a b. IndexedFold r i s t a b -> (i -> a -> r) -> s -> r

src/Data/Lens/Getter.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import Prelude (id, (<<<), ($))
1010

1111
import Data.Const (Const(..), getConst)
1212
import Data.Functor.Contravariant (Contravariant, cmap)
13+
import Data.Profunctor (Profunctor, lmap)
1314
import Data.Profunctor.Star (Star(..), runStar)
1415
import Data.Tuple (Tuple (..))
1516
import Control.Monad.State.Class (MonadState, gets)
1617

1718
import Data.Lens.Internal.Forget (Forget (..), runForget)
18-
import Data.Lens.Types (Getter(), Optic())
19+
import Data.Lens.Types (Getter(), Fold(), Optic())
1920
import Data.Lens.Types (IndexedGetter(), Indexed (..))
2021
import Data.Lens.Types (IndexedFold())
2122

@@ -34,8 +35,8 @@ iview l = runForget (l (Indexed $ Forget id))
3435
(^.) s l = view l s
3536

3637
-- | Convert a function into a getter.
37-
to :: forall s a f. (Contravariant f) => (s -> a) -> Optic (Star f) s s a a
38-
to f p = Star (cmap f <<< runStar p <<< f)
38+
to :: forall r s t a b. (s -> a) -> Fold r s t a b
39+
to f p = Forget (runForget p <<< f)
3940

4041
-- | View the focus of a `Getter` in the state of a monad.
4142
use :: forall s t a b m. (MonadState s m) => Getter s t a b -> m a

0 commit comments

Comments
 (0)