Skip to content

Commit 9ebee51

Browse files
committed
Initial commit
0 parents  commit 9ebee51

File tree

9 files changed

+468
-0
lines changed

9 files changed

+468
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
bower_components
3+
output

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# purescript-bigints
2+
3+
A library for calculations with arbitrary length integers.
4+
This is a simple wrapper around [BigInteger.js](https://github.com/peterolson/BigInteger.js)
5+
by [Peter Olson](https://github.com/peterolson).
6+
7+
## Module documentation
8+
9+
- [Data.BigInt](docs/Data/BigInt.md)

bower.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "purescript-bigints",
3+
"version": "0.1.0",
4+
"description": "Arbitrary length integers",
5+
"homepage": "https://github.com/sharkdp/purescript-bigints",
6+
"authors": [
7+
"David Peter <[email protected]>"
8+
],
9+
"ignore": [
10+
"**/.*",
11+
"node_modules",
12+
"bower_components",
13+
"output"
14+
],
15+
"dependencies": {
16+
"purescript-maybe": "^0.3.2"
17+
},
18+
"devDependencies": {
19+
"purescript-integers": "^0.2.1",
20+
"purescript-console": "^0.1.0",
21+
"purescript-assert": "^0.1.1",
22+
"purescript-quickcheck": "^0.7.0"
23+
}
24+
}

docs/Data/BigInt.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## Module Data.BigInt
2+
3+
#### `BigInt`
4+
5+
``` purescript
6+
data BigInt :: *
7+
```
8+
9+
An arbitrary length integer.
10+
11+
##### Instances
12+
``` purescript
13+
instance eqBigInt :: Eq BigInt
14+
instance ordBigInt :: Ord BigInt
15+
instance showBigInt :: Show BigInt
16+
instance semiringBigInt :: Semiring BigInt
17+
instance ringBigInt :: Ring BigInt
18+
instance moduloSemiringBigInt :: ModuloSemiring BigInt
19+
```
20+
21+
#### `fromInt`
22+
23+
``` purescript
24+
fromInt :: Int -> BigInt
25+
```
26+
27+
Convert an integer to a BigInt.
28+
29+
#### `toNumber`
30+
31+
``` purescript
32+
toNumber :: BigInt -> Number
33+
```
34+
35+
Converts a BigInt to a Number. Loses precision for numbers which are too
36+
large.
37+
38+
#### `pow`
39+
40+
``` purescript
41+
pow :: BigInt -> BigInt -> BigInt
42+
```
43+
44+
Exponentiation for BigInt. If the exponent is less than 0, `pow`
45+
returns 0. Also, `pow zero zero == one`.
46+
47+
#### `abs`
48+
49+
``` purescript
50+
abs :: BigInt -> BigInt
51+
```
52+
53+
The absolute value of a BigInt
54+
55+
#### `even`
56+
57+
``` purescript
58+
even :: BigInt -> Boolean
59+
```
60+
61+
Returns `true` if the number is even, `false` otherwise.
62+
63+
#### `odd`
64+
65+
``` purescript
66+
odd :: BigInt -> Boolean
67+
```
68+
69+
Returns `true` if the number is odd, `false` otherwise.
70+
71+
#### `prime`
72+
73+
``` purescript
74+
prime :: BigInt -> Boolean
75+
```
76+
77+
Returns `true` if the number is prime, `false` otherwise.
78+
79+
#### `fromString`
80+
81+
``` purescript
82+
fromString :: String -> Maybe BigInt
83+
```
84+
85+
Parse a string into a BigInt, assuming a decimal representation. Returns
86+
`Nothing` if the parse fails.
87+
88+
Examples:
89+
```purescript
90+
fromString "42"
91+
fromString "857981209301293808359384092830482"
92+
fromString "1e100"
93+
```
94+
95+
#### `fromBase`
96+
97+
``` purescript
98+
fromBase :: Int -> String -> Maybe BigInt
99+
```
100+
101+
Parse a string into a BigInt, assuming a representation in the given base.
102+
The letters "a-z" and "A-Z" will be interpreted as the numbers `10` to
103+
`36`. Returns `Nothing` if the parse fails.
104+
105+
```purescript
106+
fromBase 2 "100" == fromString "4"
107+
fromBase 16 "ff" == fromString "255"
108+
```
109+
110+
#### `toString`
111+
112+
``` purescript
113+
toString :: BigInt -> String
114+
```
115+
116+
A textual representation of the BigInt.
117+
118+

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "purescript-bigints",
3+
"version": "0.1.0",
4+
"dependencies": {
5+
"big-integer": "^1.5.5"
6+
}
7+
}

runtests.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
psc --ffi 'bower_components/purescript-*/src/**/*.js' \
6+
--ffi 'src/**/*.js' \
7+
'bower_components/purescript-*/src/**/*.purs' \
8+
'src/**/*.purs' \
9+
'test/**/*.purs'
10+
11+
rm -rf docs
12+
13+
psc-docs 'src/**/*.purs' \
14+
'bower_components/purescript-*/src/**/*.purs' \
15+
--docgen 'Data.BigInt:docs/Data/BigInt.md'
16+
17+
psc-bundle 'output/**/*.js' \
18+
'node_module/big-integer/BigInteger.min.js' \
19+
--module 'Test.Main' \
20+
--main 'Test.Main' \
21+
--output 'test.js'
22+
23+
node test.js
24+
25+
rm test.js

src/Data/BigInt.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// module Data.BigInt
2+
3+
var bigInt = require("big-integer");
4+
5+
exports["fromBase'"] = function(just) {
6+
return function(nothing) {
7+
return function(b) {
8+
return function(s) {
9+
try {
10+
var x = bigInt(s, b);
11+
return just(x);
12+
} catch (err) {
13+
return nothing;
14+
}
15+
};
16+
};
17+
};
18+
};
19+
20+
exports.fromInt = bigInt;
21+
22+
exports.toString = function(x) {
23+
return x.toString();
24+
};
25+
26+
exports.toNumber = function(x) {
27+
return x.toJSNumber();
28+
};
29+
30+
exports.biAdd = function(x) {
31+
return function(y) {
32+
return x.add(y);
33+
};
34+
};
35+
36+
exports.biMul = function(x) {
37+
return function(y) {
38+
return x.multiply(y);
39+
};
40+
};
41+
42+
exports.biSub = function(x) {
43+
return function(y) {
44+
return x.minus(y);
45+
};
46+
};
47+
48+
exports.biMod = function(x) {
49+
return function(y) {
50+
return x.mod(y);
51+
};
52+
};
53+
54+
exports.biDiv = function(x) {
55+
return function(y) {
56+
return x.divide(y);
57+
};
58+
};
59+
60+
exports.biEquals = function(x) {
61+
return function(y) {
62+
return x.equals(y);
63+
};
64+
};
65+
66+
exports.biCompare = function(x) {
67+
return function(y) {
68+
return x.compare(y);
69+
};
70+
};
71+
72+
exports.abs = function(x) {
73+
return x.abs();
74+
};
75+
76+
exports.even = function(x) {
77+
return x.isEven();
78+
};
79+
80+
exports.odd = function(x) {
81+
return x.isOdd();
82+
};
83+
84+
exports.prime = function(x) {
85+
return x.isPrime();
86+
};
87+
88+
exports.pow = function(x) {
89+
return function(y) {
90+
return x.pow(y);
91+
};
92+
};

src/Data/BigInt.purs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
module Data.BigInt
2+
( BigInt(..)
3+
, fromString
4+
, fromBase
5+
, fromInt
6+
, toString
7+
, abs
8+
, even
9+
, odd
10+
, prime
11+
, pow
12+
, toNumber
13+
) where
14+
15+
import Prelude
16+
17+
import Data.Maybe
18+
19+
-- | An arbitrary length integer.
20+
foreign import data BigInt :: *
21+
22+
-- | FFI wrapper to parse a String in a given base representation into a BigInt.
23+
foreign import fromBase' :: forall a. (a -> Maybe a)
24+
-> Maybe a
25+
-> Int
26+
-> String
27+
-> Maybe BigInt
28+
29+
-- | Convert an integer to a BigInt.
30+
foreign import fromInt :: Int -> BigInt
31+
32+
-- | Converts a BigInt to a Number. Loses precision for numbers which are too
33+
-- | large.
34+
foreign import toNumber :: BigInt -> Number
35+
36+
-- | Exponentiation for BigInt. If the exponent is less than 0, `pow`
37+
-- | returns 0. Also, `pow zero zero == one`.
38+
foreign import pow :: BigInt -> BigInt -> BigInt
39+
40+
-- | The absolute value of a BigInt
41+
foreign import abs :: BigInt -> BigInt
42+
43+
-- | Returns `true` if the number is even, `false` otherwise.
44+
foreign import even :: BigInt -> Boolean
45+
46+
-- | Returns `true` if the number is odd, `false` otherwise.
47+
foreign import odd :: BigInt -> Boolean
48+
49+
-- | Returns `true` if the number is prime, `false` otherwise.
50+
foreign import prime :: BigInt -> Boolean
51+
52+
-- | Parse a string into a BigInt, assuming a decimal representation. Returns
53+
-- | `Nothing` if the parse fails.
54+
-- |
55+
-- | Examples:
56+
-- | ```purescript
57+
-- | fromString "42"
58+
-- | fromString "857981209301293808359384092830482"
59+
-- | fromString "1e100"
60+
-- | ```
61+
fromString :: String -> Maybe BigInt
62+
fromString = fromBase 10
63+
64+
-- | Parse a string into a BigInt, assuming a representation in the given base.
65+
-- | The letters "a-z" and "A-Z" will be interpreted as the numbers `10` to
66+
-- | `36`. Returns `Nothing` if the parse fails.
67+
-- |
68+
-- | ```purescript
69+
-- | fromBase 2 "100" == fromString "4"
70+
-- | fromBase 16 "ff" == fromString "255"
71+
-- | ```
72+
fromBase :: Int -> String -> Maybe BigInt
73+
fromBase = fromBase' Just Nothing
74+
75+
foreign import biEquals :: BigInt -> BigInt -> Boolean
76+
77+
instance eqBigInt :: Eq BigInt where
78+
eq = biEquals
79+
80+
81+
foreign import biCompare :: BigInt -> BigInt -> Int
82+
83+
instance ordBigInt :: Ord BigInt where
84+
compare x y = case biCompare x y of
85+
1 -> GT
86+
0 -> EQ
87+
-1 -> LT
88+
89+
-- | A textual representation of the BigInt.
90+
foreign import toString :: BigInt -> String
91+
92+
instance showBigInt :: Show BigInt where
93+
show x = "fromString \"" ++ toString x ++ "\""
94+
95+
foreign import biAdd :: BigInt -> BigInt -> BigInt
96+
foreign import biMul :: BigInt -> BigInt -> BigInt
97+
98+
instance semiringBigInt :: Semiring BigInt where
99+
add = biAdd
100+
zero = fromInt 0
101+
mul = biMul
102+
one = fromInt 1
103+
104+
foreign import biSub :: BigInt -> BigInt -> BigInt
105+
106+
instance ringBigInt :: Ring BigInt where
107+
sub = biSub
108+
109+
foreign import biDiv :: BigInt -> BigInt -> BigInt
110+
foreign import biMod :: BigInt -> BigInt -> BigInt
111+
112+
instance moduloSemiringBigInt :: ModuloSemiring BigInt where
113+
div = biDiv
114+
mod = biMod

0 commit comments

Comments
 (0)