Skip to content

Commit 2f7d6d8

Browse files
committed
modify now returns the new state too
1 parent 1a2a3b3 commit 2f7d6d8

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

src/Control/Monad/State/Class.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ gets f = state \s -> Tuple (f s) s
3636
put :: forall m s. MonadState s m => s -> m Unit
3737
put s = state \_ -> Tuple unit s
3838

39-
-- | Modify the state by applying a function to the current state.
40-
modify :: forall s m. MonadState s m => (s -> s) -> m Unit
41-
modify f = state \s -> Tuple unit (f s)
39+
-- | Modify the state by applying a function to the current state. The returned
40+
-- | value is the new state value.
41+
modify :: forall s m. MonadState s m => (s -> s) -> m s
42+
modify f = state \s -> let s' = f s in Tuple s' s'

test/Example/State.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Effect (Effect)
88
import Effect.Console (log)
99

1010
incState :: State Int Unit
11-
incState = modify (_ + 1)
11+
incState = void $ modify (_ + 1)
1212

1313
testState :: State Int String
1414
testState = do

test/Example/StateEff.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pop = unsafePartial do
2323
push :: Int -> Stack Unit
2424
push x = do
2525
lift $ log $ "Pushing " <> show x
26-
modify $ (:) x
26+
_ <- modify (x : _)
2727
pure unit
2828

2929
testState :: Stack Int

0 commit comments

Comments
 (0)