gray-extended 1.3 → 1.4
raw patch · 2 files changed
+19/−1 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Codec.Gray: grayToIntegral :: (Num a, Bits a) => a -> a
+ Codec.Gray: integralToGray :: Bits a => a -> a
Files
- gray-extended.cabal +1/−1
- src/Codec/Gray.hs +18/−0
gray-extended.cabal view
@@ -1,5 +1,5 @@ name: gray-extended-version: 1.3+version: 1.4 synopsis: Gray encoding schemes description: Gray codes satisfy the property that two successive values differ in only one digit. Usually the term \"Gray code\"
src/Codec/Gray.hs view
@@ -17,10 +17,13 @@ module Codec.Gray ( grayCodes,+ integralToGray,+ grayToIntegral, naryGrayCodes ) where import Data.List (foldl')+import Data.Bits (Bits, shiftR, xor) -- | @'grayCodes' k@ generates the list of Binary Reflected Gray Code -- (BRGC) numbers of length k. This code is cyclic.@@ -29,6 +32,21 @@ grayCodes k = let xs = grayCodes (k-1) in map (False:) xs ++ map (True:) (reverse xs) +-- | @'integralToGray' n@ encodes @n@ using a BRGC, and returns the+-- resulting bits as an integer. For example, encoding @17@ in BRGC+-- results in @11001@, or 25. So @integralToGray 17@ returns @25@.+integralToGray :: Bits a => a -> a+integralToGray n = (n `shiftR` 1) `xor` n++-- | @'grayToIntegral' n@ decodes @n@ using a BRGC, and returns the+-- resulting integer. For example, 25 is @11001@, which is the code+-- for 17. So @grayToIntegral 25@ returns @17@.+-- (see @'integralToGray'@ for +grayToIntegral :: (Num a, Bits a) => a -> a+grayToIntegral n = f n (n `shiftR` 1)+ where f k m | m /= 0 = f (k `xor` m) (m `shiftR` 1)+ | otherwise = k+ -- | @'naryGrayCodes' xs k@ generates a non-Boolean (or n-ary) Gray code -- of length @k@ using the elements of @x@ as "digits". This code is -- cyclic.