digits 0.2 → 0.3
raw patch · 5 files changed
+81/−38 lines, 5 filesdep +digitsdep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: digits
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Data.Digits: mDigits :: Integral n => n -> n -> Maybe [n]
+ Data.Digits: mDigitsRev :: Integral n => n -> n -> Maybe [n]
Files
- LICENSE +1/−1
- digits.cabal +26/−10
- src/Data/Digits.hs +41/−16
- src/Tests.hs +0/−11
- test/Tests.hs +13/−0
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2009, Henry Bucklow+Copyright (c) 2009-2016, Henry Bucklow, Charlie Harvey All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
digits.cabal view
@@ -1,19 +1,35 @@-Build-Type: Custom+Build-Type: Simple Name: digits Category: Data-Version: 0.2-Cabal-Version: >= 1.2+Version: 0.3+Cabal-Version: >= 1.10 Synopsis: Converts integers to lists of digits and back. Description: Converts integers to lists of digits and back. License: BSD3 License-File: LICENSE-Copyright: (c) 2009 Henry Bucklow+Copyright: (c) 2009-2016 Henry Bucklow, Charlie Harvey Author: Henry Bucklow Maintainer: henry@elsie.org.uk-Tested-With: GHC==6.12-Build-Depends: base >= 4 && < 5, QuickCheck-Exposed-Modules: Data.Digits-Hs-Source-Dirs: src-Extra-Source-Files: src/Tests.hs-GHC-Options: -Wall+Tested-With: GHC==7.10.3 +library+ hs-source-dirs: src+ exposed-modules: Data.Digits+ build-depends: base >= 4.7 && < 5+ , QuickCheck+ default-language: Haskell2010++test-suite digits-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ , src+ main-is: Tests.hs+ build-depends: base+ , digits+ , QuickCheck+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: mercurial+ location: https://bitbucket.org/sffubs/digits
src/Data/Digits.hs view
@@ -1,36 +1,61 @@-module Data.Digits (digits, digitsRev, unDigits, prop_digitsRoundTrip) where+module Data.Digits (mDigits, digits, mDigitsRev, digitsRev, unDigits, prop_digitsRoundTrip) where import Test.QuickCheck+import Data.Maybe (fromJust)+import Data.List (genericTake) --- | Returns the digits of a positive integer as a list, in reverse order.+-- | Returns the digits of a positive integer as a Maybe list, in reverse order+-- or Nothing if a negative base is given -- This is slightly more efficient than in forward order.-digitsRev :: Integral n+mDigitsRev :: Integral n+ => n -- ^ The base to use.+ -> n -- ^ The number to convert to digit form.+ -> Maybe [n] -- ^ Nothing or Just the digits of the number in list form, in reverse.+mDigitsRev base i = if base < 0+ then Nothing -- We do not support negative bases+ else Just $ dr base i+ where+ dr _ 0 = []+ dr b x = case base of+ 0 -> [0]+ 1 -> genericTake x $ repeat 1+ _ -> let (rest, lastDigit) = quotRem x b+ in lastDigit : dr b rest++-- | Returns the digits of a positive integer as a Maybe list.+-- or Nothing if a negative base is given+mDigits :: Integral n => n -- ^ The base to use. -> n -- ^ The number to convert to digit form.- -> [n] -- ^ The digits of the number in list form, in reverse.-digitsRev base i = case i of- 0 -> []- _ -> lastDigit : digitsRev base rest- where (rest, lastDigit) = quotRem i base+ -> Maybe [n] -- ^ Nothing or Just the digits of the number in list form+mDigits base i = reverse <$> mDigitsRev base i +-- | Returns the digits of a positive integer as a list, in reverse order.+-- Throws an error if given a negative base.+digitsRev :: Integral n+ => n -- ^ The base to use.+ -> n -- ^ The number to convert to digit from.+ -> [n] -- ^ The digits of the number in list from, in reverse.+digitsRev base = fromJust . mDigitsRev base+ -- | Returns the digits of a positive integer as a list.+-- Throws an error if given a negative base. digits :: Integral n- => n -- ^ The base to use (typically 10).- -> n -- ^ The number to convert to digit form.- -> [n] -- ^ The digits of the number in list form.+ => n -- ^ The base to use (typically 10).+ -> n -- ^ The number to convert to digit form.+ -> [n] -- ^ Either Nothing or the digits of the number in list form. digits base = reverse . digitsRev base -- | Takes a list of digits, and converts them back into a positive integer. unDigits :: Integral n- => n -- ^ The base to use.+ => n -- ^ The base to use. -> [n] -- ^ The digits of the number in list form.- -> n -- ^ The original number.+ -> n -- ^ The original number. unDigits base = foldl (\ a b -> a * base + b) 0 --- | unDigits . digits should be the identity, in any base.+-- | unDigits . digits should be the identity, in any positive base. prop_digitsRoundTrip :: Integer -- ^ The integer to test. -> Integer -- ^ The base to use. -> Property-prop_digitsRoundTrip i b = i > 0 ==> b > 1 ==> i == (unDigits b . digits b) i-+prop_digitsRoundTrip i b = i > 0 ==> b > 0 ==> i == (unDigits b . digits b) i
− src/Tests.hs
@@ -1,11 +0,0 @@-module Main where--import Data.Digits-import Text.Printf-import Test.QuickCheck--tests = [- ("checkDigitsRoundTrip", quickCheck prop_digitsRoundTrip)]--main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests-
+ test/Tests.hs view
@@ -0,0 +1,13 @@+module Main where++import Data.Digits+import Text.Printf+import Test.QuickCheck++tests :: [([Char], IO ())]+tests = [+ ("checkDigitsRoundTrip", quickCheck prop_digitsRoundTrip)]++main :: IO ()+main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests+