Skip to content

Commit bb87c9f

Browse files
committed
Added fromInt
1 parent 898f35a commit bb87c9f

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- `fromInt`
1011

1112
Bugfixes:
1213

src/Data/Ring/Extra.purs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Data.Ring.Extra where
2+
3+
import Data.Ord ((>=))
4+
import Data.Ring (class Ring, one, negate)
5+
import Data.Monoid (power)
6+
import Data.Monoid.Additive (Additive(..))
7+
8+
fromInt :: forall a. Ring a => Int -> a
9+
fromInt x =
10+
if x >= 0
11+
then (\(Additive i) -> i) (power (Additive one) x)
12+
else negate (fromInt (negate x))

test/Test/Main.purs

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Data.HeytingAlgebra (ff, tt, implies)
55
import Data.Ord (abs)
66
import Test.Data.Generic.Rep (testGenericRep)
77
import Test.Utils (AlmostEff, assert)
8+
import Data.Ring.Extra (fromInt)
89

910
main :: AlmostEff
1011
main = do
@@ -15,6 +16,7 @@ main = do
1516
testIntDegree
1617
testRecordInstances
1718
testGenericRep
19+
testFromInt
1820

1921
foreign import testNumberShow :: (Number -> String) -> AlmostEff
2022

@@ -151,3 +153,9 @@ testRecordInstances = do
151153
assert "Record top" $
152154
(top :: { a :: Boolean }).a
153155
== top
156+
157+
testFromInt :: AlmostEff
158+
testFromInt = do
159+
assert "Zero" $ fromInt 0 == 0.0
160+
assert "Negative" $ fromInt (-1) == (-1.0)
161+
assert "Positive" $ fromInt 1 == 1.0

0 commit comments

Comments
 (0)