Skip to content

Adds "monadic" lenses and prisms #129

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 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Adds lenses, prisms and traversals for working with monads. (@mikesol)

Bugfixes:

Expand Down
22 changes: 20 additions & 2 deletions src/Data/Lens/Lens/Tuple.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module Data.Lens.Lens.Tuple
( _1
, _2
, _1M
, _2M
, module Data.Profunctor.Strong
) where

import Data.Lens.Lens (Lens)
import Prelude
import Data.Lens.Lens (Lens, lens)
import Data.Profunctor.Strong (first, second)
import Data.Tuple (Tuple)
import Data.Tuple (Tuple, fst, snd)
import Data.Tuple.Nested ((/\))

-- | Lens for the first component of a `Tuple`.
_1 :: forall a b c. Lens (Tuple a c) (Tuple b c) a b
Expand All @@ -15,3 +19,17 @@ _1 = first
-- | Lens for the second component of a `Tuple`.
_2 :: forall a b c. Lens (Tuple c a) (Tuple c b) a b
_2 = second

-- | Lens for the first component of a `Tuple` in a monadic context.
_1M :: forall a b c m. Monad m => Lens (Tuple a c) (m (Tuple b c)) a (m b)
_1M =
lens fst \(_ /\ b) ma -> do
a <- ma
pure $ a /\ b

-- | Lens for the second component of a `Tuple` in a monadic context.
_2M :: forall a b c m. Monad m => Lens (Tuple c a) (m (Tuple c b)) a (m b)
_2M =
lens snd \(a /\ _) mb -> do
b <- mb
pure $ a /\ b
13 changes: 11 additions & 2 deletions src/Data/Lens/Prism/Either.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module Data.Lens.Prism.Either
, module Data.Profunctor.Choice
) where

import Data.Either (Either)
import Data.Lens.Prism (Prism)
import Prelude
import Data.Either (Either(..), either)
import Data.Lens.Prism (Prism, prism)
import Data.Profunctor.Choice (left, right)

-- | Prism for the `Left` constructor of `Either`.
Expand All @@ -15,3 +16,11 @@ _Left = left
-- | Prism for the `Right` constructor of `Either`.
_Right :: forall a b c. Prism (Either c a) (Either c b) a b
_Right = right

-- | Prism for the `Left` constructor of `Either` in a monadic context.
_LeftM :: forall a b c m. Monad m => Prism (Either a c) (m (Either b c)) a (m b)
_LeftM = prism (map Left) (either Right (Left <<< pure <<< Right))

-- | Prism for the `Right` constructor of `Either` in a monadic context.
_RightM :: forall a b c m. Monad m => Prism (Either c a) (m (Either c b)) a (m b)
_RightM = prism (map Right) (either (Left <<< pure <<< Left) Right)
10 changes: 8 additions & 2 deletions src/Data/Lens/Prism/Maybe.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module Data.Lens.Prism.Maybe where

import Prelude

import Data.Either (Either(..))
import Data.Maybe (Maybe(..), maybe)

import Data.Lens.Prism (Prism, prism)

-- | Prism for the `Nothing` constructor of `Maybe`.
Expand All @@ -14,3 +12,11 @@ _Nothing = prism (const Nothing) $ maybe (Right unit) (const $ Left Nothing)
-- | Prism for the `Just` constructor of `Maybe`.
_Just :: forall a b. Prism (Maybe a) (Maybe b) a b
_Just = prism Just $ maybe (Left Nothing) Right

-- | Prism for the `Nothing` constructor of `Maybe` in a monadic context.
_NothingM :: forall a b m. Monad m => Prism (Maybe a) (m (Maybe b)) Unit (m Unit)
_NothingM = prism (const $ pure Nothing) (maybe (Right unit) (const $ Left $ pure Nothing))

-- | Prism for the `Just` constructor of `Maybe` in a monadic context.
_JustM :: forall a b m. Monad m => Prism (Maybe a) (m (Maybe b)) a (m b)
_JustM = prism (map Just) (maybe (Left $ pure Nothing) Right)
17 changes: 16 additions & 1 deletion src/Data/Lens/Record.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Data.Lens.Record (prop) where

import Prelude

import Data.Lens (Lens, lens)
import Data.Symbol (class IsSymbol)
import Prim.Row as Row
Expand All @@ -26,3 +25,19 @@ prop
=> proxy l
-> Lens (Record r1) (Record r2) a b
prop l = lens (get l) (flip (set l))

-- | Like `prop`, but for monadic contexts
propM
:: forall l r1 r2 r a b proxy m
. IsSymbol l
=> Row.Cons l a r r1
=> Row.Cons l b r r2
=> Monad m
=> proxy l
-> Lens (Record r1) (m (Record r2)) a (m b)
propM l =
lens (get l)
( \s mb -> do
b <- mb
pure $ set l b s
)
8 changes: 7 additions & 1 deletion src/Data/Lens/Traversal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ import Control.Alternative (class Alternative)
import Control.Plus (empty)
import Data.Lens.Indexed (iwander, positions, unIndex)
import Data.Lens.Internal.Bazaar (Bazaar(..), runBazaar)
import Data.Lens.Lens (lens')
import Data.Lens.Types (ATraversal, IndexedTraversal, IndexedOptic, Indexed(..), Traversal, Optic, class Wander, wander)
import Data.Lens.Types (Traversal, Traversal') as ExportTypes
import Data.Monoid.Disj (Disj(..))
import Data.Newtype (under, unwrap)
import Data.Profunctor.Star (Star(..))
import Data.Traversable (class Traversable, traverse)
import Data.Profunctor.Strong (class Strong)
import Data.Traversable (class Traversable, sequence, traverse)
import Data.Tuple (Tuple(..), uncurry)

-- | A `Traversal` for the elements of a `Traversable` functor.
Expand All @@ -52,6 +54,10 @@ import Data.Tuple (Tuple(..), uncurry)
traversed :: forall t a b. Traversable t => Traversal (t a) (t b) a b
traversed = wander traverse

-- Like `traversed`, but for monadic output.
traversedM :: forall t a b m p. Traversable t => Wander p => Strong p => Monad m => Optic p (t a) (m (t b)) a (m b)
traversedM = lens' (flip Tuple sequence) <<< traversed

-- | Turn a pure profunctor `Traversal` into a `lens`-like `Traversal`.
traverseOf
:: forall f s t a b . Optic (Star f) s t a b -> (a -> f b) -> s -> f t
Expand Down