以下工作但效率极低。
我怀疑这是我使用列表连接的原因。
import Control.Parallel.Strategies (runEval, rpar)
import Data.Text (Text)
import qualified Data.Text.IO as T (writeFile)
import Text.InterpolatedString.Perl6 (qq)
-- random data imports
import Crypto.Random.AESCtr (makeSystem)
import System.Random (split, randomR, randomRs)
-- |This function picks a random element from a list
-- choose :: RandomGen g => g -- ^ random generator
-- -> [a] -- ^ list to be taken from
-- -> (a, g) -- ^ random pick
choose g lst = let ra = randomR (0::Int, length lst -1) g
in (lst !! fst ra, snd ra)
和
-- |This function generates a set of french SSN numbers
-- http://mon-convertisseur.fr/calculateur-cle-numero-securite-sociale.php
-- sans check digits
-- genFrSSN :: RandomGen g => g -- ^ random generator
-- -> [Text] -- ^ list of data
genFrSSN g = [fin] ++ genFrSSN g5
where
(sex, g1) = choose g [1..2]
(yBirth, g2) = choose g1 [70..99]
(mBirth, g3) = choose g2 [10..12]
(depart, g4) = choose g3 [21..95]
commune = 999
(numOrd, g5) = randomR (100::Int, 300) g4
-- using interpolatedstring-perl6
-- because for some other generator I may have not have only Int data
fin = [qq|$sex$yBirth$mBirth$depart$commune$numOrd|] :: Text
我重写了
genFrSSN g = runEval $ do
a <- rpar [fin]
b <- rpar $ genFrSSN (snd sx)
return (a ++ b)
where
sx = split g
(sex, g1) = choose (fst sx) [1..2]
(yBirth, g2) = choose g1 [70..99]
(mBirth, g3) = choose g2 [10..12]
(depart, g4) = choose g3 [21..95]
commune = 999
(numOrd, _) = randomR (100::Int, 300) g4
fin = [qq|$sex$yBirth$mBirth$depart$commune$numOrd|] :: Text
但是后来我没有变低,而是用完了内存并且过度使用了cpu。生成器应该给我一个无限的列表,我将从中取出可变数量的元素。
我使用列表是因为 [Char] 在有多个数据输入时很方便
首先,我如何摆脱(++)?其次请批评。