Skip to content

modifyingTraverse #115

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

Open
wants to merge 1 commit into
base: main
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
12 changes: 12 additions & 0 deletions src/Data/Lens/Traversal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ module Data.Lens.Traversal
, elementsOf
, itraverseOf
, cloneTraversal
, (%%=), modifyingTraverse
, module ExportTypes
) where

import Prelude

import Control.Monad.State.Class (class MonadState, get, put)
import Control.Alternative (class Alternative)
import Control.Plus (empty)
import Data.Lens.Indexed (iwander, positions, unIndex)
Expand Down Expand Up @@ -146,3 +148,13 @@ iforOf = flip <<< itraverseOf

cloneTraversal :: forall s t a b. ATraversal s t a b -> Traversal s t a b
cloneTraversal l = wander (runBazaar (l (Bazaar identity)))

-- | Modify the foci of a `Setter` in a monadic state with a traversal.
modifyingTraverse
:: forall s a b m
. MonadState s m
=> Traversal s s a b
-> (a -> m b)
-> m Unit
modifyingTraverse t f = get >>= traverseOf t f >>= put
infix 4 modifyingTraverse as %%=