polysemy-RandomFu 0.2.0.0 → 0.3.0.0
raw patch · 4 files changed
+26/−10 lines, 4 filesdep +vector
Dependencies added: vector
Files
- ChangeLog.md +5/−2
- polysemy-RandomFu.cabal +3/−2
- src/Polysemy/RandomFu.hs +8/−6
- test/RandomFuSpec.hs +10/−0
ChangeLog.md view
@@ -1,4 +1,7 @@-# Changelog for polysemy-RandomFu+## v0.3.0.0+- Fixed runRandomSource to make it possible to run with sources where the MonadRandom is not Sem r. (Thanks to @Infinisil !)+- Added test to RandomFuSpec for the above change (Also, thanks to @Infinisil)+- Bumped version number accordingly ## 0.2.0.0 - Changes to keep up with ConstraintAbsorber changes in polysemy-zoo@@ -7,5 +10,5 @@ ## 0.1.0.0 (2019-06-14) - Initial Release with RandomFu effect/intepreters -## Unreleased changes+
polysemy-RandomFu.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: eae1a0503816a03e0d5218be187818539f2cba48460bc5b150f75b11a2ea2dfb+-- hash: 0c5c47c4ef21096e59da04d78100ae9d4087627b0d6969c8fe9c2549a8e45a13 name: polysemy-RandomFu-version: 0.2.0.0+version: 0.3.0.0 synopsis: Experimental, RandomFu effect and interpreters for polysemy description: Please see the README on GitHub at <https://github.com/adamConnerSax/polysemy-Extra/tree/master/polysemy-RandomFu#polysemy-randomfu> category: Polysemy@@ -70,4 +70,5 @@ , random-fu >=0.2.5.0 && <0.3.0.0 , random-source >=0.3 && <0.4.0.0 , text >=1.1.0.0 && <1.3.0.0+ , vector >=0.7 && <0.13 default-language: Haskell2010
src/Polysemy/RandomFu.hs view
@@ -64,14 +64,16 @@ ------------------------------------------------------------------------------ -- | Run a 'Random' effect using a given 'R.RandomSource' runRandomSource- :: forall s r a- . R.RandomSource (Sem r) s+ :: forall s m r a+ . ( R.RandomSource m s+ , Member (Lift m) r+ ) => s -> Sem (RandomFu ': r) a -> Sem r a runRandomSource source = interpret $ \case- SampleRVar rv -> R.runRVar (R.sample rv) source- GetRandomPrim pt -> R.runRVar (R.getRandomPrim pt) source+ SampleRVar rv -> sendM $ R.runRVar (R.sample rv) source+ GetRandomPrim pt -> sendM $ R.runRVar (R.getRandomPrim pt) source {-# INLINEABLE runRandomSource #-} ------------------------------------------------------------------------------@@ -89,11 +91,11 @@ ------------------------------------------------------------------------------ -- | Run in 'IO', using the given 'R.PureMT' source, stored in an 'IORef' runRandomIOPureMT- :: MonadIO (Sem r)+ :: Member (Lift IO) r => R.PureMT -> Sem (RandomFu ': r) a -> Sem r a runRandomIOPureMT source re =- liftIO (newIORef source) >>= flip runRandomSource re+ sendM (newIORef source) >>= flip runRandomSource re {-# INLINEABLE runRandomIOPureMT #-}
test/RandomFuSpec.hs view
@@ -14,6 +14,9 @@ import qualified Data.Random as R import qualified Data.Random.Source.PureMT as R +import qualified Data.Random.Source.MWC as MWC+import qualified Data.Vector as V+ getRandomInts :: Member RandomFu r => Int -> Sem r [Int] getRandomInts nDraws = sampleRVar $ M.replicateM nDraws (R.uniform 0 (100 :: Int))@@ -55,6 +58,13 @@ result `shouldBe` [3, 78, 53, 41, 56] + it+ "Should produce [95, 40, 24, 49, 64], 5 pseudo-random Ints seeded from the same seed on each test, from an MWC RandomSource."+ $ do+ gen <- MWC.initialize (V.fromList [0])+ result <-+ runM . runRandomSource gen $ getRandomInts 5+ result `shouldBe` [95, 40, 24, 49, 64] it "Should produce two distinct sets of psuedo-random Ints." $ do result <- runM . runRandomIO $ randomListsDifferent 5