acme-memorandom (empty) → 0.0.1
raw patch · 7 files changed
+113/−0 lines, 7 filesdep +MemoTriedep +basedep +randomsetup-changed
Dependencies added: MemoTrie, base, random
Files
- .gitignore +3/−0
- ChangeLog.md +3/−0
- LICENSE +20/−0
- README.md +11/−0
- Setup.hs +2/−0
- acme-memorandom.cabal +38/−0
- src/System/Random/MemoRandom.hs +36/−0
+ .gitignore view
@@ -0,0 +1,3 @@+.cabal-sandbox+cabal.sandbox.config+dist
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# 0.0.1 (2015-05-14)++* Initial release.
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Johan Kiviniemi++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,11 @@+# `acme-memorandom`++[](https://hackage.haskell.org/package/acme-memorandom)++A library for generating random numbers in a memoized manner. Implemented as a+lazy table indexed by serialized [`StdGen`][StdGen]. Monomorphism is used to+facilitate memoization, users should adapt their design to work with random+[`Int`][Int] values only.++[StdGen]: http://hackage.haskell.org/package/random/docs/System-Random.html#t:StdGen+[Int]: https://hackage.haskell.org/package/base/docs/Prelude.html#t:Int
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ acme-memorandom.cabal view
@@ -0,0 +1,38 @@+name: acme-memorandom+category: ACME+version: 0.0.1+license: MIT+license-file: LICENSE+author: Johan Kiviniemi <devel@johan.kiviniemi.name>+maintainer: Johan Kiviniemi <devel@johan.kiviniemi.name>+stability: provisional+homepage: https://github.com/ion1/acme-memorandom+bug-reports: https://github.com/ion1/acme-memorandom/issues+copyright: Copyright © 2015 Johan Kiviniemi+synopsis: Memoized random number generation+description:+ A library for generating random numbers in a memoized manner. Implemented as+ 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.+tested-with: GHC == 7.10.1++build-type: Simple+cabal-version: >=1.10+extra-source-files:+ .gitignore+ ChangeLog.md+ README.md++source-repository head+ type: git+ location: https://github.com/ion1/acme-memorandom.git++library+ exposed-modules: System.Random.MemoRandom+ hs-source-dirs: src+ other-extensions: TypeFamilies, TypeOperators+ build-depends: base == 4.*+ , MemoTrie == 0.6.*+ , random == 1.*+ default-language: Haskell2010
+ src/System/Random/MemoRandom.hs view
@@ -0,0 +1,36 @@+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeOperators #-}++-- |+-- Module : Systen.Random.MemoRandom+-- Description : Memoized random number generation+-- Copyright : © 2015 Johan Kiviniemi+-- License : MIT+-- Maintainer : Johan Kiviniemi <devel@johan.kiviniemi.name>+-- Stability : provisional+-- Portability : TypeFamilies, TypeOperators+--+-- A library for generating random numbers in a memoized manner. Implemented as+-- 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++import Data.MemoTrie+import System.Random++newtype StdGen' = StdGen' { unStdGen' :: StdGen }++instance HasTrie StdGen' where+ newtype StdGen' :->: a = StdGenTrie' (String :->: a)+ trie f = StdGenTrie' (trie (f . StdGen' . read))+ untrie (StdGenTrie' t) = untrie t . show . unStdGen'+ enumerate (StdGenTrie' t) = [ (StdGen' (read a), b) | (a,b) <- enumerate t ]++-- | A memoized variant of 'randomR'.+randomR' :: (Int, Int) -> StdGen -> (Int, StdGen)+randomR' ival = memo2 (\ival' -> randomR ival' . unStdGen') ival . StdGen'++-- | A memoized variant of 'random'.+random' :: StdGen -> (Int, StdGen)+random' = memo (random . unStdGen') . StdGen'