On Sat, May 30, 2015 at 01:27:22PM -0700, Aaron Wolf wrote:
> So, I wrote:
> 
> pledgeOptions =
>     zip list $ map (show . fromRational . (0.1 *) . toRational) list
> 
> And Bryan helped me fix it but then there were type issues and the
> result that worked ended up being:
> 
>         listAsCents :: [Double]
>         listAsCents = map ((0.1 *) . fromIntegral) list
>         pledgeOptions :: [(Int64, String)]
>         pledgeOptions = zip list $ map (printf "%0.1f") listAsCents
> 
> This replaces show with printf (and the extra argument it needs) and has
> only one from-number type function instead of two, but it uses two maps,
> and has to specify etra types.
> 
> Is this really an improvement?

Sorry for leaving you hanging with a half solution. :P For the record,
the type sig for pledgeOptions isn't needed — it didn't fix the type
problem, listAsCents did, and it could be removed. 25% reduction!

Still, I don't really like printf. What else can we do?

If we pull in the text-format package, we can use Data.Text.Format and
do this:

    import Data.Text.Format
    import Data.Int (Int64)
    import Control.Arrow ((&&&))

    testData :: [Int64]
    testData = [1,2,4,10,11,99,100,102]

    formatted i = fixed decimals (toRational i / 10)
      where
        decimals = if mod i 10 == 0 then 0 else 1

    pairs = map (id &&& formatted) testData

Yeah, toRational is probably okay after all. The numbers are all
pretty small, and there is only one conversion to a float in the end,
so no difference. I think.
_______________________________________________
Dev mailing list
[email protected]
http://lists.snowdrift.coop/mailman/listinfo/dev

Reply via email to