Skip to content

Commit 950bce2

Browse files
authoredSep 24, 2017
Merge pull request #5 from Unisay/master
toBase :: Int -> BigInt -> String
2 parents 15b7274 + b6c2a21 commit 950bce2

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed
 

‎src/Data/BigInt.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ exports["fromBase'"] = function(just) {
1919

2020
exports.fromInt = bigInt;
2121

22-
exports.toString = function(x) {
23-
return x.toString();
22+
exports.toBase = function(base) {
23+
return function (x) {
24+
return x.toString(base);
25+
};
2426
};
2527

2628
exports.toNumber = function(x) {
@@ -124,4 +126,3 @@ exports.shr = function(x) {
124126
return x.shiftRight(n);
125127
};
126128
};
127-

‎src/Data/BigInt.purs

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Data.BigInt
55
, fromBase
66
, fromInt
77
, toString
8+
, toBase
89
, abs
910
, even
1011
, odd
@@ -112,7 +113,11 @@ instance ordBigInt :: Ord BigInt where
112113
_ -> LT
113114

114115
-- | A decimal representation of the `BigInt` as a `String`.
115-
foreign import toString :: BigInt -> String
116+
toString :: BigInt -> String
117+
toString = toBase 10
118+
119+
-- | A base N representation of the `BigInt` as a `String`.
120+
foreign import toBase :: Int -> BigInt -> String
116121

117122
instance showBigInt :: Show BigInt where
118123
show x = "fromString \"" <> toString x <> "\""

‎test/Main.purs

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Control.Monad.Eff.Console (CONSOLE, log)
55
import Control.Monad.Eff.Exception (EXCEPTION)
66
import Control.Monad.Eff.Random (RANDOM)
77
import Data.Array (filter, range)
8-
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString, toNumber, fromBase, toString, not, or, xor, and, shl, shr)
8+
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString, toNumber, fromBase, toBase, toString, not, or, xor, and, shl, shr)
99
import Data.Foldable (fold)
1010
import Data.Int as Int
1111
import Data.Maybe (Maybe(..), fromMaybe)
@@ -31,10 +31,10 @@ runSmallInt (SmallInt n) = n
3131
-- | Arbitrary instance for BigInt
3232
newtype TestBigInt = TestBigInt BigInt
3333
derive newtype instance eqTestBigInt :: Eq TestBigInt
34-
derive newtype instance ordTestBigInt :: Ord TestBigInt
34+
derive newtype instance ordTestBigInt :: Ord TestBigInt
3535
derive newtype instance semiringTestBigInt :: Semiring TestBigInt
3636
derive newtype instance ringTestBigInt :: Ring TestBigInt
37-
derive newtype instance commutativeRingTestBigInt :: CommutativeRing TestBigInt
37+
derive newtype instance commutativeRingTestBigInt :: CommutativeRing TestBigInt
3838
derive newtype instance euclideanRingTestBigInt :: EuclideanRing TestBigInt
3939

4040
instance arbitraryBigInt :: Arbitrary TestBigInt where
@@ -80,6 +80,11 @@ main = do
8080
assert $ fromBase 2 "100" == Just four
8181
assert $ fromBase 16 "ff" == fromString "255"
8282

83+
log "Rendering bigints as strings with a different base"
84+
assert $ toBase 2 four == "100"
85+
assert $ (toBase 16 <$> fromString "255") == Just "ff"
86+
assert $ toString (fromInt 12345) == "12345"
87+
8388
log "Conversions between String, Int and BigInt should not loose precision"
8489
quickCheck (\n -> fromString (show n) == Just (fromInt n))
8590
quickCheck (\n -> Int.toNumber n == toNumber (fromInt n))
@@ -124,12 +129,11 @@ main = do
124129
log "Shifting"
125130
assert $ shl two one == four
126131
assert $ shr two one == one
127-
132+
128133
let prxBigInt = Proxy Proxy TestBigInt
129134
Data.checkEq prxBigInt
130135
Data.checkOrd prxBigInt
131-
Data.checkSemiring prxBigInt
136+
Data.checkSemiring prxBigInt
132137
Data.checkRing prxBigInt
133-
-- Data.checkEuclideanRing prxBigInt
138+
-- Data.checkEuclideanRing prxBigInt
134139
Data.checkCommutativeRing prxBigInt
135-

0 commit comments

Comments
 (0)
Please sign in to comment.