mirror of
https://github.com/tfausak/witch.git
synced 2024-11-26 09:43:03 +03:00
Add instances for Complex
This commit is contained in:
parent
d9f159b7ad
commit
d7285e0a34
@ -5,6 +5,7 @@
|
||||
module Witch.Instances where
|
||||
|
||||
import qualified Data.Bits as Bits
|
||||
import qualified Data.Complex as Complex
|
||||
import qualified Data.Fixed as Fixed
|
||||
import qualified Data.Int as Int
|
||||
import qualified Data.List.NonEmpty as NonEmpty
|
||||
@ -455,7 +456,9 @@ instance Integral a => Cast.Cast a (Ratio.Ratio a) where
|
||||
cast = (Ratio.% 1)
|
||||
|
||||
instance Integral a => TryCast.TryCast (Ratio.Ratio a) a where
|
||||
tryCast = maybeTryCast $ \ s -> if Ratio.denominator s == 1 then Just $ Ratio.numerator s else Nothing
|
||||
tryCast = maybeTryCast $ \ s -> if Ratio.denominator s == 1
|
||||
then Just $ Ratio.numerator s
|
||||
else Nothing
|
||||
|
||||
-- Fixed
|
||||
|
||||
@ -465,6 +468,16 @@ instance Cast.Cast Integer (Fixed.Fixed a) where
|
||||
instance Cast.Cast (Fixed.Fixed a) Integer where
|
||||
cast (Fixed.MkFixed t) = t
|
||||
|
||||
-- Complex
|
||||
|
||||
instance Num a => Cast.Cast a (Complex.Complex a) where
|
||||
cast = (Complex.:+ 0)
|
||||
|
||||
instance (Eq a, Num a) => TryCast.TryCast (Complex.Complex a) a where
|
||||
tryCast = maybeTryCast $ \ s -> if Complex.imagPart s == 0
|
||||
then Just $ Complex.realPart s
|
||||
else Nothing
|
||||
|
||||
fromNonNegativeIntegral :: (Integral s, Num t) => s -> Maybe t
|
||||
fromNonNegativeIntegral x = if x < 0 then Nothing else Just $ fromIntegral x
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
{-# LANGUAGE TypeApplications #-}
|
||||
|
||||
import Data.Complex
|
||||
import Data.Fixed
|
||||
import Data.Int
|
||||
import Data.List.NonEmpty
|
||||
@ -801,6 +802,19 @@ main = runTestTTAndExit $ "Witch" ~:
|
||||
, cast @Deci @Integer 1 ~?= 10
|
||||
]
|
||||
|
||||
-- Complex
|
||||
|
||||
, "Cast a (Complex a)" ~:
|
||||
[ cast @Float @(Complex Float) 1 ~?= 1
|
||||
, cast @Double @(Complex Double) 1 ~?= 1
|
||||
]
|
||||
, "TryCast (Complex a) a" ~:
|
||||
[ tryCast @(Complex Float) @Float 1 ~?= Right 1
|
||||
, tryCast @(Complex Float) @Float (0 :+ 1) ~?= Left (TryCastException $ 0 :+ 1)
|
||||
, tryCast @(Complex Double) @Double 1 ~?= Right 1
|
||||
, tryCast @(Complex Double) @Double (0 :+ 1) ~?= Left (TryCastException $ 0 :+ 1)
|
||||
]
|
||||
|
||||
]
|
||||
|
||||
untested :: [Test]
|
||||
|
Loading…
Reference in New Issue
Block a user