diff --git a/.gitignore b/.gitignore index 7050558..332b6cf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /bower_components/ /node_modules/ /output/ +package-lock.json diff --git a/LICENSE b/LICENSE index 58b0299..311379c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,20 +1,26 @@ -The MIT License (MIT) +Copyright 2018 PureScript -Copyright (c) 2014 PureScript +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/bower.json b/bower.json index ddf4b87..c985098 100644 --- a/bower.json +++ b/bower.json @@ -1,8 +1,7 @@ { "name": "purescript-integers", "homepage": "https://github.com/purescript/purescript-integers", - "description": "Functions and bitwise operators for the Int numeric type", - "license": "MIT", + "license": "BSD-3-Clause", "repository": { "type": "git", "url": "git://github.com/purescript/purescript-integers.git" @@ -17,13 +16,14 @@ "package.json" ], "dependencies": { - "purescript-globals": "^3.0.0", - "purescript-math": "^2.0.0", - "purescript-maybe": "^3.0.0", - "purescript-partial": "^1.2.0" + "purescript-globals": "^4.0.0", + "purescript-math": "^2.1.1", + "purescript-maybe": "^4.0.0", + "purescript-prelude": "^4.0.0" }, "devDependencies": { - "purescript-assert": "^3.0.0", - "purescript-console": "^3.0.0" + "purescript-assert": "^4.0.0", + "purescript-console": "^4.0.0", + "purescript-partial": "^2.0.0" } } diff --git a/package.json b/package.json index 132cefc..657080a 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,12 @@ "scripts": { "clean": "rimraf output && rimraf .pulp-cache", "build": "eslint src && pulp build -- --censor-lib --strict", - "test": "pulp test" + "test": "pulp test --check-main-type Effect.Effect" }, "devDependencies": { - "eslint": "^3.17.1", - "pulp": "^10.0.4", - "purescript-psa": "^0.5.0-rc.1", - "rimraf": "^2.6.1" + "eslint": "^4.19.1", + "pulp": "^12.2.0", + "purescript-psa": "^0.6.0", + "rimraf": "^2.6.2" } } diff --git a/src/Data/Int.js b/src/Data/Int.js index 5e0531a..9900e5e 100644 --- a/src/Data/Int.js +++ b/src/Data/Int.js @@ -1,7 +1,5 @@ "use strict"; -// module Data.Int - exports.fromNumberImpl = function (just) { return function (nothing) { return function (n) { @@ -47,6 +45,20 @@ exports.toStringAs = function (radix) { }; }; + +exports.quot = function (x) { + return function (y) { + /* jshint bitwise: false */ + return x / y | 0; + }; +}; + +exports.rem = function (x) { + return function (y) { + return x % y; + }; +}; + exports.pow = function (x) { return function (y) { /* jshint bitwise: false */ diff --git a/src/Data/Int.purs b/src/Data/Int.purs index 97a348e..9d685ac 100644 --- a/src/Data/Int.purs +++ b/src/Data/Int.purs @@ -18,6 +18,8 @@ module Data.Int , parity , even , odd + , quot + , rem , pow ) where @@ -134,9 +136,7 @@ instance euclideanRingParity :: EuclideanRing Parity where mod _ _ = Even instance divisionRingParity :: DivisionRing Parity where - recip = id - -instance fieldParity :: Field Parity + recip = identity -- | Returns whether an `Int` is `Even` or `Odd`. -- | @@ -204,6 +204,41 @@ radix n | n >= 2 && n <= 36 = Just (Radix n) fromStringAs :: Radix -> String -> Maybe Int fromStringAs = fromStringAsImpl Just Nothing +-- | The `quot` function provides _truncating_ integer division (see the +-- | documentation for the `EuclideanRing` class). It is identical to `div` in +-- | the `EuclideanRing Int` instance if the dividend is positive, but will be +-- | slightly different if the dividend is negative. For example: +-- | +-- | ```purescript +-- | div 2 3 == 0 +-- | quot 2 3 == 0 +-- | +-- | div (-2) 3 == (-1) +-- | quot (-2) 3 == 0 +-- | +-- | div 2 (-3) == 0 +-- | quot 2 (-3) == 0 +-- | ``` +foreign import quot :: Int -> Int -> Int + +-- | The `rem` function provides the remainder after _truncating_ integer +-- | division (see the documentation for the `EuclideanRing` class). It is +-- | identical to `mod` in the `EuclideanRing Int` instance if the dividend is +-- | positive, but will be slightly different if the dividend is negative. For +-- | example: +-- | +-- | ```purescript +-- | mod 2 3 == 2 +-- | rem 2 3 == 2 +-- | +-- | mod (-2) 3 == 1 +-- | rem (-2) 3 == (-2) +-- | +-- | mod 2 (-3) == 2 +-- | rem 2 (-3) == 2 +-- | ``` +foreign import rem :: Int -> Int -> Int + -- | Raise an Int to the power of another Int. foreign import pow :: Int -> Int -> Int diff --git a/test/Test/Data/Int.purs b/test/Test/Data/Int.purs index 82b44e9..4f05928 100644 --- a/test/Test/Data/Int.purs +++ b/test/Test/Data/Int.purs @@ -2,19 +2,15 @@ module Test.Data.Int (testInt) where import Prelude -import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Console (CONSOLE, log) - -import Data.Int (parity, odd, even, fromString, floor, ceil, round, toNumber, fromNumber, fromStringAs, binary, octal, hexadecimal, radix, toStringAs, pow) +import Data.Int (binary, ceil, even, floor, fromNumber, fromString, fromStringAs, hexadecimal, octal, odd, parity, pow, quot, radix, rem, round, toNumber, toStringAs) import Data.Maybe (Maybe(..), fromJust) - +import Effect (Effect) +import Effect.Console (log) import Global (nan, infinity) - import Partial.Unsafe (unsafePartial) +import Test.Assert (assert) -import Test.Assert (ASSERT, assert) - -testInt :: Eff (console :: CONSOLE, assert :: ASSERT) Unit +testInt :: Effect Unit testInt = do log "fromNumber should coerce integer values" @@ -151,6 +147,28 @@ testInt = do go 3 8 go 49 171 + log "quotient/remainder law" + do + let + go a b = + let + q = quot a b + r = rem a b + msg = show a <> " / " <> show b <> ": " + in do + assert $ q * b + r == a + -- Check when dividend goes into divisor exactly + go 8 2 + go (-8) 2 + go 8 (-2) + go (-8) (-2) + + -- Check when dividend does not go into divisor exactly + go 2 3 + go (-2) 3 + go 2 (-3) + go (-2) (-3) + log "pow" assert $ pow 2 2 == 4 assert $ pow 5 3 == 125 diff --git a/test/Test/Main.purs b/test/Test/Main.purs index d8fcde8..a760437 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -2,11 +2,9 @@ module Test.Main where import Prelude -import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Console (CONSOLE) +import Effect (Effect) -import Test.Assert (ASSERT) import Test.Data.Int (testInt) -main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit +main :: Effect Unit main = testInt