diff --git a/CHANGELOG.md b/CHANGELOG.md index d7888e1e..7aef659c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- `fromInt` Bugfixes: diff --git a/src/Data/Ring/Extra.purs b/src/Data/Ring/Extra.purs new file mode 100644 index 00000000..0ef1dfba --- /dev/null +++ b/src/Data/Ring/Extra.purs @@ -0,0 +1,12 @@ +module Data.Ring.Extra where + +import Data.Ord ((>=)) +import Data.Ring (class Ring, one, negate) +import Data.Monoid (power) +import Data.Monoid.Additive (Additive(..)) + +fromInt :: forall a. Ring a => Int -> a +fromInt x = + if x >= 0 + then (\(Additive i) -> i) (power (Additive one) x) + else negate (fromInt (negate x)) \ No newline at end of file diff --git a/test/Test/Main.purs b/test/Test/Main.purs index a3c0a806..085cebdd 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -5,6 +5,7 @@ import Data.HeytingAlgebra (ff, tt, implies) import Data.Ord (abs) import Test.Data.Generic.Rep (testGenericRep) import Test.Utils (AlmostEff, assert) +import Data.Ring.Extra (fromInt) main :: AlmostEff main = do @@ -15,6 +16,7 @@ main = do testIntDegree testRecordInstances testGenericRep + testFromInt foreign import testNumberShow :: (Number -> String) -> AlmostEff @@ -151,3 +153,9 @@ testRecordInstances = do assert "Record top" $ (top :: { a :: Boolean }).a == top + +testFromInt :: AlmostEff +testFromInt = do + assert "Zero" $ fromInt 0 == 0.0 + assert "Negative" $ fromInt (-1) == (-1.0) + assert "Positive" $ fromInt 1 == 1.0 \ No newline at end of file