Skip to content

Commit

Permalink
Fixed #191. Non-breaking spaces in show output.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcclean committed Dec 18, 2018
1 parent a665277 commit 20cb46b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 115 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.3
-------------
* Breaking: Changed the `Show` instances for `UnitName`, `Unit`, `SQuantity` to use non-breaking spaces
within unit names and between values and their units.

1.2 (2018-11)
-------------
* Add `NoStarIsType` extension and import `Data.Kind.Type` for [GHC 8.6 compitbility](https://github.com/ghc-proposals/ghc-proposals/blob/05721788de9ab6538def68c3c2c9dec50c9f24a8/proposals/0020-no-type-in-type.rst). Abandon compatibility with GHC < 8.
Expand Down
8 changes: 4 additions & 4 deletions src/Numeric/Units/Dimensional.hs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Multiplication, division and powers apply to both units and quantities.
-- | Forms the reciprocal of a 'Quantity', which has the reciprocal dimension.
--
-- >>> recip $ 47 *~ hertz
-- 2.127659574468085e-2 s
-- 2.127659574468085e-2 s
recip :: (Fractional a) => Quantity d a -> Quantity (Recip d) a
recip = liftQ Prelude.recip

Expand Down Expand Up @@ -550,10 +550,10 @@ infixl 7 *~~, /~~
-- | The sum of all elements in a foldable structure.
--
-- >>> sum ([] :: [Mass Double])
-- 0.0 kg
-- 0.0 kg
--
-- >>> sum [12.4 *~ meter, 1 *~ foot]
-- 12.7048 m
-- 12.7048 m
sum :: (Num a, Foldable f) => f (Quantity d a) -> Quantity d a
sum = foldr (+) _0

Expand Down Expand Up @@ -716,7 +716,7 @@ If you feel your work requires this instance, it is provided as an orphan in "Nu
--
-- >>> let x = (37 :: Rational) *~ poundMass
-- >>> changeRep x :: Mass Double
-- 16.78291769 kg
-- 16.78291769 kg
changeRep :: (KnownVariant v, Real a, Fractional b) => Dimensional v d a -> Dimensional v d b
changeRep = dmap realToFrac

Expand Down
2 changes: 1 addition & 1 deletion src/Numeric/Units/Dimensional/Float.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ isNegativeZero = P.isNegativeZero . unQuantity
--
-- >>> let x = 3 *~ meter
-- >>> scaleFloat 3 x
-- 24.0 m
-- 24.0 m
scaleFloat :: RealFloat a => Int -> Quantity d a -> Quantity d a
scaleFloat x = Quantity . P.scaleFloat x . unQuantity

Expand Down
10 changes: 7 additions & 3 deletions src/Numeric/Units/Dimensional/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ We will conclude by providing a reasonable 'Show' instance for
quantities. The SI unit of the quantity is inferred
from its dimension.
-}
-- | Uses non-breaking spaces between the value and the unit, and within the unit name.
instance (KnownDimension d, E.KnownExactPi s, Show a, Real a) => Show (SQuantity s d a) where
show (Quantity x) | isExactOne s' = show x ++ showName n
| otherwise = "Quantity " ++ show x ++ " {- " ++ show q ++ " -}"
Expand All @@ -238,15 +239,18 @@ instance (KnownDimension d, E.KnownExactPi s, Show a, Real a) => Show (SQuantity

-- | Shows the value of a 'Quantity' expressed in a specified 'Unit' of the same 'Dimension'.
--
-- >>> showIn watt $ (37 *~ volt) * (4 *~ ampere)
-- "148.0 W"
-- Uses non-breaking spaces between the value and the unit, and within the unit name.
--
-- >>> putStrLn $ showIn watt $ (37 *~ volt) * (4 *~ ampere)
-- 148.0 W
showIn :: (Show a, Fractional a) => Unit m d a -> Quantity d a -> String
showIn (Unit n _ y) (Quantity x) = show (x / y) ++ (showName . Name.weaken $ n)

showName :: UnitName 'NonMetric -> String
showName n | n == nOne = ""
| otherwise = " " ++ show n
| otherwise = "\xA0" ++ show n

-- | Unit names are shown with non-breaking spaces.
instance (Show a) => Show (Unit m d a) where
show (Unit n e x) = "The unit " ++ show n ++ ", with value " ++ show e ++ " (or " ++ show x ++ ")"

Expand Down
Loading

0 comments on commit 20cb46b

Please sign in to comment.