union-angle (empty) → 0.1.0.0
raw patch · 7 files changed
+220/−0 lines, 7 filesdep +basedep +union-anglesetup-changed
Dependencies added: base, union-angle
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +18/−0
- Setup.hs +2/−0
- src/Data/Angle.hs +115/−0
- test/Spec.hs +2/−0
- union-angle.cabal +50/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for union-angle++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2021++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,18 @@+# union-angle++Union type that include radian angle and degree angle.++```+> a = Radian pi+> a+Radian 3.141592653589793+> degree a+180.0+> Degree d = a+> d+180.0+> Degree 180+Degree 180+> it + Radian pi+Radian 6.283185307179586+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/Angle.hs view
@@ -0,0 +1,115 @@+{-# LANGUAGE BlockArguments, LambdaCase #-}+{-# LANGUAGE PatternSynonyms, ViewPatterns #-}+{-# OPTIONS_GHC -Wall -fno-warn-tabs #-}++module Data.Angle (Angle, pattern Radian, radian, pattern Degree, degree) where++import Control.Arrow (second)+import Text.Read (Lexeme(..), readPrec, step, parens, prec, lexP, (+++))++---------------------------------------------------------------------------++-- * DATA TYPE AND PATTERN+-- * INSTANCE DEFINITION++---------------------------------------------------------------------------+-- DATA TYPE AND PATTERN+---------------------------------------------------------------------------++data Angle f = Radian_ f | Degree_ f++-- ^ >>> Radian pi+-- Radian 3.141592653589793+-- >>> degree it+-- 180.0+--+-- >>> Degree 180+-- Degree 180.0+--+-- >>> Radian pi + Degree 180+-- Radian 6.283185307179586++{-# COMPLETE Radian #-}++pattern Radian :: Floating f => f -> Angle f+pattern Radian r <- (radian -> r) where Radian = Radian_++radian :: Floating f => Angle f -> f+radian = \case Radian_ r -> r; Degree_ d -> d * pi / 180++{-# COMPLETE Degree #-}++pattern Degree :: Floating f => f -> Angle f+pattern Degree d <- (degree -> d) where Degree = Degree_++degree :: Floating f => Angle f -> f+degree = \case Radian_ r -> r * 180 / pi; Degree_ d -> d++---------------------------------------------------------------------------+-- INSTANCE DEFINITION+---------------------------------------------------------------------------++instance Show f => Show (Angle f) where+ showsPrec d = \case+ Radian_ x -> showParen (d > 10)+ $ ("Radian " ++) . showsPrec 11 x+ Degree_ x -> showParen (d > 10)+ $ ("Degree " ++) . showsPrec 11 x++instance (Read f, Floating f) => Read (Angle f) where+ readPrec = parens $+ prec 10 do Ident "Radian" <- lexP; Radian_ <$> step readPrec+ ++++ prec 10 do Ident "Degree" <- lexP; Degree_ <$> step readPrec++instance (Eq f, Floating f) => Eq (Angle f) where+ Degree_ x == Degree_ y = x == y; Radian x == Radian y = x == y++instance (Ord f, Floating f) => Ord (Angle f) where+ Degree_ x <= Degree_ y = x <= y; Radian x <= Radian y = x <= y++instance Floating f => Num (Angle f) where+ Degree_ x + Degree_ y = Degree_ $ x + y+ Radian x + Radian y = Radian_ $ x + y+ Degree_ x * Degree_ y = Degree_ $ x * y * pi / 180+ Radian x * Radian y = Radian_ $ x * y+ negate = \case Radian_ x -> Radian_ $ - x; Degree_ x -> Degree_ $ - x+ abs = \case Radian_ x -> Radian_ $ abs x; Degree_ x -> Degree_ $ abs x+ signum = \case+ Radian_ x -> Radian_ $ signum x; Degree_ x -> Radian_ $ signum x+ fromInteger = Radian_ . fromInteger++instance Floating f => Fractional (Angle f) where+ recip = \case+ Radian_ x -> Radian_ $ recip x+ Degree_ x -> Degree_ $ recip x * (180 / pi) ^ (2 :: Int)+ fromRational = Radian_ . fromRational++instance (Floating f, Real f) => Real (Angle f) where+ toRational = toRational . radian++instance Floating f => Floating (Angle f) where+ pi = Radian_ pi+ exp = applyAngle exp+ log = applyAngle log+ sin = applyAngle sin+ cos = applyAngle cos+ asin = applyAngle asin+ acos = applyAngle acos+ atan = applyAngle atan+ sinh = applyAngle sinh+ cosh = applyAngle cosh+ asinh = applyAngle asinh+ acosh = applyAngle acosh+ atanh = applyAngle atanh++applyAngle :: Floating f => (f -> f) -> Angle f -> Angle f+applyAngle f = \case+ Radian_ x -> Radian_ $ f x+ Degree_ x -> Degree_ $ f (x * pi / 180) * 180 / pi++instance (Floating f, RealFrac f) => RealFrac (Angle f) where+ properFraction = \case+ Radian_ x -> Radian `second` properFraction x+ Degree_ x -> (Degree_ . (/ pi) . (* 180))+ `second` properFraction (x * pi / 180)
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"
+ union-angle.cabal view
@@ -0,0 +1,50 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name: union-angle+version: 0.1.0.0+synopsis: Union type that include radian angle and degree angle+description: Please see the README on GitHub at <https://github.com/githubuser/union-angle#readme>+category: Data+homepage: https://github.com/githubuser/union-angle#readme+bug-reports: https://github.com/githubuser/union-angle/issues+author: Yoshikuni Jujo+maintainer: yoshikuni.jujo.pc@gmail.com+copyright: 2021 Yoshikuni Jujo+license: BSD3+license-file: LICENSE+build-type: Simple+extra-source-files:+ README.md+ ChangeLog.md++source-repository head+ type: git+ location: https://github.com/githubuser/union-angle++library+ exposed-modules:+ Data.Angle+ other-modules:+ Paths_union_angle+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ default-language: Haskell2010++test-suite union-angle-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Paths_union_angle+ hs-source-dirs:+ test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.7 && <5+ , union-angle+ default-language: Haskell2010