From e07a86721db216894a2dc928a3572d31af01776a Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Tue, 17 Mar 2020 18:33:05 +0000 Subject: [PATCH 1/7] Add multiply function --- src/Int64.elm | 18 +++++++++++++++++- tests/TestInt64.elm | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Int64.elm b/src/Int64.elm index 780442e..57dcb68 100644 --- a/src/Int64.elm +++ b/src/Int64.elm @@ -1,6 +1,6 @@ module Int64 exposing ( Int64, fromInt, fromInt32s - , add, subtract + , add, subtract, multiply , and, or, xor, complement , shiftLeftBy, shiftRightZfBy, rotateLeftBy, rotateRightBy , signedCompare, unsignedCompare @@ -182,6 +182,22 @@ subtract (Int64 a b) (Int64 p q) = Int64 (Bitwise.shiftRightZfBy 0 higher) (Bitwise.shiftRightZfBy 0 lower) +multiply : Int64 -> Int64 -> Int64 +multiply a b = + let + one : Int64 + one = + fromInt 1 + step : Int64 -> Int64 -> Int64 -> Int64 + step x y z = + if z == one then + y + else + step x (add x y) (subtract z one) + in + step a a b + + -- BITWISE diff --git a/tests/TestInt64.elm b/tests/TestInt64.elm index 0e212bc..9978086 100644 --- a/tests/TestInt64.elm +++ b/tests/TestInt64.elm @@ -136,6 +136,10 @@ fuzzTests = \a b -> Int64.subtract (Int64.fromInt a) (Int64.fromInt b) |> Expect.equal (Int64.fromInt (a - b)) + , fuzz2 Fuzz.int Fuzz.int "multiplication" <| + \a b -> + Int64.multiply (Int64.fromInt a) (Int64.fromInt b) + |> Expect.equal (Int64.fromInt (a * b)) , fuzz Fuzz.int "from int to signed string" <| \a -> a From d6e65bdf818af2fc4af8249189e567c496b02af1 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Tue, 17 Mar 2020 11:40:05 -0700 Subject: [PATCH 2/7] Create elm.yml --- .github/workflows/elm.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/elm.yml diff --git a/.github/workflows/elm.yml b/.github/workflows/elm.yml new file mode 100644 index 0000000..e236edb --- /dev/null +++ b/.github/workflows/elm.yml @@ -0,0 +1,31 @@ +# This is a basic workflow to help you get started with Actions + +name: Elm + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + # Install Elm + - name: Setup Elm + uses: justgook/setup-elm@v1 + + # Run tests + - name: Run tests + run: elm-test From 474394ad4180ecbfb81ece3e0ccb5bd07fe8ac1e Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Tue, 17 Mar 2020 11:46:17 -0700 Subject: [PATCH 3/7] Update elm.yml --- .github/workflows/elm.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/elm.yml b/.github/workflows/elm.yml index e236edb..91c9954 100644 --- a/.github/workflows/elm.yml +++ b/.github/workflows/elm.yml @@ -1,14 +1,10 @@ # This is a basic workflow to help you get started with Actions -name: Elm +name: CI # Controls when the action will run. Triggers the workflow on push or pull request # events but only for the master branch -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] +on: [push] # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: From 5575276a3a496b59e5502f17f28118095bc05e02 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Sat, 18 Jul 2020 22:17:48 +0000 Subject: [PATCH 4/7] Revert "Update elm.yml" This reverts commit 474394ad4180ecbfb81ece3e0ccb5bd07fe8ac1e. --- .github/workflows/elm.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/elm.yml b/.github/workflows/elm.yml index 91c9954..e236edb 100644 --- a/.github/workflows/elm.yml +++ b/.github/workflows/elm.yml @@ -1,10 +1,14 @@ # This is a basic workflow to help you get started with Actions -name: CI +name: Elm # Controls when the action will run. Triggers the workflow on push or pull request # events but only for the master branch -on: [push] +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: From e83a5bf3bc9e56eee33983b2a244a7d593a5dede Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Sat, 18 Jul 2020 22:18:09 +0000 Subject: [PATCH 5/7] Revert "Create elm.yml" This reverts commit d6e65bdf818af2fc4af8249189e567c496b02af1. --- .github/workflows/elm.yml | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 .github/workflows/elm.yml diff --git a/.github/workflows/elm.yml b/.github/workflows/elm.yml deleted file mode 100644 index e236edb..0000000 --- a/.github/workflows/elm.yml +++ /dev/null @@ -1,31 +0,0 @@ -# This is a basic workflow to help you get started with Actions - -name: Elm - -# Controls when the action will run. Triggers the workflow on push or pull request -# events but only for the master branch -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "build" - build: - # The type of runner that the job will run on - runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v2 - - # Install Elm - - name: Setup Elm - uses: justgook/setup-elm@v1 - - # Run tests - - name: Run tests - run: elm-test From 40274ace7e22bb8038de41f0b4053c0b53310f58 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Sat, 18 Jul 2020 22:18:12 +0000 Subject: [PATCH 6/7] Revert "Add multiply function" This reverts commit e07a86721db216894a2dc928a3572d31af01776a. --- src/Int64.elm | 18 +----------------- tests/TestInt64.elm | 4 ---- 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/src/Int64.elm b/src/Int64.elm index 57dcb68..780442e 100644 --- a/src/Int64.elm +++ b/src/Int64.elm @@ -1,6 +1,6 @@ module Int64 exposing ( Int64, fromInt, fromInt32s - , add, subtract, multiply + , add, subtract , and, or, xor, complement , shiftLeftBy, shiftRightZfBy, rotateLeftBy, rotateRightBy , signedCompare, unsignedCompare @@ -182,22 +182,6 @@ subtract (Int64 a b) (Int64 p q) = Int64 (Bitwise.shiftRightZfBy 0 higher) (Bitwise.shiftRightZfBy 0 lower) -multiply : Int64 -> Int64 -> Int64 -multiply a b = - let - one : Int64 - one = - fromInt 1 - step : Int64 -> Int64 -> Int64 -> Int64 - step x y z = - if z == one then - y - else - step x (add x y) (subtract z one) - in - step a a b - - -- BITWISE diff --git a/tests/TestInt64.elm b/tests/TestInt64.elm index 0773d04..f9b75d8 100644 --- a/tests/TestInt64.elm +++ b/tests/TestInt64.elm @@ -142,10 +142,6 @@ fuzzTests = \a b -> Int64.subtract (Int64.fromInt a) (Int64.fromInt b) |> Expect.equal (Int64.fromInt (a - b)) - , fuzz2 Fuzz.int Fuzz.int "multiplication" <| - \a b -> - Int64.multiply (Int64.fromInt a) (Int64.fromInt b) - |> Expect.equal (Int64.fromInt (a * b)) , fuzz Fuzz.int "from int to signed string" <| \a -> a From 26fadb426fa31bc4fa2a02cdb118e7ca79c8cc19 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Sat, 18 Jul 2020 23:55:44 +0000 Subject: [PATCH 7/7] Add fromBitString --- src/Int64.elm | 65 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestInt64.elm | 9 +++++++ 2 files changed, 74 insertions(+) diff --git a/src/Int64.elm b/src/Int64.elm index 780442e..ccee6af 100644 --- a/src/Int64.elm +++ b/src/Int64.elm @@ -6,6 +6,7 @@ module Int64 exposing , signedCompare, unsignedCompare , toSignedString, toUnsignedString , toHex, toBitString + , fromBitString , decoder, encoder , toByteValues, toBits ) @@ -41,6 +42,11 @@ This is a low-level package focussed on speed. The 64-bit integers are represent @docs toHex, toBitString +## Conversion from String + +@docs fromBitString + + ## Conversion to Bytes @docs decoder, encoder @@ -493,6 +499,65 @@ toBitString input = "" +{- Create an `Int64` from a string of `0`s and `1`s in big-endian order. + + fromBitString "101" + --> fromInt 5 +-} +fromBitString : String -> Maybe Int64 +fromBitString string = + string + |> bitsFromString + |> Maybe.map fromBits + + +fromBits : List Bool -> Int64 +fromBits bits = + Int64 + (Bitwise.shiftRightZfBy 0 <| fromBits32 <| List.drop 32 bits) + (Bitwise.shiftRightZfBy 0 <| fromBits32 <| List.take 32 bits) + + +fromBits32 : List Bool -> Int +fromBits32 = + Tuple.second << List.foldl + (\bit (i, accum) -> + ( i + 1 + , if bit + then + Bitwise.or + accum + (Bitwise.shiftLeftBy i 1) + else + accum + ) + ) + (0, 0) + + +-- Lowest bit is first +bitsFromString : String -> Maybe (List Bool) +bitsFromString = + String.foldl + (\b maybeAccum -> + case maybeAccum of + Nothing -> + Nothing + + Just accum -> + case b of + '1' -> + Just <| True :: accum + + '0' -> + Just <| False :: accum + + _ -> + Nothing + ) + (Just []) + + {-| Interpret a `Int64` as an unsigned integer, and give its string representation toSignedString (fromInt 10) diff --git a/tests/TestInt64.elm b/tests/TestInt64.elm index f9b75d8..c835846 100644 --- a/tests/TestInt64.elm +++ b/tests/TestInt64.elm @@ -148,6 +148,15 @@ fuzzTests = |> Int64.fromInt |> Int64.toSignedString |> Expect.equal (String.fromInt a) + , fuzz Fuzz.int "binary string conversion" <| + \a -> + let + int64 = Int64.fromInt a + in + int64 + |> Int64.toBitString + |> Int64.fromBitString + |> Expect.equal (Just int64) , fuzz (Fuzz.map abs Fuzz.int) "from positive in to unsigned string" <| \a -> a