System.Random: enumRandomR, boundedEnumRandom

5 Dec
2007
5 Dec
'07
9:40 a.m.
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
6377
Age (days ago)
6377
Last active (days ago)
0 comments
1 participants
participants (1)
-
Henning Thielemann