From 55a24136798a2998fdf64353bc0b0fa95a9db22e Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Tue, 14 Jul 2020 07:10:02 -0400 Subject: [PATCH 1/4] overPost --- src/Data/Lens/Setter.purs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Data/Lens/Setter.purs b/src/Data/Lens/Setter.purs index 8434e9e..07bb94b 100644 --- a/src/Data/Lens/Setter.purs +++ b/src/Data/Lens/Setter.purs @@ -34,6 +34,7 @@ import Data.Maybe (Maybe(..)) import Data.Tuple (uncurry) infixr 4 over as %~ +infixr 4 overPost as <%~ infixr 4 set as .~ infixr 4 addOver as +~ infixr 4 subOver as -~ @@ -47,6 +48,7 @@ infixr 4 setJust as ?~ infix 4 assign as .= infix 4 modifying as %= +infix 4 modifyingLensPost as <%= infix 4 addModifying as += infix 4 mulModifying as *= infix 4 subModifying as -= @@ -103,6 +105,18 @@ assign p b = void (modify (set p b)) modifying :: forall s a b m. MonadState s m => Setter s s a b -> (a -> b) -> m Unit modifying p f = void (modify (over p f)) +overPost :: forall s t a b. Lens s t a b -> (a -> b) -> s -> Tuple b t +overPost l f x = Tuple y (x # l .~ y) + where + y = x ^. l <<< to f + +modifyingLensPost :: forall s a m. MonadState s m => Lens' s a -> (a -> a) -> m a +modifyingLensPost l f = do + s <- get + let Tuple x s' = s # l <%~ f + put s' + pure x + addModifying :: forall s a m. MonadState s m => Semiring a => Setter' s a -> a -> m Unit addModifying p = modifying p <<< add From df703867ce46a214cf5ca7fa82e18dbe85dab39c Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Tue, 14 Jul 2020 07:22:02 -0400 Subject: [PATCH 2/4] reordered --- src/Data/Lens/Setter.purs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Data/Lens/Setter.purs b/src/Data/Lens/Setter.purs index 07bb94b..7feb089 100644 --- a/src/Data/Lens/Setter.purs +++ b/src/Data/Lens/Setter.purs @@ -63,6 +63,11 @@ infix 4 assignJust as ?= over :: forall s t a b. Setter s t a b -> (a -> b) -> s -> t over l = l +overPost :: forall s t a b. Lens s t a b -> (a -> b) -> s -> Tuple b t +overPost l f x = Tuple y (x # l .~ y) + where + y = x ^. l <<< to f + -- | Apply a function to the foci of a `Setter` that may vary with the index. iover :: forall i s t a b. IndexedSetter i s t a b -> (i -> a -> b) -> s -> t iover l f = l (Indexed $ uncurry f) @@ -105,11 +110,6 @@ assign p b = void (modify (set p b)) modifying :: forall s a b m. MonadState s m => Setter s s a b -> (a -> b) -> m Unit modifying p f = void (modify (over p f)) -overPost :: forall s t a b. Lens s t a b -> (a -> b) -> s -> Tuple b t -overPost l f x = Tuple y (x # l .~ y) - where - y = x ^. l <<< to f - modifyingLensPost :: forall s a m. MonadState s m => Lens' s a -> (a -> a) -> m a modifyingLensPost l f = do s <- get From ef0b86e89a8a9788f0a4aff00d9a7512a751ca3f Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Tue, 14 Jul 2020 07:41:22 -0400 Subject: [PATCH 3/4] imports --- src/Data/Lens/Setter.purs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Data/Lens/Setter.purs b/src/Data/Lens/Setter.purs index 7feb089..f9b9e58 100644 --- a/src/Data/Lens/Setter.purs +++ b/src/Data/Lens/Setter.purs @@ -27,11 +27,12 @@ module Data.Lens.Setter import Prelude -import Control.Monad.State.Class (class MonadState, modify) +import Control.Monad.State.Class (class MonadState, modify, get, put) -import Data.Lens.Types (IndexedSetter, Indexed(..), Setter, Setter') +import Data.Lens.Getter ((^.), to) +import Data.Lens.Types (IndexedSetter, Indexed(..), Setter, Setter', Lens, Lens') import Data.Maybe (Maybe(..)) -import Data.Tuple (uncurry) +import Data.Tuple (Tuple(Tuple), uncurry) infixr 4 over as %~ infixr 4 overPost as <%~ From 2819e0b4e592f845cadea615e7fcf2dd3c989364 Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Tue, 14 Jul 2020 07:53:06 -0400 Subject: [PATCH 4/4] export --- src/Data/Lens/Setter.purs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Data/Lens/Setter.purs b/src/Data/Lens/Setter.purs index f9b9e58..c5f8f76 100644 --- a/src/Data/Lens/Setter.purs +++ b/src/Data/Lens/Setter.purs @@ -1,6 +1,7 @@ -- | This module defines functions for working with setters. module Data.Lens.Setter ( (%~), over, iover + , (<%~), overPost , (.~), set , (+~), addOver , (-~), subOver @@ -13,6 +14,7 @@ module Data.Lens.Setter , (?~), setJust , (.=), assign , (%=), modifying + , (<%=), modifyingLensPost , (+=), addModifying , (*=), mulModifying , (-=), subModifying