Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible fullSimplify #35

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Data/Quantity.purs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ module Data.Quantity

import Prelude

import Data.Array (notElem)
import Data.Either (Either(..))
import Data.Foldable (product, foldMap)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Number.Approximate (Fraction(..), eqRelative)

import Data.Units (DerivedUnit, toString, (.^), (./), unity, removePrefix)
import Data.Units.SI.Derived (radian) as SI
import Data.Units.SI.Derived (radian, steradian) as SI
import Data.Units.SI.Accepted (degree) as SI
import Data.Units as U
import Data.Decimal (Decimal, fromNumber, toNumber)
Expand Down Expand Up @@ -136,12 +137,16 @@ toStandard (num .*. du) =
case U.toStandardUnit du of
Tuple du' conversion → (conversion * num) ..* du'

-- | Attempt to simplify the unit of a quantity.
-- | Attempt to simplify the unit of a quantity, except angles.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be precise, the docstring should be something like:

Attempt to simplify the unit of a quantity. Quantities with units that be completely cancelled will be reduced to scalar values (except for angle-units)

.. and similar for the fullSimplify' function below.

fullSimplify ∷ Quantity → Quantity
fullSimplify q@(num .*. du) =
fullSimplify = fullSimplify' [SI.degree, SI.radian, SI.steradian]

-- | Attempt to simplify the unit of a quantity, except the given units.
fullSimplify' ∷ Array DerivedUnit → Quantity → Quantity
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to use this function, it should be exported(?)

fullSimplify' blacklist q@(num .*. du) =
case toScalar' q of
Right n →
if removePrefix du /= SI.degree && removePrefix du /= SI.radian
if removePrefix du `notElem` blacklist
then n .*. unity
else num ..* du
Left _ →
Expand Down