[Haskell-cafe] ANN: ByteString Nums

Jason Dusek jason.dusek at gmail.com
Sun Aug 23 03:26:31 EDT 2009

  This is a simple package for relatively careless parsing of
  numbers from ByteStrings. It works to parse out integer
  strings, floating point strings and hex strings.


  For integer strings, an initial sign is detected and non-digit
  characters are ignored:

Prelude> :load Data.ByteString.Nums.Careless.Int
*Data.ByteString.Nums.Careless.Int> let strings = ["121.3040", "1o2",
"-a1", "0xff", "-0x30"]
*Data.ByteString.Nums.Careless.Int> fmap (int . pack) strings :: [Int]
*Data.ByteString.Nums.Careless.Int> fmap (int . pack) strings :: [Double]

  For hexadecimal strings, no signs are recognized and
  characters that aren't hex digits are ignored. One consequence
  of this is that an initial `0x` is skipped.

Prelude> :load Data.ByteString.Nums.Careless.Hex
*Data.ByteString.Nums.Careless.Hex> let strings = ["121.3040", "1o2",
"-a1", "0xff", "-0x30"]
*Data.ByteString.Nums.Careless.Hex> fmap (hex . pack) strings :: [Int]

  The float parser recognizes the last group of digits behind a
  comma or period as the fractional part; all preceding digits
  are treated as the integral part. Naturally, signs are

Prelude> :load Data.ByteString.Nums.Careless.Float
*Data.ByteString.Nums.Careless.Float> let strings = ["121.3040",
"2.100,32", "1,o2", "-a1", "0xff", "-0x30"]
*Data.ByteString.Nums.Careless.Float> fmap (float . pack) strings :: [Double]
*Data.ByteString.Nums.Careless.Float> fmap (float . pack) strings :: [Rational]
[15163 % 125,52508 % 25,6 % 5,(-1) % 1,0 % 1,(-30) % 1]

  I have not applied any `SPECIALIZE` or `INLINE` directives at
  this time; it'd be great to have some insight in to optimizing
  collections of small functions like this library.

Jason Dusek

More information about the Haskell-Cafe mailing list