mirror of
https://github.com/tfausak/witch.git
synced 2024-11-23 00:06:27 +03:00
Add some lossy instances
This commit is contained in:
parent
108977a29b
commit
9ccfc80ede
@ -643,6 +643,9 @@ instance TryCast.TryCast Double Rational where
|
|||||||
tryCast = maybeTryCast $ \s ->
|
tryCast = maybeTryCast $ \s ->
|
||||||
if isNaN s || isInfinite s then Nothing else Just $ toRational s
|
if isNaN s || isInfinite s then Nothing else Just $ toRational s
|
||||||
|
|
||||||
|
instance Cast.Cast Double Float where
|
||||||
|
cast = realToFrac
|
||||||
|
|
||||||
-- Ratio
|
-- Ratio
|
||||||
|
|
||||||
instance Integral a => Cast.Cast a (Ratio.Ratio a) where
|
instance Integral a => Cast.Cast a (Ratio.Ratio a) where
|
||||||
@ -652,6 +655,12 @@ instance (Eq a, Num a) => TryCast.TryCast (Ratio.Ratio a) a where
|
|||||||
tryCast = maybeTryCast $ \s ->
|
tryCast = maybeTryCast $ \s ->
|
||||||
if Ratio.denominator s == 1 then Just $ Ratio.numerator s else Nothing
|
if Ratio.denominator s == 1 then Just $ Ratio.numerator s else Nothing
|
||||||
|
|
||||||
|
instance Cast.Cast Rational Float where
|
||||||
|
cast = fromRational
|
||||||
|
|
||||||
|
instance Cast.Cast Rational Double where
|
||||||
|
cast = fromRational
|
||||||
|
|
||||||
-- Fixed
|
-- Fixed
|
||||||
|
|
||||||
instance Cast.Cast Integer (Fixed.Fixed a) where
|
instance Cast.Cast Integer (Fixed.Fixed a) where
|
||||||
|
@ -1349,6 +1349,12 @@ main = Hspec.hspec . Hspec.describe "Witch" $ do
|
|||||||
test $ f (1 / 0) `Hspec.shouldSatisfy` Either.isLeft
|
test $ f (1 / 0) `Hspec.shouldSatisfy` Either.isLeft
|
||||||
test $ f (-1 / 0) `Hspec.shouldSatisfy` Either.isLeft
|
test $ f (-1 / 0) `Hspec.shouldSatisfy` Either.isLeft
|
||||||
|
|
||||||
|
Hspec.describe "Cast Double Float" $ do
|
||||||
|
let f = Witch.cast @Double @Float
|
||||||
|
test $ f 0 `Hspec.shouldBe` 0
|
||||||
|
test $ f 0.5 `Hspec.shouldBe` 0.5
|
||||||
|
test $ f (-0.5) `Hspec.shouldBe` (-0.5)
|
||||||
|
|
||||||
-- NonEmpty
|
-- NonEmpty
|
||||||
|
|
||||||
Hspec.describe "TryCast [a] (NonEmpty a)" $ do
|
Hspec.describe "TryCast [a] (NonEmpty a)" $ do
|
||||||
@ -1376,6 +1382,18 @@ main = Hspec.hspec . Hspec.describe "Witch" $ do
|
|||||||
test $ f 0 `Hspec.shouldBe` Right 0
|
test $ f 0 `Hspec.shouldBe` Right 0
|
||||||
test $ f 0.5 `Hspec.shouldSatisfy` Either.isLeft
|
test $ f 0.5 `Hspec.shouldSatisfy` Either.isLeft
|
||||||
|
|
||||||
|
Hspec.describe "Cast Rational Float" $ do
|
||||||
|
let f = Witch.cast @Rational @Float
|
||||||
|
test $ f 0 `Hspec.shouldBe` 0
|
||||||
|
test $ f 0.5 `Hspec.shouldBe` 0.5
|
||||||
|
test $ f (-0.5) `Hspec.shouldBe` (-0.5)
|
||||||
|
|
||||||
|
Hspec.describe "Cast Rational Double" $ do
|
||||||
|
let f = Witch.cast @Rational @Double
|
||||||
|
test $ f 0 `Hspec.shouldBe` 0
|
||||||
|
test $ f 0.5 `Hspec.shouldBe` 0.5
|
||||||
|
test $ f (-0.5) `Hspec.shouldBe` (-0.5)
|
||||||
|
|
||||||
-- Fixed
|
-- Fixed
|
||||||
|
|
||||||
Hspec.describe "Cast Integer (Fixed a)" $ do
|
Hspec.describe "Cast Integer (Fixed a)" $ do
|
||||||
|
Loading…
Reference in New Issue
Block a user