packages feed

acme-memorandom 0.0.1 → 0.0.2

raw patch · 3 files changed

+57/−3 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ System.Random.MemoRandom: randomIO' :: IO Int
+ System.Random.MemoRandom: randomRIO' :: (Int, Int) -> IO Int
+ System.Random.MemoRandom: randomRs' :: (Int, Int) -> StdGen -> [Int]
+ System.Random.MemoRandom: randoms' :: StdGen -> [Int]

Files

ChangeLog.md view
@@ -1,3 +1,7 @@+# 0.0.2 (2015-05-15)++* Implement memoized randomRs', randoms', randomRIO', randomIO'.+ # 0.0.1 (2015-05-14)  * Initial release.
acme-memorandom.cabal view
@@ -1,6 +1,6 @@ name: acme-memorandom category: ACME-version: 0.0.1+version: 0.0.2 license: MIT license-file: LICENSE author: Johan Kiviniemi <devel@johan.kiviniemi.name>@@ -31,7 +31,7 @@ library   exposed-modules: System.Random.MemoRandom   hs-source-dirs: src-  other-extensions: TypeFamilies, TypeOperators+  other-extensions: CPP, TypeFamilies, TypeOperators   build-depends: base == 4.*                , MemoTrie == 0.6.*                , random == 1.*
src/System/Random/MemoRandom.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} @@ -14,11 +15,27 @@ -- a lazy table indexed by serialized 'StdGen'. Monomorphism is used to -- facilitate memoization, users should adapt their design to work with random -- 'Int' values only.-module System.Random.MemoRandom (randomR', random') where+module System.Random.MemoRandom+  ( randomR'+  , random'+  , randomRs'+  , randoms'+  , randomRIO'+  , randomIO'+  ) where  import Data.MemoTrie import System.Random +#ifdef __GLASGOW_HASKELL__+import GHC.Exts (build)+#else+-- | A dummy variant of build without fusion.+{-# INLINE build #-}+build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]+build g = g (:) []+#endif+ newtype StdGen' = StdGen' { unStdGen' :: StdGen }  instance HasTrie StdGen' where@@ -34,3 +51,36 @@ -- | A memoized variant of 'random'. random' :: StdGen -> (Int, StdGen) random' = memo (random . unStdGen') . StdGen'++-- | A memoized variant of 'randomRs'.+{-# INLINE randomRs' #-}+randomRs' :: (Int, Int) -> StdGen -> [Int]+randomRs' ival g = build (\cons _nil -> buildRandoms cons (randomR' ival) g)++-- | A memoized variant of 'randoms'.+{-# INLINE randoms' #-}+randoms' :: StdGen -> [Int]+randoms' g = build (\cons _nil -> buildRandoms cons random' g)++-- | A memoized variant of 'randomRIO'.+randomRIO' :: (Int, Int) -> IO Int+randomRIO' ival = getStdRandom (randomR' ival)++-- | A memoized variant of 'randomIO'.+randomIO' :: IO Int+randomIO' = getStdRandom random'++-- | Produce an infinite list-equivalent of random values.+--+-- Copied from System.Random verbatim (but originally written by the author of+-- MemoRandom, commit 4695ffa).+{-# INLINE buildRandoms #-}+buildRandoms :: RandomGen g+             => (a -> as -> as)  -- ^ E.g. '(:)' but subject to fusion+             -> (g -> (a,g))     -- ^ E.g. 'random'+             -> g                -- ^ A 'RandomGen' instance+             -> as+buildRandoms cons rand = go+  where+    -- The seq fixes part of #4218 and also makes fused Core simpler.+    go g = x `seq` (x `cons` go g') where (x,g') = rand g