Skip to content

Commit 66f0f10

Browse files
committed
Add overwriteGen to FrozenGen
1 parent c324ab0 commit 66f0f10

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 1.3.0
22

3-
* Add `modifyGen` to the `FrozenGen` type class
3+
* Add `modifyGen` and `overwriteGen` to the `FrozenGen` type class
44
* Add `splitFrozenM` and `splitMutableM`
55
* Switch `randomM` and `randomRM` to use `FrozenGen` instead of `RandomGenM`
66
* Deprecate `RandomGenM` in favor of a more powerful `FrozenGen`

src/System/Random/Internal.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ class StatefulGen (MutableGen f m) m => FrozenGen f m where
315315
-- @since 1.3.0
316316
modifyGen :: MutableGen f m -> (f -> (a, f)) -> m a
317317

318+
-- | Overwrite the mutable generator with the supplied frozen one
319+
--
320+
-- @since 1.3.0
321+
overwriteGen :: MutableGen f m -> f -> m ()
322+
overwriteGen mg fg = modifyGen mg (const ((), fg))
318323

319324
-- | Splits a pseudo-random number generator into two. Overwrites the mutable
320325
-- wrapper with one of the resulting generators and returns the other.
@@ -479,6 +484,8 @@ instance (RandomGen g, MonadState g m) => FrozenGen (StateGen g) m where
479484
thawGen (StateGen g) = StateGenM <$ put g
480485
modifyGen _ f = state (coerce f)
481486
{-# INLINE modifyGen #-}
487+
overwriteGen _ f = put (coerce f)
488+
{-# INLINE overwriteGen #-}
482489

483490
-- | Splits a pseudo-random number generator into two. Updates the state with
484491
-- one of the resulting generators and returns the other.

src/System/Random/Stateful.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ instance (RandomGen g, MonadIO m) => FrozenGen (IOGen g) m where
473473
g' `seq` writeIORef ref g'
474474
pure a
475475
{-# INLINE modifyGen #-}
476+
overwriteGen (IOGenM ref) = liftIO . writeIORef ref . unIOGen
477+
{-# INLINE overwriteGen #-}
476478

477479
-- | Applies a pure operation to the wrapped pseudo-random number generator.
478480
--
@@ -538,6 +540,8 @@ instance RandomGen g => FrozenGen (STGen g) (ST s) where
538540
g' `seq` writeSTRef ref g'
539541
pure a
540542
{-# INLINE modifyGen #-}
543+
overwriteGen (STGenM ref) = writeSTRef ref . unSTGen
544+
{-# INLINE overwriteGen #-}
541545

542546

543547
-- | Applies a pure operation to the wrapped pseudo-random number generator.
@@ -639,6 +643,8 @@ instance RandomGen g => FrozenGen (TGen g) STM where
639643
g' `seq` writeTVar ref g'
640644
pure a
641645
{-# INLINE modifyGen #-}
646+
overwriteGen (TGenM ref) = writeTVar ref . unTGen
647+
{-# INLINE overwriteGen #-}
642648

643649

644650
-- | Applies a pure operation to the wrapped pseudo-random number generator.

0 commit comments

Comments
 (0)