Skip to content

Commit

Permalink
Merge pull request #6 from dustin/master
Browse files Browse the repository at this point in the history
Explicitly implement Semigroups for Monoids.
  • Loading branch information
PaulJohnson authored Apr 13, 2018
2 parents eb186f7 + c79b09d commit f1ac376
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/Geodetics/Ellipsoids.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ module Geodetics.Ellipsoids (
) where

import Data.Monoid
import Data.Monoid (Monoid)
import Data.Semigroup (Semigroup, (<>))
import Numeric.Units.Dimensional
import Numeric.Units.Dimensional.Prelude
import Prelude () -- Numeric instances.
Expand Down Expand Up @@ -110,12 +112,14 @@ data Helmert = Helmert {
helmertScale :: Dimensionless Double, -- ^ Parts per million
rX, rY, rZ :: Dimensionless Double } deriving (Eq, Show)

instance Semigroup Helmert where
h1 <> h2 = Helmert (cX h1 + cX h2) (cY h1 + cY h2) (cZ h1 + cZ h2)
(helmertScale h1 + helmertScale h2)
(rX h1 + rX h2) (rY h1 + rY h2) (rZ h1 + rZ h2)

instance Monoid Helmert where
mempty = Helmert (0 *~ meter) (0 *~ meter) (0 *~ meter) _0 _0 _0 _0
mappend h1 h2 = Helmert (cX h1 + cX h2) (cY h1 + cY h2) (cZ h1 + cZ h2)
(helmertScale h1 + helmertScale h2)
(rX h1 + rX h2) (rY h1 + rY h2) (rZ h1 + rZ h2)

mappend = (<>)

-- | The inverse of a Helmert transformation.
inverseHelmert :: Helmert -> Helmert
Expand Down
12 changes: 8 additions & 4 deletions src/Geodetics/Grid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module Geodetics.Grid (

import Data.Char
import Data.Function
import Data.Monoid
import Data.Monoid (Monoid)
import Data.Semigroup (Semigroup, (<>))
import Geodetics.Altitude
import Geodetics.Geodetic
import Numeric.Units.Dimensional.Prelude hiding ((.))
Expand Down Expand Up @@ -69,11 +70,14 @@ data GridOffset = GridOffset {
deltaEast, deltaNorth, deltaAltitude :: Length Double
} deriving (Eq, Show)

instance Semigroup GridOffset where
g1 <> g2 = GridOffset (deltaEast g1 + deltaEast g2)
(deltaNorth g1 + deltaNorth g2)
(deltaAltitude g1 + deltaAltitude g2)

instance Monoid GridOffset where
mempty = GridOffset _0 _0 _0
mappend g1 g2 = GridOffset (deltaEast g1 + deltaEast g2)
(deltaNorth g1 + deltaNorth g2)
(deltaAltitude g1 + deltaAltitude g2)
mappend = (<>)

-- | An offset defined by a distance and a bearing to the right of North.
--
Expand Down

0 comments on commit f1ac376

Please sign in to comment.