digits 0.2 → 0.3.2
raw patch · 7 files changed
Files
- CHANGELOG.md +3/−0
- LICENSE +1/−1
- Setup.lhs +0/−7
- digits.cabal +32/−16
- src/Data/Digits.hs +40/−16
- src/Tests.hs +0/−11
- test/Tests.hs +13/−0
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+# 0.3.2++Updated for GHC 9.12.2 and current Cabal.
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2009, Henry Bucklow+Copyright (c) 2009-2025, Henry Bucklow, (c) 2016 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:
− Setup.lhs
@@ -1,7 +0,0 @@-#! /usr/bin/env runhaskell--> import Distribution.Simple-> import System.Cmd-> tests _ _ _ _ = system "runhaskell src/Tests.hs" >> return ()-> main = defaultMainWithHooks (simpleUserHooks {runTests = tests})-
digits.cabal view
@@ -1,19 +1,35 @@-Build-Type: Custom-Name: digits-Category: Data-Version: 0.2-Cabal-Version: >= 1.2-Synopsis: Converts integers to lists of digits and back.-Description: Converts integers to lists of digits and back.-License: BSD3+cabal-version: 3.8+name: digits+category: Data+version: 0.3.2+synopsis: Converts integers to lists of digits and back.+description: Converts integers to lists of digits and back. Supports arbitrary bases.+License: BSD-3-Clause License-File: LICENSE-Copyright: (c) 2009 Henry Bucklow+Copyright: (c) 2009-2025 Henry Bucklow, (c) 2016 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+Maintainer: opensource@elsie.org.uk+Tested-With: GHC==9.12.2+extra-doc-files: CHANGELOG.md +source-repository head+ type: git+ location: https://github.com/sffubs/digits.git++library+ hs-source-dirs: src+ exposed-modules: Data.Digits+ build-depends: base >= 4.7 && < 5+ , QuickCheck < 3+ default-language: Haskell2010+ ghc-options: -Wall++test-suite digits-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Tests.hs+ build-depends: base+ , digits+ , QuickCheck < 3+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010
src/Data/Digits.hs view
@@ -1,36 +1,60 @@-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 zero or 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 < 1+ then Nothing -- We do not support zero or negative bases+ else Just $ dr base i+ where+ dr _ 0 = []+ dr b x = case base of+ 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 zero or 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 zero or 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 zero or 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+