5 Dec 15:38
System.Random: enumRandomR, boundedEnumRandom
From: Henning Thielemann <lemming <at> henning-thielemann.de>
Subject: System.Random: enumRandomR, boundedEnumRandom
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2007-12-05 14:40:45 GMT
Subject: System.Random: enumRandomR, boundedEnumRandom
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2007-12-05 14:40:45 GMT
Did you also already need Random instances of enumerations? Wouldn't it be nice to have some helper functions in System.Random: enumRandomR :: (Enum a, RandomGen g) => (a,a) -> g -> (a,g) enumRandomR (l,r) = mapFst toEnum . randomR (fromEnum l, fromEnum r) boundedEnumRandom :: (Enum a, Bounded a, RandomGen g) => g -> (a,g) boundedEnumRandom = enumRandomR (minBound, maxBound) mapFst :: (a -> c) -> (a,b) -> (c,b) mapFst f ~(x,y) = (f x, y) Example application: instance Random Day where random = boundedEnumRandom randomR = enumRandomR
RSS Feed