From b2559b5f97b22e05a1a9139b7a56b0d10a60c6e3 Mon Sep 17 00:00:00 2001 From: Sam Gronblom Date: Wed, 18 Jan 2017 01:01:30 +0200 Subject: [PATCH 001/236] Make ArrayBuffer creation effectful - Remove the Writer effect from Typed.purs - Move down the WRITER and READER effects from DataView to ArrayBuffer - Simplify by combining WRITER and READER into a single ARRAYBUFFER effect - Updated tests to deal with the Eff values --- bower.json | 3 +- src/Data/ArrayBuffer/ArrayBuffer.js | 30 ++++--- src/Data/ArrayBuffer/ArrayBuffer.purs | 25 ++++-- src/Data/ArrayBuffer/DataView.js | 20 +++-- src/Data/ArrayBuffer/DataView.purs | 34 ++++---- src/Data/ArrayBuffer/Typed.js | 36 +++++--- src/Data/ArrayBuffer/Typed.purs | 44 +++++----- test/Main.purs | 113 ++++++++++++++------------ 8 files changed, 172 insertions(+), 133 deletions(-) diff --git a/bower.json b/bower.json index 0ecf0e0..02b2211 100644 --- a/bower.json +++ b/bower.json @@ -21,6 +21,7 @@ }, "devDependencies": { "purescript-debug": "^2.0.0", - "purescript-quickcheck": "^3.0.0" + "purescript-quickcheck": "^3.0.0", + "purescript-partial": "^1.2.0" } } diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 66e2870..e0dc5ce 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -3,26 +3,36 @@ // module Data.ArrayBuffer.ArrayBuffer exports.create = function(s) { - return new ArrayBuffer(s); + return function () { + return new ArrayBuffer(s); + } } exports.byteLength = function(a) { - return a.byteLength; + return function () { + return a.byteLength; + } } exports.sliceImpl = function(s, e, a) { - return a.slice(s,e); + return function () { + return a.slice(s, e); + } } exports.fromArray = function(s) { - return (new Uint8Array(s)).buffer; + return function() { + return (new Uint8Array(s)).buffer; + } } exports.fromString = function(s) { - var l = s.length; - var ab = new ArrayBuffer(l * 2); - var a = new Uint16Array(ab); - for (var i = 0; i < l; i++) - a[i] = s.charCodeAt(i); - return ab; + return function() { + var l = s.length; + var ab = new ArrayBuffer(l * 2); + var a = new Uint16Array(ab); + for (var i = 0; i < l; i++) + a[i] = s.charCodeAt(i); + return ab; + } } diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 9c5449f..6bb3898 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,24 +1,31 @@ -module Data.ArrayBuffer.ArrayBuffer where - +module Data.ArrayBuffer.ArrayBuffer( ARRAYBUFFER() + , create + , byteLength + , slice + , fromArray + , fromString + ) where + +import Control.Monad.Eff (Eff) import Data.Function.Uncurried (Fn3, runFn3) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) +foreign import data ARRAYBUFFER :: ! -- | Create an `ArrayBuffer` with the given capacity. -foreign import create :: ByteLength -> ArrayBuffer +foreign import create :: forall e. ByteLength -> Eff (arrayBuffer :: ARRAYBUFFER | e) ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. -foreign import byteLength :: ArrayBuffer -> ByteLength +foreign import byteLength :: forall e. ArrayBuffer -> Eff (arrayBuffer :: ARRAYBUFFER | e) ByteLength -foreign import sliceImpl :: Fn3 ByteOffset ByteOffset ArrayBuffer ArrayBuffer +foreign import sliceImpl :: forall e. Fn3 ByteOffset ByteOffset ArrayBuffer (Eff (arrayBuffer :: ARRAYBUFFER | e) ArrayBuffer) -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. -slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer +slice :: forall e. ByteOffset -> ByteOffset -> ArrayBuffer -> Eff (arrayBuffer :: ARRAYBUFFER | e) ArrayBuffer slice = runFn3 sliceImpl -- | Convert an array into an `ArrayBuffer` representation. -foreign import fromArray :: Array Number -> ArrayBuffer +foreign import fromArray :: forall e. Array Number -> Eff (arrayBuffer :: ARRAYBUFFER | e) ArrayBuffer -- | Convert a string into an `ArrayBuffer` representation. -foreign import fromString :: String -> ArrayBuffer - +foreign import fromString :: forall e. String -> Eff (arrayBuffer :: ARRAYBUFFER | e) ArrayBuffer diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 53282ae..f492fde 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -4,23 +4,33 @@ exports.whole = function(b) { - return new DataView(b); + return function() { + return new DataView(b); + } } exports.sliceImpl = function(just, nothing, s, l, b) { - return s + l <= b.byteLength? just(new DataView(b, s, l)) : nothing; + return function() { + return s + l <= b.byteLength ? just(new DataView(b, s, l)) : nothing; + } } exports.buffer = function(v) { - return v.buffer; + return function() { + return v.buffer; + } } exports.byteOffset = function(v) { - return v.byteOffset; + return function() { + return v.byteOffset; + } } exports.byteLength = function(v) { - return v.byteLength; + return function() { + return v.byteLength; + } } exports.getterImpl = function(just, nothing, s, l, e, v, o) { diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 99bb2f3..d5a30ad 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -1,6 +1,4 @@ -module Data.ArrayBuffer.DataView( READER() - , WRITER() - , whole +module Data.ArrayBuffer.DataView( whole , slice , buffer , byteOffset @@ -38,6 +36,7 @@ module Data.ArrayBuffer.DataView( READER() ) where import Prelude +import Data.ArrayBuffer.ArrayBuffer (ARRAYBUFFER) import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) import Data.Function.Uncurried (Fn5, Fn7, runFn5, runFn7) import Data.Maybe (Maybe(..)) @@ -45,43 +44,39 @@ import Control.Monad.Eff (Eff) import Data.UInt (UInt) -- | Type for all fetching functions. -type Getter r = forall e. DataView -> ByteOffset -> Eff (reader :: READER | e) (Maybe r) +type Getter r = forall e. DataView -> ByteOffset -> Eff (arrayBuffer :: ARRAYBUFFER | e) (Maybe r) -- | Type for all storing functions. -type Setter r = forall e. DataView -> r -> ByteOffset -> Eff (writer :: WRITER | e) Unit +type Setter r = forall e. DataView -> r -> ByteOffset -> Eff (arrayBuffer :: ARRAYBUFFER | e) Unit -- | View mapping the whole `ArrayBuffer`. -foreign import whole :: ArrayBuffer -> DataView +foreign import whole :: forall e. ArrayBuffer -> Eff (arrayBuffer :: ARRAYBUFFER | e) DataView -foreign import sliceImpl :: Fn5 (DataView -> Maybe DataView) (Maybe DataView) ByteOffset ByteLength ArrayBuffer (Maybe DataView) +foreign import sliceImpl :: forall e. Fn5 (DataView -> Maybe DataView) (Maybe DataView) ByteOffset ByteLength ArrayBuffer (Eff (arrayBuffer :: ARRAYBUFFER | e) (Maybe DataView)) -- | View mapping a region of the `ArrayBuffer`. -slice :: ByteOffset -> ByteLength -> ArrayBuffer -> Maybe DataView +slice :: forall e. ByteOffset -> ByteLength -> ArrayBuffer -> (Eff (arrayBuffer :: ARRAYBUFFER | e) (Maybe DataView)) slice = runFn5 sliceImpl Just Nothing -- | `ArrayBuffer` being mapped by the view. -foreign import buffer :: DataView -> ArrayBuffer +foreign import buffer :: forall e. DataView -> Eff (arrayBuffer :: ARRAYBUFFER | e) ArrayBuffer -- | Represents the offset of this view from the start of its `ArrayBuffer`. -foreign import byteOffset :: DataView -> ByteOffset +foreign import byteOffset :: forall e. DataView -> Eff (arrayBuffer :: ARRAYBUFFER | e) ByteOffset -- | Represents the length of this view. -foreign import byteLength :: DataView -> ByteLength +foreign import byteLength :: forall e. DataView -> Eff (arrayBuffer :: ARRAYBUFFER | e) ByteLength -foreign import data READER :: ! - type Endianness = Boolean -foreign import getterImpl :: forall e r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Eff (reader :: READER | e) (Maybe r)) +foreign import getterImpl :: forall e r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Eff (arrayBuffer :: ARRAYBUFFER | e) (Maybe r)) -getter :: forall e r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Eff (reader :: READER | e) (Maybe r) +getter :: forall e r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Eff (arrayBuffer :: ARRAYBUFFER | e) (Maybe r) getter = runFn7 getterImpl Just Nothing -foreign import data WRITER :: ! - -foreign import setter :: forall e r. String -> Endianness -> DataView -> r -> ByteOffset -> Eff (writer :: WRITER | e) Unit +foreign import setter :: forall e r. String -> Endianness -> DataView -> r -> ByteOffset -> Eff (arrayBuffer :: ARRAYBUFFER | e) Unit -- | Fetch int8 value at a certain index in a `DataView`. @@ -182,5 +177,4 @@ setFloat64be :: Setter Number setFloat64be = setter "setFloat64" false setFloat64le :: Setter Number -setFloat64le = setter "setFloat64" true - +setFloat64le = setter "setFloat64" true \ No newline at end of file diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 3c5a789..41d89ff 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -39,7 +39,9 @@ exports.asFloat64Array = function(v) { } exports.dataView = function(a) { - return a; + return function() { + return a; + } } exports.setImpl = function(ra, off, a) { @@ -49,25 +51,33 @@ exports.setImpl = function(ra, off, a) { } exports.unsafeAtImpl = function(a, i) { + return function() { return a[i]; + } } exports.hasIndexImpl = function(a, i) { - return i in a; + return function() { + return i in a; + } } exports.toArray = function(a) { - var l = a.length; - var ret = new Array(l); - for (var i = 0; i < l; i++) - ret[i] = a[i]; - return ret; + return function() { + var l = a.length; + var ret = new Array(l); + for (var i = 0; i < l; i++) + ret[i] = a[i]; + return ret; + } } exports.toIntArray = function(a) { - var l = a.length; - var ret = new Array(l); - for (var i = 0; i < l; i++) - ret[i] = a[i]|0; - return ret; -} + return function() { + var l = a.length; + var ret = new Array(l); + for (var i = 0; i < l; i++) + ret[i] = a[i] | 0; + return ret; + } +} \ No newline at end of file diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index eff78e5..89b3885 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -1,5 +1,4 @@ -module Data.ArrayBuffer.Typed( Writer() - , asInt8Array +module Data.ArrayBuffer.Typed( asInt8Array , asInt16Array , asInt32Array , asUint8Array @@ -17,18 +16,16 @@ module Data.ArrayBuffer.Typed( Writer() , toIntArray ) where -import Prelude (Unit, ($)) +import Control.Monad.Eff (Eff) +import Data.ArrayBuffer.ArrayBuffer (ARRAYBUFFER) import Data.ArrayBuffer.Types (ArrayView, ByteOffset, DataView, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) -import Control.Monad.Eff (Eff) - -foreign import data Writer :: ! +import Prelude (Unit, ($), bind, pure) -- | Create typed int8 array viewing the buffer mapped by the `DataView` foreign import asInt8Array :: DataView -> Int8Array - -- | Create typed int16 array viewing the buffer mapped by the `DataView` foreign import asInt16Array :: DataView -> Int16Array @@ -54,36 +51,39 @@ foreign import asFloat32Array :: DataView -> Float32Array foreign import asFloat64Array :: DataView -> Float64Array -- | Interpret typed array as a `DataView`. -foreign import dataView :: forall a. ArrayView a -> DataView +foreign import dataView :: forall a e. ArrayView a -> Eff (arrayBuffer :: ARRAYBUFFER | e) DataView -foreign import setImpl :: forall a e. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Eff (writer :: Writer | e) Unit) +foreign import setImpl :: forall a e. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Eff (arrayBuffer :: ARRAYBUFFER | e) Unit) -- | Stores multiple values in the last typed array, reading input values from ther first typed array. -set :: forall a e. ArrayView a -> ByteOffset -> ArrayView a -> Eff (writer :: Writer | e) Unit +set :: forall a e. ArrayView a -> ByteOffset -> ArrayView a -> Eff (arrayBuffer :: ARRAYBUFFER | e) Unit set = runFn3 setImpl -foreign import unsafeAtImpl :: forall a. Fn2 (ArrayView a) Int Number +foreign import unsafeAtImpl :: forall a e. Fn2 (ArrayView a) Int (Eff (arrayBuffer :: ARRAYBUFFER | e) Number) -- | Fetch element at index. -unsafeAt :: forall a. ArrayView a -> Int -> Number +unsafeAt :: forall a e. ArrayView a -> Int -> Eff (arrayBuffer :: ARRAYBUFFER | e) Number unsafeAt = runFn2 unsafeAtImpl -foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Int Boolean +foreign import hasIndexImpl :: forall a e. Fn2 (ArrayView a) Int (Eff (arrayBuffer :: ARRAYBUFFER | e) Boolean) -- | Determine if a certain index is valid. -hasIndex :: forall a. ArrayView a -> Int -> Boolean +hasIndex :: forall a e. ArrayView a -> Int -> Eff (arrayBuffer :: ARRAYBUFFER | e) Boolean hasIndex = runFn2 hasIndexImpl -- | Fetch element at index. -at :: forall a. ArrayView a -> Int -> Maybe Number -at a n = if a `hasIndex` n then - Just $ unsafeAt a n - else - Nothing +at :: forall a e. ArrayView a -> Int -> Eff (arrayBuffer :: ARRAYBUFFER | e) (Maybe Number) +at a n = do + indexExists <- a `hasIndex` n + if indexExists + then do + element <- unsafeAt a n + pure $ Just element + else + pure Nothing -- | Turn typed array into an array. -foreign import toArray :: forall a. ArrayView a -> Array Number +foreign import toArray :: forall a e. ArrayView a -> Eff (arrayBuffer :: ARRAYBUFFER | e) (Array Number) -- | Turn typed array into integer array. -foreign import toIntArray :: forall a. ArrayView a -> Array Int - +foreign import toIntArray :: forall a e. ArrayView a -> Eff (arrayBuffer :: ARRAYBUFFER | e) (Array Int) diff --git a/test/Main.purs b/test/Main.purs index 8e76c11..691601f 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,75 +1,82 @@ module Test.Main where import Prelude -import Data.Maybe (Maybe(..)) -import Test.QuickCheck (QC, quickCheck') +import Data.ArrayBuffer.ArrayBuffer as AB +import Data.ArrayBuffer.DataView as DV +import Data.ArrayBuffer.Typed as TA import Control.Monad.Eff (Eff) import Control.Monad.Eff.Console (CONSOLE) -import Control.Monad.Eff.Random (RANDOM) import Control.Monad.Eff.Exception (EXCEPTION) +import Control.Monad.Eff.Random (RANDOM) +import Data.Maybe (Maybe(..), fromJust) import Data.UInt (fromInt, pow) -import Data.ArrayBuffer.ArrayBuffer as AB -import Data.ArrayBuffer.DataView as DV -import Data.ArrayBuffer.Typed as TA +import Partial.Unsafe (unsafePartial) +import Test.QuickCheck (QC, quickCheck') + +assertEffEquals :: forall a e. Eq a => QC e a -> a -> QC e Unit +assertEffEquals computation expectedValue = do + actualValue <- computation + quickCheck' 1 $ actualValue == expectedValue main :: forall e . Eff ( console :: CONSOLE , random :: RANDOM , err :: EXCEPTION - , reader :: DV.READER - , writer :: DV.WRITER + , arrayBuffer :: AB.ARRAYBUFFER | e ) Unit main = do - let ab = AB.create 4 - assert $ AB.byteLength ab == 4 - assert $ AB.byteLength (AB.slice 0 2 ab) == 2 - assert $ AB.byteLength (AB.slice 2 4 ab) == 2 - assert $ AB.byteLength (AB.slice (-2) (-2) ab) == 0 - assert $ AB.byteLength (AB.slice (-2) (-1) ab) == 1 - assert $ (DV.byteLength <$> (DV.slice 0 2 ab)) == Just 2 - assert $ (DV.byteLength <$> (DV.slice 2 2 ab)) == Just 2 - let aab = AB.fromArray [1.0, 2.0, 3.0, 4.0] - assert $ AB.byteLength aab == 4 - let sab = AB.fromString "hola" - assert $ AB.byteLength sab == 8 - - let nab = AB.create 8 - let dv = DV.whole nab - assert $ AB.byteLength (DV.buffer dv) == 8 + ab <- AB.create 4 + assertEffEquals (AB.byteLength ab) 4 + assertEffEquals (AB.byteLength =<< (AB.slice 0 2 ab)) 2 + assertEffEquals (AB.byteLength =<< (AB.slice 2 4 ab)) 2 + assertEffEquals (AB.byteLength =<< (AB.slice (-2) (-2) ab)) 0 + assertEffEquals (AB.byteLength =<< (AB.slice (-2) (-1) ab)) 1 + maybeSliceFromStart <- DV.slice 0 2 ab + let sliceFromStart = unsafePartial $ fromJust maybeSliceFromStart + assertEffEquals (DV.byteLength sliceFromStart) 2 + maybeSliceFromSecond <- DV.slice 2 2 ab + let sliceFromSecond = unsafePartial $ fromJust maybeSliceFromSecond + assertEffEquals (DV.byteLength sliceFromSecond) 2 + aab <- AB.fromArray [1.0, 2.0, 3.0, 4.0] + assertEffEquals (AB.byteLength aab) 4 + sab <- AB.fromString "hola" + assertEffEquals (AB.byteLength sab) 8 - assert $ AB.byteLength (DV.buffer $ TA.dataView (TA.asInt8Array dv)) == 8 + nab <- AB.create 8 + dv <- DV.whole nab + nab' <- DV.buffer dv + assertEffEquals (AB.byteLength nab') 8 - assert $ (DV.byteLength <$> DV.slice 0 4 nab) == Just 4 - assert $ (DV.byteLength <$> DV.slice 0 40 nab) == Nothing + assertEffEquals (AB.byteLength =<< DV.buffer =<< TA.dataView (TA.asInt8Array dv)) 8 - assert $ do - let ab' = AB.fromArray [1.0,2.0,3.0,4.0] - let dv' = DV.whole ab' - let i8 = TA.asInt8Array dv' - ((Just 2.0) == i8 `TA.at` 1) && (Nothing == i8 `TA.at` 4) && (Nothing == i8 `TA.at` (-1)) + maybeNabSlice <- (DV.slice 0 4 nab) + let nabSlice = unsafePartial $ fromJust maybeNabSlice + assertEffEquals (DV.byteLength nabSlice) 4 + -- assertEffEquals (DV.slice 0 40 nab) Nothing - assert $ [1.0,2.0,3.0] == (TA.toArray $ TA.asInt8Array $ DV.whole $ AB.fromArray [1.0,2.0,3.0]) + fourElementArrayBuffer <- AB.fromArray [1.0, 2.0, 3.0, 4.0] + fourElementDataView <- DV.whole fourElementArrayBuffer + let i8 = TA.asInt8Array fourElementDataView + assertEffEquals (TA.at i8 1) (Just 2.0) + assertEffEquals (TA.at i8 4) Nothing + assertEffEquals (TA.at i8 (-1)) Nothing - assert =<< do - let ab' = AB.create 2 - let dv' = DV.whole ab' - DV.setUint8 dv' 123 0 - DV.setUint8 dv' 0 1 - leVal <- DV.getUint16le dv' 0 - beVal <- DV.getUint16be dv' 0 - pure ((Just 123 == leVal) && (Just 31488 == beVal)) + threeDataView <- AB.fromArray [1.0, 2.0, 3.0] >>= DV.whole + let threeInt8Array = TA.asInt8Array threeDataView + assertEffEquals (TA.toArray threeInt8Array) [1.0, 2.0, 3.0] - assert =<< do - let ab' = AB.create 4 - let dv' = DV.whole ab' - DV.setUint8 dv' 255 0 - DV.setUint8 dv' 255 1 - DV.setUint8 dv' 255 2 - DV.setUint8 dv' 255 3 - val <- DV.getUint32be dv' 0 - let expected = fromInt 2 `pow` fromInt 32 - fromInt 1 - pure (Just expected == val) + twoElementArrayBuffer <- AB.create 2 + twoElementDataView <- DV.whole twoElementArrayBuffer + DV.setUint8 twoElementDataView 123 0 + DV.setUint8 twoElementDataView 0 1 + assertEffEquals (DV.getUint16le twoElementDataView 0) (Just 123) + assertEffEquals (DV.getUint16be twoElementDataView 0) (Just 31488) -assert :: forall e. Boolean -> QC e Unit -assert = quickCheck' 1 + fourElementArrayBuffer' <- AB.create 4 + fourElementDataView' <- DV.whole fourElementArrayBuffer' + DV.setUint8 fourElementDataView' 255 0 + DV.setUint8 fourElementDataView' 255 1 + DV.setUint8 fourElementDataView' 255 2 + DV.setUint8 fourElementDataView' 255 3 + assertEffEquals (DV.getUint32be fourElementDataView' 0) (Just $ fromInt 2 `pow` fromInt 32 - fromInt 1) From cffe1c3fb76f605123df981b93a89fcf44e8462c Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 25 Jan 2017 23:10:01 +0100 Subject: [PATCH 002/236] Make unsigned setters/getters use UInt --- src/Data/ArrayBuffer/DataView.purs | 12 ++++++------ test/Main.purs | 17 +++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 02ed599..75a3d28 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -98,14 +98,14 @@ getInt32le :: Getter Int getInt32le = getter "getInt32" 4 true -- | Fetch uint8 value at a certain index in a `DataView`. -getUint8 :: Getter Int +getUint8 :: Getter UInt getUint8 = getter "getUint8" 1 false -- | Fetch uint16 value at a certain index in a `DataView`. -getUint16be :: Getter Int +getUint16be :: Getter UInt getUint16be = getter "getUint16" 2 false -getUint16le :: Getter Int +getUint16le :: Getter UInt getUint16le = getter "getUint16" 2 true -- | Fetch uint32 value at a certain index in a `DataView`. @@ -148,14 +148,14 @@ setInt32le :: Setter Int setInt32le = setter "setInt32" true -- | Store uint8 value at a certain index in a `DataView`. -setUint8 :: Setter Int +setUint8 :: Setter UInt setUint8 = setter "setUint8" false -- | Store uint16 value at a certain index in a `DataView`. -setUint16be :: Setter Int +setUint16be :: Setter UInt setUint16be = setter "setUint16" false -setUint16le :: Setter Int +setUint16le :: Setter UInt setUint16le = setter "setUint16" true -- | Store uint32 value at a certain index in a `DataView`. diff --git a/test/Main.purs b/test/Main.purs index e6c9e89..c6f4f72 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -55,16 +55,17 @@ main = do twoElementDataView <- do ab' <- AB.create 2 let dv = DV.whole ab' - DV.setUint8 dv 123 0 - DV.setUint8 dv 0 1 + DV.setUint8 dv (fromInt 123) 0 + DV.setUint8 dv (fromInt 0) 1 pure dv - assertEffEquals (Just 123) $ DV.getUint16le twoElementDataView 0 - assertEffEquals (Just 31488) $ DV.getUint16be twoElementDataView 0 + assertEffEquals (Just $ fromInt 123) $ DV.getUint16le twoElementDataView 0 + assertEffEquals (Just $ fromInt 31488) $ DV.getUint16be twoElementDataView 0 assertEffEquals (Just $ fromInt 2 `pow` fromInt 32 - fromInt 1) $ do ab' <- AB.create 4 let dv = DV.whole ab' - DV.setUint8 dv 255 0 - DV.setUint8 dv 255 1 - DV.setUint8 dv 255 2 - DV.setUint8 dv 255 3 + t = fromInt 255 + DV.setUint8 dv t 0 + DV.setUint8 dv t 1 + DV.setUint8 dv t 2 + DV.setUint8 dv t 3 DV.getUint32be dv 0 From fbf8b197a2388b5950a80b078276f920121dca89 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 2 Apr 2017 23:39:55 +0200 Subject: [PATCH 003/236] Updates for v0.11.1 --- bower.json | 12 ++++++------ src/Data/ArrayBuffer/ArrayBuffer.purs | 4 ++-- test/Main.purs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bower.json b/bower.json index 94d732b..e2bc3d3 100644 --- a/bower.json +++ b/bower.json @@ -13,15 +13,15 @@ "output" ], "dependencies": { - "purescript-functions": "^2.0.0", - "purescript-arraybuffer-types": "^0.2.0", - "purescript-maybe": "^2.0.0", - "purescript-eff": "^2.0.0", - "purescript-uint": "^0.3.0" + "purescript-functions": "^3.0.0", + "purescript-arraybuffer-types": "^1.0.0", + "purescript-maybe": "^3.0.0", + "purescript-eff": "^3.0.0", + "purescript-uint": "^1.0.0" }, "devDependencies": { "purescript-debug": "^2.0.0", - "purescript-quickcheck": "^3.0.0", + "purescript-quickcheck": "^4.0.0", "purescript-partial": "^1.2.0" } } diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 1bd5a0a..46e1f17 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -6,11 +6,11 @@ module Data.ArrayBuffer.ArrayBuffer( ARRAY_BUFFER() , fromString ) where -import Control.Monad.Eff (Eff) +import Control.Monad.Eff (kind Effect, Eff) import Data.Function.Uncurried (Fn3, runFn3) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) -foreign import data ARRAY_BUFFER :: ! +foreign import data ARRAY_BUFFER :: Effect -- | Create an `ArrayBuffer` with the given capacity. foreign import create :: forall e. ByteLength -> Eff (arrayBuffer :: ARRAY_BUFFER | e) ArrayBuffer diff --git a/test/Main.purs b/test/Main.purs index c6f4f72..d33ad9b 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -23,7 +23,7 @@ assertEquals expected actual = quickCheck' 1 $ expected == actual main :: forall e . Eff ( console :: CONSOLE , random :: RANDOM - , err :: EXCEPTION + , exception :: EXCEPTION , arrayBuffer :: AB.ARRAY_BUFFER | e ) Unit From 888afc3663d74457ccaacc27a5f1bfc49ac370a4 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Mon, 10 Apr 2017 23:23:20 +0200 Subject: [PATCH 004/236] Update dependencies --- bower.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index e2bc3d3..d25f2a5 100644 --- a/bower.json +++ b/bower.json @@ -17,10 +17,10 @@ "purescript-arraybuffer-types": "^1.0.0", "purescript-maybe": "^3.0.0", "purescript-eff": "^3.0.0", - "purescript-uint": "^1.0.0" + "purescript-uint": "^0.4.0" }, "devDependencies": { - "purescript-debug": "^2.0.0", + "purescript-debug": "^3.0.0", "purescript-quickcheck": "^4.0.0", "purescript-partial": "^1.2.0" } From a165f37bedaa696346f0deb0c8447feb03e6b8c6 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 18 Apr 2017 22:43:37 +0200 Subject: [PATCH 005/236] Update arraybuffer-types version --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index d25f2a5..c45ff70 100644 --- a/bower.json +++ b/bower.json @@ -14,7 +14,7 @@ ], "dependencies": { "purescript-functions": "^3.0.0", - "purescript-arraybuffer-types": "^1.0.0", + "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^3.0.0", "purescript-eff": "^3.0.0", "purescript-uint": "^0.4.0" From 0f23d585399b4365060b4c7926d2a66bb2c6f0cc Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 6 Oct 2017 10:39:31 +0200 Subject: [PATCH 006/236] Fix DV.slice bug --- bower.json | 2 +- src/Data/ArrayBuffer/DataView.js | 4 ++-- test/Main.purs | 21 +++++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bower.json b/bower.json index c45ff70..4db5a6c 100644 --- a/bower.json +++ b/bower.json @@ -17,7 +17,7 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^3.0.0", "purescript-eff": "^3.0.0", - "purescript-uint": "^0.4.0" + "purescript-uint": "^0.5.0" }, "devDependencies": { "purescript-debug": "^3.0.0", diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 75c0346..c1587c3 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -8,7 +8,7 @@ exports.whole = function(b) { } exports.sliceImpl = function(just, nothing, s, l, b) { - return s + l <= b.byteLength ? just(new DataView(b, s, l)) : nothing; + return ((s + l)>>>0) <= b.byteLength ? just(new DataView(b, s, l)) : nothing; } exports.buffer = function(v) { @@ -25,7 +25,7 @@ exports.byteLength = function(v) { exports.getterImpl = function(just, nothing, s, l, e, v, o) { return function() { - return (o + l) <= v.byteLength? just(v[s].call(v,o,e)) : nothing; + return ((o + l)>>>0) <= v.byteLength? just(v[s].call(v,o,e)) : nothing; }; } diff --git a/test/Main.purs b/test/Main.purs index d33ad9b..1b36bd6 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,24 +1,28 @@ module Test.Main where import Prelude -import Data.ArrayBuffer.ArrayBuffer as AB -import Data.ArrayBuffer.DataView as DV -import Data.ArrayBuffer.Typed as TA + import Control.Monad.Eff (Eff) import Control.Monad.Eff.Console (CONSOLE) import Control.Monad.Eff.Exception (EXCEPTION) import Control.Monad.Eff.Random (RANDOM) +import Data.ArrayBuffer.ArrayBuffer as AB +import Data.ArrayBuffer.DataView as DV +import Data.ArrayBuffer.Typed as TA import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) -import Test.QuickCheck (QC, quickCheck') +import Test.QuickCheck (QC, quickCheck', ()) -assertEffEquals :: forall a e. Eq a => a -> QC e a -> QC e Unit +assertEffEquals :: forall a e. Eq a => Show a => a -> QC e a -> QC e Unit assertEffEquals expectedValue computation = do actualValue <- computation - quickCheck' 1 $ actualValue == expectedValue + let msg = show expectedValue <> " /= " <> show actualValue + quickCheck' 1 $ actualValue == expectedValue msg -assertEquals :: forall a e. Eq a => a -> a -> QC e Unit -assertEquals expected actual = quickCheck' 1 $ expected == actual +assertEquals :: forall a e. Eq a => Show a => a -> a -> QC e Unit +assertEquals expected actual = do + let msg = show expected <> " /= " <> show actual + quickCheck' 1 $ expected == actual msg main :: forall e . Eff ( console :: CONSOLE @@ -35,6 +39,7 @@ main = do assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 2 4 ab4 assertEffEquals 0 $ pure <<< AB.byteLength =<< AB.slice (-2) (-2) ab4 assertEffEquals 1 $ pure <<< AB.byteLength =<< (AB.slice (-2) (-1) ab4) + assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 assertEffEquals 4 $ pure <<< AB.byteLength =<< AB.fromArray [1.0, 2.0, 3.0, 4.0] From 26c0eed84589d2c5e6dbf56ecda940a4cb7639da Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 6 Oct 2017 10:40:13 +0200 Subject: [PATCH 007/236] v5.0.1 From 9b102a8c62e9b0fcf5195e8571488a9744792c5e Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 7 Oct 2017 15:09:42 +0200 Subject: [PATCH 008/236] Removed effect for fromArray and fromString --- src/Data/ArrayBuffer/ArrayBuffer.js | 18 +++++++----------- src/Data/ArrayBuffer/ArrayBuffer.purs | 4 ++-- test/Main.purs | 8 ++++---- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 402f947..e94f14f 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -19,18 +19,14 @@ exports.sliceImpl = function(s, e, a) { } exports.fromArray = function(s) { - return function() { - return (new Uint8Array(s)).buffer; - }; + return (new Uint8Array(s)).buffer; } exports.fromString = function(s) { - return function() { - var l = s.length; - var ab = new ArrayBuffer(l * 2); - var a = new Uint16Array(ab); - for (var i = 0; i < l; i++) - a[i] = s.charCodeAt(i); - return ab; - }; + var l = s.length; + var ab = new ArrayBuffer(l * 2); + var a = new Uint16Array(ab); + for (var i = 0; i < l; i++) + a[i] = s.charCodeAt(i); + return a.buffer; } diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 46e1f17..c513c6b 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -25,8 +25,8 @@ slice :: forall e. ByteOffset -> ByteOffset -> ArrayBuffer -> Eff (arrayBuffer : slice = runFn3 sliceImpl -- | Convert an array into an `ArrayBuffer` representation. -foreign import fromArray :: forall e. Array Number -> Eff (arrayBuffer :: ARRAY_BUFFER | e) ArrayBuffer +foreign import fromArray :: Array Number -> ArrayBuffer -- | Convert a string into an `ArrayBuffer` representation. -foreign import fromString :: forall e. String -> Eff (arrayBuffer :: ARRAY_BUFFER | e) ArrayBuffer +foreign import fromString :: String -> ArrayBuffer diff --git a/test/Main.purs b/test/Main.purs index 1b36bd6..fc00d23 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -42,20 +42,20 @@ main = do assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 - assertEffEquals 4 $ pure <<< AB.byteLength =<< AB.fromArray [1.0, 2.0, 3.0, 4.0] - assertEffEquals 8 $ pure <<< AB.byteLength =<< AB.fromString "hola" + assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] + assertEquals 8 $ AB.byteLength $ AB.fromString "hola" assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 assertEquals (Just 8) $ DV.byteLength <$> DV.slice 0 8 ab8 assertEquals true $ isNothing $ DV.slice 0 40 ab8 - fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole =<< AB.fromArray [1.0, 2.0, 3.0, 4.0] + fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0, 4.0] assertEffEquals (Just 2.0) $ TA.at fourElementInt8Array 1 assertEffEquals Nothing $ TA.at fourElementInt8Array 4 assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) - assertEffEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole =<< AB.fromArray [1.0, 2.0, 3.0] + assertEffEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] twoElementDataView <- do ab' <- AB.create 2 From d7886bb6fc3b0dde93596ea69b304036d73118cb Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 7 Oct 2017 15:15:05 +0200 Subject: [PATCH 009/236] Remove effect from toArray and toIntArray --- src/Data/ArrayBuffer/Typed.js | 24 ++++++++++-------------- src/Data/ArrayBuffer/Typed.purs | 4 ++-- test/Main.purs | 2 +- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 7c29ec8..d1d213a 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -59,21 +59,17 @@ exports.hasIndexImpl = function(a, i) { } exports.toArray = function(a) { - return function() { - var l = a.length; - var ret = new Array(l); - for (var i = 0; i < l; i++) - ret[i] = a[i]; - return ret; - }; + var l = a.length; + var ret = new Array(l); + for (var i = 0; i < l; i++) + ret[i] = a[i]; + return ret; } exports.toIntArray = function(a) { - return function() { - var l = a.length; - var ret = new Array(l); - for (var i = 0; i < l; i++) - ret[i] = a[i] | 0; - return ret; - }; + var l = a.length; + var ret = new Array(l); + for (var i = 0; i < l; i++) + ret[i] = a[i] | 0; + return ret; } diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 464dd76..99dbdc8 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -82,8 +82,8 @@ at a n = do pure Nothing -- | Turn typed array into an array. -foreign import toArray :: forall a e. ArrayView a -> Eff (arrayBuffer :: ARRAY_BUFFER | e) (Array Number) +foreign import toArray :: forall a. ArrayView a -> Array Number -- | Turn typed array into integer array. -foreign import toIntArray :: forall a e. ArrayView a -> Eff (arrayBuffer :: ARRAY_BUFFER | e) (Array Int) +foreign import toIntArray :: forall a. ArrayView a -> Array Int diff --git a/test/Main.purs b/test/Main.purs index fc00d23..1f82b45 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -55,7 +55,7 @@ main = do assertEffEquals Nothing $ TA.at fourElementInt8Array 4 assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) - assertEffEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] + assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] twoElementDataView <- do ab' <- AB.create 2 From 1a93289c5a99bc3c1ed9669ec927d2cd32456261 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 7 Oct 2017 15:29:49 +0200 Subject: [PATCH 010/236] v6.0.0 From fb3bff257766993423eec0a7bfd74492c761202f Mon Sep 17 00:00:00 2001 From: Alex Kelley Date: Sat, 2 Jun 2018 12:01:05 -0700 Subject: [PATCH 011/236] 0.12 compiler support --- bower.json | 14 +++++++------- src/Data/ArrayBuffer/ArrayBuffer.purs | 21 +++++++++------------ src/Data/ArrayBuffer/DataView.purs | 14 +++++++------- src/Data/ArrayBuffer/Typed.purs | 13 ++++++------- test/Main.purs | 19 +++++-------------- 5 files changed, 34 insertions(+), 47 deletions(-) diff --git a/bower.json b/bower.json index 4db5a6c..1372215 100644 --- a/bower.json +++ b/bower.json @@ -13,15 +13,15 @@ "output" ], "dependencies": { - "purescript-functions": "^3.0.0", + "purescript-functions": "^4.0.0", "purescript-arraybuffer-types": "^2.0.0", - "purescript-maybe": "^3.0.0", - "purescript-eff": "^3.0.0", - "purescript-uint": "^0.5.0" + "purescript-maybe": "^4.0.0", + "purescript-effect": "^2.0.0", + "purescript-uint": "adkelley/purescript-uint#compiler/0.12" }, "devDependencies": { - "purescript-debug": "^3.0.0", - "purescript-quickcheck": "^4.0.0", - "purescript-partial": "^1.2.0" + "purescript-debug": "^4.0.0", + "purescript-quickcheck": "^5.0.0", + "purescript-partial": "^2.0.0" } } diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index c513c6b..97c47dc 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,27 +1,24 @@ -module Data.ArrayBuffer.ArrayBuffer( ARRAY_BUFFER() - , create - , byteLength - , slice - , fromArray - , fromString +module Data.ArrayBuffer.ArrayBuffer ( create + , byteLength + , slice + , fromArray + , fromString ) where -import Control.Monad.Eff (kind Effect, Eff) +import Effect (Effect) import Data.Function.Uncurried (Fn3, runFn3) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) -foreign import data ARRAY_BUFFER :: Effect - -- | Create an `ArrayBuffer` with the given capacity. -foreign import create :: forall e. ByteLength -> Eff (arrayBuffer :: ARRAY_BUFFER | e) ArrayBuffer +foreign import create :: ByteLength -> Effect ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength -foreign import sliceImpl :: forall e. Fn3 ByteOffset ByteOffset ArrayBuffer (Eff (arrayBuffer :: ARRAY_BUFFER | e) ArrayBuffer) +foreign import sliceImpl :: Fn3 ByteOffset ByteOffset ArrayBuffer (Effect ArrayBuffer) -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. -slice :: forall e. ByteOffset -> ByteOffset -> ArrayBuffer -> Eff (arrayBuffer :: ARRAY_BUFFER | e) ArrayBuffer +slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> Effect ArrayBuffer slice = runFn3 sliceImpl -- | Convert an array into an `ArrayBuffer` representation. diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 75a3d28..785d1ab 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -36,18 +36,18 @@ module Data.ArrayBuffer.DataView( whole ) where import Prelude -import Data.ArrayBuffer.ArrayBuffer (ARRAY_BUFFER) +--import Data.ArrayBuffer.ArrayBuffer (ARRAY_BUFFER) import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) import Data.Function.Uncurried (Fn5, Fn7, runFn5, runFn7) import Data.Maybe (Maybe(..)) -import Control.Monad.Eff (Eff) +import Effect (Effect) import Data.UInt (UInt) -- | Type for all fetching functions. -type Getter r = forall e. DataView -> ByteOffset -> Eff (arrayBuffer :: ARRAY_BUFFER | e) (Maybe r) +type Getter r = DataView -> ByteOffset -> Effect (Maybe r) -- | Type for all storing functions. -type Setter r = forall e. DataView -> r -> ByteOffset -> Eff (arrayBuffer :: ARRAY_BUFFER | e) Unit +type Setter r = DataView -> r -> ByteOffset -> Effect Unit -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView @@ -70,13 +70,13 @@ foreign import byteLength :: DataView -> ByteLength type Endianness = Boolean -foreign import getterImpl :: forall e r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Eff (arrayBuffer :: ARRAY_BUFFER | e) (Maybe r)) +foreign import getterImpl :: ∀ r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Effect (Maybe r)) -getter :: forall e r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Eff (arrayBuffer :: ARRAY_BUFFER | e) (Maybe r) +getter :: ∀ r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Effect (Maybe r) getter = runFn7 getterImpl Just Nothing -foreign import setter :: forall e r. String -> Endianness -> DataView -> r -> ByteOffset -> Eff (arrayBuffer :: ARRAY_BUFFER | e) Unit +foreign import setter :: ∀ r. String -> Endianness -> DataView -> r -> ByteOffset -> Effect Unit -- | Fetch int8 value at a certain index in a `DataView`. diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 99dbdc8..4f202a2 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -17,8 +17,7 @@ module Data.ArrayBuffer.Typed( asInt8Array ) where import Prelude -import Control.Monad.Eff (Eff) -import Data.ArrayBuffer.ArrayBuffer (ARRAY_BUFFER) +import Effect (Effect) import Data.ArrayBuffer.Types (ArrayView, ByteOffset, DataView, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) @@ -53,16 +52,16 @@ foreign import asFloat64Array :: DataView -> Float64Array -- | Interpret typed array as a `DataView`. foreign import dataView :: forall a. ArrayView a -> DataView -foreign import setImpl :: forall a e. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Eff (arrayBuffer :: ARRAY_BUFFER | e) Unit) +foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit) -- | Stores multiple values in the last typed array, reading input values from ther first typed array. -set :: forall a e. ArrayView a -> ByteOffset -> ArrayView a -> Eff (arrayBuffer :: ARRAY_BUFFER | e) Unit +set :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit set = runFn3 setImpl -foreign import unsafeAtImpl :: forall a e. Fn2 (ArrayView a) Int (Eff (arrayBuffer :: ARRAY_BUFFER | e) Number) +foreign import unsafeAtImpl :: forall a. Fn2 (ArrayView a) Int (Effect Number) -- | Fetch element at index. -unsafeAt :: forall a e. ArrayView a -> Int -> Eff (arrayBuffer :: ARRAY_BUFFER | e) Number +unsafeAt :: forall a. ArrayView a -> Int -> Effect Number unsafeAt = runFn2 unsafeAtImpl foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Int Boolean @@ -72,7 +71,7 @@ hasIndex :: forall a. ArrayView a -> Int -> Boolean hasIndex = runFn2 hasIndexImpl -- | Fetch element at index. -at :: forall a e. ArrayView a -> Int -> Eff (arrayBuffer :: ARRAY_BUFFER | e) (Maybe Number) +at :: forall a. ArrayView a -> Int -> Effect (Maybe Number) at a n = do if a `hasIndex` n then do diff --git a/test/Main.purs b/test/Main.purs index 1f82b45..592f5d0 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,35 +2,26 @@ module Test.Main where import Prelude -import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Console (CONSOLE) -import Control.Monad.Eff.Exception (EXCEPTION) -import Control.Monad.Eff.Random (RANDOM) +import Effect (Effect) import Data.ArrayBuffer.ArrayBuffer as AB import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.Typed as TA import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) -import Test.QuickCheck (QC, quickCheck', ()) +import Test.QuickCheck (quickCheck', ()) -assertEffEquals :: forall a e. Eq a => Show a => a -> QC e a -> QC e Unit +assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit assertEffEquals expectedValue computation = do actualValue <- computation let msg = show expectedValue <> " /= " <> show actualValue quickCheck' 1 $ actualValue == expectedValue msg -assertEquals :: forall a e. Eq a => Show a => a -> a -> QC e Unit +assertEquals :: forall a. Eq a => Show a => a -> a -> Effect Unit assertEquals expected actual = do let msg = show expected <> " /= " <> show actual quickCheck' 1 $ expected == actual msg -main :: forall e - . Eff ( console :: CONSOLE - , random :: RANDOM - , exception :: EXCEPTION - , arrayBuffer :: AB.ARRAY_BUFFER - | e ) - Unit +main :: Effect Unit main = do ab4 <- AB.create 4 ab8 <- AB.create 8 From 9ea2793dc36075be0d89744c89f85a6578c9bc35 Mon Sep 17 00:00:00 2001 From: Alex Kelley Date: Sun, 3 Jun 2018 11:28:37 -0700 Subject: [PATCH 012/236] bower:purescript-uint 4.0.0 --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 1372215..da333d3 100644 --- a/bower.json +++ b/bower.json @@ -17,7 +17,7 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "adkelley/purescript-uint#compiler/0.12" + "purescript-uint": "^4.0.0" }, "devDependencies": { "purescript-debug": "^4.0.0", From 9d62fca6db9d64a25fcc440ed1998c80566ba7b1 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 6 Jun 2018 22:06:04 +0200 Subject: [PATCH 013/236] Uniform forall usage --- src/Data/ArrayBuffer/DataView.purs | 6 +++--- src/Data/ArrayBuffer/Typed.purs | 1 - test/Main.purs | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 785d1ab..6206f2d 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -70,13 +70,13 @@ foreign import byteLength :: DataView -> ByteLength type Endianness = Boolean -foreign import getterImpl :: ∀ r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Effect (Maybe r)) +foreign import getterImpl :: forall r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Effect (Maybe r)) -getter :: ∀ r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Effect (Maybe r) +getter :: forall r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Effect (Maybe r) getter = runFn7 getterImpl Just Nothing -foreign import setter :: ∀ r. String -> Endianness -> DataView -> r -> ByteOffset -> Effect Unit +foreign import setter :: forall r. String -> Endianness -> DataView -> r -> ByteOffset -> Effect Unit -- | Fetch int8 value at a certain index in a `DataView`. diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 4f202a2..fbfbdc5 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -85,4 +85,3 @@ foreign import toArray :: forall a. ArrayView a -> Array Number -- | Turn typed array into integer array. foreign import toIntArray :: forall a. ArrayView a -> Array Int - diff --git a/test/Main.purs b/test/Main.purs index 592f5d0..166ee86 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -30,7 +30,7 @@ main = do assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 2 4 ab4 assertEffEquals 0 $ pure <<< AB.byteLength =<< AB.slice (-2) (-2) ab4 assertEffEquals 1 $ pure <<< AB.byteLength =<< (AB.slice (-2) (-1) ab4) - assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 + assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] @@ -55,7 +55,7 @@ main = do DV.setUint8 dv (fromInt 0) 1 pure dv assertEffEquals (Just $ fromInt 123) $ DV.getUint16le twoElementDataView 0 - assertEffEquals (Just $ fromInt 31488) $ DV.getUint16be twoElementDataView 0 + assertEffEquals (Just $ fromInt 31488) $ DV.getUint16be twoElementDataView 0 assertEffEquals (Just $ fromInt 2 `pow` fromInt 32 - fromInt 1) $ do ab' <- AB.create 4 let dv = DV.whole ab' From 4e93d2817a0c6440bde23f7a1b6fedbfde5176a0 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 6 Jun 2018 22:09:56 +0200 Subject: [PATCH 014/236] Remove erroneous version field --- bower.json | 1 - 1 file changed, 1 deletion(-) diff --git a/bower.json b/bower.json index da333d3..0a61192 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,5 @@ { "name": "purescript-arraybuffer", - "version": "4.0.0", "license": "MIT", "repository": { "type": "git", From 963eead544b15e8ffbb47eec1d3f8a17c4975487 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 6 Jun 2018 22:14:19 +0200 Subject: [PATCH 015/236] Add fromIntArray --- src/Data/ArrayBuffer/ArrayBuffer.js | 4 ++++ src/Data/ArrayBuffer/ArrayBuffer.purs | 5 ++++- test/Main.purs | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index e94f14f..da7a25d 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -22,6 +22,10 @@ exports.fromArray = function(s) { return (new Uint8Array(s)).buffer; } +exports.fromIntArray = function(s) { + return (new Uint8Array(s)).buffer; +} + exports.fromString = function(s) { var l = s.length; var ab = new ArrayBuffer(l * 2); diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 97c47dc..6ad938b 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -2,6 +2,7 @@ module Data.ArrayBuffer.ArrayBuffer ( create , byteLength , slice , fromArray + , fromIntArray , fromString ) where @@ -24,6 +25,8 @@ slice = runFn3 sliceImpl -- | Convert an array into an `ArrayBuffer` representation. foreign import fromArray :: Array Number -> ArrayBuffer +-- | Convert an array into an `ArrayBuffer` representation. +foreign import fromIntArray :: Array Int -> ArrayBuffer + -- | Convert a string into an `ArrayBuffer` representation. foreign import fromString :: String -> ArrayBuffer - diff --git a/test/Main.purs b/test/Main.purs index 166ee86..9a660d7 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -34,6 +34,7 @@ main = do assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] + assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] assertEquals 8 $ AB.byteLength $ AB.fromString "hola" assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 @@ -41,7 +42,7 @@ main = do assertEquals (Just 8) $ DV.byteLength <$> DV.slice 0 8 ab8 assertEquals true $ isNothing $ DV.slice 0 40 ab8 - fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0, 4.0] + fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole $ AB.fromIntArray [1, 2, 3, 4] assertEffEquals (Just 2.0) $ TA.at fourElementInt8Array 1 assertEffEquals Nothing $ TA.at fourElementInt8Array 4 assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) From 5bb201c6ea29da68a63b2c7c77e8e4a10b106a2d Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Fri, 22 Jun 2018 16:14:09 +0200 Subject: [PATCH 016/236] Add buffer encoding and decoding functions Refactor and cleanup Add prop tests for arraybuffer and base64 encoding Node compat for base64 Cleanup and working toward isomorphic interface between node and browser Need to determine where test failures are occurring. Better testing needed Better test feedback Delete faulty browser implementations Green tests, but still needing a browser implementation for base64 and arraybuffer manipulation return to old reliable fromStr function Working towards a unified interface for buffer handling Add test vector for b64 Remove test vector TYPE ERROR Cleanup preparing some front-end testing tools Remove old references to mocha Remove base64 encoding as it is growing in complexity. Moving base64 to its own pursuit package Remove stuff after change of mind Last of the cleanup --- .gitignore | 2 +- src/Data/ArrayBuffer/ArrayBuffer.js | 38 ++++++++++++++++++--------- src/Data/ArrayBuffer/ArrayBuffer.purs | 4 +++ test/Main.purs | 18 ++++++++++++- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 0d1e361..12e2c0c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ output/ bower_components/ node_modules/ .psci -.psci_modules/ +.psci_modules/ \ No newline at end of file diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index da7a25d..dcf0737 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -6,31 +6,45 @@ exports.create = function(s) { return function () { return new ArrayBuffer(s); }; -} +}; exports.byteLength = function(a) { return a.byteLength; -} +}; exports.sliceImpl = function(s, e, a) { return function () { return a.slice(s, e); }; -} +}; exports.fromArray = function(s) { return (new Uint8Array(s)).buffer; -} +}; exports.fromIntArray = function(s) { return (new Uint8Array(s)).buffer; -} +}; + +exports.fromInt16Array = function(s) { + return (new Uint16Array(s)).buffer; +}; exports.fromString = function(s) { - var l = s.length; - var ab = new ArrayBuffer(l * 2); - var a = new Uint16Array(ab); - for (var i = 0; i < l; i++) - a[i] = s.charCodeAt(i); - return a.buffer; -} + var buf = new ArrayBuffer(s.length*2); + var bufView = new Uint16Array(buf); + for (var i=0, strLen=s.length; i ArrayBuffer -- | Convert a string into an `ArrayBuffer` representation. foreign import fromString :: String -> ArrayBuffer + +foreign import decodeToString :: ArrayBuffer -> String + diff --git a/test/Main.purs b/test/Main.purs index 9a660d7..3bb5c76 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,9 +6,16 @@ import Effect (Effect) import Data.ArrayBuffer.ArrayBuffer as AB import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.Typed as TA +import Data.ArrayBuffer.Types as AT import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) -import Test.QuickCheck (quickCheck', ()) +import Test.QuickCheck (quickCheck', (), quickCheck) +import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary) + +newtype ABuffer = ABuffer AT.ArrayBuffer + +instance arbitraryArrayBuffer :: Arbitrary ABuffer where + arbitrary = map (ABuffer <<< AB.fromString) arbitrary assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit assertEffEquals expectedValue computation = do @@ -36,6 +43,8 @@ main = do assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] assertEquals 8 $ AB.byteLength $ AB.fromString "hola" + assertEquals 8 $ AB.byteLength $ AB.fromString "hóla" + assertEquals 10 $ AB.byteLength $ AB.fromString "hóla¡" assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 @@ -47,6 +56,12 @@ main = do assertEffEquals Nothing $ TA.at fourElementInt8Array 4 assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) + quickCheck + \(s) -> + s == (AB.decodeToString $ AB.fromString s) + "Isormorphic arraybuffer conversion with string failed for input " + <> s + assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] twoElementDataView <- do @@ -66,3 +81,4 @@ main = do DV.setUint8 dv t 2 DV.setUint8 dv t 3 DV.getUint32be dv 0 + From 9ef90c92b055c1e3faca4942ea6558dc77451cb5 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 23 Jun 2018 11:47:02 +0200 Subject: [PATCH 017/236] Remove old work --- src/Data/ArrayBuffer/ArrayBuffer.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index dcf0737..b4b7249 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -26,10 +26,6 @@ exports.fromIntArray = function(s) { return (new Uint8Array(s)).buffer; }; -exports.fromInt16Array = function(s) { - return (new Uint16Array(s)).buffer; -}; - exports.fromString = function(s) { var buf = new ArrayBuffer(s.length*2); var bufView = new Uint16Array(buf); From 83002a356e6cb337e774588aed39925b6ec250e8 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 23 Jun 2018 17:05:25 +0200 Subject: [PATCH 018/236] Missing documentation comment --- src/Data/ArrayBuffer/ArrayBuffer.purs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 550029b..a227225 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -32,5 +32,6 @@ foreign import fromIntArray :: Array Int -> ArrayBuffer -- | Convert a string into an `ArrayBuffer` representation. foreign import fromString :: String -> ArrayBuffer +-- | Convert an ArrayBuffer into a string. Uses fromCharCode and thus does not support full utf-16 foreign import decodeToString :: ArrayBuffer -> String From 75d970f17ddae052950d125df122ea1c8e138bed Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 30 Jun 2018 21:23:46 +0200 Subject: [PATCH 019/236] Add Maybe wrapping to decodeToString for safety on odd number of byte inputs. --- src/Data/ArrayBuffer/ArrayBuffer.js | 21 +++++++++++++-------- src/Data/ArrayBuffer/ArrayBuffer.purs | 5 ++++- test/Main.purs | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index b4b7249..80f327e 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -35,12 +35,17 @@ exports.fromString = function(s) { return buf; }; -exports.decodeToString = function(buffer) { - const uintBuffer = new Uint16Array(buffer); - const reducer = function(accum, point) { - // use concat instead of es6 syntax for compatibility - return accum.concat([String.fromCharCode(point)]); - }; - const points = uintBuffer.reduce(reducer, new Array()); - return points.join(""); +exports.decodeToStringImpl = function(just, nothing, buffer) { + try { + const uintBuffer = new Uint16Array(buffer); + const reducer = function(accum, point) { + // use concat instead of es6 syntax for compatibility + return accum.concat([String.fromCharCode(point)]); + }; + const points = uintBuffer.reduce(reducer, new Array()); + return just(points.join("")); + } + catch (e) { + return nothing; + } }; diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index a227225..1acd4ee 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -8,6 +8,7 @@ module Data.ArrayBuffer.ArrayBuffer ( create ) where import Effect (Effect) +import Data.Maybe (Maybe(..)) import Data.Function.Uncurried (Fn3, runFn3) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) @@ -33,5 +34,7 @@ foreign import fromIntArray :: Array Int -> ArrayBuffer foreign import fromString :: String -> ArrayBuffer -- | Convert an ArrayBuffer into a string. Uses fromCharCode and thus does not support full utf-16 -foreign import decodeToString :: ArrayBuffer -> String +foreign import decodeToStringImpl :: Fn3 (String -> Maybe String) (Maybe String) ArrayBuffer (Maybe String) +decodeToString :: ArrayBuffer -> Maybe String +decodeToString = runFn3 decodeToStringImpl Just Nothing diff --git a/test/Main.purs b/test/Main.purs index 3bb5c76..e0aa4ba 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -58,7 +58,7 @@ main = do quickCheck \(s) -> - s == (AB.decodeToString $ AB.fromString s) + Just s == (AB.decodeToString $ AB.fromString s) "Isormorphic arraybuffer conversion with string failed for input " <> s From a46da082ecbc3b14aaf71e7335a64b11e1277bc0 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 30 Jun 2018 21:24:45 +0200 Subject: [PATCH 020/236] re-add newline --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 12e2c0c..0d1e361 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ output/ bower_components/ node_modules/ .psci -.psci_modules/ \ No newline at end of file +.psci_modules/ From 73d6823e7cd36bba4e2ee709deed0449229e7b2e Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 30 Jun 2018 21:29:04 +0200 Subject: [PATCH 021/236] Improve docs --- src/Data/ArrayBuffer/ArrayBuffer.purs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 1acd4ee..82fed09 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -33,8 +33,10 @@ foreign import fromIntArray :: Array Int -> ArrayBuffer -- | Convert a string into an `ArrayBuffer` representation. foreign import fromString :: String -> ArrayBuffer --- | Convert an ArrayBuffer into a string. Uses fromCharCode and thus does not support full utf-16 foreign import decodeToStringImpl :: Fn3 (String -> Maybe String) (Maybe String) ArrayBuffer (Maybe String) +-- | Convert an ArrayBuffer into a string. Uses fromCharCode and thus does not support full utf-16 +-- | Is currently only defined for ArrayBuffers with even numbers of bytes, as it assumes the ArrayBuffer encodes string data. +-- | For more general string-encoding forms of data, use a base64 or other encoding scheme. decodeToString :: ArrayBuffer -> Maybe String decodeToString = runFn3 decodeToStringImpl Just Nothing From eb50555d5529ceb3f3539767417d3197601da140 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 30 Jun 2018 21:33:45 +0200 Subject: [PATCH 022/236] Isomorphism passes. --- src/Data/ArrayBuffer/ArrayBuffer.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 80f327e..cbc82e3 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -37,13 +37,7 @@ exports.fromString = function(s) { exports.decodeToStringImpl = function(just, nothing, buffer) { try { - const uintBuffer = new Uint16Array(buffer); - const reducer = function(accum, point) { - // use concat instead of es6 syntax for compatibility - return accum.concat([String.fromCharCode(point)]); - }; - const points = uintBuffer.reduce(reducer, new Array()); - return just(points.join("")); + return just(String.fromCharCode.apply(null, new Uint16Array(buffer))); } catch (e) { return nothing; From 12495768e74cb7887fccb0b9364ac91056e79909 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 30 Jun 2018 23:04:22 +0200 Subject: [PATCH 023/236] Cleanup --- test/Main.purs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index e0aa4ba..66247f4 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,16 +6,9 @@ import Effect (Effect) import Data.ArrayBuffer.ArrayBuffer as AB import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Types as AT import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) import Test.QuickCheck (quickCheck', (), quickCheck) -import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary) - -newtype ABuffer = ABuffer AT.ArrayBuffer - -instance arbitraryArrayBuffer :: Arbitrary ABuffer where - arbitrary = map (ABuffer <<< AB.fromString) arbitrary assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit assertEffEquals expectedValue computation = do From e012152513d2c3bf12421b5d6995ff8487c87277 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Fri, 6 Jul 2018 18:08:20 +0200 Subject: [PATCH 024/236] Initial prototype with text encoding backend for arraybuffer morphisms with string --- .gitignore | 2 ++ README.md | 4 +++ bower.json | 3 ++- package.json | 12 +++++++++ src/Data/ArrayBuffer/ArrayBuffer.js | 18 -------------- src/Data/ArrayBuffer/ArrayBuffer.purs | 36 +++++++++++++++++---------- test/Main.purs | 19 +++++++++----- 7 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 package.json diff --git a/.gitignore b/.gitignore index 0d1e361..ff18e04 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ bower_components/ node_modules/ .psci .psci_modules/ +yarn-error.log +yarn.lock diff --git a/README.md b/README.md index edfcc5c..7bd53a2 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,7 @@ ArrayBuffer bindings for PureScript. Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). + +## Important Usage Notes + +- Usage of the ArrayBuffer<->String conversion functions requires the import of the NPM package 'text-encoding'. diff --git a/bower.json b/bower.json index 0a61192..0cf11a1 100644 --- a/bower.json +++ b/bower.json @@ -16,7 +16,8 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0" + "purescript-uint": "^4.0.0", + "purescript-text-encoding": "^0.0.7" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/package.json b/package.json new file mode 100644 index 0000000..ffc2434 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "purescript-arraybuffer", + "version": "7.0.0", + "main": "index.js", + "repository": "git@github.com:jacereda/purescript-arraybuffer.git", + "author": "https://github.com/jacereda", + "license": "MIT", + "devDependencies": { + "text-encoding": "^0.6.4" + } + +} diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index cbc82e3..695ed48 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -25,21 +25,3 @@ exports.fromArray = function(s) { exports.fromIntArray = function(s) { return (new Uint8Array(s)).buffer; }; - -exports.fromString = function(s) { - var buf = new ArrayBuffer(s.length*2); - var bufView = new Uint16Array(buf); - for (var i=0, strLen=s.length; i Effect ArrayBuffer @@ -30,13 +37,16 @@ foreign import fromArray :: Array Number -> ArrayBuffer -- | Convert an array into an `ArrayBuffer` representation. foreign import fromIntArray :: Array Int -> ArrayBuffer --- | Convert a string into an `ArrayBuffer` representation. -foreign import fromString :: String -> ArrayBuffer - -foreign import decodeToStringImpl :: Fn3 (String -> Maybe String) (Maybe String) ArrayBuffer (Maybe String) - --- | Convert an ArrayBuffer into a string. Uses fromCharCode and thus does not support full utf-16 --- | Is currently only defined for ArrayBuffers with even numbers of bytes, as it assumes the ArrayBuffer encodes string data. --- | For more general string-encoding forms of data, use a base64 or other encoding scheme. -decodeToString :: ArrayBuffer -> Maybe String -decodeToString = runFn3 decodeToStringImpl Just Nothing +-- | Convert a UTF-8 encoded `ArrayBuffer` into a `String`. +-- | Serves as a quick utility function for a common use-case. For more use-cases, +-- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.7) +-- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" +decodeToString :: ArrayBuffer -> Either Error String +decodeToString = decodeUtf8 <<< asUint8Array <<< whole + +-- | Convert a `String` into a UTF-8 encoded `ArrayBuffer`. +-- | Serves as a quick utility function for a common use-case. For more use-cases, +-- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.7) +-- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" +fromString :: String -> ArrayBuffer +fromString = buffer <<< dataView <<< encodeUtf8 diff --git a/test/Main.purs b/test/Main.purs index 66247f4..a5ae7b5 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,8 +6,10 @@ import Effect (Effect) import Data.ArrayBuffer.ArrayBuffer as AB import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.Typed as TA +import Data.Either (fromRight) import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) +import Partial.Unsafe (unsafePartial) import Test.QuickCheck (quickCheck', (), quickCheck) assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit @@ -35,9 +37,9 @@ main = do assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] - assertEquals 8 $ AB.byteLength $ AB.fromString "hola" - assertEquals 8 $ AB.byteLength $ AB.fromString "hóla" - assertEquals 10 $ AB.byteLength $ AB.fromString "hóla¡" + assertEquals 4 $ AB.byteLength $ AB.fromString "hola" + assertEquals 5 $ AB.byteLength $ AB.fromString "hóla" + assertEquals 7 $ AB.byteLength $ AB.fromString "hóla¡" assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 @@ -51,9 +53,14 @@ main = do quickCheck \(s) -> - Just s == (AB.decodeToString $ AB.fromString s) - "Isormorphic arraybuffer conversion with string failed for input " - <> s + let + result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) + in + s == result + "Isormorphic arraybuffer conversion with string failed for input\n" + <> s + <> " which, after the round trip, result in\n" + <> result assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] From 1b61f84902d2053eb8305698ada1b323a28b7ba7 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Fri, 6 Jul 2018 18:23:58 +0200 Subject: [PATCH 025/236] Works infact for this text statically as can be pasted from terminal. I will investigate possibly corrupt data points being generated by quickcheck --- test/Main.purs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Main.purs b/test/Main.purs index a5ae7b5..202a271 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -51,6 +51,8 @@ main = do assertEffEquals Nothing $ TA.at fourElementInt8Array 4 assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) + assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") + quickCheck \(s) -> let From 0c7608eb708a3968008c5bcb2f6eeacd5ad6772e Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Fri, 6 Jul 2018 18:39:22 +0200 Subject: [PATCH 026/236] Reorganize for QC-first --- test/Main.purs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 202a271..134cbea 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -25,6 +25,19 @@ assertEquals expected actual = do main :: Effect Unit main = do + assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") + + quickCheck + \(s) -> + let + result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) + in + s == result + "Isormorphic arraybuffer conversion with string failed for input\n" + <> s + <> " which, after the round trip, result in\n" + <> result + ab4 <- AB.create 4 ab8 <- AB.create 8 assertEquals 4 $ AB.byteLength ab4 @@ -51,19 +64,6 @@ main = do assertEffEquals Nothing $ TA.at fourElementInt8Array 4 assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) - assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") - - quickCheck - \(s) -> - let - result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) - in - s == result - "Isormorphic arraybuffer conversion with string failed for input\n" - <> s - <> " which, after the round trip, result in\n" - <> result - assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] twoElementDataView <- do From e9918388c7e65c77f20210ef92929f42f0d49b42 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 7 Jul 2018 15:54:39 +0200 Subject: [PATCH 027/236] Constrain inputs to only the valid spaces --- bower.json | 3 ++- test/Input.purs | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/Main.purs | 3 ++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/Input.purs diff --git a/bower.json b/bower.json index 0cf11a1..841463d 100644 --- a/bower.json +++ b/bower.json @@ -22,6 +22,7 @@ "devDependencies": { "purescript-debug": "^4.0.0", "purescript-quickcheck": "^5.0.0", - "purescript-partial": "^2.0.0" + "purescript-partial": "^2.0.0", + "purescript-unicode": "^4.0.1" } } diff --git a/test/Input.purs b/test/Input.purs new file mode 100644 index 0000000..926c66b --- /dev/null +++ b/test/Input.purs @@ -0,0 +1,44 @@ +-- Uses code originally found in the purescript-encoding library. +{- + Copyright 2018 Andreas Schacker + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +-} +module Test.Input where + +import Data.Char.Unicode (isPrint) +import Prelude ((<$>), ($), (<<<)) +import Data.Array (filter) +import Data.String.CodeUnits (fromCharArray, toCharArray) +import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary) + + +-- When UTF8-encoding a string, surrogate code points and other non-characters +-- are simply replaced by the replacement character � (U+FFFD). +-- This entails that the `encodeUtf8` function is not injective anymore and +-- thus the desired property `decodeUtf8 <<< encodeUtf8 == id` does not hold +-- in general. +-- +-- For well-formed input strings, however, we can expect the property to hold. + +-- Use a newtype in order to define an `Arbitrary` instance. +newtype WellFormedInput = WellFormedInput String + +-- The `Arbitrary` instance for `String` currently simply chooses characters +-- out of the first 65536 unicode code points. +-- See `charGen` in `purescript-strongcheck`. +instance arbWellFormedInput :: Arbitrary WellFormedInput where + arbitrary = WellFormedInput <<< filterString isPrint <$> arbitrary + +filterString :: (Char -> Boolean) -> String -> String +filterString f s = fromCharArray <<< filter f $ toCharArray s diff --git a/test/Main.purs b/test/Main.purs index 134cbea..746abd9 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -11,6 +11,7 @@ import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) import Partial.Unsafe (unsafePartial) import Test.QuickCheck (quickCheck', (), quickCheck) +import Test.Input assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit assertEffEquals expectedValue computation = do @@ -28,7 +29,7 @@ main = do assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") quickCheck - \(s) -> + \(WellFormedInput s) -> let result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) in From 271e9b56b9ddf331082abfc55a5b3a45cf0d25a0 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sat, 7 Jul 2018 15:57:11 +0200 Subject: [PATCH 028/236] Bump purescript-text-encoding version --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 841463d..3cb29ff 100644 --- a/bower.json +++ b/bower.json @@ -17,7 +17,7 @@ "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", "purescript-uint": "^4.0.0", - "purescript-text-encoding": "^0.0.7" + "purescript-text-encoding": "^0.0.8" }, "devDependencies": { "purescript-debug": "^4.0.0", From b58361dcf874581d5619c415b292453568e92373 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Sun, 8 Jul 2018 12:19:39 +0200 Subject: [PATCH 029/236] Update docs --- src/Data/ArrayBuffer/ArrayBuffer.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 7cb510e..d501ab8 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -39,14 +39,14 @@ foreign import fromIntArray :: Array Int -> ArrayBuffer -- | Convert a UTF-8 encoded `ArrayBuffer` into a `String`. -- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.7) +-- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) -- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" decodeToString :: ArrayBuffer -> Either Error String decodeToString = decodeUtf8 <<< asUint8Array <<< whole -- | Convert a `String` into a UTF-8 encoded `ArrayBuffer`. -- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.7) +-- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) -- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" fromString :: String -> ArrayBuffer fromString = buffer <<< dataView <<< encodeUtf8 From e20d41e9fb718132141ba19ad005e52501caab34 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 15 Jul 2018 21:05:08 +0200 Subject: [PATCH 030/236] Fix warning --- test/Main.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 746abd9..c98266b 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -11,7 +11,8 @@ import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) import Partial.Unsafe (unsafePartial) import Test.QuickCheck (quickCheck', (), quickCheck) -import Test.Input +import Test.Input (WellFormedInput(..)) + assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit assertEffEquals expectedValue computation = do @@ -84,4 +85,3 @@ main = do DV.setUint8 dv t 2 DV.setUint8 dv t 3 DV.getUint32be dv 0 - From e07178673feb62bac017c546a3303fa88c093351 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 15 Jul 2018 21:06:12 +0200 Subject: [PATCH 031/236] v8.0.0 From 0382464f237e482d881bfd224fe299123840e09c Mon Sep 17 00:00:00 2001 From: Alexa DeWit Date: Thu, 11 Oct 2018 08:46:05 +0200 Subject: [PATCH 032/236] Bump purescript-text-encoding version dependency The main reason for this is that it will improve user problem solving ability when running into issues with the transient text-encoding dependency in node environments. With this version users should now encounter a message like this: ``` You appear to be trying to use the purescript-text-encoding in a node based environment without having the text-encoding polyfill available in your node_modules. This can be easily resolved by adding it to your package.json dependencies. if this is not sufficient, please feel free to contact the maintainer of this library via its github here: https://github.com/AlexaDeWit/purescript-text-encoding /home/alexa/Workspace/purescript-encoding/output/Data.TextDecoder/foreign.js:15 throw new Error("Text encoding polyfill library could not be imported."); ``` When lacking the appropriate polyfill. --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 3cb29ff..b50facd 100644 --- a/bower.json +++ b/bower.json @@ -17,7 +17,7 @@ "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", "purescript-uint": "^4.0.0", - "purescript-text-encoding": "^0.0.8" + "purescript-text-encoding": "^0.0.9" }, "devDependencies": { "purescript-debug": "^4.0.0", From 5715c163ac202b7ae260d78110698de0e2e5c271 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 11 Oct 2018 21:39:22 +0200 Subject: [PATCH 033/236] v8.0.1 From a609638895d12c4bbc4c3a3848e9e90e8700114d Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 2 Dec 2018 22:35:23 +0100 Subject: [PATCH 034/236] Fix , closes #19 --- src/Data/ArrayBuffer/DataView.purs | 1 - src/Data/ArrayBuffer/Typed.js | 2 +- test/Main.purs | 9 +++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 6206f2d..31d99b9 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -36,7 +36,6 @@ module Data.ArrayBuffer.DataView( whole ) where import Prelude ---import Data.ArrayBuffer.ArrayBuffer (ARRAY_BUFFER) import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) import Data.Function.Uncurried (Fn5, Fn7, runFn5, runFn7) import Data.Maybe (Maybe(..)) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index d1d213a..5bda73f 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -39,7 +39,7 @@ exports.asFloat64Array = function(v) { } exports.dataView = function(a) { - return a; + return new DataView(a.buffer); } exports.setImpl = function(ra, off, a) { diff --git a/test/Main.purs b/test/Main.purs index c98266b..19ad101 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -85,3 +85,12 @@ main = do DV.setUint8 dv t 2 DV.setUint8 dv t 3 DV.getUint32be dv 0 + + let arr = DV.whole (AB.fromIntArray [0x4, 0x3, 0x2, 0x1]) + + assertEffEquals (Just 0x04) (DV.getInt8 arr 0) + assertEffEquals (Just 0x04) (DV.getInt8 (TA.dataView (TA.asInt8Array arr)) 0) + assertEffEquals (Just 0x0304) (DV.getInt16le arr 0) + assertEffEquals (Just 0x0304) (DV.getInt16le (TA.dataView (TA.asInt16Array arr)) 0) + assertEffEquals (Just 0x01020304) (DV.getInt32le arr 0) + assertEffEquals (Just 0x01020304) (DV.getInt32le (TA.dataView (TA.asInt32Array arr)) 0) From 680f00ffe7ee03b1b16c7c7ce40c1b54d82267a5 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 2 Dec 2018 22:51:54 +0100 Subject: [PATCH 035/236] v8.0.2 From 53c7b2369b842e82db52dfb7b44a836073b5f921 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 10:29:21 -0700 Subject: [PATCH 036/236] ripping out untrue functionality of arraybuffers --- bower.json | 3 +- src/Data/ArrayBuffer/ArrayBuffer.js | 16 ++-------- src/Data/ArrayBuffer/ArrayBuffer.purs | 42 ++++----------------------- 3 files changed, 8 insertions(+), 53 deletions(-) diff --git a/bower.json b/bower.json index b50facd..27ccd59 100644 --- a/bower.json +++ b/bower.json @@ -16,8 +16,7 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0", - "purescript-text-encoding": "^0.0.9" + "purescript-uint": "^4.0.0" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 695ed48..ebaa22a 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -2,26 +2,14 @@ // module Data.ArrayBuffer.ArrayBuffer -exports.create = function(s) { - return function () { +exports.empty = function empty (s) { return new ArrayBuffer(s); - }; }; exports.byteLength = function(a) { - return a.byteLength; + return a.byteLength; }; exports.sliceImpl = function(s, e, a) { - return function () { return a.slice(s, e); - }; -}; - -exports.fromArray = function(s) { - return (new Uint8Array(s)).buffer; -}; - -exports.fromIntArray = function(s) { - return (new Uint8Array(s)).buffer; }; diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index d501ab8..de33518 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,52 +1,20 @@ -module Data.ArrayBuffer.ArrayBuffer ( create +module Data.ArrayBuffer.ArrayBuffer ( empty , byteLength , slice - , fromArray - , fromIntArray - , fromString - , decodeToString - ) where + ) where -import Data.ArrayBuffer.DataView (whole, buffer) -import Data.ArrayBuffer.Typed (asUint8Array, dataView) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) -import Data.Either (Either) import Data.Function.Uncurried (Fn3, runFn3) -import Data.TextDecoder (decodeUtf8) -import Data.TextEncoder (encodeUtf8) -import Effect (Effect) -import Effect.Exception (Error) -import Prelude ((<<<)) -- | Create an `ArrayBuffer` with the given capacity. -foreign import create :: ByteLength -> Effect ArrayBuffer +foreign import empty :: ByteLength -> ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength -foreign import sliceImpl :: Fn3 ByteOffset ByteOffset ArrayBuffer (Effect ArrayBuffer) +foreign import sliceImpl :: Fn3 ByteOffset ByteOffset ArrayBuffer ArrayBuffer -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. -slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> Effect ArrayBuffer +slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer slice = runFn3 sliceImpl - --- | Convert an array into an `ArrayBuffer` representation. -foreign import fromArray :: Array Number -> ArrayBuffer - --- | Convert an array into an `ArrayBuffer` representation. -foreign import fromIntArray :: Array Int -> ArrayBuffer - --- | Convert a UTF-8 encoded `ArrayBuffer` into a `String`. --- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) --- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" -decodeToString :: ArrayBuffer -> Either Error String -decodeToString = decodeUtf8 <<< asUint8Array <<< whole - --- | Convert a `String` into a UTF-8 encoded `ArrayBuffer`. --- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) --- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" -fromString :: String -> ArrayBuffer -fromString = buffer <<< dataView <<< encodeUtf8 From 97a49c6799ff7e64309869db1cb9b9d99ac1798e Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 10:35:00 -0700 Subject: [PATCH 037/236] rename to stay true to implementation --- src/Data/ArrayBuffer/Show.js | 2 +- src/Data/ArrayBuffer/Show.purs | 15 +-------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/Data/ArrayBuffer/Show.js b/src/Data/ArrayBuffer/Show.js index 44eab29..4eca64a 100644 --- a/src/Data/ArrayBuffer/Show.js +++ b/src/Data/ArrayBuffer/Show.js @@ -2,6 +2,6 @@ // module Show -exports.showImpl = function(a) { +exports.showViaInspect = function showViaInspect (a) { return require('util').inspect(a); } diff --git a/src/Data/ArrayBuffer/Show.purs b/src/Data/ArrayBuffer/Show.purs index c22eccf..8bea296 100644 --- a/src/Data/ArrayBuffer/Show.purs +++ b/src/Data/ArrayBuffer/Show.purs @@ -1,18 +1,5 @@ module Data.ArrayBuffer.Show where --- import Prelude import Data.ArrayBuffer.Types (ArrayView) --- import Data.ArrayBuffer.ArrayBuffer as AB --- import Data.ArrayBuffer.DataView as DV --- import Data.ArrayBuffer.Typed as T - --- instance showArrayView :: Show (ArrayView a) where --- show = showImpl --- --- instance showDataView :: Show DataView where --- show = show <<< T.asInt8Array --- --- instance showArrayBuffer :: Show ArrayBuffer where --- show = show <<< DV.whole -- -foreign import showImpl :: forall a. ArrayView a -> String +foreign import showViaInspect :: forall a. ArrayView a -> String From d650148792ab62db029e58ca6f523f6cbf43adf7 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 11:03:17 -0700 Subject: [PATCH 038/236] converting to Effect.Uncurried, using exception catching, naming anonymous foreign functions for stack traces --- src/Data/ArrayBuffer/DataView.js | 68 +++++++++------------ src/Data/ArrayBuffer/DataView.purs | 95 ++++++++++++++++-------------- 2 files changed, 79 insertions(+), 84 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index c1587c3..fbd5782 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -3,43 +3,31 @@ // module Data.ArrayBuffer.DataView -exports.whole = function(b) { - return new DataView(b); -} - -exports.sliceImpl = function(just, nothing, s, l, b) { - return ((s + l)>>>0) <= b.byteLength ? just(new DataView(b, s, l)) : nothing; -} - -exports.buffer = function(v) { - return v.buffer; -} - -exports.byteOffset = function(v) { - return v.byteOffset; -} - -exports.byteLength = function(v) { - return v.byteLength; -} - -exports.getterImpl = function(just, nothing, s, l, e, v, o) { - return function() { - return ((o + l)>>>0) <= v.byteLength? just(v[s].call(v,o,e)) : nothing; - }; -} - -exports.setter = function(s) { - return function(e) { - return function(v) { - var f = v[s]; - return function(n) { - return function(o) { - return function() { - f.call(v,o,n,e); - }; - }; - }; - }; - }; -} +exports.whole = function whole (b) { + return new DataView(b); +}; + +exports.partImpl = function partImpl (b,i,j) { + return new DataView(b,i,j); +}; + +exports.buffer = function buffer (v) { + return v.buffer; +}; + +exports.byteOffset = function byteOffset (v) { + return v.byteOffset; +}; + +exports.byteLength = function byteLength (v) { + return v.byteLength; +}; + +exports.getterImpl = function getterImpl (s, l, e, v, o) { + return v[s].call(v,o,e); +}; + +exports.setterImpl = function setterImpl (s,e,v,n,o) { + var f = v[s]; + f.call(v,o,n,e); +}; diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 31d99b9..84d174c 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -1,45 +1,47 @@ -module Data.ArrayBuffer.DataView( whole - , slice - , buffer - , byteOffset - , byteLength - , Getter() - , getInt8 - , getInt16be - , getInt32be - , getUint8 - , getUint16be - , getUint32be - , getFloat32be - , getFloat64be - , getInt16le - , getInt32le - , getUint16le - , getUint32le - , getFloat32le - , getFloat64le - , Setter() - , setInt8 - , setInt16be - , setInt32be - , setUint8 - , setUint16be - , setUint32be - , setFloat32be - , setFloat64be - , setInt16le - , setInt32le - , setUint16le - , setUint32le - , setFloat32le - , setFloat64le - ) where +module Data.ArrayBuffer.DataView + ( whole + , part + , buffer + , byteOffset + , byteLength + , Getter() + , getInt8 + , getInt16be + , getInt32be + , getUint8 + , getUint16be + , getUint32be + , getFloat32be + , getFloat64be + , getInt16le + , getInt32le + , getUint16le + , getUint32le + , getFloat32le + , getFloat64le + , Setter() + , setInt8 + , setInt16be + , setInt32be + , setUint8 + , setUint16be + , setUint32be + , setFloat32be + , setFloat64be + , setInt16le + , setInt32le + , setUint16le + , setUint32le + , setFloat32le + , setFloat64le + ) where import Prelude import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) -import Data.Function.Uncurried (Fn5, Fn7, runFn5, runFn7) import Data.Maybe (Maybe(..)) import Effect (Effect) +import Effect.Exception (catchException) +import Effect.Uncurried (EffectFn5, EffectFn3, runEffectFn5, runEffectFn3) import Data.UInt (UInt) -- | Type for all fetching functions. @@ -51,11 +53,11 @@ type Setter r = DataView -> r -> ByteOffset -> Effect Unit -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView -foreign import sliceImpl :: Fn5 (DataView -> Maybe DataView) (Maybe DataView) ByteOffset ByteLength ArrayBuffer (Maybe DataView) +foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView -- | View mapping a region of the `ArrayBuffer`. -slice :: ByteOffset -> ByteLength -> ArrayBuffer -> (Maybe DataView) -slice = runFn5 sliceImpl Just Nothing +part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView +part = runEffectFn3 partImpl -- | `ArrayBuffer` being mapped by the view. foreign import buffer :: DataView -> ArrayBuffer @@ -69,13 +71,18 @@ foreign import byteLength :: DataView -> ByteLength type Endianness = Boolean -foreign import getterImpl :: forall r. Fn7 (r -> Maybe r) (Maybe r) String ByteLength Endianness DataView ByteOffset (Effect (Maybe r)) +foreign import getterImpl :: forall r. EffectFn5 String ByteLength Endianness DataView ByteOffset r -getter :: forall r. String -> ByteLength -> Endianness -> DataView -> ByteOffset -> Effect (Maybe r) -getter = runFn7 getterImpl Just Nothing +getter :: forall r. String -> ByteLength -> Endianness -> Getter r +getter p l e d o = + let x = runEffectFn5 getterImpl p l e d o + in catchException (const (pure Nothing)) (Just <$> x) +foreign import setterImpl :: forall r. EffectFn5 String Endianness DataView r ByteOffset Unit -foreign import setter :: forall r. String -> Endianness -> DataView -> r -> ByteOffset -> Effect Unit +setter :: forall r. String -> Endianness -> Setter r +setter p e d x o = + runEffectFn5 setterImpl p e d x o -- | Fetch int8 value at a certain index in a `DataView`. From a4c7cd065386586477e3e7e5242fa68776cf40bf Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 11:28:13 -0700 Subject: [PATCH 039/236] module documentation --- src/Data/ArrayBuffer/ArrayBuffer.purs | 3 +++ src/Data/ArrayBuffer/DataView.purs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index de33518..95335c7 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,3 +1,6 @@ +-- | This module represents the functional bindings to JavaScript's `ArrayBuffer` +-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) for details. + module Data.ArrayBuffer.ArrayBuffer ( empty , byteLength , slice diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 84d174c..616665f 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -1,3 +1,6 @@ +-- | This module represents the functional bindings to JavaScript's `DataView` +-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. + module Data.ArrayBuffer.DataView ( whole , part From addf6647ba0735d0db4e4a1e898c5b6801fa1d23 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 11:46:09 -0700 Subject: [PATCH 040/236] preparing for major redesign of typed interface - supporting all functions --- src/Data/ArrayBuffer/Typed.js | 57 ++++++++------------- src/Data/ArrayBuffer/Typed.purs | 89 ++++++++++++++++++--------------- 2 files changed, 70 insertions(+), 76 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 5bda73f..2ea5a16 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,46 +1,35 @@ "use strict"; -// module Data.ArrayBuffer.Typed -exports.asInt8Array = function(v) { - return new Int8Array(v.buffer, v.byteOffset, v.byteLength); -} +// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill +var typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, + Uint16Array, ​​​Int32Array, Uint32Array, ​​​Float32Array, Float64Array]; -exports.asInt16Array = function(v) { - return new Int16Array(v.buffer, v.byteOffset, v.byteLength >>> 1); -} +for (var k in typedArrayTypes) + for (var v in Array.prototype) + if (Array.prototype.hasOwnProperty(v) && + !typedArrayTypes[k].prototype.hasOwnProperty(v)) + typedArrayTypes[k].prototype[v] = Array.prototype[v]; -exports.asInt32Array = function(v) { - return new Int32Array(v.buffer, v.byteOffset, v.byteLength >>> 2); -} -exports.asUint8Array = function(v) { - return new Uint8Array(v.buffer, v.byteOffset, v.byteLength); -} +// module Data.ArrayBuffer.Typed -exports.asUint16Array = function(v) { - return new Uint16Array(v.buffer, v.byteOffset, v.byteLength >>> 1); -} +exports.buffer = function buffer (v) { + return v.buffer; +}; -exports.asUint32Array = function(v) { - return new Uint32Array(v.buffer, v.byteOffset, v.byteLength >>> 2); -} +exports.byteOffset = function byteOffset (v) { + return v.byteOffset; +}; -exports.asUint8ClampedArray = function(v) { - return new Uint8ClampedArray(v.buffer, v.byteOffset, v.byteLength); -} +exports.byteLength = function byteLength (v) { + return v.byteLength; +}; -exports.asFloat32Array = function(v) { - return new Float32Array(v.buffer, v.byteOffset, v.byteLength >>> 2); -} +exports.lengthImpl = function lemgthImpl (v) { + return v.length; +}; -exports.asFloat64Array = function(v) { - return new Float64Array(v.buffer, v.byteOffset, v.byteLength >>> 3); -} - -exports.dataView = function(a) { - return new DataView(a.buffer); -} exports.setImpl = function(ra, off, a) { return function() { @@ -49,9 +38,7 @@ exports.setImpl = function(ra, off, a) { } exports.unsafeAtImpl = function(a, i) { - return function() { - return a[i]; - }; + return a[i]; } exports.hasIndexImpl = function(a, i) { diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index fbfbdc5..39162b7 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -1,56 +1,63 @@ -module Data.ArrayBuffer.Typed( asInt8Array - , asInt16Array - , asInt32Array - , asUint8Array - , asUint16Array - , asUint32Array - , asUint8ClampedArray - , asFloat32Array - , asFloat64Array - , dataView - , set - , unsafeAt - , hasIndex - , at - , toArray - , toIntArray - ) where +module Data.ArrayBuffer.Typed + ( buffer + , byteOffset + , byteLength + , class Bytes + , bytesPer + , length + , set + , unsafeAt + , hasIndex + , at + , toArray + , toIntArray + ) where import Prelude import Effect (Effect) -import Data.ArrayBuffer.Types (ArrayView, ByteOffset, DataView, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array) +import Data.ArrayBuffer.Types + ( ArrayView, ByteOffset + , Float64Array, Float32Array + , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array + , Float64, Float32 + , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) +import Type.Proxy (Proxy(..)) --- | Create typed int8 array viewing the buffer mapped by the `DataView` -foreign import asInt8Array :: DataView -> Int8Array --- | Create typed int16 array viewing the buffer mapped by the `DataView` -foreign import asInt16Array :: DataView -> Int16Array +-- | `ArrayBuffer` being mapped by the typed array. +foreign import buffer :: forall a. ArrayView a -> ArrayBuffer --- | Create typed int32 array viewing the buffer mapped by the `DataView` -foreign import asInt32Array :: DataView -> Int32Array +-- | Represents the offset of this view from the start of its `ArrayBuffer`. +foreign import byteOffset :: forall a. ArrayView a -> ByteOffset --- | Create typed uint8 array viewing the buffer mapped by the `DataView` -foreign import asUint8Array :: DataView -> Uint8Array +-- | Represents the length of this typed array, in bytes. +foreign import byteLength :: forall a. ArrayView a -> ByteLength --- | Create typed uint16 array viewing the buffer mapped by the `DataView` -foreign import asUint16Array :: DataView -> Uint16Array +foreign import lengthImpl :: forall a. ArrayView a -> Int --- | Create typed uint32 array viewing the buffer mapped by the `DataView` -foreign import asUint32Array :: DataView -> Uint32Array +class Bytes a where + bytesPer :: Proxy a -> Int --- | Create typed uint8 clamped array viewing the buffer mapped by the `DataView` -foreign import asUint8ClampedArray :: DataView -> Uint8ClampedArray +instance bytesUint8Clamped :: Bytes Uint8Clamped where + bytesPer Proxy = 1 +instance bytesUint32 :: Bytes Uint32 where + bytesPer Proxy = 4 +instance bytesUint16 :: Bytes Uint16 where + bytesPer Proxy = 2 +instance bytesUint8 :: Bytes Uint8 where + bytesPer Proxy = 1 +instance bytesInt32 :: Bytes Int32 where + bytesPer Proxy = 4 +instance bytesInt16 :: Bytes Int16 where + bytesPer Proxy = 2 +instance bytesInt8 :: Bytes Int8 where + bytesPer Proxy = 1 --- | Create typed float32 array viewing the buffer mapped by the `DataView` -foreign import asFloat32Array :: DataView -> Float32Array +length :: forall a. Bytes a => ArrayView a -> Int +length = lengthImpl --- | Create typed float64 array viewing the buffer mapped by the `DataView` -foreign import asFloat64Array :: DataView -> Float64Array - --- | Interpret typed array as a `DataView`. -foreign import dataView :: forall a. ArrayView a -> DataView foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit) @@ -58,11 +65,11 @@ foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) ( set :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit set = runFn3 setImpl -foreign import unsafeAtImpl :: forall a. Fn2 (ArrayView a) Int (Effect Number) +foreign import unsafeAtImpl :: forall a. EffectFn2 (ArrayView a) Int Number -- | Fetch element at index. unsafeAt :: forall a. ArrayView a -> Int -> Effect Number -unsafeAt = runFn2 unsafeAtImpl +unsafeAt = runEffectFn2 unsafeAtImpl foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Int Boolean From 5200641bdc89e8c821519c6407ff50f067eec17d Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 11:53:26 -0700 Subject: [PATCH 041/236] polyfill --- src/Data/ArrayBuffer/Typed.js | 22 +++++++++++++--------- src/Data/ArrayBuffer/Typed.purs | 31 +++++++++++++++++++------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 2ea5a16..3a67a5b 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,15 +1,19 @@ "use strict"; -// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -var typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, - Uint16Array, ​​​Int32Array, Uint32Array, ​​​Float32Array, Float64Array]; - -for (var k in typedArrayTypes) - for (var v in Array.prototype) - if (Array.prototype.hasOwnProperty(v) && - !typedArrayTypes[k].prototype.hasOwnProperty(v)) - typedArrayTypes[k].prototype[v] = Array.prototype[v]; +exports.polyFill = function () { + var typedArrayTypes = + [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array + , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array + ]; + + for (var k in typedArrayTypes) { + for (var v in Array.prototype) { + if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) + typedArrayTypes[k].prototype[v] = Array.prototype[v]; + } + } +}; // module Data.ArrayBuffer.Typed diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 39162b7..40d25a0 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -1,7 +1,9 @@ module Data.ArrayBuffer.Typed - ( buffer + ( polyFill + , buffer , byteOffset , byteLength + , AProxy (..) , class Bytes , bytesPer , length @@ -15,17 +17,20 @@ module Data.ArrayBuffer.Typed import Prelude import Effect (Effect) +import Effect.Uncurried (EffectFn2, runEffectFn2) import Data.ArrayBuffer.Types - ( ArrayView, ByteOffset + ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength , Float64Array, Float32Array , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array , Float64, Float32 , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) -import Type.Proxy (Proxy(..)) +-- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill +foreign import polyFill :: Effect Unit + -- | `ArrayBuffer` being mapped by the typed array. foreign import buffer :: forall a. ArrayView a -> ArrayBuffer @@ -37,23 +42,25 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength foreign import lengthImpl :: forall a. ArrayView a -> Int -class Bytes a where - bytesPer :: Proxy a -> Int +data AProxy (a :: ArrayViewType) = AProxy + +class Bytes (a :: ArrayViewType) where + bytesPer :: AProxy a -> Int instance bytesUint8Clamped :: Bytes Uint8Clamped where - bytesPer Proxy = 1 + bytesPer AProxy = 1 instance bytesUint32 :: Bytes Uint32 where - bytesPer Proxy = 4 + bytesPer AProxy = 4 instance bytesUint16 :: Bytes Uint16 where - bytesPer Proxy = 2 + bytesPer AProxy = 2 instance bytesUint8 :: Bytes Uint8 where - bytesPer Proxy = 1 + bytesPer AProxy = 1 instance bytesInt32 :: Bytes Int32 where - bytesPer Proxy = 4 + bytesPer AProxy = 4 instance bytesInt16 :: Bytes Int16 where - bytesPer Proxy = 2 + bytesPer AProxy = 2 instance bytesInt8 :: Bytes Int8 where - bytesPer Proxy = 1 + bytesPer AProxy = 1 length :: forall a. Bytes a => ArrayView a -> Int length = lengthImpl From 5d49921a3582c33cf3686033840c4e7adeb60a10 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 11:54:18 -0700 Subject: [PATCH 042/236] module doc --- src/Data/ArrayBuffer/Typed.purs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 40d25a0..7dd03dd 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -1,3 +1,6 @@ +-- | This module represents the functional bindings to JavaScript's `TypedArray` and other +-- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) for details. + module Data.ArrayBuffer.Typed ( polyFill , buffer From 75a91e67a0a17ac93e3bd17847a3b1bd8c7d6d04 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:18:55 -0700 Subject: [PATCH 043/236] using `new` as binder - also, remainder function --- src/Data/ArrayBuffer/DataView.js | 4 +++ src/Data/ArrayBuffer/DataView.purs | 9 ++++- src/Data/ArrayBuffer/Typed.js | 13 +++++++ src/Data/ArrayBuffer/Typed.purs | 57 ++++++++++++++++++++++++------ 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index fbd5782..55b47f7 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -7,6 +7,10 @@ exports.whole = function whole (b) { return new DataView(b); }; +exports.remainderImpl = function remainderImpl (b,i) { + return new DataView(b,i); +}; + exports.partImpl = function partImpl (b,i,j) { return new DataView(b,i,j); }; diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 616665f..4bbd5ab 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -3,6 +3,7 @@ module Data.ArrayBuffer.DataView ( whole + , remainder , part , buffer , byteOffset @@ -44,7 +45,7 @@ import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) import Data.Maybe (Maybe(..)) import Effect (Effect) import Effect.Exception (catchException) -import Effect.Uncurried (EffectFn5, EffectFn3, runEffectFn5, runEffectFn3) +import Effect.Uncurried (EffectFn5, EffectFn3, EffectFn2, runEffectFn5, runEffectFn3, runEffectFn2) import Data.UInt (UInt) -- | Type for all fetching functions. @@ -56,6 +57,12 @@ type Setter r = DataView -> r -> ByteOffset -> Effect Unit -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView +foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView + +-- | View mapping the rest of an `ArrayBuffer` after an index. +remainder :: ArrayBuffer -> ByteOffset -> Effect DataView +remainder = runEffectFn2 remainderImpl + foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView -- | View mapping a region of the `ArrayBuffer`. diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 3a67a5b..407b0a2 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -35,6 +35,19 @@ exports.lengthImpl = function lemgthImpl (v) { }; +// Uint8Clamped + +exports.newUint8ClampedArray = function newUint8ClampedArray (a) { + return new Uint8ClampedArray(a); +}; +exports.newUint8ClampedArray2 = function newUint8ClampedArray2 (a,b) { + return new Uint8ClampedArray(a,b); +}; +exports.newUint8ClampedArray3 = function newUint8ClampedArray3 (a,b,c) { + return new Uint8ClampedArray(a,b,c); +}; + + exports.setImpl = function(ra, off, a) { return function() { a.set(ra, off); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 7dd03dd..c15b34e 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -7,9 +7,12 @@ module Data.ArrayBuffer.Typed , byteOffset , byteLength , AProxy (..) - , class Bytes + , class BytesPer , bytesPer , length + , whole, remainder, part + , class ValuesPer + , empty, fromArray , set , unsafeAt , hasIndex @@ -20,7 +23,8 @@ module Data.ArrayBuffer.Typed import Prelude import Effect (Effect) -import Effect.Uncurried (EffectFn2, runEffectFn2) +import Effect.Uncurried (EffectFn3, EffectFn2, EffectFn1, runEffectFn3, runEffectFn2, runEffectFn1) +import Effect.Unsafe (unsafePerformEffect) import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength , Float64Array, Float32Array @@ -47,28 +51,59 @@ foreign import lengthImpl :: forall a. ArrayView a -> Int data AProxy (a :: ArrayViewType) = AProxy -class Bytes (a :: ArrayViewType) where +class BytesPer (a :: ArrayViewType) where bytesPer :: AProxy a -> Int -instance bytesUint8Clamped :: Bytes Uint8Clamped where +instance bytesPerUint8Clamped :: BytesPer Uint8Clamped where bytesPer AProxy = 1 -instance bytesUint32 :: Bytes Uint32 where +instance bytesPerUint32 :: BytesPer Uint32 where bytesPer AProxy = 4 -instance bytesUint16 :: Bytes Uint16 where +instance bytesPerUint16 :: BytesPer Uint16 where bytesPer AProxy = 2 -instance bytesUint8 :: Bytes Uint8 where +instance bytesPerUint8 :: BytesPer Uint8 where bytesPer AProxy = 1 -instance bytesInt32 :: Bytes Int32 where +instance bytesPerInt32 :: BytesPer Int32 where bytesPer AProxy = 4 -instance bytesInt16 :: Bytes Int16 where +instance bytesPerInt16 :: BytesPer Int16 where bytesPer AProxy = 2 -instance bytesInt8 :: Bytes Int8 where +instance bytesPerInt8 :: BytesPer Int8 where bytesPer AProxy = 1 -length :: forall a. Bytes a => ArrayView a -> Int +length :: forall a. BytesPer a => ArrayView a -> Int length = lengthImpl +foreign import newUint8ClampedArray :: forall a. EffectFn1 a Uint8ClampedArray +foreign import newUint8ClampedArray2 :: EffectFn2 ArrayBuffer ByteOffset Uint8ClampedArray +foreign import newUint8ClampedArray3 :: EffectFn3 ArrayBuffer ByteOffset ByteLength Uint8ClampedArray + + +-- TODO use purescript-quotient +class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where + -- | View mapping the whole `ArrayBuffer`. + whole :: ArrayBuffer -> ArrayView a + -- | View mapping the rest of an `ArrayBuffer` after an index. + remainder :: ArrayBuffer -> ByteOffset -> Effect (ArrayView a) + -- | View mapping a region of the `ArrayBuffer`. + part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect (ArrayView a) + -- | Creates an empty typed array, where each value is assigned 0 + empty :: Int -> ArrayView a + fromArray :: Array t -> Effect (ArrayView a) + +instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where + whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) + remainder = runEffectFn2 newUint8ClampedArray2 + part = runEffectFn3 newUint8ClampedArray3 + empty n = unsafePerformEffect (runEffectFn1 newUint8ClampedArray n) + fromArray = runEffectFn1 newUint8ClampedArray +-- instance valuesPerUint32 :: ValuesPer Uint32 Number +-- instance valuesPerUint16 :: ValuesPer Uint16 Int +-- instance valuesPerUint8 :: ValuesPer Uint8 Int +-- instance valuesPerInt32 :: ValuesPer Int32 Number +-- instance valuesPerInt16 :: ValuesPer Int16 Int +-- instance valuesPerInt8 :: ValuesPer Int8 Int + + foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit) -- | Stores multiple values in the last typed array, reading input values from ther first typed array. From 827b29e236b4843d6470488a636ee754e1ad5b30 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:27:35 -0700 Subject: [PATCH 044/236] copyWithin --- src/Data/ArrayBuffer/Typed.js | 8 ++++++++ src/Data/ArrayBuffer/Typed.purs | 23 ++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 407b0a2..db05ebb 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -48,6 +48,14 @@ exports.newUint8ClampedArray3 = function newUint8ClampedArray3 (a,b,c) { }; +exports.copyWithinImpl = function copyWithinImpl (a,t,s) { + a.copyWithin(t,s); +}; +exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { + a.copyWithin(t,s,e); +}; + + exports.setImpl = function(ra, off, a) { return function() { a.set(ra, off); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index c15b34e..97fce7e 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -10,9 +10,9 @@ module Data.ArrayBuffer.Typed , class BytesPer , bytesPer , length - , whole, remainder, part , class ValuesPer - , empty, fromArray + , whole, remainder, part, empty, fromArray + , copyWithin, copyWithin' , set , unsafeAt , hasIndex @@ -23,7 +23,9 @@ module Data.ArrayBuffer.Typed import Prelude import Effect (Effect) -import Effect.Uncurried (EffectFn3, EffectFn2, EffectFn1, runEffectFn3, runEffectFn2, runEffectFn1) +import Effect.Uncurried + ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 + , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1) import Effect.Unsafe (unsafePerformEffect) import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength @@ -88,14 +90,15 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect (ArrayView a) -- | Creates an empty typed array, where each value is assigned 0 empty :: Int -> ArrayView a - fromArray :: Array t -> Effect (ArrayView a) + -- | Creates a typed array from an input array of values, to be binary serialized + fromArray :: Array t -> ArrayView a instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) remainder = runEffectFn2 newUint8ClampedArray2 part = runEffectFn3 newUint8ClampedArray3 empty n = unsafePerformEffect (runEffectFn1 newUint8ClampedArray n) - fromArray = runEffectFn1 newUint8ClampedArray + fromArray a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int @@ -104,6 +107,16 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where -- instance valuesPerInt8 :: ValuesPer Int8 Int +foreign import copyWithinImpl :: forall a. EffectFn3 (ArrayView a) ByteOffset ByteOffset Unit +foreign import copyWithinImpl3 :: forall a. EffectFn4 (ArrayView a) ByteOffset ByteOffset ByteOffset Unit + +-- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. +copyWithin :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> Effect Unit +copyWithin = runEffectFn3 copyWithinImpl +copyWithin' :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffset -> Effect Unit +copyWithin' = runEffectFn4 copyWithinImpl3 + + foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit) -- | Stores multiple values in the last typed array, reading input values from ther first typed array. From 7a3435f1364ef6bec12d56fd76f5a336597c95fe Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:35:45 -0700 Subject: [PATCH 045/236] all, any ~ every, some --- src/Data/ArrayBuffer/Typed.js | 8 ++++++++ src/Data/ArrayBuffer/Typed.purs | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index db05ebb..251ca3e 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -48,6 +48,14 @@ exports.newUint8ClampedArray3 = function newUint8ClampedArray3 (a,b,c) { }; +exports.everyImpl = function everyImpl (a,p) { + return a.every(p); +}; +exports.someImpl = function someImpl (a,p) { + return a.some(p); +}; + + exports.copyWithinImpl = function copyWithinImpl (a,t,s) { a.copyWithin(t,s); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 97fce7e..1944e76 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -11,7 +11,7 @@ module Data.ArrayBuffer.Typed , bytesPer , length , class ValuesPer - , whole, remainder, part, empty, fromArray + , whole, remainder, part, empty, fromArray, all, any , copyWithin, copyWithin' , set , unsafeAt @@ -80,6 +80,10 @@ foreign import newUint8ClampedArray2 :: EffectFn2 ArrayBuffer ByteOffset Uint8Cl foreign import newUint8ClampedArray3 :: EffectFn3 ArrayBuffer ByteOffset ByteLength Uint8ClampedArray +foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean +foreign import someImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean + + -- TODO use purescript-quotient class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where -- | View mapping the whole `ArrayBuffer`. @@ -92,6 +96,10 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where empty :: Int -> ArrayView a -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: Array t -> ArrayView a + -- | Test a predicate to pass on all values + all :: (t -> Boolean) -> ArrayView a -> Boolean + -- | Test a predicate to pass on any value + any :: (t -> Boolean) -> ArrayView a -> Boolean instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) @@ -99,6 +107,8 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where part = runEffectFn3 newUint8ClampedArray3 empty n = unsafePerformEffect (runEffectFn1 newUint8ClampedArray n) fromArray a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) + all p a = runFn2 everyImpl a p + any p a = runFn2 someImpl a p -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int From ba3f0893b9e09f7281f9f6d3cea6c94154488929 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:42:02 -0700 Subject: [PATCH 046/236] fill semantics --- src/Data/ArrayBuffer/Typed.js | 11 +++++++++++ src/Data/ArrayBuffer/Typed.purs | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 251ca3e..c5c92a9 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -56,6 +56,17 @@ exports.someImpl = function someImpl (a,p) { }; +exports.fillImpl = function fillImpl (a,x) { + return a.fill(x); +}; +exports.fillImpl2 = function fillImpl2 (a,x,s) { + return a.fill(x,s); +}; +exports.fillImpl3 = function fillImpl3 (a,x,s,e) { + return a.fill(x,s,e); +}; + + exports.copyWithinImpl = function copyWithinImpl (a,t,s) { a.copyWithin(t,s); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 1944e76..0cbaba6 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -11,7 +11,7 @@ module Data.ArrayBuffer.Typed , bytesPer , length , class ValuesPer - , whole, remainder, part, empty, fromArray, all, any + , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart , copyWithin, copyWithin' , set , unsafeAt @@ -83,6 +83,10 @@ foreign import newUint8ClampedArray3 :: EffectFn3 ArrayBuffer ByteOffset ByteLen foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean foreign import someImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean +foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit +foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b ByteOffset Unit +foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b ByteOffset ByteOffset Unit + -- TODO use purescript-quotient class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where @@ -100,6 +104,12 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where all :: (t -> Boolean) -> ArrayView a -> Boolean -- | Test a predicate to pass on any value any :: (t -> Boolean) -> ArrayView a -> Boolean + -- | Fill the array with a value + fill :: ArrayView a -> t -> Effect Unit + -- | Fill the remainder of the array with a value + fillRemainder :: ArrayView a -> t -> ByteOffset -> Effect Unit + -- | Fill part of the array with a value + fillPart :: ArrayView a -> t -> ByteOffset -> ByteOffset -> Effect Unit instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) @@ -109,6 +119,9 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where fromArray a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) all p a = runFn2 everyImpl a p any p a = runFn2 someImpl a p + fill = runEffectFn2 fillImpl + fillRemainder = runEffectFn3 fillImpl2 + fillPart = runEffectFn4 fillImpl3 -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int From b203cd99673c2585e0463a341eabf075eef453e0 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:47:42 -0700 Subject: [PATCH 047/236] toString --- src/Data/ArrayBuffer/Typed.js | 5 +++++ src/Data/ArrayBuffer/Typed.purs | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index c5c92a9..de3234a 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -75,6 +75,11 @@ exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { }; +exports.toString = function toString (a) { + return a.toString(); +}; + + exports.setImpl = function(ra, off, a) { return function() { a.set(ra, off); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 0cbaba6..b770d33 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -12,7 +12,8 @@ module Data.ArrayBuffer.Typed , length , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart - , copyWithin, copyWithin' + , copyWithin, copyWithinPart + , toString , set , unsafeAt , hasIndex @@ -89,6 +90,7 @@ foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b ByteOffset Byt -- TODO use purescript-quotient +-- | Measured user-level values stored in each typed array class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where -- | View mapping the whole `ArrayBuffer`. whole :: ArrayBuffer -> ArrayView a @@ -136,8 +138,12 @@ foreign import copyWithinImpl3 :: forall a. EffectFn4 (ArrayView a) ByteOffset B -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. copyWithin :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> Effect Unit copyWithin = runEffectFn3 copyWithinImpl -copyWithin' :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffset -> Effect Unit -copyWithin' = runEffectFn4 copyWithinImpl3 +copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffset -> Effect Unit +copyWithinPart = runEffectFn4 copyWithinImpl3 + + +-- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. +foreign import toString :: forall a. ArrayView a -> String foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit) From e2b70aa616b1ed28575348081546ac8de3ed7c10 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:56:30 -0700 Subject: [PATCH 048/236] subarray --- src/Data/ArrayBuffer/Typed.js | 8 ++++++++ src/Data/ArrayBuffer/Typed.purs | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index de3234a..9753627 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -75,6 +75,14 @@ exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { }; +exports.subArrayImpl = function subArrayImpl (a,s) { + return a.subarray(s); +}; +exports.subArrayImpl2 = function subArrayImpl2 (a,s,e) { + return a.subarray(s,e); +}; + + exports.toString = function toString (a) { return a.toString(); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b770d33..7f4fa6c 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -13,6 +13,7 @@ module Data.ArrayBuffer.Typed , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart , copyWithin, copyWithinPart + , subArray, subArrayRemainder , toString , set , unsafeAt @@ -142,6 +143,17 @@ copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffse copyWithinPart = runEffectFn4 copyWithinImpl3 +foreign import subArrayImpl :: forall a. Fn2 (ArrayView a) ByteOffset (ArrayView a) +foreign import subArrayImpl2 :: forall a. Fn3 (ArrayView a) ByteOffset ByteOffset (ArrayView a) + +-- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. +subArray :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ArrayView a +subArray = runFn3 subArrayImpl2 +-- | Returns a new typed array view of the same buffer, beginning at the index +subArrayRemainder :: forall a. ArrayView a -> ByteOffset -> ArrayView a +subArrayRemainder = runFn2 subArrayImpl + + -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String From 16f384c8db35a07d48075ce3307479192f91d834 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 12:58:41 -0700 Subject: [PATCH 049/236] in-place sort --- src/Data/ArrayBuffer/Typed.js | 5 +++++ src/Data/ArrayBuffer/Typed.purs | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 9753627..33ff9c4 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -75,6 +75,11 @@ exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { }; +exports.sortImpl = function sortImpl (a) { + a.sort(); +}; + + exports.subArrayImpl = function subArrayImpl (a,s) { return a.subarray(s); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 7f4fa6c..ce90458 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -13,6 +13,7 @@ module Data.ArrayBuffer.Typed , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart , copyWithin, copyWithinPart + , sort , subArray, subArrayRemainder , toString , set @@ -143,9 +144,17 @@ copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffse copyWithinPart = runEffectFn4 copyWithinImpl3 +foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit + +-- | Sorts the values in-place +sort :: forall a. ArrayView a -> Effect Unit +sort = runEffectFn1 sortImpl + + foreign import subArrayImpl :: forall a. Fn2 (ArrayView a) ByteOffset (ArrayView a) foreign import subArrayImpl2 :: forall a. Fn3 (ArrayView a) ByteOffset ByteOffset (ArrayView a) + -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. subArray :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ArrayView a subArray = runFn3 subArrayImpl2 From 95a79cb0020627a39f591308b3b0a1f41dcc4a55 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:05:55 -0700 Subject: [PATCH 050/236] slice --- src/Data/ArrayBuffer/Typed.js | 11 +++++++++++ src/Data/ArrayBuffer/Typed.purs | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 33ff9c4..829a9e5 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -75,6 +75,17 @@ exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { }; +exports.copy = function copy (a) { + return a.slice(); +}; +exports.sliceRemainderImpl = function sliceRemainderImpl (a,s) { + return a.slice(s); +}; +exports.sliceImpl = function sliceImpl (a,s,e) { + return a.slice(s,e); +}; + + exports.sortImpl = function sortImpl (a) { a.sort(); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index ce90458..684b0f6 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -13,6 +13,7 @@ module Data.ArrayBuffer.Typed , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart , copyWithin, copyWithinPart + , copy, sliceRemainder, slice , sort , subArray, subArrayRemainder , toString @@ -144,6 +145,19 @@ copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffse copyWithinPart = runEffectFn4 copyWithinImpl3 +-- | Copy the entire contents of the typed array into a new buffer. +foreign import copy :: forall a. ArrayView a -> ArrayView a +foreign import sliceRemainderImpl :: forall a. Fn2 (ArrayView a) ByteOffset (ArrayView a) +foreign import sliceImpl :: forall a. Fn3 (ArrayView a) ByteOffset ByteOffset (ArrayView a) + +-- | Copy the remainder of contents of the typed array into a new buffer, after some start index. +sliceRemainder :: forall a. ArrayView a -> ByteOffset -> ArrayView a +sliceRemainder = runFn2 sliceRemainderImpl +-- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. +slice :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ArrayView a +slice = runFn3 sliceImpl + + foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Sorts the values in-place From b281b9d76c1456d3127fc223dbf7803b19abe566 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:15:40 -0700 Subject: [PATCH 051/236] moving `set` around a little --- bower.json | 3 ++- src/Data/ArrayBuffer/Typed.js | 16 +++++++++------- src/Data/ArrayBuffer/Typed.purs | 30 +++++++++++++++++++++--------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/bower.json b/bower.json index 27ccd59..625fe88 100644 --- a/bower.json +++ b/bower.json @@ -16,7 +16,8 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0" + "purescript-uint": "^4.0.0", + "purescript-nullable": "^4.1.0" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 829a9e5..e989975 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -75,6 +75,15 @@ exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { }; +exports.setImpl = function(a, off, b) { + if (off === null) { + a.set(b); + } else { + a.set(b,off); + } +} + + exports.copy = function copy (a) { return a.slice(); }; @@ -103,13 +112,6 @@ exports.toString = function toString (a) { return a.toString(); }; - -exports.setImpl = function(ra, off, a) { - return function() { - a.set(ra, off); - }; -} - exports.unsafeAtImpl = function(a, i) { return a[i]; } diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 684b0f6..b6ddf61 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -11,13 +11,13 @@ module Data.ArrayBuffer.Typed , bytesPer , length , class ValuesPer - , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart + , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart, set, set' , copyWithin, copyWithinPart + , setTyped, setTyped' , copy, sliceRemainder, slice , sort , subArray, subArrayRemainder , toString - , set , unsafeAt , hasIndex , at @@ -31,6 +31,7 @@ import Effect.Uncurried ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1) import Effect.Unsafe (unsafePerformEffect) +import Data.Nullable (Nullable, toNullable) import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength , Float64Array, Float32Array @@ -115,6 +116,10 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where fillRemainder :: ArrayView a -> t -> ByteOffset -> Effect Unit -- | Fill part of the array with a value fillPart :: ArrayView a -> t -> ByteOffset -> ByteOffset -> Effect Unit + -- | Stores multiple values into the typed array + set :: ArrayView a -> Array t -> Effect Unit + -- | Stores multiple values into the typed array, with offset + set' :: ArrayView a -> ByteOffset -> Array t -> Effect Unit instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) @@ -127,6 +132,8 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where fill = runEffectFn2 fillImpl fillRemainder = runEffectFn3 fillImpl2 fillPart = runEffectFn4 fillImpl3 + set a x = runEffectFn3 setImpl a (toNullable Nothing) x + set' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int @@ -145,6 +152,18 @@ copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffse copyWithinPart = runEffectFn4 copyWithinImpl3 +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) (Nullable ByteOffset) b Unit + + +-- | Stores multiple values in the typed array, reading input values from the second typed array. +setTyped :: forall a. ArrayView a -> ArrayView a -> Effect Unit +setTyped a x = runEffectFn3 setImpl a (toNullable Nothing) x + +-- | Stores multiple values in the typed array, reading input values from the second typed array, with offset. +setTyped' :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit +setTyped' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x + + -- | Copy the entire contents of the typed array into a new buffer. foreign import copy :: forall a. ArrayView a -> ArrayView a foreign import sliceRemainderImpl :: forall a. Fn2 (ArrayView a) ByteOffset (ArrayView a) @@ -180,13 +199,6 @@ subArrayRemainder = runFn2 subArrayImpl -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String - -foreign import setImpl :: forall a. Fn3 (ArrayView a) ByteOffset (ArrayView a) (Effect Unit) - --- | Stores multiple values in the last typed array, reading input values from ther first typed array. -set :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit -set = runFn3 setImpl - foreign import unsafeAtImpl :: forall a. EffectFn2 (ArrayView a) Int Number -- | Fetch element at index. From f8ae9adc1640d635a959ed7934fbb497a4506183 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:17:42 -0700 Subject: [PATCH 052/236] reverse in-place --- src/Data/ArrayBuffer/Typed.js | 9 +++++++-- src/Data/ArrayBuffer/Typed.purs | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index e989975..79454f4 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -75,13 +75,18 @@ exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { }; -exports.setImpl = function(a, off, b) { +exports.reverseImpl = function reverseImpl (a) { + a.reverse(); +}; + + +exports.setImpl = function setImpl (a, off, b) { if (off === null) { a.set(b); } else { a.set(b,off); } -} +}; exports.copy = function copy (a) { diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b6ddf61..77ae274 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -13,6 +13,7 @@ module Data.ArrayBuffer.Typed , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart, set, set' , copyWithin, copyWithinPart + , reverse , setTyped, setTyped' , copy, sliceRemainder, slice , sort @@ -152,6 +153,12 @@ copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffse copyWithinPart = runEffectFn4 copyWithinImpl3 +foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit + +-- | Reverses a typed array in-place. +reverse :: forall a. ArrayView a -> Effect Unit +reverse = runEffectFn1 reverseImpl + foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) (Nullable ByteOffset) b Unit From be5e28b45cfdccebee9a02af067091e74404abc1 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:21:09 -0700 Subject: [PATCH 053/236] typed map --- src/Data/ArrayBuffer/Typed.js | 5 +++++ src/Data/ArrayBuffer/Typed.purs | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 79454f4..70ff633 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -67,6 +67,11 @@ exports.fillImpl3 = function fillImpl3 (a,x,s,e) { }; +exports.mapImpl = function mapImpl (a,f) { + return a.map(f); +}; + + exports.copyWithinImpl = function copyWithinImpl (a,t,s) { a.copyWithin(t,s); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 77ae274..996c0fd 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -12,6 +12,7 @@ module Data.ArrayBuffer.Typed , length , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart, set, set' + , map' , copyWithin, copyWithinPart , reverse , setTyped, setTyped' @@ -93,6 +94,8 @@ foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b ByteOffset Unit foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b ByteOffset ByteOffset Unit +foreign import mapImpl :: forall a b. Fn2 (ArrayView a) (b -> b) (ArrayView a) + -- TODO use purescript-quotient -- | Measured user-level values stored in each typed array @@ -121,6 +124,8 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where set :: ArrayView a -> Array t -> Effect Unit -- | Stores multiple values into the typed array, with offset set' :: ArrayView a -> ByteOffset -> Array t -> Effect Unit + -- | Maps a new value over the typed array, creating a new buffer and typed array as well. + map' :: (t -> t) -> ArrayView a -> ArrayView a instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) @@ -135,6 +140,7 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where fillPart = runEffectFn4 fillImpl3 set a x = runEffectFn3 setImpl a (toNullable Nothing) x set' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x + map' f a = runFn2 mapImpl a f -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int @@ -152,7 +158,6 @@ copyWithin = runEffectFn3 copyWithinImpl copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffset -> Effect Unit copyWithinPart = runEffectFn4 copyWithinImpl3 - foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Reverses a typed array in-place. From f2be9a47b6d49a24df801f1ea65359d19c94f338 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:25:09 -0700 Subject: [PATCH 054/236] joining an array --- src/Data/ArrayBuffer/Typed.js | 4 ++++ src/Data/ArrayBuffer/Typed.purs | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 70ff633..96cea0c 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -122,6 +122,10 @@ exports.toString = function toString (a) { return a.toString(); }; +exports.joinImpl = function joinImpl (a,s) { + return a.join(s); +}; + exports.unsafeAtImpl = function(a, i) { return a[i]; } diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 996c0fd..d9886be 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -20,6 +20,7 @@ module Data.ArrayBuffer.Typed , sort , subArray, subArrayRemainder , toString + , toString' , unsafeAt , hasIndex , at @@ -211,6 +212,12 @@ subArrayRemainder = runFn2 subArrayImpl -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String +foreign import joinImpl :: forall a. Fn2 (ArrayView a) String String + +-- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. +toString' :: forall a. ArrayView a -> String -> String +toString' = runFn2 joinImpl + foreign import unsafeAtImpl :: forall a. EffectFn2 (ArrayView a) Int Number -- | Fetch element at index. From fa53dca8856aceb18285c3f0213c30a88dc831ad Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:35:22 -0700 Subject: [PATCH 055/236] filter --- src/Data/ArrayBuffer/Typed.js | 8 ++++++++ src/Data/ArrayBuffer/Typed.purs | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 96cea0c..ad3f378 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -71,6 +71,14 @@ exports.mapImpl = function mapImpl (a,f) { return a.map(f); }; +exports.forEachImpl = function forEachImpl (a,f) { + a.forEach(f); +}; + +exports.filterImpl = function filterImpl (a,p) { + return a.filter(p); +}; + exports.copyWithinImpl = function copyWithinImpl (a,t,s) { a.copyWithin(t,s); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index d9886be..15c4d81 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -12,7 +12,7 @@ module Data.ArrayBuffer.Typed , length , class ValuesPer , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart, set, set' - , map' + , map', traverse', traverse_', filter , copyWithin, copyWithinPart , reverse , setTyped, setTyped' @@ -32,7 +32,8 @@ import Prelude import Effect (Effect) import Effect.Uncurried ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 - , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1) + , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1 + , mkEffectFn1) import Effect.Unsafe (unsafePerformEffect) import Data.Nullable (Nullable, toNullable) import Data.ArrayBuffer.Types @@ -95,7 +96,9 @@ foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b ByteOffset Unit foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b ByteOffset ByteOffset Unit -foreign import mapImpl :: forall a b. Fn2 (ArrayView a) (b -> b) (ArrayView a) +foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b b) (ArrayView a) +foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b Unit) Unit +foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) (ArrayView a) -- TODO use purescript-quotient @@ -127,6 +130,12 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where set' :: ArrayView a -> ByteOffset -> Array t -> Effect Unit -- | Maps a new value over the typed array, creating a new buffer and typed array as well. map' :: (t -> t) -> ArrayView a -> ArrayView a + -- | Traverses over each value, returning a new one + traverse' :: (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) + -- | Traverses over each value + traverse_' :: (t -> Effect Unit) -> ArrayView a -> Effect Unit + -- | Returns a new typed array with all values that pass the predicate + filter :: (t -> Boolean) -> ArrayView a -> ArrayView a instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) @@ -141,7 +150,10 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where fillPart = runEffectFn4 fillImpl3 set a x = runEffectFn3 setImpl a (toNullable Nothing) x set' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x - map' f a = runFn2 mapImpl a f + map' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn1 (pure <<< f))) + traverse' f a = runEffectFn2 mapImpl a (mkEffectFn1 f) + traverse_' f a = runEffectFn2 forEachImpl a (mkEffectFn1 f) + filter p a = runFn2 filterImpl a p -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int From a3da940e438d62ee57123d7e49cff2edd32fb88a Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 13:48:18 -0700 Subject: [PATCH 056/236] toArray typed properly --- src/Data/ArrayBuffer/Typed.js | 10 +--- src/Data/ArrayBuffer/Typed.purs | 83 +++++++++++++++++---------------- 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index ad3f378..911b446 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -142,18 +142,10 @@ exports.hasIndexImpl = function(a, i) { return i in a; } -exports.toArray = function(a) { +exports.toArrayImpl = function(a) { var l = a.length; var ret = new Array(l); for (var i = 0; i < l; i++) ret[i] = a[i]; return ret; } - -exports.toIntArray = function(a) { - var l = a.length; - var ret = new Array(l); - for (var i = 0; i < l; i++) - ret[i] = a[i] | 0; - return ret; -} diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 15c4d81..f1e57bb 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -4,6 +4,7 @@ module Data.ArrayBuffer.Typed ( polyFill , buffer + , Offset, Length , byteOffset , byteLength , AProxy (..) @@ -25,7 +26,6 @@ module Data.ArrayBuffer.Typed , hasIndex , at , toArray - , toIntArray ) where import Prelude @@ -58,7 +58,7 @@ foreign import byteOffset :: forall a. ArrayView a -> ByteOffset -- | Represents the length of this typed array, in bytes. foreign import byteLength :: forall a. ArrayView a -> ByteLength -foreign import lengthImpl :: forall a. ArrayView a -> Int +foreign import lengthImpl :: forall a. ArrayView a -> Length data AProxy (a :: ArrayViewType) = AProxy @@ -93,25 +93,31 @@ foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean foreign import someImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit -foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b ByteOffset Unit -foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b ByteOffset ByteOffset Unit +foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b Offset Unit +foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b Unit) Unit foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) (ArrayView a) +-- | Value-oriented array offset +type Offset = Int +-- | Value-oriented array length +type Length = Int + + -- TODO use purescript-quotient -- | Measured user-level values stored in each typed array class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where -- | View mapping the whole `ArrayBuffer`. whole :: ArrayBuffer -> ArrayView a -- | View mapping the rest of an `ArrayBuffer` after an index. - remainder :: ArrayBuffer -> ByteOffset -> Effect (ArrayView a) + remainder :: ArrayBuffer -> Offset -> Effect (ArrayView a) -- | View mapping a region of the `ArrayBuffer`. - part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect (ArrayView a) + part :: ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) -- | Creates an empty typed array, where each value is assigned 0 - empty :: Int -> ArrayView a + empty :: Length -> ArrayView a -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: Array t -> ArrayView a -- | Test a predicate to pass on all values @@ -121,13 +127,13 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where -- | Fill the array with a value fill :: ArrayView a -> t -> Effect Unit -- | Fill the remainder of the array with a value - fillRemainder :: ArrayView a -> t -> ByteOffset -> Effect Unit + fillRemainder :: ArrayView a -> t -> Offset -> Effect Unit -- | Fill part of the array with a value - fillPart :: ArrayView a -> t -> ByteOffset -> ByteOffset -> Effect Unit + fillPart :: ArrayView a -> t -> Offset -> Offset -> Effect Unit -- | Stores multiple values into the typed array set :: ArrayView a -> Array t -> Effect Unit -- | Stores multiple values into the typed array, with offset - set' :: ArrayView a -> ByteOffset -> Array t -> Effect Unit + set' :: ArrayView a -> Offset -> Array t -> Effect Unit -- | Maps a new value over the typed array, creating a new buffer and typed array as well. map' :: (t -> t) -> ArrayView a -> ArrayView a -- | Traverses over each value, returning a new one @@ -136,6 +142,8 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where traverse_' :: (t -> Effect Unit) -> ArrayView a -> Effect Unit -- | Returns a new typed array with all values that pass the predicate filter :: (t -> Boolean) -> ArrayView a -> ArrayView a + -- | Fetch element at index. + unsafeAt :: ArrayView a -> Offset -> Effect t instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) @@ -154,6 +162,7 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where traverse' f a = runEffectFn2 mapImpl a (mkEffectFn1 f) traverse_' f a = runEffectFn2 forEachImpl a (mkEffectFn1 f) filter p a = runFn2 filterImpl a p + unsafeAt = runEffectFn2 unsafeAtImpl -- instance valuesPerUint32 :: ValuesPer Uint32 Number -- instance valuesPerUint16 :: ValuesPer Uint16 Int -- instance valuesPerUint8 :: ValuesPer Uint8 Int @@ -162,13 +171,13 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where -- instance valuesPerInt8 :: ValuesPer Int8 Int -foreign import copyWithinImpl :: forall a. EffectFn3 (ArrayView a) ByteOffset ByteOffset Unit -foreign import copyWithinImpl3 :: forall a. EffectFn4 (ArrayView a) ByteOffset ByteOffset ByteOffset Unit +foreign import copyWithinImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset Unit +foreign import copyWithinImpl3 :: forall a. EffectFn4 (ArrayView a) Offset Offset Offset Unit -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. -copyWithin :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> Effect Unit +copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Effect Unit copyWithin = runEffectFn3 copyWithinImpl -copyWithinPart :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ByteOffset -> Effect Unit +copyWithinPart :: forall a. ArrayView a -> Offset -> Offset -> Offset -> Effect Unit copyWithinPart = runEffectFn4 copyWithinImpl3 foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit @@ -177,7 +186,7 @@ foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit reverse :: forall a. ArrayView a -> Effect Unit reverse = runEffectFn1 reverseImpl -foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) (Nullable ByteOffset) b Unit +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) (Nullable Offset) b Unit -- | Stores multiple values in the typed array, reading input values from the second typed array. @@ -185,20 +194,20 @@ setTyped :: forall a. ArrayView a -> ArrayView a -> Effect Unit setTyped a x = runEffectFn3 setImpl a (toNullable Nothing) x -- | Stores multiple values in the typed array, reading input values from the second typed array, with offset. -setTyped' :: forall a. ArrayView a -> ByteOffset -> ArrayView a -> Effect Unit +setTyped' :: forall a. ArrayView a -> Offset -> ArrayView a -> Effect Unit setTyped' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x -- | Copy the entire contents of the typed array into a new buffer. foreign import copy :: forall a. ArrayView a -> ArrayView a -foreign import sliceRemainderImpl :: forall a. Fn2 (ArrayView a) ByteOffset (ArrayView a) -foreign import sliceImpl :: forall a. Fn3 (ArrayView a) ByteOffset ByteOffset (ArrayView a) +foreign import sliceRemainderImpl :: forall a. Fn2 (ArrayView a) Offset (ArrayView a) +foreign import sliceImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) -- | Copy the remainder of contents of the typed array into a new buffer, after some start index. -sliceRemainder :: forall a. ArrayView a -> ByteOffset -> ArrayView a +sliceRemainder :: forall a. ArrayView a -> Offset -> ArrayView a sliceRemainder = runFn2 sliceRemainderImpl -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ArrayView a +slice :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a slice = runFn3 sliceImpl @@ -209,15 +218,15 @@ sort :: forall a. ArrayView a -> Effect Unit sort = runEffectFn1 sortImpl -foreign import subArrayImpl :: forall a. Fn2 (ArrayView a) ByteOffset (ArrayView a) -foreign import subArrayImpl2 :: forall a. Fn3 (ArrayView a) ByteOffset ByteOffset (ArrayView a) +foreign import subArrayImpl :: forall a. Fn2 (ArrayView a) Offset (ArrayView a) +foreign import subArrayImpl2 :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -subArray :: forall a. ArrayView a -> ByteOffset -> ByteOffset -> ArrayView a +subArray :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a subArray = runFn3 subArrayImpl2 -- | Returns a new typed array view of the same buffer, beginning at the index -subArrayRemainder :: forall a. ArrayView a -> ByteOffset -> ArrayView a +subArrayRemainder :: forall a. ArrayView a -> Offset -> ArrayView a subArrayRemainder = runFn2 subArrayImpl @@ -230,30 +239,24 @@ foreign import joinImpl :: forall a. Fn2 (ArrayView a) String String toString' :: forall a. ArrayView a -> String -> String toString' = runFn2 joinImpl -foreign import unsafeAtImpl :: forall a. EffectFn2 (ArrayView a) Int Number --- | Fetch element at index. -unsafeAt :: forall a. ArrayView a -> Int -> Effect Number -unsafeAt = runEffectFn2 unsafeAtImpl +foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b -foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Int Boolean +foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean -- | Determine if a certain index is valid. -hasIndex :: forall a. ArrayView a -> Int -> Boolean +hasIndex :: forall a. ArrayView a -> Offset -> Boolean hasIndex = runFn2 hasIndexImpl -- | Fetch element at index. -at :: forall a. ArrayView a -> Int -> Effect (Maybe Number) +at :: forall a t. ValuesPer a t => ArrayView a -> Offset -> Maybe t at a n = do if a `hasIndex` n - then do - element <- unsafeAt a n - pure $ Just element - else - pure Nothing + then Just (unsafePerformEffect (unsafeAt a n)) + else Nothing --- | Turn typed array into an array. -foreign import toArray :: forall a. ArrayView a -> Array Number +foreign import toArrayImpl :: forall a b. ArrayView a -> Array b --- | Turn typed array into integer array. -foreign import toIntArray :: forall a. ArrayView a -> Array Int +-- | Turn typed array into an array. +toArray :: forall a t. ValuesPer a t => ArrayView a -> Array t +toArray = toArrayImpl From 588284db831c53fc904d22d2ecf6d2dc2a2d82c8 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 14:51:07 -0700 Subject: [PATCH 057/236] folds --- .../Data/ArrayBuffer/ArrayBuffer.md | 30 ++ generated-docs/Data/ArrayBuffer/DataView.md | 270 ++++++++++++++++++ generated-docs/Data/ArrayBuffer/Show.md | 9 + generated-docs/Data/ArrayBuffer/Typed.md | 241 ++++++++++++++++ src/Data/ArrayBuffer/Typed.js | 13 + src/Data/ArrayBuffer/Typed.purs | 99 ++++--- 6 files changed, 626 insertions(+), 36 deletions(-) create mode 100644 generated-docs/Data/ArrayBuffer/ArrayBuffer.md create mode 100644 generated-docs/Data/ArrayBuffer/DataView.md create mode 100644 generated-docs/Data/ArrayBuffer/Show.md create mode 100644 generated-docs/Data/ArrayBuffer/Typed.md diff --git a/generated-docs/Data/ArrayBuffer/ArrayBuffer.md b/generated-docs/Data/ArrayBuffer/ArrayBuffer.md new file mode 100644 index 0000000..35ed8a4 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/ArrayBuffer.md @@ -0,0 +1,30 @@ +## Module Data.ArrayBuffer.ArrayBuffer + +This module represents the functional bindings to JavaScript's `ArrayBuffer` +objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) for details. + +#### `empty` + +``` purescript +empty :: ByteLength -> ArrayBuffer +``` + +Create an `ArrayBuffer` with the given capacity. + +#### `byteLength` + +``` purescript +byteLength :: ArrayBuffer -> ByteLength +``` + +Represents the length of an `ArrayBuffer` in bytes. + +#### `slice` + +``` purescript +slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer +``` + +Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. + + diff --git a/generated-docs/Data/ArrayBuffer/DataView.md b/generated-docs/Data/ArrayBuffer/DataView.md new file mode 100644 index 0000000..88a3c48 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/DataView.md @@ -0,0 +1,270 @@ +## Module Data.ArrayBuffer.DataView + +This module represents the functional bindings to JavaScript's `DataView` +objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. + +#### `whole` + +``` purescript +whole :: ArrayBuffer -> DataView +``` + +View mapping the whole `ArrayBuffer`. + +#### `remainder` + +``` purescript +remainder :: ArrayBuffer -> ByteOffset -> Effect DataView +``` + +View mapping the rest of an `ArrayBuffer` after an index. + +#### `part` + +``` purescript +part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView +``` + +View mapping a region of the `ArrayBuffer`. + +#### `buffer` + +``` purescript +buffer :: DataView -> ArrayBuffer +``` + +`ArrayBuffer` being mapped by the view. + +#### `byteOffset` + +``` purescript +byteOffset :: DataView -> ByteOffset +``` + +Represents the offset of this view from the start of its `ArrayBuffer`. + +#### `byteLength` + +``` purescript +byteLength :: DataView -> ByteLength +``` + +Represents the length of this view. + +#### `Getter` + +``` purescript +type Getter r = DataView -> ByteOffset -> Effect (Maybe r) +``` + +Type for all fetching functions. + +#### `getInt8` + +``` purescript +getInt8 :: Getter Int +``` + +Fetch int8 value at a certain index in a `DataView`. + +#### `getInt16be` + +``` purescript +getInt16be :: Getter Int +``` + +Fetch int16 value at a certain index in a `DataView`. + +#### `getInt32be` + +``` purescript +getInt32be :: Getter Int +``` + +Fetch int32 value at a certain index in a `DataView`. + +#### `getUint8` + +``` purescript +getUint8 :: Getter UInt +``` + +Fetch uint8 value at a certain index in a `DataView`. + +#### `getUint16be` + +``` purescript +getUint16be :: Getter UInt +``` + +Fetch uint16 value at a certain index in a `DataView`. + +#### `getUint32be` + +``` purescript +getUint32be :: Getter UInt +``` + +Fetch uint32 value at a certain index in a `DataView`. + +#### `getFloat32be` + +``` purescript +getFloat32be :: Getter Number +``` + +Fetch float32 value at a certain index in a `DataView`. + +#### `getFloat64be` + +``` purescript +getFloat64be :: Getter Number +``` + +Fetch float64 value at a certain index in a `DataView`. + +#### `getInt16le` + +``` purescript +getInt16le :: Getter Int +``` + +#### `getInt32le` + +``` purescript +getInt32le :: Getter Int +``` + +#### `getUint16le` + +``` purescript +getUint16le :: Getter UInt +``` + +#### `getUint32le` + +``` purescript +getUint32le :: Getter UInt +``` + +#### `getFloat32le` + +``` purescript +getFloat32le :: Getter Number +``` + +#### `getFloat64le` + +``` purescript +getFloat64le :: Getter Number +``` + +#### `Setter` + +``` purescript +type Setter r = DataView -> r -> ByteOffset -> Effect Unit +``` + +Type for all storing functions. + +#### `setInt8` + +``` purescript +setInt8 :: Setter Int +``` + +Store int8 value at a certain index in a `DataView`. + +#### `setInt16be` + +``` purescript +setInt16be :: Setter Int +``` + +Store int16 value at a certain index in a `DataView`. + +#### `setInt32be` + +``` purescript +setInt32be :: Setter Int +``` + +Store int32 value at a certain index in a `DataView`. + +#### `setUint8` + +``` purescript +setUint8 :: Setter UInt +``` + +Store uint8 value at a certain index in a `DataView`. + +#### `setUint16be` + +``` purescript +setUint16be :: Setter UInt +``` + +Store uint16 value at a certain index in a `DataView`. + +#### `setUint32be` + +``` purescript +setUint32be :: Setter UInt +``` + +Store uint32 value at a certain index in a `DataView`. + +#### `setFloat32be` + +``` purescript +setFloat32be :: Setter Number +``` + +Store float32 value at a certain index in a `DataView`. + +#### `setFloat64be` + +``` purescript +setFloat64be :: Setter Number +``` + +Store float64 value at a certain index in a `DataView`. + +#### `setInt16le` + +``` purescript +setInt16le :: Setter Int +``` + +#### `setInt32le` + +``` purescript +setInt32le :: Setter Int +``` + +#### `setUint16le` + +``` purescript +setUint16le :: Setter UInt +``` + +#### `setUint32le` + +``` purescript +setUint32le :: Setter UInt +``` + +#### `setFloat32le` + +``` purescript +setFloat32le :: Setter Number +``` + +#### `setFloat64le` + +``` purescript +setFloat64le :: Setter Number +``` + + diff --git a/generated-docs/Data/ArrayBuffer/Show.md b/generated-docs/Data/ArrayBuffer/Show.md new file mode 100644 index 0000000..88a5008 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/Show.md @@ -0,0 +1,9 @@ +## Module Data.ArrayBuffer.Show + +#### `showViaInspect` + +``` purescript +showViaInspect :: forall a. ArrayView a -> String +``` + + diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md new file mode 100644 index 0000000..3aecec6 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -0,0 +1,241 @@ +## Module Data.ArrayBuffer.Typed + +This module represents the functional bindings to JavaScript's `TypedArray` and other +objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) for details. + +#### `polyFill` + +``` purescript +polyFill :: Effect Unit +``` + +Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill + +#### `buffer` + +``` purescript +buffer :: forall a. ArrayView a -> ArrayBuffer +``` + +`ArrayBuffer` being mapped by the typed array. + +#### `Offset` + +``` purescript +type Offset = Int +``` + +Value-oriented array offset + +#### `Length` + +``` purescript +type Length = Int +``` + +Value-oriented array length + +#### `byteOffset` + +``` purescript +byteOffset :: forall a. ArrayView a -> ByteOffset +``` + +Represents the offset of this view from the start of its `ArrayBuffer`. + +#### `byteLength` + +``` purescript +byteLength :: forall a. ArrayView a -> ByteLength +``` + +Represents the length of this typed array, in bytes. + +#### `AProxy` + +``` purescript +data AProxy (a :: ArrayViewType) + = AProxy +``` + +#### `BytesPerValue` + +``` purescript +class BytesPerValue (a :: ArrayViewType) where + bytesPerValue :: AProxy a -> Int +``` + +##### Instances +``` purescript +BytesPerValue Uint8Clamped +BytesPerValue Uint32 +BytesPerValue Uint16 +BytesPerValue Uint8 +BytesPerValue Int32 +BytesPerValue Int16 +BytesPerValue Int8 +``` + +#### `length` + +``` purescript +length :: forall a. BytesPerValue a => ArrayView a -> Int +``` + +#### `TypedArray` + +``` purescript +class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where + whole :: ArrayBuffer -> ArrayView a + remainder :: ArrayBuffer -> Offset -> Effect (ArrayView a) + part :: ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) + empty :: Length -> ArrayView a + fromArray :: Array t -> ArrayView a + all :: (t -> Boolean) -> ArrayView a -> Boolean + any :: (t -> Boolean) -> ArrayView a -> Boolean + fill :: ArrayView a -> t -> Effect Unit + fillRemainder :: ArrayView a -> t -> Offset -> Effect Unit + fillPart :: ArrayView a -> t -> Offset -> Offset -> Effect Unit + set :: ArrayView a -> Array t -> Effect Unit + set' :: ArrayView a -> Offset -> Array t -> Effect Unit + map' :: (t -> t) -> ArrayView a -> ArrayView a + traverse' :: (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) + traverse_' :: (t -> Effect Unit) -> ArrayView a -> Effect Unit + filter :: (t -> Boolean) -> ArrayView a -> ArrayView a + unsafeAt :: ArrayView a -> Offset -> Effect t +``` + +Measured user-level values stored in each typed array + +##### Instances +``` purescript +TypedArray Uint8Clamped Int +``` + +#### `copyWithin` + +``` purescript +copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Effect Unit +``` + +Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. + +#### `copyWithinPart` + +``` purescript +copyWithinPart :: forall a. ArrayView a -> Offset -> Offset -> Offset -> Effect Unit +``` + +#### `reverse` + +``` purescript +reverse :: forall a. ArrayView a -> Effect Unit +``` + +Reverses a typed array in-place. + +#### `setTyped` + +``` purescript +setTyped :: forall a. ArrayView a -> ArrayView a -> Effect Unit +``` + +Stores multiple values in the typed array, reading input values from the second typed array. + +#### `setTyped'` + +``` purescript +setTyped' :: forall a. ArrayView a -> Offset -> ArrayView a -> Effect Unit +``` + +Stores multiple values in the typed array, reading input values from the second typed array, with offset. + +#### `copy` + +``` purescript +copy :: forall a. ArrayView a -> ArrayView a +``` + +Copy the entire contents of the typed array into a new buffer. + +#### `sliceRemainder` + +``` purescript +sliceRemainder :: forall a. ArrayView a -> Offset -> ArrayView a +``` + +Copy the remainder of contents of the typed array into a new buffer, after some start index. + +#### `slice` + +``` purescript +slice :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a +``` + +Copy part of the contents of a typed array into a new buffer, between some start and end indices. + +#### `sort` + +``` purescript +sort :: forall a. ArrayView a -> Effect Unit +``` + +Sorts the values in-place + +#### `subArray` + +``` purescript +subArray :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a +``` + +Returns a new typed array view of the same buffer, beginning at the index and ending at the second. + +#### `subArrayRemainder` + +``` purescript +subArrayRemainder :: forall a. ArrayView a -> Offset -> ArrayView a +``` + +Returns a new typed array view of the same buffer, beginning at the index + +#### `toString` + +``` purescript +toString :: forall a. ArrayView a -> String +``` + +Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. + +#### `toString'` + +``` purescript +toString' :: forall a. ArrayView a -> String -> String +``` + +Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. + +#### `hasIndex` + +``` purescript +hasIndex :: forall a. ArrayView a -> Offset -> Boolean +``` + +Determine if a certain index is valid. + +#### `at` + +``` purescript +at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t +``` + +Fetch element at index. + +#### `toArray` + +``` purescript +toArray :: forall a t. TypedArray a t => ArrayView a -> Array t +``` + +Turn typed array into an array. + + diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 911b446..77f0275 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -79,6 +79,19 @@ exports.filterImpl = function filterImpl (a,p) { return a.filter(p); }; +exports.reduceImpl = function reduceImpl (a,f,i) { + return a.reduce(f,i); +}; +exports.reduce1Impl = function reduce1Impl (a,f) { + return a.reduce(f); +}; +exports.reduceRightImpl = function reduceRightImpl (a,f,i) { + return a.reduceRight(f,i); +}; +exports.reduceRight1Impl = function reduceRight1Impl (a,f) { + return a.reduceRight(f); +}; + exports.copyWithinImpl = function copyWithinImpl (a,t,s) { a.copyWithin(t,s); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f1e57bb..6e48b18 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -8,12 +8,12 @@ module Data.ArrayBuffer.Typed , byteOffset , byteLength , AProxy (..) - , class BytesPer - , bytesPer + , class BytesPerValue + , bytesPerValue , length - , class ValuesPer + , class TypedArray , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart, set, set' - , map', traverse', traverse_', filter + , map', traverse', traverse_', filter, foldlM', foldl', foldl1', foldrM', foldr', foldr1' , copyWithin, copyWithinPart , reverse , setTyped, setTyped' @@ -33,7 +33,7 @@ import Effect (Effect) import Effect.Uncurried ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1 - , mkEffectFn1) + , mkEffectFn1, mkEffectFn2) import Effect.Unsafe (unsafePerformEffect) import Data.Nullable (Nullable, toNullable) import Data.ArrayBuffer.Types @@ -42,7 +42,7 @@ import Data.ArrayBuffer.Types , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array , Float64, Float32 , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) +import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3, mkFn2) import Data.Maybe (Maybe(..)) @@ -62,33 +62,37 @@ foreign import lengthImpl :: forall a. ArrayView a -> Length data AProxy (a :: ArrayViewType) = AProxy -class BytesPer (a :: ArrayViewType) where - bytesPer :: AProxy a -> Int - -instance bytesPerUint8Clamped :: BytesPer Uint8Clamped where - bytesPer AProxy = 1 -instance bytesPerUint32 :: BytesPer Uint32 where - bytesPer AProxy = 4 -instance bytesPerUint16 :: BytesPer Uint16 where - bytesPer AProxy = 2 -instance bytesPerUint8 :: BytesPer Uint8 where - bytesPer AProxy = 1 -instance bytesPerInt32 :: BytesPer Int32 where - bytesPer AProxy = 4 -instance bytesPerInt16 :: BytesPer Int16 where - bytesPer AProxy = 2 -instance bytesPerInt8 :: BytesPer Int8 where - bytesPer AProxy = 1 - -length :: forall a. BytesPer a => ArrayView a -> Int +class BytesPerValue (a :: ArrayViewType) where + bytesPerValue :: AProxy a -> Int + +instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped where + bytesPerValue AProxy = 1 +instance bytesPerValueUint32 :: BytesPerValue Uint32 where + bytesPerValue AProxy = 4 +instance bytesPerValueUint16 :: BytesPerValue Uint16 where + bytesPerValue AProxy = 2 +instance bytesPerValueUint8 :: BytesPerValue Uint8 where + bytesPerValue AProxy = 1 +instance bytesPerValueInt32 :: BytesPerValue Int32 where + bytesPerValue AProxy = 4 +instance bytesPerValueInt16 :: BytesPerValue Int16 where + bytesPerValue AProxy = 2 +instance bytesPerValueInt8 :: BytesPerValue Int8 where + bytesPerValue AProxy = 1 + +length :: forall a. BytesPerValue a => ArrayView a -> Int length = lengthImpl +-- object creator implementations for each typed array + foreign import newUint8ClampedArray :: forall a. EffectFn1 a Uint8ClampedArray foreign import newUint8ClampedArray2 :: EffectFn2 ArrayBuffer ByteOffset Uint8ClampedArray foreign import newUint8ClampedArray3 :: EffectFn3 ArrayBuffer ByteOffset ByteLength Uint8ClampedArray +-- ---- + foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean foreign import someImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean @@ -99,6 +103,10 @@ foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b Offset Offset foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b Unit) Unit foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) (ArrayView a) +foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn2 c b c) c c +foreign import reduce1Impl :: forall a b. Fn2 (ArrayView a) (Fn2 b b b) b +foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn2 c b c) c c +foreign import reduceRight1Impl :: forall a b. Fn2 (ArrayView a) (Fn2 b b b) b -- | Value-oriented array offset @@ -109,7 +117,7 @@ type Length = Int -- TODO use purescript-quotient -- | Measured user-level values stored in each typed array -class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where +class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where -- | View mapping the whole `ArrayBuffer`. whole :: ArrayBuffer -> ArrayView a -- | View mapping the rest of an `ArrayBuffer` after an index. @@ -144,8 +152,16 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where filter :: (t -> Boolean) -> ArrayView a -> ArrayView a -- | Fetch element at index. unsafeAt :: ArrayView a -> Offset -> Effect t - -instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where + -- | Folding from the left + foldlM' :: forall b. ArrayView a -> (b -> t -> Effect b) -> b -> Effect b + -- | Assumes the typed array is non-empty + foldl1' :: ArrayView a -> (t -> t -> t) -> t + -- | Folding from the right + foldrM' :: forall b. ArrayView a -> (t -> b -> Effect b) -> b -> Effect b + -- | Assumes the typed array is non-empty + foldr1' :: ArrayView a -> (t -> t -> t) -> t + +instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) remainder = runEffectFn2 newUint8ClampedArray2 part = runEffectFn3 newUint8ClampedArray3 @@ -163,12 +179,23 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where traverse_' f a = runEffectFn2 forEachImpl a (mkEffectFn1 f) filter p a = runFn2 filterImpl a p unsafeAt = runEffectFn2 unsafeAtImpl --- instance valuesPerUint32 :: ValuesPer Uint32 Number --- instance valuesPerUint16 :: ValuesPer Uint16 Int --- instance valuesPerUint8 :: ValuesPer Uint8 Int --- instance valuesPerInt32 :: ValuesPer Int32 Number --- instance valuesPerInt16 :: ValuesPer Int16 Int --- instance valuesPerInt8 :: ValuesPer Int8 Int + foldlM' a f = runEffectFn3 reduceImpl a (mkEffectFn2 f) + foldl1' a f = runFn2 reduce1Impl a (mkFn2 f) + foldrM' a f = runEffectFn3 reduceRightImpl a (mkEffectFn2 (flip f)) + foldr1' a f = runFn2 reduceRight1Impl a (mkFn2 (flip f)) +-- instance typedArrayUint32 :: TypedArray Uint32 Number +-- instance typedArrayUint16 :: TypedArray Uint16 Int +-- instance typedArrayUint8 :: TypedArray Uint8 Int +-- instance typedArrayInt32 :: TypedArray Int32 Number +-- instance typedArrayInt16 :: TypedArray Int16 Int +-- instance typedArrayInt8 :: TypedArray Int8 Int + + +foldl' :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> b) -> b -> b +foldl' a f i = unsafePerformEffect (foldlM' a (\acc x -> pure (f acc x)) i) + +foldr' :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> b) -> b -> b +foldr' a f i = unsafePerformEffect (foldrM' a (\x acc -> pure (f x acc)) i) foreign import copyWithinImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset Unit @@ -249,7 +276,7 @@ hasIndex :: forall a. ArrayView a -> Offset -> Boolean hasIndex = runFn2 hasIndexImpl -- | Fetch element at index. -at :: forall a t. ValuesPer a t => ArrayView a -> Offset -> Maybe t +at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t at a n = do if a `hasIndex` n then Just (unsafePerformEffect (unsafeAt a n)) @@ -258,5 +285,5 @@ at a n = do foreign import toArrayImpl :: forall a b. ArrayView a -> Array b -- | Turn typed array into an array. -toArray :: forall a t. ValuesPer a t => ArrayView a -> Array t +toArray :: forall a t. TypedArray a t => ArrayView a -> Array t toArray = toArrayImpl From 3b246b9b964a1e066aae6e0f3e70b8cd2d690585 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 16:32:20 -0700 Subject: [PATCH 058/236] elem, need to do find and indexing --- src/Data/ArrayBuffer/Typed.js | 84 +++++++++------ src/Data/ArrayBuffer/Typed.purs | 180 +++++++++++++++----------------- 2 files changed, 133 insertions(+), 131 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 77f0275..99b3561 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -37,14 +37,16 @@ exports.lengthImpl = function lemgthImpl (v) { // Uint8Clamped -exports.newUint8ClampedArray = function newUint8ClampedArray (a) { - return new Uint8ClampedArray(a); -}; -exports.newUint8ClampedArray2 = function newUint8ClampedArray2 (a,b) { - return new Uint8ClampedArray(a,b); -}; -exports.newUint8ClampedArray3 = function newUint8ClampedArray3 (a,b,c) { - return new Uint8ClampedArray(a,b,c); +exports.newUint8ClampedArray = function newUint8ClampedArray (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Uint8ClampedArray(a); + } else { + return new Uint8ClampedArray(a,mb); + } + } else { + return new Uint8ClampedArray(a,mb,mc); + } }; @@ -56,14 +58,16 @@ exports.someImpl = function someImpl (a,p) { }; -exports.fillImpl = function fillImpl (a,x) { - return a.fill(x); -}; -exports.fillImpl2 = function fillImpl2 (a,x,s) { - return a.fill(x,s); -}; -exports.fillImpl3 = function fillImpl3 (a,x,s,e) { - return a.fill(x,s,e); +exports.fillImpl = function fillImpl (a,x,ms,me) { + if (me === null) { + if (ms === null) { + return a.fill(x); + } else { + return a.fill(x,ms); + } + } else { + return a.fill(x,ms,me); + } }; @@ -79,6 +83,14 @@ exports.filterImpl = function filterImpl (a,p) { return a.filter(p); }; +exports.includesImpl = function includesImpl (a,x,mo) { + if (mo === null) { + return a.includes(x); + } else { + return a.includes(x,mo); + } +}; + exports.reduceImpl = function reduceImpl (a,f,i) { return a.reduce(f,i); }; @@ -93,11 +105,12 @@ exports.reduceRight1Impl = function reduceRight1Impl (a,f) { }; -exports.copyWithinImpl = function copyWithinImpl (a,t,s) { - a.copyWithin(t,s); -}; -exports.copyWithinImpl3 = function copyWithinImpl (a,t,s,e) { - a.copyWithin(t,s,e); +exports.copyWithinImpl = function copyWithinImpl (a,t,s,me) { + if (me === null) { + a.copyWithin(t,s); + } else { + a.copyWithin(t,s,me); + } }; @@ -115,14 +128,16 @@ exports.setImpl = function setImpl (a, off, b) { }; -exports.copy = function copy (a) { - return a.slice(); -}; -exports.sliceRemainderImpl = function sliceRemainderImpl (a,s) { - return a.slice(s); -}; -exports.sliceImpl = function sliceImpl (a,s,e) { - return a.slice(s,e); +exports.sliceImpl = function sliceImpl (a,ms,me) { + if (me === null) { + if (ms === null) { + return a.slice(); + } else { + return a.slice(ms); + } + } else { + return a.slice(s,e); + } }; @@ -131,11 +146,12 @@ exports.sortImpl = function sortImpl (a) { }; -exports.subArrayImpl = function subArrayImpl (a,s) { - return a.subarray(s); -}; -exports.subArrayImpl2 = function subArrayImpl2 (a,s,e) { - return a.subarray(s,e); +exports.subArrayImpl = function subArrayImpl (a,s,me) { + if (me === null) { + return a.subarray(s); + } else { + return a.subarray(s,me); + } }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 6e48b18..14a59c5 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -12,19 +12,16 @@ module Data.ArrayBuffer.Typed , bytesPerValue , length , class TypedArray - , whole, remainder, part, empty, fromArray, all, any, fill, fillRemainder, fillPart, set, set' - , map', traverse', traverse_', filter, foldlM', foldl', foldl1', foldrM', foldr', foldr1' - , copyWithin, copyWithinPart + , whole, remainder, part, empty, fromArray, all, any, fill, set + , map, traverse, traverse_, filter, elem, foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 + , copyWithin , reverse - , setTyped, setTyped' - , copy, sliceRemainder, slice + , setTyped + , slice , sort - , subArray, subArrayRemainder - , toString - , toString' - , unsafeAt - , hasIndex - , at + , subArray + , toString, toString' + , unsafeAt, hasIndex, at , toArray ) where @@ -33,8 +30,9 @@ import Effect (Effect) import Effect.Uncurried ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1 - , mkEffectFn1, mkEffectFn2) + , mkEffectFn1, mkEffectFn2, mkEffectFn3) import Effect.Unsafe (unsafePerformEffect) +import Data.Tuple (Tuple (..)) import Data.Nullable (Nullable, toNullable) import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength @@ -86,27 +84,24 @@ length = lengthImpl -- object creator implementations for each typed array -foreign import newUint8ClampedArray :: forall a. EffectFn1 a Uint8ClampedArray -foreign import newUint8ClampedArray2 :: EffectFn2 ArrayBuffer ByteOffset Uint8ClampedArray -foreign import newUint8ClampedArray3 :: EffectFn3 ArrayBuffer ByteOffset ByteLength Uint8ClampedArray +foreign import newUint8ClampedArray :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray -- ---- -foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean -foreign import someImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) Boolean +foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) Boolean +foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) Boolean -foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit -foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b Offset Unit -foreign import fillImpl3 :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit +foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b (Nullable Offset) (Nullable Offset) Unit -foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b b) (ArrayView a) -foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn1 b Unit) Unit -foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (b -> Boolean) (ArrayView a) -foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn2 c b c) c c -foreign import reduce1Impl :: forall a b. Fn2 (ArrayView a) (Fn2 b b b) b -foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn2 c b c) c c -foreign import reduceRight1Impl :: forall a b. Fn2 (ArrayView a) (Fn2 b b b) b +foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) +foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit +foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (ArrayView a) +foreign import includesImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) Boolean +foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c +foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b +foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c +foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -- | Value-oriented array offset @@ -129,60 +124,58 @@ class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: Array t -> ArrayView a -- | Test a predicate to pass on all values - all :: (t -> Boolean) -> ArrayView a -> Boolean + all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean -- | Test a predicate to pass on any value - any :: (t -> Boolean) -> ArrayView a -> Boolean + any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean -- | Fill the array with a value - fill :: ArrayView a -> t -> Effect Unit - -- | Fill the remainder of the array with a value - fillRemainder :: ArrayView a -> t -> Offset -> Effect Unit - -- | Fill part of the array with a value - fillPart :: ArrayView a -> t -> Offset -> Offset -> Effect Unit + fill :: ArrayView a -> t -> Maybe (Tuple Offset (Maybe Offset)) -> Effect Unit -- | Stores multiple values into the typed array - set :: ArrayView a -> Array t -> Effect Unit - -- | Stores multiple values into the typed array, with offset - set' :: ArrayView a -> Offset -> Array t -> Effect Unit + set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit -- | Maps a new value over the typed array, creating a new buffer and typed array as well. - map' :: (t -> t) -> ArrayView a -> ArrayView a + map :: (t -> Offset -> t) -> ArrayView a -> ArrayView a -- | Traverses over each value, returning a new one - traverse' :: (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) + traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) -- | Traverses over each value - traverse_' :: (t -> Effect Unit) -> ArrayView a -> Effect Unit + traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit -- | Returns a new typed array with all values that pass the predicate - filter :: (t -> Boolean) -> ArrayView a -> ArrayView a + filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) + -- | Tests if a value is an element of the typed array + elem :: t -> Maybe Offset -> ArrayView a -> Boolean -- | Fetch element at index. unsafeAt :: ArrayView a -> Offset -> Effect t -- | Folding from the left - foldlM' :: forall b. ArrayView a -> (b -> t -> Effect b) -> b -> Effect b + foldlM :: forall b. ArrayView a -> (b -> t -> Offset -> Effect b) -> b -> Effect b -- | Assumes the typed array is non-empty - foldl1' :: ArrayView a -> (t -> t -> t) -> t + foldl1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t -- | Folding from the right - foldrM' :: forall b. ArrayView a -> (t -> b -> Effect b) -> b -> Effect b + foldrM :: forall b. ArrayView a -> (t -> b -> Offset -> Effect b) -> b -> Effect b -- | Assumes the typed array is non-empty - foldr1' :: ArrayView a -> (t -> t -> t) -> t + foldr1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where - whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) - remainder = runEffectFn2 newUint8ClampedArray2 - part = runEffectFn3 newUint8ClampedArray3 - empty n = unsafePerformEffect (runEffectFn1 newUint8ClampedArray n) - fromArray a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a) - all p a = runFn2 everyImpl a p - any p a = runFn2 someImpl a p - fill = runEffectFn2 fillImpl - fillRemainder = runEffectFn3 fillImpl2 - fillPart = runEffectFn4 fillImpl3 - set a x = runEffectFn3 setImpl a (toNullable Nothing) x - set' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x - map' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn1 (pure <<< f))) - traverse' f a = runEffectFn2 mapImpl a (mkEffectFn1 f) - traverse_' f a = runEffectFn2 forEachImpl a (mkEffectFn1 f) - filter p a = runFn2 filterImpl a p + whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt = runEffectFn2 unsafeAtImpl - foldlM' a f = runEffectFn3 reduceImpl a (mkEffectFn2 f) - foldl1' a f = runFn2 reduce1Impl a (mkFn2 f) - foldrM' a f = runEffectFn3 reduceRightImpl a (mkEffectFn2 (flip f)) - foldr1' a f = runFn2 reduceRight1Impl a (mkFn2 (flip f)) + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) -- instance typedArrayUint32 :: TypedArray Uint32 Number -- instance typedArrayUint16 :: TypedArray Uint16 Int -- instance typedArrayUint8 :: TypedArray Uint8 Int @@ -191,21 +184,24 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where -- instance typedArrayInt8 :: TypedArray Int8 Int -foldl' :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> b) -> b -> b -foldl' a f i = unsafePerformEffect (foldlM' a (\acc x -> pure (f acc x)) i) +foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b +foldl a f i = unsafePerformEffect (foldlM a (\acc x o -> pure (f acc x o)) i) -foldr' :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> b) -> b -> b -foldr' a f i = unsafePerformEffect (foldrM' a (\x acc -> pure (f x acc)) i) +foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) -> b -> b +foldr a f i = unsafePerformEffect (foldrM a (\x acc o -> pure (f x acc o)) i) +foldl1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +foldl1 a f = unsafePerformEffect (foldl1M a (\acc x o -> pure (f acc x o))) -foreign import copyWithinImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset Unit -foreign import copyWithinImpl3 :: forall a. EffectFn4 (ArrayView a) Offset Offset Offset Unit +foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +foldr1 a f = unsafePerformEffect (foldr1M a (\x acc o -> pure (f x acc o))) + + +foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. -copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Effect Unit -copyWithin = runEffectFn3 copyWithinImpl -copyWithinPart :: forall a. ArrayView a -> Offset -> Offset -> Offset -> Effect Unit -copyWithinPart = runEffectFn4 copyWithinImpl3 +copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit +copyWithin a t s me = runEffectFn4 copyWithinImpl a t s (toNullable me) foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit @@ -217,25 +213,20 @@ foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) (Nullable Offset) -- | Stores multiple values in the typed array, reading input values from the second typed array. -setTyped :: forall a. ArrayView a -> ArrayView a -> Effect Unit -setTyped a x = runEffectFn3 setImpl a (toNullable Nothing) x - --- | Stores multiple values in the typed array, reading input values from the second typed array, with offset. -setTyped' :: forall a. ArrayView a -> Offset -> ArrayView a -> Effect Unit -setTyped' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x +setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Unit +setTyped a mo x = runEffectFn3 setImpl a (toNullable mo) x -- | Copy the entire contents of the typed array into a new buffer. -foreign import copy :: forall a. ArrayView a -> ArrayView a -foreign import sliceRemainderImpl :: forall a. Fn2 (ArrayView a) Offset (ArrayView a) -foreign import sliceImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) +foreign import sliceImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) --- | Copy the remainder of contents of the typed array into a new buffer, after some start index. -sliceRemainder :: forall a. ArrayView a -> Offset -> ArrayView a -sliceRemainder = runFn2 sliceRemainderImpl -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a -slice = runFn3 sliceImpl +slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +slice a mz = case mz of + Nothing -> runFn3 sliceImpl a (toNullable Nothing) (toNullable Nothing) + Just (Tuple s me) -> case me of + Nothing -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable Nothing) + Just e -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable (Just e)) foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit @@ -245,16 +236,11 @@ sort :: forall a. ArrayView a -> Effect Unit sort = runEffectFn1 sortImpl -foreign import subArrayImpl :: forall a. Fn2 (ArrayView a) Offset (ArrayView a) -foreign import subArrayImpl2 :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) - +foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset (Nullable Offset) (ArrayView a) -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -subArray :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a -subArray = runFn3 subArrayImpl2 --- | Returns a new typed array view of the same buffer, beginning at the index -subArrayRemainder :: forall a. ArrayView a -> Offset -> ArrayView a -subArrayRemainder = runFn2 subArrayImpl +subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a +subArray a o mo = runFn3 subArrayImpl a o (toNullable mo) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. From 4759fe4a306b75fe51f27b6213e540e1bd5dfcce Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 16:49:15 -0700 Subject: [PATCH 059/236] better arraybuffer slice, more stuff --- src/Data/ArrayBuffer/ArrayBuffer.js | 14 +++++++++++--- src/Data/ArrayBuffer/ArrayBuffer.purs | 13 ++++++++++--- src/Data/ArrayBuffer/Typed.js | 2 +- src/Data/ArrayBuffer/Typed.purs | 12 ++---------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index ebaa22a..c7a1ea0 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -6,10 +6,18 @@ exports.empty = function empty (s) { return new ArrayBuffer(s); }; -exports.byteLength = function(a) { +exports.byteLength = function byteLength (a) { return a.byteLength; }; -exports.sliceImpl = function(s, e, a) { - return a.slice(s, e); +exports.sliceImpl = function sliceImpl (a, ms, me) { + if (me === null) { + if (ms === null) { + return a.slice(); + } else { + return a.slice(ms); + } + } else { + return a.slice(ms,me); + } }; diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 95335c7..a4e49b9 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -8,6 +8,9 @@ module Data.ArrayBuffer.ArrayBuffer ( empty import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) +import Data.Nullable (Nullable, toNullable) +import Data.Maybe (Maybe (..)) +import Data.Tuple (Tuple (..)) -- | Create an `ArrayBuffer` with the given capacity. @@ -16,8 +19,12 @@ foreign import empty :: ByteLength -> ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength -foreign import sliceImpl :: Fn3 ByteOffset ByteOffset ArrayBuffer ArrayBuffer +foreign import sliceImpl :: Fn3 ArrayBuffer (Nullable ByteOffset) (Nullable ByteOffset) ArrayBuffer -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. -slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer -slice = runFn3 sliceImpl +slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer +slice a mz = case mz of + Nothing -> runFn3 sliceImpl a (toNullable Nothing) (toNullable Nothing) + Just (Tuple s me) -> case me of + Nothing -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable Nothing) + Just e -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable (Just e)) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 99b3561..5a806c2 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,7 +1,7 @@ "use strict"; -exports.polyFill = function () { +exports.polyFill = function polyFill () { var typedArrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 14a59c5..3d577c9 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -3,23 +3,15 @@ module Data.ArrayBuffer.Typed ( polyFill - , buffer , Offset, Length - , byteOffset - , byteLength + , buffer, byteOffset, byteLength, length , AProxy (..) , class BytesPerValue , bytesPerValue - , length , class TypedArray , whole, remainder, part, empty, fromArray, all, any, fill, set , map, traverse, traverse_, filter, elem, foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 - , copyWithin - , reverse - , setTyped - , slice - , sort - , subArray + , setTyped, copyWithin, slice, sort, subArray, reverse , toString, toString' , unsafeAt, hasIndex, at , toArray From aa789b71e88344adb7b167345fe148858f4cd412 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 16:51:26 -0700 Subject: [PATCH 060/236] the uint library stores values greater than 2^31 as a negative integer - this is not a correct implementation currently in DataView --- src/Data/ArrayBuffer/ArrayBuffer.purs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index a4e49b9..931df9c 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,10 +1,11 @@ -- | This module represents the functional bindings to JavaScript's `ArrayBuffer` -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) for details. -module Data.ArrayBuffer.ArrayBuffer ( empty - , byteLength - , slice - ) where +module Data.ArrayBuffer.ArrayBuffer + ( empty + , byteLength + , slice + ) where import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) From d0c40385d5cb7db47eefe4561ef7b6a28ac7da3b Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 5 Dec 2018 19:34:43 -0700 Subject: [PATCH 061/236] index of --- src/Data/ArrayBuffer/Typed.js | 28 ++++++++++++++++++++++++++++ src/Data/ArrayBuffer/Typed.purs | 23 +++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 5a806c2..6cf4c00 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -104,6 +104,34 @@ exports.reduceRight1Impl = function reduceRight1Impl (a,f) { return a.reduceRight(f); }; +exports.findImpl = function findImpl (a,f) { + var x = a.find(f); + return (x === undefined) ? null : x; +}; +exports.findIndexImpl = function findIndexImpl (a,f) { + var x = a.findIndex(f); + return (x === -1) ? null : x; +}; +exports.indexOfImpl = function indexOfImpl (a,x,mo) { + var r; + if (mo === null) { + r = a.indexOf(x); + } else { + r = a.indexOf(x,mo); + } + return r === -1 ? null : r; +}; +exports.lastIndexOfImpl = function lastIndexOfImpl (a,x,mo) { + var r; + if (mo === null) { + r = a.lastIndexOf(x); + } else { + r = a.lastIndexOf(x,mo); + } + return r === -1 ? null : r; +}; + + exports.copyWithinImpl = function copyWithinImpl (a,t,s,me) { if (me === null) { diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 3d577c9..3c1df09 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -10,7 +10,9 @@ module Data.ArrayBuffer.Typed , bytesPerValue , class TypedArray , whole, remainder, part, empty, fromArray, all, any, fill, set - , map, traverse, traverse_, filter, elem, foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 + , map, traverse, traverse_, filter, elem + , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 + , find, findIndex, indexOf, lastIndexOf , setTyped, copyWithin, slice, sort, subArray, reverse , toString, toString' , unsafeAt, hasIndex, at @@ -25,7 +27,7 @@ import Effect.Uncurried , mkEffectFn1, mkEffectFn2, mkEffectFn3) import Effect.Unsafe (unsafePerformEffect) import Data.Tuple (Tuple (..)) -import Data.Nullable (Nullable, toNullable) +import Data.Nullable (Nullable, toNullable, toMaybe) import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength , Float64Array, Float32Array @@ -94,6 +96,10 @@ foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b +foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (Nullable b) +foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (Nullable Offset) +foreign import indexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) +foreign import lastIndexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) -- | Value-oriented array offset @@ -143,6 +149,15 @@ class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where foldrM :: forall b. ArrayView a -> (t -> b -> Offset -> Effect b) -> b -> Effect b -- | Assumes the typed array is non-empty foldr1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t + -- | Returns the first value satisfying the predicate + find :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe t) + -- | Returns the first index of the value satisfying the predicate + findIndex :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe Offset) + -- | Returns the first index of the element, if it exists, from the left + indexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset + -- | Returns the first index of the element, if it exists, from the right + lastIndexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset + instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) @@ -168,6 +183,10 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -- instance typedArrayUint32 :: TypedArray Uint32 Number -- instance typedArrayUint16 :: TypedArray Uint16 Int -- instance typedArrayUint8 :: TypedArray Uint8 Int From 2ffbb008a1e99836f283da8fbf775e52db861d15 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 5 Dec 2018 20:33:08 -0700 Subject: [PATCH 062/236] more instances --- src/Data/ArrayBuffer/Typed.js | 70 ++++++++++++- src/Data/ArrayBuffer/Typed.purs | 180 ++++++++++++++++++++++++++++++-- 2 files changed, 243 insertions(+), 7 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 6cf4c00..2478e15 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -35,7 +35,7 @@ exports.lengthImpl = function lemgthImpl (v) { }; -// Uint8Clamped +// Typed Arrays exports.newUint8ClampedArray = function newUint8ClampedArray (a,mb,mc) { if (mc === null) { @@ -48,7 +48,75 @@ exports.newUint8ClampedArray = function newUint8ClampedArray (a,mb,mc) { return new Uint8ClampedArray(a,mb,mc); } }; +exports.newUint32Array = function newUint32Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Uint32Array(a); + } else { + return new Uint32Array(a,mb); + } + } else { + return new Uint32Array(a,mb,mc); + } +}; +exports.newUint16Array = function newUint16Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Uint16Array(a); + } else { + return new Uint16Array(a,mb); + } + } else { + return new Uint16Array(a,mb,mc); + } +}; +exports.newUint8Array = function newUint8Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Uint8Array(a); + } else { + return new Uint8Array(a,mb); + } + } else { + return new Uint8Array(a,mb,mc); + } +}; +exports.newInt32Array = function newInt32Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Int32Array(a); + } else { + return new Int32Array(a,mb); + } + } else { + return new Int32Array(a,mb,mc); + } +}; +exports.newInt16Array = function newInt16Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Int16Array(a); + } else { + return new Int16Array(a,mb); + } + } else { + return new Int16Array(a,mb,mc); + } +}; +exports.newInt8Array = function newInt8Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Int8Array(a); + } else { + return new Int8Array(a,mb); + } + } else { + return new Int8Array(a,mb,mc); + } +}; + +// ------ exports.everyImpl = function everyImpl (a,p) { return a.every(p); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 3c1df09..e1caeb7 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -79,6 +79,12 @@ length = lengthImpl -- object creator implementations for each typed array foreign import newUint8ClampedArray :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray +foreign import newUint32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array +foreign import newUint16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array +foreign import newUint8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array +foreign import newInt32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array +foreign import newInt16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array +foreign import newInt8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array -- ---- @@ -187,12 +193,174 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) --- instance typedArrayUint32 :: TypedArray Uint32 Number --- instance typedArrayUint16 :: TypedArray Uint16 Int --- instance typedArrayUint8 :: TypedArray Uint8 Int --- instance typedArrayInt32 :: TypedArray Int32 Number --- instance typedArrayInt16 :: TypedArray Int16 Int --- instance typedArrayInt8 :: TypedArray Int8 Int +instance typedArrayUint32 :: TypedArray Uint32 Number where + whole a = unsafePerformEffect (runEffectFn3 newUint32Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newUint32Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayUint16 :: TypedArray Uint16 Int where + whole a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newUint16Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayUint8 :: TypedArray Uint8 Int where + whole a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newUint8Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayInt32 :: TypedArray Int32 Number where + whole a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newInt32Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayInt16 :: TypedArray Int16 Int where + whole a = unsafePerformEffect (runEffectFn3 newInt16Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newInt16Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayInt8 :: TypedArray Int8 Int where + whole a = unsafePerformEffect (runEffectFn3 newInt8Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newInt8Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b From 3c7066e130d7a2445c047414304dfdd814ad21dd Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 5 Dec 2018 20:44:18 -0700 Subject: [PATCH 063/236] honestly I forgot about floating points --- bower.json | 3 +- src/Data/ArrayBuffer/Typed.js | 22 ++++++++ src/Data/ArrayBuffer/Typed.purs | 99 ++++++++++++++++++++++++--------- 3 files changed, 98 insertions(+), 26 deletions(-) diff --git a/bower.json b/bower.json index 625fe88..e5b8430 100644 --- a/bower.json +++ b/bower.json @@ -17,7 +17,8 @@ "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", "purescript-uint": "^4.0.0", - "purescript-nullable": "^4.1.0" + "purescript-nullable": "^4.1.0", + "purescript-typelevel": "^4.0.0" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 2478e15..169bd6a 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -114,6 +114,28 @@ exports.newInt8Array = function newInt8Array (a,mb,mc) { return new Int8Array(a,mb,mc); } }; +exports.newFloat32Array = function newFloat32Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Float32Array(a); + } else { + return new Float32Array(a,mb); + } + } else { + return new Float32Array(a,mb,mc); + } +}; +exports.newFloat64Array = function newFloat64Array (a,mb,mc) { + if (mc === null) { + if (mb === null) { + return new Float64Array(a); + } else { + return new Float64Array(a,mb); + } + } else { + return new Float64Array(a,mb,mc); + } +}; // ------ diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index e1caeb7..23c9369 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -5,9 +5,7 @@ module Data.ArrayBuffer.Typed ( polyFill , Offset, Length , buffer, byteOffset, byteLength, length - , AProxy (..) , class BytesPerValue - , bytesPerValue , class TypedArray , whole, remainder, part, empty, fromArray, all, any, fill, set , map, traverse, traverse_, filter, elem @@ -24,7 +22,7 @@ import Effect (Effect) import Effect.Uncurried ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1 - , mkEffectFn1, mkEffectFn2, mkEffectFn3) + , mkEffectFn2, mkEffectFn3) import Effect.Unsafe (unsafePerformEffect) import Data.Tuple (Tuple (..)) import Data.Nullable (Nullable, toNullable, toMaybe) @@ -34,8 +32,9 @@ import Data.ArrayBuffer.Types , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array , Float64, Float32 , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3, mkFn2) +import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) +import Data.Typelevel.Num (D1,D2,D4,D8) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -52,27 +51,19 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength foreign import lengthImpl :: forall a. ArrayView a -> Length -data AProxy (a :: ArrayViewType) = AProxy - -class BytesPerValue (a :: ArrayViewType) where - bytesPerValue :: AProxy a -> Int - -instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped where - bytesPerValue AProxy = 1 -instance bytesPerValueUint32 :: BytesPerValue Uint32 where - bytesPerValue AProxy = 4 -instance bytesPerValueUint16 :: BytesPerValue Uint16 where - bytesPerValue AProxy = 2 -instance bytesPerValueUint8 :: BytesPerValue Uint8 where - bytesPerValue AProxy = 1 -instance bytesPerValueInt32 :: BytesPerValue Int32 where - bytesPerValue AProxy = 4 -instance bytesPerValueInt16 :: BytesPerValue Int16 where - bytesPerValue AProxy = 2 -instance bytesPerValueInt8 :: BytesPerValue Int8 where - bytesPerValue AProxy = 1 - -length :: forall a. BytesPerValue a => ArrayView a -> Int +class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b + +instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped D1 +instance bytesPerValueUint32 :: BytesPerValue Uint32 D4 +instance bytesPerValueUint16 :: BytesPerValue Uint16 D2 +instance bytesPerValueUint8 :: BytesPerValue Uint8 D1 +instance bytesPerValueInt32 :: BytesPerValue Int32 D4 +instance bytesPerValueInt16 :: BytesPerValue Int16 D2 +instance bytesPerValueInt8 :: BytesPerValue Int8 D1 +instance bytesPerValueFloat32 :: BytesPerValue Float32 D4 +instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 + +length :: forall a b. BytesPerValue a b => ArrayView a -> Int length = lengthImpl @@ -85,6 +76,8 @@ foreign import newUint8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nul foreign import newInt32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array foreign import newInt16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array foreign import newInt8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array +foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array +foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array -- ---- @@ -361,6 +354,62 @@ instance typedArrayInt8 :: TypedArray Int8 Int where findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayFloat32 :: TypedArray Float32 Number where + whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +instance typedArrayFloat64 :: TypedArray Float64 Number where + whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a (toNullable Nothing) (toNullable Nothing)) + remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable Nothing) + part a x y = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable (Just y)) + empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n (toNullable Nothing) (toNullable Nothing)) + fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) + any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) x + map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + elem x mo a = runFn3 includesImpl a x (toNullable mo) + unsafeAt = runEffectFn2 unsafeAtImpl + foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) + foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) + foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b From fd29b0b70821a61770196430f473ac4906879639 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 5 Dec 2018 20:49:31 -0700 Subject: [PATCH 064/236] the binary serialization of javascript Numbers for TypedArrays follows the intended values that would be expected in javascript - negative encodings for proper Uint32 is invalid in this case. Because of this, Uint32s are represented in the positive domain of 64bit floats (js Number) --- bower.json | 1 - src/Data/ArrayBuffer/Typed.purs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bower.json b/bower.json index e5b8430..22e8222 100644 --- a/bower.json +++ b/bower.json @@ -16,7 +16,6 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0", "purescript-nullable": "^4.1.0", "purescript-typelevel": "^4.0.0" }, diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 23c9369..e3e8154 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -25,6 +25,7 @@ import Effect.Uncurried , mkEffectFn2, mkEffectFn3) import Effect.Unsafe (unsafePerformEffect) import Data.Tuple (Tuple (..)) +import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, toNullable, toMaybe) import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength @@ -33,7 +34,6 @@ import Data.ArrayBuffer.Types , Float64, Float32 , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) -import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (D1,D2,D4,D8) From ea66b5f2a8ec4ad69cb70166b59fcd518ad8850f Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Thu, 6 Dec 2018 13:59:32 +0100 Subject: [PATCH 065/236] Remove text encoding dependency --- bower.json | 5 ++- package.json | 9 ++---- src/Data/ArrayBuffer/ArrayBuffer.purs | 25 +-------------- test/Input.purs | 44 --------------------------- test/Main.purs | 21 +------------ 5 files changed, 7 insertions(+), 97 deletions(-) delete mode 100644 test/Input.purs diff --git a/bower.json b/bower.json index b50facd..f2e0abf 100644 --- a/bower.json +++ b/bower.json @@ -16,8 +16,7 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0", - "purescript-text-encoding": "^0.0.9" + "purescript-uint": "^4.0.0" }, "devDependencies": { "purescript-debug": "^4.0.0", @@ -25,4 +24,4 @@ "purescript-partial": "^2.0.0", "purescript-unicode": "^4.0.1" } -} +} \ No newline at end of file diff --git a/package.json b/package.json index ffc2434..110d9f1 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,9 @@ { "name": "purescript-arraybuffer", - "version": "7.0.0", + "version": "9.0.0", "main": "index.js", "repository": "git@github.com:jacereda/purescript-arraybuffer.git", "author": "https://github.com/jacereda", "license": "MIT", - "devDependencies": { - "text-encoding": "^0.6.4" - } - -} + "devDependencies": {} +} \ No newline at end of file diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index d501ab8..539a766 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -3,20 +3,11 @@ module Data.ArrayBuffer.ArrayBuffer ( create , slice , fromArray , fromIntArray - , fromString - , decodeToString ) where -import Data.ArrayBuffer.DataView (whole, buffer) -import Data.ArrayBuffer.Typed (asUint8Array, dataView) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) -import Data.Either (Either) import Data.Function.Uncurried (Fn3, runFn3) -import Data.TextDecoder (decodeUtf8) -import Data.TextEncoder (encodeUtf8) import Effect (Effect) -import Effect.Exception (Error) -import Prelude ((<<<)) -- | Create an `ArrayBuffer` with the given capacity. @@ -35,18 +26,4 @@ slice = runFn3 sliceImpl foreign import fromArray :: Array Number -> ArrayBuffer -- | Convert an array into an `ArrayBuffer` representation. -foreign import fromIntArray :: Array Int -> ArrayBuffer - --- | Convert a UTF-8 encoded `ArrayBuffer` into a `String`. --- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) --- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" -decodeToString :: ArrayBuffer -> Either Error String -decodeToString = decodeUtf8 <<< asUint8Array <<< whole - --- | Convert a `String` into a UTF-8 encoded `ArrayBuffer`. --- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) --- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" -fromString :: String -> ArrayBuffer -fromString = buffer <<< dataView <<< encodeUtf8 +foreign import fromIntArray :: Array Int -> ArrayBuffer \ No newline at end of file diff --git a/test/Input.purs b/test/Input.purs deleted file mode 100644 index 926c66b..0000000 --- a/test/Input.purs +++ /dev/null @@ -1,44 +0,0 @@ --- Uses code originally found in the purescript-encoding library. -{- - Copyright 2018 Andreas Schacker - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --} -module Test.Input where - -import Data.Char.Unicode (isPrint) -import Prelude ((<$>), ($), (<<<)) -import Data.Array (filter) -import Data.String.CodeUnits (fromCharArray, toCharArray) -import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary) - - --- When UTF8-encoding a string, surrogate code points and other non-characters --- are simply replaced by the replacement character � (U+FFFD). --- This entails that the `encodeUtf8` function is not injective anymore and --- thus the desired property `decodeUtf8 <<< encodeUtf8 == id` does not hold --- in general. --- --- For well-formed input strings, however, we can expect the property to hold. - --- Use a newtype in order to define an `Arbitrary` instance. -newtype WellFormedInput = WellFormedInput String - --- The `Arbitrary` instance for `String` currently simply chooses characters --- out of the first 65536 unicode code points. --- See `charGen` in `purescript-strongcheck`. -instance arbWellFormedInput :: Arbitrary WellFormedInput where - arbitrary = WellFormedInput <<< filterString isPrint <$> arbitrary - -filterString :: (Char -> Boolean) -> String -> String -filterString f s = fromCharArray <<< filter f $ toCharArray s diff --git a/test/Main.purs b/test/Main.purs index 19ad101..017bc05 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,12 +6,9 @@ import Effect (Effect) import Data.ArrayBuffer.ArrayBuffer as AB import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.Typed as TA -import Data.Either (fromRight) import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) -import Partial.Unsafe (unsafePartial) -import Test.QuickCheck (quickCheck', (), quickCheck) -import Test.Input (WellFormedInput(..)) +import Test.QuickCheck (quickCheck', ()) assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit @@ -27,19 +24,6 @@ assertEquals expected actual = do main :: Effect Unit main = do - assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") - - quickCheck - \(WellFormedInput s) -> - let - result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) - in - s == result - "Isormorphic arraybuffer conversion with string failed for input\n" - <> s - <> " which, after the round trip, result in\n" - <> result - ab4 <- AB.create 4 ab8 <- AB.create 8 assertEquals 4 $ AB.byteLength ab4 @@ -52,9 +36,6 @@ main = do assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] - assertEquals 4 $ AB.byteLength $ AB.fromString "hola" - assertEquals 5 $ AB.byteLength $ AB.fromString "hóla" - assertEquals 7 $ AB.byteLength $ AB.fromString "hóla¡" assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 From 00d4a2e8ecf3a670985e200fa5fb73c42db5467b Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Thu, 6 Dec 2018 14:01:04 +0100 Subject: [PATCH 066/236] Remove show and update readme --- README.md | 5 ----- src/Data/ArrayBuffer/Show.js | 7 ------- src/Data/ArrayBuffer/Show.purs | 18 ------------------ 3 files changed, 30 deletions(-) delete mode 100644 src/Data/ArrayBuffer/Show.js delete mode 100644 src/Data/ArrayBuffer/Show.purs diff --git a/README.md b/README.md index 7bd53a2..bbdcf1e 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,3 @@ ArrayBuffer bindings for PureScript. ## Documentation Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). - - -## Important Usage Notes - -- Usage of the ArrayBuffer<->String conversion functions requires the import of the NPM package 'text-encoding'. diff --git a/src/Data/ArrayBuffer/Show.js b/src/Data/ArrayBuffer/Show.js deleted file mode 100644 index 44eab29..0000000 --- a/src/Data/ArrayBuffer/Show.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -// module Show - -exports.showImpl = function(a) { - return require('util').inspect(a); -} diff --git a/src/Data/ArrayBuffer/Show.purs b/src/Data/ArrayBuffer/Show.purs deleted file mode 100644 index c22eccf..0000000 --- a/src/Data/ArrayBuffer/Show.purs +++ /dev/null @@ -1,18 +0,0 @@ -module Data.ArrayBuffer.Show where - --- import Prelude -import Data.ArrayBuffer.Types (ArrayView) --- import Data.ArrayBuffer.ArrayBuffer as AB --- import Data.ArrayBuffer.DataView as DV --- import Data.ArrayBuffer.Typed as T - --- instance showArrayView :: Show (ArrayView a) where --- show = showImpl --- --- instance showDataView :: Show DataView where --- show = show <<< T.asInt8Array --- --- instance showArrayBuffer :: Show ArrayBuffer where --- show = show <<< DV.whole --- -foreign import showImpl :: forall a. ArrayView a -> String From 71b5b08d3fd6cc46ecac780c2de97c52e6d74d78 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 6 Dec 2018 08:31:33 -0700 Subject: [PATCH 067/236] generation functions - need to generate a bona-fide float32 --- src/Data/ArrayBuffer/DataView.purs | 22 ++-- src/Data/ArrayBuffer/Typed.purs | 2 +- src/Data/ArrayBuffer/Typed/Gen.purs | 65 ++++++++++ test/Main.purs | 186 +++++++++++++++------------- test/Properties.purs | 3 + test/Properties/ArrayBuffer.purs | 2 + test/Properties/DataView.purs | 2 + test/Properties/TypedArray.purs | 17 +++ 8 files changed, 198 insertions(+), 101 deletions(-) create mode 100644 src/Data/ArrayBuffer/Typed/Gen.purs create mode 100644 test/Properties.purs create mode 100644 test/Properties/ArrayBuffer.purs create mode 100644 test/Properties/DataView.purs create mode 100644 test/Properties/TypedArray.purs diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 4bbd5ab..e5e54df 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -46,7 +46,7 @@ import Data.Maybe (Maybe(..)) import Effect (Effect) import Effect.Exception (catchException) import Effect.Uncurried (EffectFn5, EffectFn3, EffectFn2, runEffectFn5, runEffectFn3, runEffectFn2) -import Data.UInt (UInt) + -- | Type for all fetching functions. type Getter r = DataView -> ByteOffset -> Effect (Maybe r) @@ -114,21 +114,21 @@ getInt32le :: Getter Int getInt32le = getter "getInt32" 4 true -- | Fetch uint8 value at a certain index in a `DataView`. -getUint8 :: Getter UInt +getUint8 :: Getter Int getUint8 = getter "getUint8" 1 false -- | Fetch uint16 value at a certain index in a `DataView`. -getUint16be :: Getter UInt +getUint16be :: Getter Int getUint16be = getter "getUint16" 2 false -getUint16le :: Getter UInt +getUint16le :: Getter Int getUint16le = getter "getUint16" 2 true -- | Fetch uint32 value at a certain index in a `DataView`. -getUint32be :: Getter UInt +getUint32be :: Getter Number getUint32be = getter "getUint32" 4 false -getUint32le :: Getter UInt +getUint32le :: Getter Number getUint32le = getter "getUint32" 4 true -- | Fetch float32 value at a certain index in a `DataView`. @@ -164,21 +164,21 @@ setInt32le :: Setter Int setInt32le = setter "setInt32" true -- | Store uint8 value at a certain index in a `DataView`. -setUint8 :: Setter UInt +setUint8 :: Setter Int setUint8 = setter "setUint8" false -- | Store uint16 value at a certain index in a `DataView`. -setUint16be :: Setter UInt +setUint16be :: Setter Int setUint16be = setter "setUint16" false -setUint16le :: Setter UInt +setUint16le :: Setter Int setUint16le = setter "setUint16" true -- | Store uint32 value at a certain index in a `DataView`. -setUint32be :: Setter UInt +setUint32be :: Setter Number setUint32be = setter "setUint32" false -setUint32le :: Setter UInt +setUint32le :: Setter Number setUint32le = setter "setUint32" true -- | Store float32 value at a certain index in a `DataView`. diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index e3e8154..9adf515 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -270,7 +270,7 @@ instance typedArrayUint8 :: TypedArray Uint8 Int where findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -instance typedArrayInt32 :: TypedArray Int32 Number where +instance typedArrayInt32 :: TypedArray Int32 Int where whole a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable Nothing) part a x y = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable (Just y)) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs new file mode 100644 index 0000000..4ac7fca --- /dev/null +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -0,0 +1,65 @@ +-- | Functions for generating random typed arrays. + +module Data.ArrayBuffer.Typed.Gen where + +import Data.ArrayBuffer.Types + ( Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array + , Int8Array, Int16Array, Int32Array + , Float32Array, Float64Array + ) +import Data.ArrayBuffer.Typed as TA + +import Prelude +import Math as M +import Data.List.Lazy (replicateM) +import Data.Int as I +import Data.Array as Array +import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) + + +arbitraryUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray +arbitraryUint8ClampedArray = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUByte + +arbitraryUint32Array :: forall m. MonadGen m => m Uint32Array +arbitraryUint32Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUWord + +arbitraryUint16Array :: forall m. MonadGen m => m Uint16Array +arbitraryUint16Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUNibble + +arbitraryUint8Array :: forall m. MonadGen m => m Uint8Array +arbitraryUint8Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUByte + + +arbitraryUByte :: forall m. MonadGen m => m Int +arbitraryUByte = chooseInt 0 ((I.pow 2 8) - 1) + +arbitraryByte :: forall m. MonadGen m => m Int +arbitraryByte = + let j = I.pow 2 4 + in chooseInt (negate j) (j - 1) + +arbitraryUNibble :: forall m. MonadGen m => m Int +arbitraryUNibble = chooseInt 0 ((I.pow 2 16) - 1) + +arbitraryNibble :: forall m. MonadGen m => m Int +arbitraryNibble = + let j = I.pow 2 8 + in chooseInt (negate j) (j - 1) + +arbitraryUWord :: forall m. MonadGen m => m Number +arbitraryUWord = M.round <$> chooseFloat 0.0 ((M.pow 2.0 32.0) - 1.0) + +arbitraryWord :: forall m. MonadGen m => m Int +arbitraryWord = + let j = I.pow 2 16 + in chooseInt (negate j) (j - 1) + +arbitraryFloat32 :: forall m. MonadGen m => m Number +arbitraryFloat32 = + let maxFloat32 = (2.0 - (M.pow 2.0 (-23.0))) * (M.pow 2.0 127.0) + minFloat32 = -maxFloat32 -- because of sign bit + in chooseFloat minFloat32 maxFloat32 -- roughly estimated because of variable precision between 6 and 9 digs diff --git a/test/Main.purs b/test/Main.purs index 19ad101..bd51e27 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -3,94 +3,102 @@ module Test.Main where import Prelude import Effect (Effect) -import Data.ArrayBuffer.ArrayBuffer as AB -import Data.ArrayBuffer.DataView as DV -import Data.ArrayBuffer.Typed as TA -import Data.Either (fromRight) -import Data.Maybe (Maybe(..), isNothing) -import Data.UInt (fromInt, pow) -import Partial.Unsafe (unsafePartial) -import Test.QuickCheck (quickCheck', (), quickCheck) -import Test.Input (WellFormedInput(..)) - - -assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit -assertEffEquals expectedValue computation = do - actualValue <- computation - let msg = show expectedValue <> " /= " <> show actualValue - quickCheck' 1 $ actualValue == expectedValue msg - -assertEquals :: forall a. Eq a => Show a => a -> a -> Effect Unit -assertEquals expected actual = do - let msg = show expected <> " /= " <> show actual - quickCheck' 1 $ expected == actual msg + + + main :: Effect Unit -main = do - assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") - - quickCheck - \(WellFormedInput s) -> - let - result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) - in - s == result - "Isormorphic arraybuffer conversion with string failed for input\n" - <> s - <> " which, after the round trip, result in\n" - <> result - - ab4 <- AB.create 4 - ab8 <- AB.create 8 - assertEquals 4 $ AB.byteLength ab4 - assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 0 2 ab4 - assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 2 4 ab4 - assertEffEquals 0 $ pure <<< AB.byteLength =<< AB.slice (-2) (-2) ab4 - assertEffEquals 1 $ pure <<< AB.byteLength =<< (AB.slice (-2) (-1) ab4) - assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 - assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 - assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 - assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] - assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] - assertEquals 4 $ AB.byteLength $ AB.fromString "hola" - assertEquals 5 $ AB.byteLength $ AB.fromString "hóla" - assertEquals 7 $ AB.byteLength $ AB.fromString "hóla¡" - assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 - assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 - - assertEquals (Just 8) $ DV.byteLength <$> DV.slice 0 8 ab8 - assertEquals true $ isNothing $ DV.slice 0 40 ab8 - - fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole $ AB.fromIntArray [1, 2, 3, 4] - assertEffEquals (Just 2.0) $ TA.at fourElementInt8Array 1 - assertEffEquals Nothing $ TA.at fourElementInt8Array 4 - assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) - - assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] - - twoElementDataView <- do - ab' <- AB.create 2 - let dv = DV.whole ab' - DV.setUint8 dv (fromInt 123) 0 - DV.setUint8 dv (fromInt 0) 1 - pure dv - assertEffEquals (Just $ fromInt 123) $ DV.getUint16le twoElementDataView 0 - assertEffEquals (Just $ fromInt 31488) $ DV.getUint16be twoElementDataView 0 - assertEffEquals (Just $ fromInt 2 `pow` fromInt 32 - fromInt 1) $ do - ab' <- AB.create 4 - let dv = DV.whole ab' - t = fromInt 255 - DV.setUint8 dv t 0 - DV.setUint8 dv t 1 - DV.setUint8 dv t 2 - DV.setUint8 dv t 3 - DV.getUint32be dv 0 - - let arr = DV.whole (AB.fromIntArray [0x4, 0x3, 0x2, 0x1]) - - assertEffEquals (Just 0x04) (DV.getInt8 arr 0) - assertEffEquals (Just 0x04) (DV.getInt8 (TA.dataView (TA.asInt8Array arr)) 0) - assertEffEquals (Just 0x0304) (DV.getInt16le arr 0) - assertEffEquals (Just 0x0304) (DV.getInt16le (TA.dataView (TA.asInt16Array arr)) 0) - assertEffEquals (Just 0x01020304) (DV.getInt32le arr 0) - assertEffEquals (Just 0x01020304) (DV.getInt32le (TA.dataView (TA.asInt32Array arr)) 0) +main = pure unit + + +-- import Data.ArrayBuffer.ArrayBuffer as AB +-- import Data.ArrayBuffer.DataView as DV +-- import Data.ArrayBuffer.Typed as TA +-- import Data.Either (fromRight) +-- import Data.Maybe (Maybe(..), isNothing) +-- import Data.UInt (fromInt, pow) +-- import Partial.Unsafe (unsafePartial) +-- import Test.QuickCheck (quickCheck', (), quickCheck) +-- import Test.Input (WellFormedInput(..)) + + +-- assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit +-- assertEffEquals expectedValue computation = do +-- actualValue <- computation +-- let msg = show expectedValue <> " /= " <> show actualValue +-- quickCheck' 1 $ actualValue == expectedValue msg + +-- assertEquals :: forall a. Eq a => Show a => a -> a -> Effect Unit +-- assertEquals expected actual = do +-- let msg = show expected <> " /= " <> show actual +-- quickCheck' 1 $ expected == actual msg + +-- main :: Effect Unit +-- main = do +-- assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") + +-- quickCheck +-- \(WellFormedInput s) -> +-- let +-- result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) +-- in +-- s == result +-- "Isormorphic arraybuffer conversion with string failed for input\n" +-- <> s +-- <> " which, after the round trip, result in\n" +-- <> result + +-- ab4 <- AB.create 4 +-- ab8 <- AB.create 8 +-- assertEquals 4 $ AB.byteLength ab4 +-- assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 0 2 ab4 +-- assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 2 4 ab4 +-- assertEffEquals 0 $ pure <<< AB.byteLength =<< AB.slice (-2) (-2) ab4 +-- assertEffEquals 1 $ pure <<< AB.byteLength =<< (AB.slice (-2) (-1) ab4) +-- assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 +-- assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 +-- assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 +-- assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] +-- assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] +-- assertEquals 4 $ AB.byteLength $ AB.fromString "hola" +-- assertEquals 5 $ AB.byteLength $ AB.fromString "hóla" +-- assertEquals 7 $ AB.byteLength $ AB.fromString "hóla¡" +-- assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 +-- assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 + +-- assertEquals (Just 8) $ DV.byteLength <$> DV.slice 0 8 ab8 +-- assertEquals true $ isNothing $ DV.slice 0 40 ab8 + +-- fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole $ AB.fromIntArray [1, 2, 3, 4] +-- assertEffEquals (Just 2.0) $ TA.at fourElementInt8Array 1 +-- assertEffEquals Nothing $ TA.at fourElementInt8Array 4 +-- assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) + +-- assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] + +-- twoElementDataView <- do +-- ab' <- AB.create 2 +-- let dv = DV.whole ab' +-- DV.setUint8 dv (fromInt 123) 0 +-- DV.setUint8 dv (fromInt 0) 1 +-- pure dv +-- assertEffEquals (Just $ fromInt 123) $ DV.getUint16le twoElementDataView 0 +-- assertEffEquals (Just $ fromInt 31488) $ DV.getUint16be twoElementDataView 0 +-- assertEffEquals (Just $ fromInt 2 `pow` fromInt 32 - fromInt 1) $ do +-- ab' <- AB.create 4 +-- let dv = DV.whole ab' +-- t = fromInt 255 +-- DV.setUint8 dv t 0 +-- DV.setUint8 dv t 1 +-- DV.setUint8 dv t 2 +-- DV.setUint8 dv t 3 +-- DV.getUint32be dv 0 + +-- let arr = DV.whole (AB.fromIntArray [0x4, 0x3, 0x2, 0x1]) + +-- assertEffEquals (Just 0x04) (DV.getInt8 arr 0) +-- assertEffEquals (Just 0x04) (DV.getInt8 (TA.dataView (TA.asInt8Array arr)) 0) +-- assertEffEquals (Just 0x0304) (DV.getInt16le arr 0) +-- assertEffEquals (Just 0x0304) (DV.getInt16le (TA.dataView (TA.asInt16Array arr)) 0) +-- assertEffEquals (Just 0x01020304) (DV.getInt32le arr 0) +-- assertEffEquals (Just 0x01020304) (DV.getInt32le (TA.dataView (TA.asInt32Array arr)) 0) diff --git a/test/Properties.purs b/test/Properties.purs new file mode 100644 index 0000000..a82bbb7 --- /dev/null +++ b/test/Properties.purs @@ -0,0 +1,3 @@ +module Test.Properties where + + diff --git a/test/Properties/ArrayBuffer.purs b/test/Properties/ArrayBuffer.purs new file mode 100644 index 0000000..3be4160 --- /dev/null +++ b/test/Properties/ArrayBuffer.purs @@ -0,0 +1,2 @@ +module Test.Properties.ArrayBuffer where + diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs new file mode 100644 index 0000000..71e7bd1 --- /dev/null +++ b/test/Properties/DataView.purs @@ -0,0 +1,2 @@ +module Test.Properties.DataView where + diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs new file mode 100644 index 0000000..f99b493 --- /dev/null +++ b/test/Properties/TypedArray.purs @@ -0,0 +1,17 @@ +module Test.Properties.TypedArray where + + +import Data.ArrayBuffer.Types (ArrayView) +import Data.ArrayBuffer.Typed as TA +import Data.ArrayBuffer.Typed (class BytesPerValue) + +import Prelude +import Data.Typelevel.Num (toInt', class Nat) +import Type.Proxy (Proxy (..)) +import Test.QuickCheck (Result, (===)) + + +byteLengthDivBytesPerValueEqLength :: forall a n. BytesPerValue a n => Nat n => ArrayView a -> Result +byteLengthDivBytesPerValueEqLength a = + let n = toInt' (Proxy :: Proxy n) + in TA.length a === (TA.byteLength a `div` n) From 4a83a7260b5d4d18d56a0d754aaf08df53c83492 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 6 Dec 2018 12:50:11 -0700 Subject: [PATCH 068/236] arbitrary floating point values --- bower.json | 3 ++- src/Data/ArrayBuffer/Typed/Gen.purs | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 22e8222..cfd91b5 100644 --- a/bower.json +++ b/bower.json @@ -17,7 +17,8 @@ "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", "purescript-nullable": "^4.1.0", - "purescript-typelevel": "^4.0.0" + "purescript-typelevel": "^4.0.0", + "purescript-parseint": "^1.1.0" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 4ac7fca..689770b 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -11,10 +11,14 @@ import Data.ArrayBuffer.Typed as TA import Prelude import Math as M +import Data.Maybe (Maybe (..)) import Data.List.Lazy (replicateM) import Data.Int as I +import Data.String.CodeUnits as S +import Data.Float.Parse (parseFloat) import Data.Array as Array import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) +import Partial.Unsafe (unsafePartial) arbitraryUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray @@ -62,4 +66,16 @@ arbitraryFloat32 :: forall m. MonadGen m => m Number arbitraryFloat32 = let maxFloat32 = (2.0 - (M.pow 2.0 (-23.0))) * (M.pow 2.0 127.0) minFloat32 = -maxFloat32 -- because of sign bit - in chooseFloat minFloat32 maxFloat32 -- roughly estimated because of variable precision between 6 and 9 digs + reformat :: String -> String + reformat s = + let pre = S.takeWhile (\c -> c /= '.') s + suf = S.dropWhile (\c -> c /= '.') s + in pre <> "." <> S.take 6 suf + fix :: Number -> Number + fix x = unsafePartial $ case parseFloat (reformat (show x)) of + Just y -> y + in fix <$> chooseFloat minFloat32 maxFloat32 + -- roughly estimated because of variable precision between 6 and 9 digs + +arbitraryFloat64 :: forall m. MonadGen m => m Number +arbitraryFloat64 = chooseFloat (-1.7e308) 1.7e308 From 3adab9ee17f260efef6eafc8b30588ef54406e82 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 6 Dec 2018 12:50:32 -0700 Subject: [PATCH 069/236] more correct --- src/Data/ArrayBuffer/Typed/Gen.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 689770b..2a07d5c 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -64,7 +64,7 @@ arbitraryWord = arbitraryFloat32 :: forall m. MonadGen m => m Number arbitraryFloat32 = - let maxFloat32 = (2.0 - (M.pow 2.0 (-23.0))) * (M.pow 2.0 127.0) + let maxFloat32 = (1.0 - (M.pow 2.0 (-24.0))) * (M.pow 2.0 128.0) minFloat32 = -maxFloat32 -- because of sign bit reformat :: String -> String reformat s = From e84aec72bfdec08b9afff20176c828d3d066a3af Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 6 Dec 2018 12:53:55 -0700 Subject: [PATCH 070/236] more generators --- src/Data/ArrayBuffer/Typed/Gen.purs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 2a07d5c..7631092 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -37,6 +37,28 @@ arbitraryUint8Array :: forall m. MonadGen m => m Uint8Array arbitraryUint8Array = sized \s -> TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUByte +arbitraryInt32Array :: forall m. MonadGen m => m Int32Array +arbitraryInt32Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryWord + +arbitraryInt16Array :: forall m. MonadGen m => m Int16Array +arbitraryInt16Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryNibble + +arbitraryInt8Array :: forall m. MonadGen m => m Int8Array +arbitraryInt8Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryByte + +arbitraryFloat32Array :: forall m. MonadGen m => m Float32Array +arbitraryFloat32Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryFloat32 + +arbitraryFloat64Array :: forall m. MonadGen m => m Float64Array +arbitraryFloat64Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryFloat64 + + + arbitraryUByte :: forall m. MonadGen m => m Int arbitraryUByte = chooseInt 0 ((I.pow 2 8) - 1) From 350c48fe14e8a18d546b5ac102f9a2db40203deb Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 6 Dec 2018 12:59:54 -0700 Subject: [PATCH 071/236] simple byteLength / bytesPerValue === length test --- test/Main.purs | 8 ++++++-- test/Properties/TypedArray.purs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index bd51e27..05e015c 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,14 +1,18 @@ module Test.Main where -import Prelude +import Test.Properties.TypedArray as TATests +import Prelude import Effect (Effect) +import Effect.Console (log) main :: Effect Unit -main = pure unit +main = do + log "Starting tests..." + TATests.byteLengthDivBytesPerValueTests -- import Data.ArrayBuffer.ArrayBuffer as AB diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index f99b493..f86cbf1 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -4,11 +4,41 @@ module Test.Properties.TypedArray where import Data.ArrayBuffer.Types (ArrayView) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed (class BytesPerValue) +import Data.ArrayBuffer.Typed.Gen + ( arbitraryUint8ClampedArray, arbitraryUint8Array, arbitraryUint16Array, arbitraryUint32Array + , arbitraryInt8Array, arbitraryInt16Array, arbitraryInt32Array + , arbitraryFloat32Array, arbitraryFloat64Array) import Prelude import Data.Typelevel.Num (toInt', class Nat) import Type.Proxy (Proxy (..)) -import Test.QuickCheck (Result, (===)) +import Test.QuickCheck (quickCheckGen, Result, (===)) +import Effect (Effect) +import Effect.Console (log) + + + +byteLengthDivBytesPerValueTests :: Effect Unit +byteLengthDivBytesPerValueTests = do + log " - byteLength x / bytesPerValue === length x" + log " - Uint8ClampedArray" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint8ClampedArray) + log " - Uint32Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint32Array) + log " - Uint16Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint16Array) + log " - Uint8Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint8Array) + log " - Int32Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryInt32Array) + log " - Int16Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryInt16Array) + log " - Int8Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryInt8Array) + log " - Float32Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryFloat32Array) + log " - Float64Array" + quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryFloat64Array) byteLengthDivBytesPerValueEqLength :: forall a n. BytesPerValue a n => Nat n => ArrayView a -> Result From b848ecb917d36b0a5fd235f893114198758ac8ff Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 6 Dec 2018 13:16:11 -0700 Subject: [PATCH 072/236] fromArray <<< toArray iso --- test/Main.purs | 5 ++- test/Properties/TypedArray.purs | 78 +++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 05e015c..1925879 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,6 +1,6 @@ module Test.Main where -import Test.Properties.TypedArray as TATests +import Test.Properties.TypedArray (typedArrayTests) import Prelude import Effect (Effect) @@ -12,7 +12,8 @@ import Effect.Console (log) main :: Effect Unit main = do log "Starting tests..." - TATests.byteLengthDivBytesPerValueTests + log " - TypedArray Tests:" + typedArrayTests -- import Data.ArrayBuffer.ArrayBuffer as AB diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index f86cbf1..571dca9 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -3,7 +3,7 @@ module Test.Properties.TypedArray where import Data.ArrayBuffer.Types (ArrayView) import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Typed (class BytesPerValue) +import Data.ArrayBuffer.Typed (class BytesPerValue, class TypedArray) import Data.ArrayBuffer.Typed.Gen ( arbitraryUint8ClampedArray, arbitraryUint8Array, arbitraryUint16Array, arbitraryUint32Array , arbitraryInt8Array, arbitraryInt16Array, arbitraryInt32Array @@ -17,31 +17,55 @@ import Effect (Effect) import Effect.Console (log) +typedArrayTests :: Effect Unit +typedArrayTests = do + log " - byteLength x / bytesPerValue === length x" + byteLengthDivBytesPerValueTests + log " - fromArray (toArray x) === x" + fromArrayToArrayIsoTests + + +type TestableArrayF a t n = + Show t + => Eq t + => TypedArray a t + => BytesPerValue a n + => Nat n + => ArrayView a -> Result + + +overAll :: (forall a t n. TestableArrayF a t n) -> Effect Unit +overAll f = do + log " - Uint8ClampedArray" + quickCheckGen (f <$> arbitraryUint8ClampedArray) + log " - Uint32Array" + quickCheckGen (f <$> arbitraryUint32Array) + log " - Uint16Array" + quickCheckGen (f <$> arbitraryUint16Array) + log " - Uint8Array" + quickCheckGen (f <$> arbitraryUint8Array) + log " - Int32Array" + quickCheckGen (f <$> arbitraryInt32Array) + log " - Int16Array" + quickCheckGen (f <$> arbitraryInt16Array) + log " - Int8Array" + quickCheckGen (f <$> arbitraryInt8Array) + log " - Float32Array" + quickCheckGen (f <$> arbitraryFloat32Array) + log " - Float64Array" + quickCheckGen (f <$> arbitraryFloat64Array) + byteLengthDivBytesPerValueTests :: Effect Unit -byteLengthDivBytesPerValueTests = do - log " - byteLength x / bytesPerValue === length x" - log " - Uint8ClampedArray" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint8ClampedArray) - log " - Uint32Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint32Array) - log " - Uint16Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint16Array) - log " - Uint8Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryUint8Array) - log " - Int32Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryInt32Array) - log " - Int16Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryInt16Array) - log " - Int8Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryInt8Array) - log " - Float32Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryFloat32Array) - log " - Float64Array" - quickCheckGen (byteLengthDivBytesPerValueEqLength <$> arbitraryFloat64Array) - - -byteLengthDivBytesPerValueEqLength :: forall a n. BytesPerValue a n => Nat n => ArrayView a -> Result -byteLengthDivBytesPerValueEqLength a = - let n = toInt' (Proxy :: Proxy n) - in TA.length a === (TA.byteLength a `div` n) +byteLengthDivBytesPerValueTests = overAll byteLengthDivBytesPerValueEqLength + where + byteLengthDivBytesPerValueEqLength :: forall a t n. TestableArrayF a t n + byteLengthDivBytesPerValueEqLength a = + let n = toInt' (Proxy :: Proxy n) + in TA.length a === (TA.byteLength a `div` n) + +fromArrayToArrayIsoTests :: Effect Unit +fromArrayToArrayIsoTests = overAll fromArrayToArrayIso + where + fromArrayToArrayIso :: forall a t n. TestableArrayF a t n + fromArrayToArrayIso x = TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x From f7b256e66590919a945edcf0fd5783fe62d0dd17 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Fri, 7 Dec 2018 17:36:48 -0700 Subject: [PATCH 073/236] better docs --- .../Data/ArrayBuffer/ArrayBuffer.md | 2 +- generated-docs/Data/ArrayBuffer/DataView.md | 20 +-- generated-docs/Data/ArrayBuffer/Typed.md | 163 ++++++++++-------- generated-docs/Data/ArrayBuffer/Typed/Gen.md | 107 ++++++++++++ src/Data/ArrayBuffer/Typed.purs | 37 +++- 5 files changed, 242 insertions(+), 87 deletions(-) create mode 100644 generated-docs/Data/ArrayBuffer/Typed/Gen.md diff --git a/generated-docs/Data/ArrayBuffer/ArrayBuffer.md b/generated-docs/Data/ArrayBuffer/ArrayBuffer.md index 35ed8a4..4e9eff0 100644 --- a/generated-docs/Data/ArrayBuffer/ArrayBuffer.md +++ b/generated-docs/Data/ArrayBuffer/ArrayBuffer.md @@ -22,7 +22,7 @@ Represents the length of an `ArrayBuffer` in bytes. #### `slice` ``` purescript -slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer +slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer ``` Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. diff --git a/generated-docs/Data/ArrayBuffer/DataView.md b/generated-docs/Data/ArrayBuffer/DataView.md index 88a3c48..e6d3eb3 100644 --- a/generated-docs/Data/ArrayBuffer/DataView.md +++ b/generated-docs/Data/ArrayBuffer/DataView.md @@ -86,7 +86,7 @@ Fetch int32 value at a certain index in a `DataView`. #### `getUint8` ``` purescript -getUint8 :: Getter UInt +getUint8 :: Getter Int ``` Fetch uint8 value at a certain index in a `DataView`. @@ -94,7 +94,7 @@ Fetch uint8 value at a certain index in a `DataView`. #### `getUint16be` ``` purescript -getUint16be :: Getter UInt +getUint16be :: Getter Int ``` Fetch uint16 value at a certain index in a `DataView`. @@ -102,7 +102,7 @@ Fetch uint16 value at a certain index in a `DataView`. #### `getUint32be` ``` purescript -getUint32be :: Getter UInt +getUint32be :: Getter Number ``` Fetch uint32 value at a certain index in a `DataView`. @@ -138,13 +138,13 @@ getInt32le :: Getter Int #### `getUint16le` ``` purescript -getUint16le :: Getter UInt +getUint16le :: Getter Int ``` #### `getUint32le` ``` purescript -getUint32le :: Getter UInt +getUint32le :: Getter Number ``` #### `getFloat32le` @@ -194,7 +194,7 @@ Store int32 value at a certain index in a `DataView`. #### `setUint8` ``` purescript -setUint8 :: Setter UInt +setUint8 :: Setter Int ``` Store uint8 value at a certain index in a `DataView`. @@ -202,7 +202,7 @@ Store uint8 value at a certain index in a `DataView`. #### `setUint16be` ``` purescript -setUint16be :: Setter UInt +setUint16be :: Setter Int ``` Store uint16 value at a certain index in a `DataView`. @@ -210,7 +210,7 @@ Store uint16 value at a certain index in a `DataView`. #### `setUint32be` ``` purescript -setUint32be :: Setter UInt +setUint32be :: Setter Number ``` Store uint32 value at a certain index in a `DataView`. @@ -246,13 +246,13 @@ setInt32le :: Setter Int #### `setUint16le` ``` purescript -setUint16le :: Setter UInt +setUint16le :: Setter Int ``` #### `setUint32le` ``` purescript -setUint32le :: Setter UInt +setUint32le :: Setter Number ``` #### `setFloat32le` diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md index 3aecec6..7d35237 100644 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -11,14 +11,6 @@ polyFill :: Effect Unit Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -#### `buffer` - -``` purescript -buffer :: forall a. ArrayView a -> ArrayBuffer -``` - -`ArrayBuffer` being mapped by the typed array. - #### `Offset` ``` purescript @@ -35,6 +27,14 @@ type Length = Int Value-oriented array length +#### `buffer` + +``` purescript +buffer :: forall a. ArrayView a -> ArrayBuffer +``` + +`ArrayBuffer` being mapped by the typed array. + #### `byteOffset` ``` purescript @@ -51,35 +51,29 @@ byteLength :: forall a. ArrayView a -> ByteLength Represents the length of this typed array, in bytes. -#### `AProxy` +#### `length` ``` purescript -data AProxy (a :: ArrayViewType) - = AProxy +length :: forall a b. BytesPerValue a b => ArrayView a -> Int ``` #### `BytesPerValue` ``` purescript -class BytesPerValue (a :: ArrayViewType) where - bytesPerValue :: AProxy a -> Int +class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b ``` ##### Instances ``` purescript -BytesPerValue Uint8Clamped -BytesPerValue Uint32 -BytesPerValue Uint16 -BytesPerValue Uint8 -BytesPerValue Int32 -BytesPerValue Int16 -BytesPerValue Int8 -``` - -#### `length` - -``` purescript -length :: forall a. BytesPerValue a => ArrayView a -> Int +BytesPerValue Uint8Clamped D1 +BytesPerValue Uint32 D4 +BytesPerValue Uint16 D2 +BytesPerValue Uint8 D1 +BytesPerValue Int32 D4 +BytesPerValue Int16 D2 +BytesPerValue Int8 D1 +BytesPerValue Float32 D4 +BytesPerValue Float64 D8 ``` #### `TypedArray` @@ -91,85 +85,112 @@ class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where part :: ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) empty :: Length -> ArrayView a fromArray :: Array t -> ArrayView a - all :: (t -> Boolean) -> ArrayView a -> Boolean - any :: (t -> Boolean) -> ArrayView a -> Boolean - fill :: ArrayView a -> t -> Effect Unit - fillRemainder :: ArrayView a -> t -> Offset -> Effect Unit - fillPart :: ArrayView a -> t -> Offset -> Offset -> Effect Unit - set :: ArrayView a -> Array t -> Effect Unit - set' :: ArrayView a -> Offset -> Array t -> Effect Unit - map' :: (t -> t) -> ArrayView a -> ArrayView a - traverse' :: (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) - traverse_' :: (t -> Effect Unit) -> ArrayView a -> Effect Unit - filter :: (t -> Boolean) -> ArrayView a -> ArrayView a + fill :: ArrayView a -> t -> Maybe (Tuple Offset (Maybe Offset)) -> Effect Unit + set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit + map :: (t -> Offset -> t) -> ArrayView a -> ArrayView a + traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) + traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit + all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) + elem :: t -> Maybe Offset -> ArrayView a -> Boolean unsafeAt :: ArrayView a -> Offset -> Effect t + foldlM :: forall b. ArrayView a -> (b -> t -> Offset -> Effect b) -> b -> Effect b + foldl1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t + foldrM :: forall b. ArrayView a -> (t -> b -> Offset -> Effect b) -> b -> Effect b + foldr1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t + find :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe t) + findIndex :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe Offset) + indexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset + lastIndexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset ``` -Measured user-level values stored in each typed array +Typeclass that associates a measured user-level type with a typed array. -##### Instances -``` purescript -TypedArray Uint8Clamped Int -``` +# Creation -#### `copyWithin` +- `whole`, `remainder`, and `part` are methods for building a typed array accessible interface + on top of an existing `ArrayBuffer`. +- `empty` and `fromArray` are methods for creating pure typed arrays -``` purescript -copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Effect Unit -``` +# Modification -Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. +- `fill`, `set`, and `setTyped` are methods for assigning values from external sources +- `map` and `traverse` allow you to create a new array from the existing values in another +- `copyWithin` allows you to set values to the array that exist in other parts of the array +- `filter` creates a new array without the values that don't pass a predicate +- `reverse` modifies an existing array in-place, with all values reversed +- `sort` modifies an existing array in-place, with all values sorted + +# Access -#### `copyWithinPart` +- `elem`, `all`, and `any` are functions for testing the contents of an array +- `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset +- `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array +- `find` and `findIndex` are searching functions via a predicate +- `indexOf` and `lastIndexOf` are searching functions via equality +- `slice` returns a new typed array on the same array buffer content as the input +- `subArray` returns a new typed array with a separate array buffer +- `toString` prints to a CSV, `toString'` allows you to supply the delimiter +- `toArray` returns an array of numeric values +##### Instances ``` purescript -copyWithinPart :: forall a. ArrayView a -> Offset -> Offset -> Offset -> Effect Unit +TypedArray Uint8Clamped Int +TypedArray Uint32 Number +TypedArray Uint16 Int +TypedArray Uint8 Int +TypedArray Int32 Int +TypedArray Int16 Int +TypedArray Int8 Int +TypedArray Float32 Number +TypedArray Float64 Number ``` -#### `reverse` +#### `foldl` ``` purescript -reverse :: forall a. ArrayView a -> Effect Unit +foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b ``` -Reverses a typed array in-place. - -#### `setTyped` +#### `foldl1` ``` purescript -setTyped :: forall a. ArrayView a -> ArrayView a -> Effect Unit +foldl1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t ``` -Stores multiple values in the typed array, reading input values from the second typed array. - -#### `setTyped'` +#### `foldr` ``` purescript -setTyped' :: forall a. ArrayView a -> Offset -> ArrayView a -> Effect Unit +foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) -> b -> b ``` -Stores multiple values in the typed array, reading input values from the second typed array, with offset. +#### `foldr1` + +``` purescript +foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +``` -#### `copy` +#### `setTyped` ``` purescript -copy :: forall a. ArrayView a -> ArrayView a +setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Unit ``` -Copy the entire contents of the typed array into a new buffer. +Stores multiple values in the typed array, reading input values from the second typed array. -#### `sliceRemainder` +#### `copyWithin` ``` purescript -sliceRemainder :: forall a. ArrayView a -> Offset -> ArrayView a +copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit ``` -Copy the remainder of contents of the typed array into a new buffer, after some start index. +Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. #### `slice` ``` purescript -slice :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a +slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a ``` Copy part of the contents of a typed array into a new buffer, between some start and end indices. @@ -185,18 +206,18 @@ Sorts the values in-place #### `subArray` ``` purescript -subArray :: forall a. ArrayView a -> Offset -> Offset -> ArrayView a +subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a ``` Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -#### `subArrayRemainder` +#### `reverse` ``` purescript -subArrayRemainder :: forall a. ArrayView a -> Offset -> ArrayView a +reverse :: forall a. ArrayView a -> Effect Unit ``` -Returns a new typed array view of the same buffer, beginning at the index +Reverses a typed array in-place. #### `toString` diff --git a/generated-docs/Data/ArrayBuffer/Typed/Gen.md b/generated-docs/Data/ArrayBuffer/Typed/Gen.md new file mode 100644 index 0000000..ccff978 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/Typed/Gen.md @@ -0,0 +1,107 @@ +## Module Data.ArrayBuffer.Typed.Gen + +Functions for generating random typed arrays. + +#### `arbitraryUint8ClampedArray` + +``` purescript +arbitraryUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray +``` + +#### `arbitraryUint32Array` + +``` purescript +arbitraryUint32Array :: forall m. MonadGen m => m Uint32Array +``` + +#### `arbitraryUint16Array` + +``` purescript +arbitraryUint16Array :: forall m. MonadGen m => m Uint16Array +``` + +#### `arbitraryUint8Array` + +``` purescript +arbitraryUint8Array :: forall m. MonadGen m => m Uint8Array +``` + +#### `arbitraryInt32Array` + +``` purescript +arbitraryInt32Array :: forall m. MonadGen m => m Int32Array +``` + +#### `arbitraryInt16Array` + +``` purescript +arbitraryInt16Array :: forall m. MonadGen m => m Int16Array +``` + +#### `arbitraryInt8Array` + +``` purescript +arbitraryInt8Array :: forall m. MonadGen m => m Int8Array +``` + +#### `arbitraryFloat32Array` + +``` purescript +arbitraryFloat32Array :: forall m. MonadGen m => m Float32Array +``` + +#### `arbitraryFloat64Array` + +``` purescript +arbitraryFloat64Array :: forall m. MonadGen m => m Float64Array +``` + +#### `arbitraryUByte` + +``` purescript +arbitraryUByte :: forall m. MonadGen m => m Int +``` + +#### `arbitraryByte` + +``` purescript +arbitraryByte :: forall m. MonadGen m => m Int +``` + +#### `arbitraryUNibble` + +``` purescript +arbitraryUNibble :: forall m. MonadGen m => m Int +``` + +#### `arbitraryNibble` + +``` purescript +arbitraryNibble :: forall m. MonadGen m => m Int +``` + +#### `arbitraryUWord` + +``` purescript +arbitraryUWord :: forall m. MonadGen m => m Number +``` + +#### `arbitraryWord` + +``` purescript +arbitraryWord :: forall m. MonadGen m => m Int +``` + +#### `arbitraryFloat32` + +``` purescript +arbitraryFloat32 :: forall m. MonadGen m => m Number +``` + +#### `arbitraryFloat64` + +``` purescript +arbitraryFloat64 :: forall m. MonadGen m => m Number +``` + + diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 9adf515..2c5a746 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -108,7 +108,34 @@ type Length = Int -- TODO use purescript-quotient --- | Measured user-level values stored in each typed array +-- | Typeclass that associates a measured user-level type with a typed array. +-- | +-- | # Creation +-- | +-- | - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface +-- | on top of an existing `ArrayBuffer`. +-- | - `empty` and `fromArray` are methods for creating pure typed arrays +-- | +-- | # Modification +-- | +-- | - `fill`, `set`, and `setTyped` are methods for assigning values from external sources +-- | - `map` and `traverse` allow you to create a new array from the existing values in another +-- | - `copyWithin` allows you to set values to the array that exist in other parts of the array +-- | - `filter` creates a new array without the values that don't pass a predicate +-- | - `reverse` modifies an existing array in-place, with all values reversed +-- | - `sort` modifies an existing array in-place, with all values sorted +-- | +-- | # Access +-- | +-- | - `elem`, `all`, and `any` are functions for testing the contents of an array +-- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset +-- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array +-- | - `find` and `findIndex` are searching functions via a predicate +-- | - `indexOf` and `lastIndexOf` are searching functions via equality +-- | - `slice` returns a new typed array on the same array buffer content as the input +-- | - `subArray` returns a new typed array with a separate array buffer +-- | - `toString` prints to a CSV, `toString'` allows you to supply the delimiter +-- | - `toArray` returns an array of numeric values class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where -- | View mapping the whole `ArrayBuffer`. whole :: ArrayBuffer -> ArrayView a @@ -120,10 +147,6 @@ class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where empty :: Length -> ArrayView a -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: Array t -> ArrayView a - -- | Test a predicate to pass on all values - all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean - -- | Test a predicate to pass on any value - any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean -- | Fill the array with a value fill :: ArrayView a -> t -> Maybe (Tuple Offset (Maybe Offset)) -> Effect Unit -- | Stores multiple values into the typed array @@ -134,6 +157,10 @@ class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) -- | Traverses over each value traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit + -- | Test a predicate to pass on all values + all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + -- | Test a predicate to pass on any value + any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean -- | Returns a new typed array with all values that pass the predicate filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) -- | Tests if a value is an element of the typed array From 875662ecf44b247a784c3dffb2ea2f2854b796b4 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Fri, 7 Dec 2018 17:37:55 -0700 Subject: [PATCH 074/236] cosmetic --- src/Data/ArrayBuffer/Typed.purs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 2c5a746..534aae7 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -110,13 +110,13 @@ type Length = Int -- TODO use purescript-quotient -- | Typeclass that associates a measured user-level type with a typed array. -- | --- | # Creation +-- | #### Creation -- | -- | - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface -- | on top of an existing `ArrayBuffer`. -- | - `empty` and `fromArray` are methods for creating pure typed arrays -- | --- | # Modification +-- | #### Modification -- | -- | - `fill`, `set`, and `setTyped` are methods for assigning values from external sources -- | - `map` and `traverse` allow you to create a new array from the existing values in another @@ -125,7 +125,7 @@ type Length = Int -- | - `reverse` modifies an existing array in-place, with all values reversed -- | - `sort` modifies an existing array in-place, with all values sorted -- | --- | # Access +-- | #### Access -- | -- | - `elem`, `all`, and `any` are functions for testing the contents of an array -- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset From 89da71daed9be193cef160a05d82dd250012bf18 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Fri, 7 Dec 2018 18:13:59 -0700 Subject: [PATCH 075/236] using uint --- bower.json | 3 +- generated-docs/Data/ArrayBuffer/Typed.md | 88 ++++++++++++------------ src/Data/ArrayBuffer/Typed.purs | 45 ++++++------ src/Data/ArrayBuffer/Typed/Gen.purs | 6 +- 4 files changed, 75 insertions(+), 67 deletions(-) diff --git a/bower.json b/bower.json index cfd91b5..21ebe99 100644 --- a/bower.json +++ b/bower.json @@ -18,7 +18,8 @@ "purescript-effect": "^2.0.0", "purescript-nullable": "^4.1.0", "purescript-typelevel": "^4.0.0", - "purescript-parseint": "^1.1.0" + "purescript-parseint": "^1.1.0", + "purescript-uint": "^4.0.2" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md index 7d35237..633f418 100644 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -107,13 +107,13 @@ class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where Typeclass that associates a measured user-level type with a typed array. -# Creation +#### Creation - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface on top of an existing `ArrayBuffer`. - `empty` and `fromArray` are methods for creating pure typed arrays -# Modification +#### Modification - `fill`, `set`, and `setTyped` are methods for assigning values from external sources - `map` and `traverse` allow you to create a new array from the existing values in another @@ -122,7 +122,7 @@ Typeclass that associates a measured user-level type with a typed array. - `reverse` modifies an existing array in-place, with all values reversed - `sort` modifies an existing array in-place, with all values sorted -# Access +#### Access - `elem`, `all`, and `any` are functions for testing the contents of an array - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset @@ -147,45 +147,37 @@ TypedArray Float32 Number TypedArray Float64 Number ``` -#### `foldl` +#### `setTyped` ``` purescript -foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b +setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Unit ``` -#### `foldl1` - -``` purescript -foldl1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t -``` +Stores multiple values in the typed array, reading input values from the second typed array. -#### `foldr` +#### `copyWithin` ``` purescript -foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) -> b -> b +copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit ``` -#### `foldr1` - -``` purescript -foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t -``` +Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. -#### `setTyped` +#### `sort` ``` purescript -setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Unit +sort :: forall a. ArrayView a -> Effect Unit ``` -Stores multiple values in the typed array, reading input values from the second typed array. +Sorts the values in-place -#### `copyWithin` +#### `reverse` ``` purescript -copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit +reverse :: forall a. ArrayView a -> Effect Unit ``` -Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. +Reverses a typed array in-place. #### `slice` @@ -195,61 +187,69 @@ slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayVi Copy part of the contents of a typed array into a new buffer, between some start and end indices. -#### `sort` +#### `subArray` ``` purescript -sort :: forall a. ArrayView a -> Effect Unit +subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a ``` -Sorts the values in-place +Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -#### `subArray` +#### `hasIndex` ``` purescript -subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a +hasIndex :: forall a. ArrayView a -> Offset -> Boolean ``` -Returns a new typed array view of the same buffer, beginning at the index and ending at the second. +Determine if a certain index is valid. -#### `reverse` +#### `at` ``` purescript -reverse :: forall a. ArrayView a -> Effect Unit +at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t ``` -Reverses a typed array in-place. +Fetch element at index. -#### `toString` +#### `foldl` ``` purescript -toString :: forall a. ArrayView a -> String +foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b ``` -Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. +#### `foldl1` -#### `toString'` +``` purescript +foldl1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +``` + +#### `foldr` ``` purescript -toString' :: forall a. ArrayView a -> String -> String +foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) -> b -> b ``` -Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. +#### `foldr1` -#### `hasIndex` +``` purescript +foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +``` + +#### `toString` ``` purescript -hasIndex :: forall a. ArrayView a -> Offset -> Boolean +toString :: forall a. ArrayView a -> String ``` -Determine if a certain index is valid. +Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. -#### `at` +#### `toString'` ``` purescript -at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t +toString' :: forall a. ArrayView a -> String -> String ``` -Fetch element at index. +Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. #### `toArray` diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 534aae7..d2379d4 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -7,14 +7,16 @@ module Data.ArrayBuffer.Typed , buffer, byteOffset, byteLength, length , class BytesPerValue , class TypedArray - , whole, remainder, part, empty, fromArray, all, any, fill, set - , map, traverse, traverse_, filter, elem + , whole, remainder, part, empty, fromArray + , fill, set, setTyped, copyWithin + , map, traverse, traverse_, filter + , sort, reverse + , elem, all, any + , unsafeAt, hasIndex, at , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 , find, findIndex, indexOf, lastIndexOf - , setTyped, copyWithin, slice, sort, subArray, reverse - , toString, toString' - , unsafeAt, hasIndex, at - , toArray + , slice, subArray + , toString, toString', toArray ) where import Prelude @@ -27,6 +29,8 @@ import Effect.Unsafe (unsafePerformEffect) import Data.Tuple (Tuple (..)) import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, toNullable, toMaybe) +import Data.UInt (UInt) +import Data.UInt (fromNumber, toNumber) as UInt import Data.ArrayBuffer.Types ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength , Float64Array, Float32Array @@ -213,26 +217,27 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -instance typedArrayUint32 :: TypedArray Uint32 Number where +instance typedArrayUint32 :: TypedArray Uint32 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint32Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable Nothing) part a x y = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newUint32Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) (toNullable Nothing) (toNullable Nothing)) + all p a = runEffectFn2 everyImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) + any p a = runEffectFn2 someImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl + Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) (toNullable Nothing) + Just e -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) (toNullable (Just e)) + set a mo x = runEffectFn3 setImpl a (toNullable mo) (UInt.toNumber <$> x) + map f a = unsafePerformEffect $ runEffectFn2 mapImpl a $ + mkEffectFn2 \x o -> pure $ UInt.toNumber $ f (UInt.fromNumber x) o + traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> UInt.toNumber <$> f (UInt.fromNumber x) o)) + traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 (f <<< UInt.fromNumber)) + filter p a = runEffectFn2 filterImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) + elem x mo a = runFn3 includesImpl a (UInt.toNumber x) (toNullable mo) + unsafeAt o ms = UInt.fromNumber <$> runEffectFn2 unsafeAtImpl o ms foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 7631092..934d67f 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -14,6 +14,8 @@ import Math as M import Data.Maybe (Maybe (..)) import Data.List.Lazy (replicateM) import Data.Int as I +import Data.UInt (UInt) +import Data.UInt as UInt import Data.String.CodeUnits as S import Data.Float.Parse (parseFloat) import Data.Array as Array @@ -76,8 +78,8 @@ arbitraryNibble = let j = I.pow 2 8 in chooseInt (negate j) (j - 1) -arbitraryUWord :: forall m. MonadGen m => m Number -arbitraryUWord = M.round <$> chooseFloat 0.0 ((M.pow 2.0 32.0) - 1.0) +arbitraryUWord :: forall m. MonadGen m => m UInt +arbitraryUWord = UInt.fromNumber <$> chooseFloat 0.0 ((M.pow 2.0 32.0) - 1.0) arbitraryWord :: forall m. MonadGen m => m Int arbitraryWord = From f93917227e8bd4651403359bbd02590c6dbe8a4b Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Mon, 10 Dec 2018 04:57:38 -0700 Subject: [PATCH 076/236] fill y xs => all (== y) xs --- test/Properties/TypedArray.purs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 571dca9..d125d18 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -10,10 +10,12 @@ import Data.ArrayBuffer.Typed.Gen , arbitraryFloat32Array, arbitraryFloat64Array) import Prelude +import Data.Maybe (Maybe (..)) import Data.Typelevel.Num (toInt', class Nat) import Type.Proxy (Proxy (..)) import Test.QuickCheck (quickCheckGen, Result, (===)) import Effect (Effect) +import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) @@ -23,6 +25,8 @@ typedArrayTests = do byteLengthDivBytesPerValueTests log " - fromArray (toArray x) === x" fromArrayToArrayIsoTests + log " - fill y x => all (== y) x" + allAreFilledTests type TestableArrayF a t n = @@ -30,6 +34,7 @@ type TestableArrayF a t n = => Eq t => TypedArray a t => BytesPerValue a n + => Semiring t => Nat n => ArrayView a -> Result @@ -69,3 +74,16 @@ fromArrayToArrayIsoTests = overAll fromArrayToArrayIso where fromArrayToArrayIso :: forall a t n. TestableArrayF a t n fromArrayToArrayIso x = TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x + + +allAreFilledTests :: Effect Unit +allAreFilledTests = overAll allAreFilled + where + allAreFilled :: forall a t n. TestableArrayF a t n + allAreFilled xs = unsafePerformEffect do + let x = case TA.at xs 0 of + Nothing -> zero + Just y -> y + TA.fill xs x Nothing + b <- TA.all (\y o -> pure (y == x)) xs + pure (b === true) From 3b0f0ab330a2f973f5e76f2dc4156ef84455a611 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 11 Dec 2018 14:02:53 -0700 Subject: [PATCH 077/236] generic instance of `WithOffset` structure --- bower.json | 3 +- generated-docs/Data/ArrayBuffer/Typed.md | 34 +++---- generated-docs/Data/ArrayBuffer/Typed/Gen.md | 2 +- src/Data/ArrayBuffer/Typed/Gen.purs | 95 +++++++++++--------- test/Properties/TypedArray.purs | 20 +++-- 5 files changed, 83 insertions(+), 71 deletions(-) diff --git a/bower.json b/bower.json index 21ebe99..ebcdd15 100644 --- a/bower.json +++ b/bower.json @@ -19,7 +19,8 @@ "purescript-nullable": "^4.1.0", "purescript-typelevel": "^4.0.0", "purescript-parseint": "^1.1.0", - "purescript-uint": "^4.0.2" + "purescript-uint": "^4.0.2", + "purescript-sized-vectors": "^3.1.0" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md index 633f418..1b2c56a 100644 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -137,7 +137,7 @@ Typeclass that associates a measured user-level type with a typed array. ##### Instances ``` purescript TypedArray Uint8Clamped Int -TypedArray Uint32 Number +TypedArray Uint32 UInt TypedArray Uint16 Int TypedArray Uint8 Int TypedArray Int32 Int @@ -179,22 +179,6 @@ reverse :: forall a. ArrayView a -> Effect Unit Reverses a typed array in-place. -#### `slice` - -``` purescript -slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a -``` - -Copy part of the contents of a typed array into a new buffer, between some start and end indices. - -#### `subArray` - -``` purescript -subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a -``` - -Returns a new typed array view of the same buffer, beginning at the index and ending at the second. - #### `hasIndex` ``` purescript @@ -235,6 +219,22 @@ foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t ``` +#### `slice` + +``` purescript +slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +``` + +Copy part of the contents of a typed array into a new buffer, between some start and end indices. + +#### `subArray` + +``` purescript +subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a +``` + +Returns a new typed array view of the same buffer, beginning at the index and ending at the second. + #### `toString` ``` purescript diff --git a/generated-docs/Data/ArrayBuffer/Typed/Gen.md b/generated-docs/Data/ArrayBuffer/Typed/Gen.md index ccff978..94645c3 100644 --- a/generated-docs/Data/ArrayBuffer/Typed/Gen.md +++ b/generated-docs/Data/ArrayBuffer/Typed/Gen.md @@ -83,7 +83,7 @@ arbitraryNibble :: forall m. MonadGen m => m Int #### `arbitraryUWord` ``` purescript -arbitraryUWord :: forall m. MonadGen m => m Number +arbitraryUWord :: forall m. MonadGen m => m UInt ``` #### `arbitraryWord` diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 934d67f..7723389 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -5,7 +5,7 @@ module Data.ArrayBuffer.Typed.Gen where import Data.ArrayBuffer.Types ( Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array , Int8Array, Int16Array, Int32Array - , Float32Array, Float64Array + , Float32Array, Float64Array, ArrayView ) import Data.ArrayBuffer.Typed as TA @@ -19,75 +19,77 @@ import Data.UInt as UInt import Data.String.CodeUnits as S import Data.Float.Parse (parseFloat) import Data.Array as Array +import Data.Vec (Vec) +import Data.Generic.Rep (class Generic) import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Partial.Unsafe (unsafePartial) -arbitraryUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray -arbitraryUint8ClampedArray = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUByte +genUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray +genUint8ClampedArray = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genUByte -arbitraryUint32Array :: forall m. MonadGen m => m Uint32Array -arbitraryUint32Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUWord +genUint32Array :: forall m. MonadGen m => m Uint32Array +genUint32Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genUWord -arbitraryUint16Array :: forall m. MonadGen m => m Uint16Array -arbitraryUint16Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUNibble +genUint16Array :: forall m. MonadGen m => m Uint16Array +genUint16Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genUNibble -arbitraryUint8Array :: forall m. MonadGen m => m Uint8Array -arbitraryUint8Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryUByte +genUint8Array :: forall m. MonadGen m => m Uint8Array +genUint8Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genUByte -arbitraryInt32Array :: forall m. MonadGen m => m Int32Array -arbitraryInt32Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryWord +genInt32Array :: forall m. MonadGen m => m Int32Array +genInt32Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genWord -arbitraryInt16Array :: forall m. MonadGen m => m Int16Array -arbitraryInt16Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryNibble +genInt16Array :: forall m. MonadGen m => m Int16Array +genInt16Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genNibble -arbitraryInt8Array :: forall m. MonadGen m => m Int8Array -arbitraryInt8Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryByte +genInt8Array :: forall m. MonadGen m => m Int8Array +genInt8Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genByte -arbitraryFloat32Array :: forall m. MonadGen m => m Float32Array -arbitraryFloat32Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryFloat32 +genFloat32Array :: forall m. MonadGen m => m Float32Array +genFloat32Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genFloat32 -arbitraryFloat64Array :: forall m. MonadGen m => m Float64Array -arbitraryFloat64Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s arbitraryFloat64 +genFloat64Array :: forall m. MonadGen m => m Float64Array +genFloat64Array = sized \s -> + TA.fromArray <<< Array.fromFoldable <$> replicateM s genFloat64 -arbitraryUByte :: forall m. MonadGen m => m Int -arbitraryUByte = chooseInt 0 ((I.pow 2 8) - 1) +genUByte :: forall m. MonadGen m => m Int +genUByte = chooseInt 0 ((I.pow 2 8) - 1) -arbitraryByte :: forall m. MonadGen m => m Int -arbitraryByte = +genByte :: forall m. MonadGen m => m Int +genByte = let j = I.pow 2 4 in chooseInt (negate j) (j - 1) -arbitraryUNibble :: forall m. MonadGen m => m Int -arbitraryUNibble = chooseInt 0 ((I.pow 2 16) - 1) +genUNibble :: forall m. MonadGen m => m Int +genUNibble = chooseInt 0 ((I.pow 2 16) - 1) -arbitraryNibble :: forall m. MonadGen m => m Int -arbitraryNibble = +genNibble :: forall m. MonadGen m => m Int +genNibble = let j = I.pow 2 8 in chooseInt (negate j) (j - 1) -arbitraryUWord :: forall m. MonadGen m => m UInt -arbitraryUWord = UInt.fromNumber <$> chooseFloat 0.0 ((M.pow 2.0 32.0) - 1.0) +genUWord :: forall m. MonadGen m => m UInt +genUWord = UInt.fromNumber <$> chooseFloat 0.0 ((M.pow 2.0 32.0) - 1.0) -arbitraryWord :: forall m. MonadGen m => m Int -arbitraryWord = +genWord :: forall m. MonadGen m => m Int +genWord = let j = I.pow 2 16 in chooseInt (negate j) (j - 1) -arbitraryFloat32 :: forall m. MonadGen m => m Number -arbitraryFloat32 = +genFloat32 :: forall m. MonadGen m => m Number +genFloat32 = let maxFloat32 = (1.0 - (M.pow 2.0 (-24.0))) * (M.pow 2.0 128.0) minFloat32 = -maxFloat32 -- because of sign bit reformat :: String -> String @@ -101,5 +103,10 @@ arbitraryFloat32 = in fix <$> chooseFloat minFloat32 maxFloat32 -- roughly estimated because of variable precision between 6 and 9 digs -arbitraryFloat64 :: forall m. MonadGen m => m Number -arbitraryFloat64 = chooseFloat (-1.7e308) 1.7e308 +genFloat64 :: forall m. MonadGen m => m Number +genFloat64 = chooseFloat (-1.7e308) 1.7e308 + + +-- For generating some set of offsets, inside the array +data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) +derive instance genericWithOffset :: Generic (ArrayView a) a' => Generic (WithOffset n a) _ diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index d125d18..36e73de 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -13,7 +13,7 @@ import Prelude import Data.Maybe (Maybe (..)) import Data.Typelevel.Num (toInt', class Nat) import Type.Proxy (Proxy (..)) -import Test.QuickCheck (quickCheckGen, Result, (===)) +import Test.QuickCheck (quickCheckGen, Result, (===), class Testable, class Arbitrary) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) @@ -29,17 +29,18 @@ typedArrayTests = do allAreFilledTests -type TestableArrayF a t n = +type TestableArrayF a t n q = Show t => Eq t => TypedArray a t => BytesPerValue a n + -- => Arbitrary t => Semiring t => Nat n - => ArrayView a -> Result + => ArrayView a -> q -overAll :: (forall a t n. TestableArrayF a t n) -> Effect Unit +overAll :: forall q. Testable q => (forall a t n. TestableArrayF a t n q) -> Effect Unit overAll f = do log " - Uint8ClampedArray" quickCheckGen (f <$> arbitraryUint8ClampedArray) @@ -64,7 +65,7 @@ overAll f = do byteLengthDivBytesPerValueTests :: Effect Unit byteLengthDivBytesPerValueTests = overAll byteLengthDivBytesPerValueEqLength where - byteLengthDivBytesPerValueEqLength :: forall a t n. TestableArrayF a t n + byteLengthDivBytesPerValueEqLength :: forall a t n. TestableArrayF a t n Result byteLengthDivBytesPerValueEqLength a = let n = toInt' (Proxy :: Proxy n) in TA.length a === (TA.byteLength a `div` n) @@ -72,18 +73,21 @@ byteLengthDivBytesPerValueTests = overAll byteLengthDivBytesPerValueEqLength fromArrayToArrayIsoTests :: Effect Unit fromArrayToArrayIsoTests = overAll fromArrayToArrayIso where - fromArrayToArrayIso :: forall a t n. TestableArrayF a t n + fromArrayToArrayIso :: forall a t n. TestableArrayF a t n Result fromArrayToArrayIso x = TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x allAreFilledTests :: Effect Unit allAreFilledTests = overAll allAreFilled where - allAreFilled :: forall a t n. TestableArrayF a t n - allAreFilled xs = unsafePerformEffect do + allAreFilled :: forall a t n. TestableArrayF a t n Result -- (t -> Result) + allAreFilled xs {-x =-} = unsafePerformEffect do let x = case TA.at xs 0 of Nothing -> zero Just y -> y TA.fill xs x Nothing b <- TA.all (\y o -> pure (y == x)) xs pure (b === true) + + +-- setSingletonIsEq From cb6350692952dbdf12a066ff31da4fca23fe239b Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 12 Dec 2018 08:12:33 -0700 Subject: [PATCH 078/236] cosmetic, with offset --- README.md | 5 -- bower.json | 2 +- generated-docs/Data/ArrayBuffer/Typed/Gen.md | 90 ++++++++++++-------- src/Data/ArrayBuffer/Typed/Gen.purs | 38 +++++++-- test/Properties/TypedArray.purs | 33 +++---- 5 files changed, 103 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 7bd53a2..bbdcf1e 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,3 @@ ArrayBuffer bindings for PureScript. ## Documentation Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). - - -## Important Usage Notes - -- Usage of the ArrayBuffer<->String conversion functions requires the import of the NPM package 'text-encoding'. diff --git a/bower.json b/bower.json index ebcdd15..e604d3f 100644 --- a/bower.json +++ b/bower.json @@ -19,7 +19,7 @@ "purescript-nullable": "^4.1.0", "purescript-typelevel": "^4.0.0", "purescript-parseint": "^1.1.0", - "purescript-uint": "^4.0.2", + "purescript-uint": "^4.1.0", "purescript-sized-vectors": "^3.1.0" }, "devDependencies": { diff --git a/generated-docs/Data/ArrayBuffer/Typed/Gen.md b/generated-docs/Data/ArrayBuffer/Typed/Gen.md index 94645c3..45dabfd 100644 --- a/generated-docs/Data/ArrayBuffer/Typed/Gen.md +++ b/generated-docs/Data/ArrayBuffer/Typed/Gen.md @@ -1,107 +1,127 @@ ## Module Data.ArrayBuffer.Typed.Gen -Functions for generating random typed arrays. +Functions for generating typed arrays and values. -#### `arbitraryUint8ClampedArray` +#### `genUint8ClampedArray` ``` purescript -arbitraryUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray +genUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray ``` -#### `arbitraryUint32Array` +#### `genUint32Array` ``` purescript -arbitraryUint32Array :: forall m. MonadGen m => m Uint32Array +genUint32Array :: forall m. MonadGen m => m Uint32Array ``` -#### `arbitraryUint16Array` +#### `genUint16Array` ``` purescript -arbitraryUint16Array :: forall m. MonadGen m => m Uint16Array +genUint16Array :: forall m. MonadGen m => m Uint16Array ``` -#### `arbitraryUint8Array` +#### `genUint8Array` ``` purescript -arbitraryUint8Array :: forall m. MonadGen m => m Uint8Array +genUint8Array :: forall m. MonadGen m => m Uint8Array ``` -#### `arbitraryInt32Array` +#### `genInt32Array` ``` purescript -arbitraryInt32Array :: forall m. MonadGen m => m Int32Array +genInt32Array :: forall m. MonadGen m => m Int32Array ``` -#### `arbitraryInt16Array` +#### `genInt16Array` ``` purescript -arbitraryInt16Array :: forall m. MonadGen m => m Int16Array +genInt16Array :: forall m. MonadGen m => m Int16Array ``` -#### `arbitraryInt8Array` +#### `genInt8Array` ``` purescript -arbitraryInt8Array :: forall m. MonadGen m => m Int8Array +genInt8Array :: forall m. MonadGen m => m Int8Array ``` -#### `arbitraryFloat32Array` +#### `genFloat32Array` ``` purescript -arbitraryFloat32Array :: forall m. MonadGen m => m Float32Array +genFloat32Array :: forall m. MonadGen m => m Float32Array ``` -#### `arbitraryFloat64Array` +#### `genFloat64Array` ``` purescript -arbitraryFloat64Array :: forall m. MonadGen m => m Float64Array +genFloat64Array :: forall m. MonadGen m => m Float64Array ``` -#### `arbitraryUByte` +#### `genUByte` ``` purescript -arbitraryUByte :: forall m. MonadGen m => m Int +genUByte :: forall m. MonadGen m => m Int ``` -#### `arbitraryByte` +#### `genByte` ``` purescript -arbitraryByte :: forall m. MonadGen m => m Int +genByte :: forall m. MonadGen m => m Int ``` -#### `arbitraryUNibble` +#### `genUChomp` ``` purescript -arbitraryUNibble :: forall m. MonadGen m => m Int +genUChomp :: forall m. MonadGen m => m Int ``` -#### `arbitraryNibble` +#### `genChomp` ``` purescript -arbitraryNibble :: forall m. MonadGen m => m Int +genChomp :: forall m. MonadGen m => m Int ``` -#### `arbitraryUWord` +#### `genUWord` ``` purescript -arbitraryUWord :: forall m. MonadGen m => m UInt +genUWord :: forall m. MonadGen m => m UInt ``` -#### `arbitraryWord` +#### `genWord` ``` purescript -arbitraryWord :: forall m. MonadGen m => m Int +genWord :: forall m. MonadGen m => m Int ``` -#### `arbitraryFloat32` +#### `genFloat32` ``` purescript -arbitraryFloat32 :: forall m. MonadGen m => m Number +genFloat32 :: forall m. MonadGen m => m Number ``` -#### `arbitraryFloat64` +#### `genFloat64` ``` purescript -arbitraryFloat64 :: forall m. MonadGen m => m Number +genFloat64 :: forall m. MonadGen m => m Number +``` + +#### `WithOffset` + +``` purescript +data WithOffset n a + = WithOffset (Vec n Offset) (ArrayView a) +``` + +For generating some set of offsets residing inside the generated array + +##### Instances +``` purescript +(Generic (ArrayView a) a') => Generic (WithOffset n a) _ +``` + +#### `genWithOffset` + +``` purescript +genWithOffset :: forall m n b a. MonadGen m => Nat n => BytesPerValue a b => m (ArrayView a) -> m (WithOffset n a) ``` diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 7723389..5a88d10 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -1,4 +1,4 @@ --- | Functions for generating random typed arrays. +-- | Functions for generating typed arrays and values. module Data.ArrayBuffer.Typed.Gen where @@ -20,11 +20,17 @@ import Data.String.CodeUnits as S import Data.Float.Parse (parseFloat) import Data.Array as Array import Data.Vec (Vec) +import Data.Vec (fromArray) as Vec import Data.Generic.Rep (class Generic) +import Data.Typelevel.Num (class Nat, toInt') +import Data.Unfoldable (replicateA) +import Type.Proxy (Proxy (..)) import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Partial.Unsafe (unsafePartial) + + genUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray genUint8ClampedArray = sized \s -> TA.fromArray <<< Array.fromFoldable <$> replicateM s genUByte @@ -35,7 +41,7 @@ genUint32Array = sized \s -> genUint16Array :: forall m. MonadGen m => m Uint16Array genUint16Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genUNibble + TA.fromArray <<< Array.fromFoldable <$> replicateM s genUChomp genUint8Array :: forall m. MonadGen m => m Uint8Array genUint8Array = sized \s -> @@ -47,7 +53,7 @@ genInt32Array = sized \s -> genInt16Array :: forall m. MonadGen m => m Int16Array genInt16Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genNibble + TA.fromArray <<< Array.fromFoldable <$> replicateM s genChomp genInt8Array :: forall m. MonadGen m => m Int8Array genInt8Array = sized \s -> @@ -72,11 +78,11 @@ genByte = let j = I.pow 2 4 in chooseInt (negate j) (j - 1) -genUNibble :: forall m. MonadGen m => m Int -genUNibble = chooseInt 0 ((I.pow 2 16) - 1) +genUChomp :: forall m. MonadGen m => m Int +genUChomp = chooseInt 0 ((I.pow 2 16) - 1) -genNibble :: forall m. MonadGen m => m Int -genNibble = +genChomp :: forall m. MonadGen m => m Int +genChomp = let j = I.pow 2 8 in chooseInt (negate j) (j - 1) @@ -107,6 +113,22 @@ genFloat64 :: forall m. MonadGen m => m Number genFloat64 = chooseFloat (-1.7e308) 1.7e308 --- For generating some set of offsets, inside the array + +-- | For generating some set of offsets residing inside the generated array data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) derive instance genericWithOffset :: Generic (ArrayView a) a' => Generic (WithOffset n a) _ + +genWithOffset :: forall m n b a + . MonadGen m + => Nat n + => TA.BytesPerValue a b + => m (ArrayView a) + -> m (WithOffset n a) +genWithOffset genArrayView = do + let n = toInt' (Proxy :: Proxy n) + xs <- genArrayView + let l = TA.length xs + mos <- replicateA n (chooseInt 0 (l - 1)) + let os = unsafePartial $ case Vec.fromArray mos of + Just q -> q + pure (WithOffset os xs) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 36e73de..0930f50 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -5,9 +5,9 @@ import Data.ArrayBuffer.Types (ArrayView) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed (class BytesPerValue, class TypedArray) import Data.ArrayBuffer.Typed.Gen - ( arbitraryUint8ClampedArray, arbitraryUint8Array, arbitraryUint16Array, arbitraryUint32Array - , arbitraryInt8Array, arbitraryInt16Array, arbitraryInt32Array - , arbitraryFloat32Array, arbitraryFloat64Array) + ( genUint8ClampedArray, genUint8Array, genUint16Array, genUint32Array + , genInt8Array, genInt16Array, genInt32Array + , genFloat32Array, genFloat64Array) import Prelude import Data.Maybe (Maybe (..)) @@ -34,32 +34,33 @@ type TestableArrayF a t n q = => Eq t => TypedArray a t => BytesPerValue a n - -- => Arbitrary t + => Arbitrary t => Semiring t => Nat n - => ArrayView a -> q + => ArrayView a + -> q overAll :: forall q. Testable q => (forall a t n. TestableArrayF a t n q) -> Effect Unit overAll f = do log " - Uint8ClampedArray" - quickCheckGen (f <$> arbitraryUint8ClampedArray) + quickCheckGen (f <$> genUint8ClampedArray) log " - Uint32Array" - quickCheckGen (f <$> arbitraryUint32Array) + quickCheckGen (f <$> genUint32Array) log " - Uint16Array" - quickCheckGen (f <$> arbitraryUint16Array) + quickCheckGen (f <$> genUint16Array) log " - Uint8Array" - quickCheckGen (f <$> arbitraryUint8Array) + quickCheckGen (f <$> genUint8Array) log " - Int32Array" - quickCheckGen (f <$> arbitraryInt32Array) + quickCheckGen (f <$> genInt32Array) log " - Int16Array" - quickCheckGen (f <$> arbitraryInt16Array) + quickCheckGen (f <$> genInt16Array) log " - Int8Array" - quickCheckGen (f <$> arbitraryInt8Array) + quickCheckGen (f <$> genInt8Array) log " - Float32Array" - quickCheckGen (f <$> arbitraryFloat32Array) + quickCheckGen (f <$> genFloat32Array) log " - Float64Array" - quickCheckGen (f <$> arbitraryFloat64Array) + quickCheckGen (f <$> genFloat64Array) byteLengthDivBytesPerValueTests :: Effect Unit @@ -80,8 +81,8 @@ fromArrayToArrayIsoTests = overAll fromArrayToArrayIso allAreFilledTests :: Effect Unit allAreFilledTests = overAll allAreFilled where - allAreFilled :: forall a t n. TestableArrayF a t n Result -- (t -> Result) - allAreFilled xs {-x =-} = unsafePerformEffect do + allAreFilled :: forall a t n. TestableArrayF a t n Result + allAreFilled xs = unsafePerformEffect do let x = case TA.at xs 0 of Nothing -> zero Just y -> y From fa8d1b6f931124e38ba4f3a2d216f4abcfa47991 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 12 Dec 2018 08:16:34 -0700 Subject: [PATCH 079/236] removed funky show module --- src/Data/ArrayBuffer/Show.js | 7 ------- src/Data/ArrayBuffer/Show.purs | 5 ----- 2 files changed, 12 deletions(-) delete mode 100644 src/Data/ArrayBuffer/Show.js delete mode 100644 src/Data/ArrayBuffer/Show.purs diff --git a/src/Data/ArrayBuffer/Show.js b/src/Data/ArrayBuffer/Show.js deleted file mode 100644 index 4eca64a..0000000 --- a/src/Data/ArrayBuffer/Show.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -// module Show - -exports.showViaInspect = function showViaInspect (a) { - return require('util').inspect(a); -} diff --git a/src/Data/ArrayBuffer/Show.purs b/src/Data/ArrayBuffer/Show.purs deleted file mode 100644 index 8bea296..0000000 --- a/src/Data/ArrayBuffer/Show.purs +++ /dev/null @@ -1,5 +0,0 @@ -module Data.ArrayBuffer.Show where - -import Data.ArrayBuffer.Types (ArrayView) --- -foreign import showViaInspect :: forall a. ArrayView a -> String From bb4f0f33287104c2490d9a8228ded564fc6ea895 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 12 Dec 2018 08:31:13 -0700 Subject: [PATCH 080/236] set singleton is eq --- test/Properties/TypedArray.purs | 64 ++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 0930f50..0eabe09 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -7,11 +7,13 @@ import Data.ArrayBuffer.Typed (class BytesPerValue, class TypedArray) import Data.ArrayBuffer.Typed.Gen ( genUint8ClampedArray, genUint8Array, genUint16Array, genUint32Array , genInt8Array, genInt16Array, genInt32Array - , genFloat32Array, genFloat64Array) + , genFloat32Array, genFloat64Array, WithOffset (..), genWithOffset) import Prelude import Data.Maybe (Maybe (..)) -import Data.Typelevel.Num (toInt', class Nat) +import Data.Tuple (Tuple (..)) +import Data.Typelevel.Num (toInt', class Nat, D0, D1) +import Data.Vec (head) as Vec import Type.Proxy (Proxy (..)) import Test.QuickCheck (quickCheckGen, Result, (===), class Testable, class Arbitrary) import Effect (Effect) @@ -27,62 +29,65 @@ typedArrayTests = do fromArrayToArrayIsoTests log " - fill y x => all (== y) x" allAreFilledTests + log " - set x [y] o => (at x o == Just y)" + setSingletonIsEqTests -type TestableArrayF a t n q = +type TestableArrayF a b n t q = Show t => Eq t => TypedArray a t - => BytesPerValue a n + => Nat b + => BytesPerValue a b => Arbitrary t => Semiring t - => Nat n - => ArrayView a + => WithOffset n a -> q -overAll :: forall q. Testable q => (forall a t n. TestableArrayF a t n q) -> Effect Unit +overAll :: forall q n. Testable q => Nat n => (forall a b t. TestableArrayF a b n t q) -> Effect Unit overAll f = do log " - Uint8ClampedArray" - quickCheckGen (f <$> genUint8ClampedArray) + quickCheckGen (f <$> genWithOffset genUint8ClampedArray) log " - Uint32Array" - quickCheckGen (f <$> genUint32Array) + quickCheckGen (f <$> genWithOffset genUint32Array) log " - Uint16Array" - quickCheckGen (f <$> genUint16Array) + quickCheckGen (f <$> genWithOffset genUint16Array) log " - Uint8Array" - quickCheckGen (f <$> genUint8Array) + quickCheckGen (f <$> genWithOffset genUint8Array) log " - Int32Array" - quickCheckGen (f <$> genInt32Array) + quickCheckGen (f <$> genWithOffset genInt32Array) log " - Int16Array" - quickCheckGen (f <$> genInt16Array) + quickCheckGen (f <$> genWithOffset genInt16Array) log " - Int8Array" - quickCheckGen (f <$> genInt8Array) + quickCheckGen (f <$> genWithOffset genInt8Array) log " - Float32Array" - quickCheckGen (f <$> genFloat32Array) + quickCheckGen (f <$> genWithOffset genFloat32Array) log " - Float64Array" - quickCheckGen (f <$> genFloat64Array) + quickCheckGen (f <$> genWithOffset genFloat64Array) byteLengthDivBytesPerValueTests :: Effect Unit byteLengthDivBytesPerValueTests = overAll byteLengthDivBytesPerValueEqLength where - byteLengthDivBytesPerValueEqLength :: forall a t n. TestableArrayF a t n Result - byteLengthDivBytesPerValueEqLength a = - let n = toInt' (Proxy :: Proxy n) - in TA.length a === (TA.byteLength a `div` n) + byteLengthDivBytesPerValueEqLength :: forall a b t. TestableArrayF a b D0 t Result + byteLengthDivBytesPerValueEqLength (WithOffset _ a) = + let b = toInt' (Proxy :: Proxy b) + in TA.length a === (TA.byteLength a `div` b) fromArrayToArrayIsoTests :: Effect Unit fromArrayToArrayIsoTests = overAll fromArrayToArrayIso where - fromArrayToArrayIso :: forall a t n. TestableArrayF a t n Result - fromArrayToArrayIso x = TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x + fromArrayToArrayIso :: forall a b t. TestableArrayF a b D0 t Result + fromArrayToArrayIso (WithOffset _ x) = + TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x allAreFilledTests :: Effect Unit allAreFilledTests = overAll allAreFilled where - allAreFilled :: forall a t n. TestableArrayF a t n Result - allAreFilled xs = unsafePerformEffect do + allAreFilled :: forall a b t. TestableArrayF a b D0 t Result + allAreFilled (WithOffset _ xs) = unsafePerformEffect do let x = case TA.at xs 0 of Nothing -> zero Just y -> y @@ -91,4 +96,13 @@ allAreFilledTests = overAll allAreFilled pure (b === true) --- setSingletonIsEq +setSingletonIsEqTests :: Effect Unit +setSingletonIsEqTests = overAll setSingletonIsEq + where + setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result + setSingletonIsEq (WithOffset os xs) = unsafePerformEffect do + let x = case TA.at xs 0 of + Nothing -> zero + Just y -> y + TA.set xs (Just (Vec.head os)) [x] + pure (TA.at xs (Vec.head os) === Just x) From d73a98cc2b896c483b5ff0f183068d66bfcaa540 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 08:36:02 -0700 Subject: [PATCH 081/236] all implies any --- test/Properties/TypedArray.purs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 0eabe09..f706c7f 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -31,6 +31,8 @@ typedArrayTests = do allAreFilledTests log " - set x [y] o => (at x o == Just y)" setSingletonIsEqTests + log " - all p x => any p x" + allImpliesAnyTests type TestableArrayF a b n t q = @@ -106,3 +108,16 @@ setSingletonIsEqTests = overAll setSingletonIsEq Just y -> y TA.set xs (Just (Vec.head os)) [x] pure (TA.at xs (Vec.head os) === Just x) + + +-- | Should work with any arbitrary predicate, but we can't generate them +allImpliesAnyTests :: Effect Unit +allImpliesAnyTests = overAll allImpliesAny + where + allImpliesAny :: forall a b t. TestableArrayF a b D1 t Result + allImpliesAny (WithOffset _ xs) = + let pred x o = pure (x /= zero) + all' = unsafePerformEffect (TA.all pred xs) + any' = unsafePerformEffect (TA.any pred xs) + in (all' `implies` any') === true + implies x y = if x == true && y == false then false else true From e8324a1fa1d828a92c3848acbdc6e62ae9642683 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 08:41:57 -0700 Subject: [PATCH 082/236] filter, all / any --- test/Properties/TypedArray.purs | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index f706c7f..705e08f 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -33,6 +33,12 @@ typedArrayTests = do setSingletonIsEqTests log " - all p x => any p x" allImpliesAnyTests + log " - all p (filter p x)" + filterImpliesAllTests + log " - filter (not . p) (filter p x) == []" + filterIsTotalTests + log " - filter p (filter p x) == filter p x" + filterIsIdempotentTests type TestableArrayF a b n t q = @@ -121,3 +127,41 @@ allImpliesAnyTests = overAll allImpliesAny any' = unsafePerformEffect (TA.any pred xs) in (all' `implies` any') === true implies x y = if x == true && y == false then false else true + + +-- | Should work with any arbitrary predicate, but we can't generate them +filterImpliesAllTests :: Effect Unit +filterImpliesAllTests = overAll filterImpliesAll + where + filterImpliesAll :: forall a b t. TestableArrayF a b D1 t Result + filterImpliesAll (WithOffset _ xs) = + let pred x o = pure (x /= zero) + ys = unsafePerformEffect (TA.filter pred xs) + all' = unsafePerformEffect (TA.all pred ys) + in all' === true + implies x y = if x == true && y == false then false else true + + +-- | Should work with any arbitrary predicate, but we can't generate them +filterIsTotalTests :: Effect Unit +filterIsTotalTests = overAll filterIsTotal + where + filterIsTotal :: forall a b t. TestableArrayF a b D1 t Result + filterIsTotal (WithOffset _ xs) = + let pred x o = pure (x /= zero) + ys = unsafePerformEffect (TA.filter pred xs) + zs = unsafePerformEffect (TA.filter (\x o -> not <$> pred x o) ys) + in TA.toArray zs === [] + + +-- | Should work with any arbitrary predicate, but we can't generate them +filterIsIdempotentTests :: Effect Unit +filterIsIdempotentTests = overAll filterIsIdempotent + where + filterIsIdempotent :: forall a b t. TestableArrayF a b D1 t Result + filterIsIdempotent (WithOffset _ xs) = + let pred x o = pure (x /= zero) + ys = unsafePerformEffect (TA.filter pred xs) + zs = unsafePerformEffect (TA.filter pred ys) + in TA.toArray zs === TA.toArray ys + From 9f09755c4a9f5d6437ee0d2647ca682102c80e6f Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 08:49:07 -0700 Subject: [PATCH 083/236] elem with unsafeAt --- test/Properties/TypedArray.purs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 705e08f..ddbdf48 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -12,8 +12,9 @@ import Data.ArrayBuffer.Typed.Gen import Prelude import Data.Maybe (Maybe (..)) import Data.Tuple (Tuple (..)) -import Data.Typelevel.Num (toInt', class Nat, D0, D1) +import Data.Typelevel.Num (toInt', class Nat, D0, D1, D5) import Data.Vec (head) as Vec +import Data.Array as Array import Type.Proxy (Proxy (..)) import Test.QuickCheck (quickCheckGen, Result, (===), class Testable, class Arbitrary) import Effect (Effect) @@ -39,6 +40,10 @@ typedArrayTests = do filterIsTotalTests log " - filter p (filter p x) == filter p x" filterIsIdempotentTests + log " - forall os `in` xs. all (\\o -> hasIndex o xs)" + withOffsetHasIndexTests + log " - forall os `in` xs. all (\\o -> elem (at o xs) xs)" + withOffsetElemTests type TestableArrayF a b n t q = @@ -120,7 +125,7 @@ setSingletonIsEqTests = overAll setSingletonIsEq allImpliesAnyTests :: Effect Unit allImpliesAnyTests = overAll allImpliesAny where - allImpliesAny :: forall a b t. TestableArrayF a b D1 t Result + allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result allImpliesAny (WithOffset _ xs) = let pred x o = pure (x /= zero) all' = unsafePerformEffect (TA.all pred xs) @@ -133,7 +138,7 @@ allImpliesAnyTests = overAll allImpliesAny filterImpliesAllTests :: Effect Unit filterImpliesAllTests = overAll filterImpliesAll where - filterImpliesAll :: forall a b t. TestableArrayF a b D1 t Result + filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result filterImpliesAll (WithOffset _ xs) = let pred x o = pure (x /= zero) ys = unsafePerformEffect (TA.filter pred xs) @@ -146,7 +151,7 @@ filterImpliesAllTests = overAll filterImpliesAll filterIsTotalTests :: Effect Unit filterIsTotalTests = overAll filterIsTotal where - filterIsTotal :: forall a b t. TestableArrayF a b D1 t Result + filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result filterIsTotal (WithOffset _ xs) = let pred x o = pure (x /= zero) ys = unsafePerformEffect (TA.filter pred xs) @@ -158,10 +163,26 @@ filterIsTotalTests = overAll filterIsTotal filterIsIdempotentTests :: Effect Unit filterIsIdempotentTests = overAll filterIsIdempotent where - filterIsIdempotent :: forall a b t. TestableArrayF a b D1 t Result + filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result filterIsIdempotent (WithOffset _ xs) = let pred x o = pure (x /= zero) ys = unsafePerformEffect (TA.filter pred xs) zs = unsafePerformEffect (TA.filter pred ys) in TA.toArray zs === TA.toArray ys + +withOffsetHasIndexTests :: Effect Unit +withOffsetHasIndexTests = overAll withOffsetHasIndex + where + withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result + withOffsetHasIndex (WithOffset os xs) = + Array.all (\o -> TA.hasIndex xs o) os === true + + +withOffsetElemTests :: Effect Unit +withOffsetElemTests = overAll withOffsetElem + where + withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result + withOffsetElem (WithOffset os xs) = + Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt xs o)) Nothing xs) os === true + From 2d7e141d4cf6f81203e70a6656b639146b10efdc Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 12:24:28 -0700 Subject: [PATCH 084/236] any implies find --- test/Properties/TypedArray.purs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index ddbdf48..8dae446 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -44,6 +44,8 @@ typedArrayTests = do withOffsetHasIndexTests log " - forall os `in` xs. all (\\o -> elem (at o xs) xs)" withOffsetElemTests + log " - any p x => p (find p x)" + anyImpliesFindTests type TestableArrayF a b n t q = @@ -186,3 +188,19 @@ withOffsetElemTests = overAll withOffsetElem withOffsetElem (WithOffset os xs) = Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt xs o)) Nothing xs) os === true + +-- | Should work with any arbitrary predicate, but we can't generate them +anyImpliesFindTests :: Effect Unit +anyImpliesFindTests = overAll anyImpliesFind + where + anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result + anyImpliesFind (WithOffset _ xs) = + let pred x o = pure (x /= zero) + q = unsafePerformEffect (TA.any pred xs) + is = unsafePerformEffect do + mzs <- TA.find xs pred + case mzs of + Nothing -> pure Nothing + Just z -> Just <$> pred z 0 + in q `implies` (Just true == is) === true + implies x y = if x == true && y == false then false else true From 097e8ad10c12e91f5c990b8e9791651d13200bbf Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 12:29:04 -0700 Subject: [PATCH 085/236] findIndex implies at --- test/Properties/TypedArray.purs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 8dae446..fac74b9 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -46,6 +46,8 @@ typedArrayTests = do withOffsetElemTests log " - any p x => p (find p x)" anyImpliesFindTests + log " - p (at x (findIndex p x))" + findIndexImpliesAtTests type TestableArrayF a b n t q = @@ -204,3 +206,19 @@ anyImpliesFindTests = overAll anyImpliesFind Just z -> Just <$> pred z 0 in q `implies` (Just true == is) === true implies x y = if x == true && y == false then false else true + + +-- | Should work with any arbitrary predicate, but we can't generate them +findIndexImpliesAtTests :: Effect Unit +findIndexImpliesAtTests = overAll findIndexImpliesAt + where + findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result + findIndexImpliesAt (WithOffset _ xs) = + let pred x o = pure (x /= zero) + mo = unsafePerformEffect (TA.findIndex xs pred) + v = case mo of + Nothing -> true + Just o -> case TA.at xs o of + Nothing -> false + Just x -> unsafePerformEffect (pred x o) + in v === true From ab5d5783ca5d53706058b9b6306f7fa223ec7911 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 12:37:55 -0700 Subject: [PATCH 086/236] indexOf implies at --- test/Properties/TypedArray.purs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index fac74b9..87a0946 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -15,8 +15,9 @@ import Data.Tuple (Tuple (..)) import Data.Typelevel.Num (toInt', class Nat, D0, D1, D5) import Data.Vec (head) as Vec import Data.Array as Array +import Data.HeytingAlgebra (implies) import Type.Proxy (Proxy (..)) -import Test.QuickCheck (quickCheckGen, Result, (===), class Testable, class Arbitrary) +import Test.QuickCheck (quickCheckGen, Result (..), (===), class Testable, class Arbitrary) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) @@ -48,6 +49,8 @@ typedArrayTests = do anyImpliesFindTests log " - p (at x (findIndex p x))" findIndexImpliesAtTests + log " - at x (indexOf y x) == y" + indexOfImpliesAtTests type TestableArrayF a b n t q = @@ -135,7 +138,6 @@ allImpliesAnyTests = overAll allImpliesAny all' = unsafePerformEffect (TA.all pred xs) any' = unsafePerformEffect (TA.any pred xs) in (all' `implies` any') === true - implies x y = if x == true && y == false then false else true -- | Should work with any arbitrary predicate, but we can't generate them @@ -148,7 +150,6 @@ filterImpliesAllTests = overAll filterImpliesAll ys = unsafePerformEffect (TA.filter pred xs) all' = unsafePerformEffect (TA.all pred ys) in all' === true - implies x y = if x == true && y == false then false else true -- | Should work with any arbitrary predicate, but we can't generate them @@ -205,7 +206,6 @@ anyImpliesFindTests = overAll anyImpliesFind Nothing -> pure Nothing Just z -> Just <$> pred z 0 in q `implies` (Just true == is) === true - implies x y = if x == true && y == false then false else true -- | Should work with any arbitrary predicate, but we can't generate them @@ -216,9 +216,21 @@ findIndexImpliesAtTests = overAll findIndexImpliesAt findIndexImpliesAt (WithOffset _ xs) = let pred x o = pure (x /= zero) mo = unsafePerformEffect (TA.findIndex xs pred) - v = case mo of - Nothing -> true - Just o -> case TA.at xs o of - Nothing -> false - Just x -> unsafePerformEffect (pred x o) - in v === true + in case mo of + Nothing -> Success + Just o -> case TA.at xs o of + Nothing -> Failed "No value at found index" + Just x -> unsafePerformEffect (pred x o) === true + + + +indexOfImpliesAtTests :: Effect Unit +indexOfImpliesAtTests = overAll indexOfImpliesAt + where + indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result + indexOfImpliesAt (WithOffset _ xs) = + case TA.at xs 0 of + Nothing -> Success + Just y -> case TA.indexOf xs y Nothing of + Nothing -> Failed "no index of" + Just o -> TA.at xs o === Just y From 276495999aefbf3fc47470b9c6d63981cac322fb Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 12:38:49 -0700 Subject: [PATCH 087/236] lastIndexOf imples at --- test/Properties/TypedArray.purs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 87a0946..43a93bc 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -51,6 +51,8 @@ typedArrayTests = do findIndexImpliesAtTests log " - at x (indexOf y x) == y" indexOfImpliesAtTests + log " - at x (lastIndexOf y x) == y" + lastIndexOfImpliesAtTests type TestableArrayF a b n t q = @@ -234,3 +236,15 @@ indexOfImpliesAtTests = overAll indexOfImpliesAt Just y -> case TA.indexOf xs y Nothing of Nothing -> Failed "no index of" Just o -> TA.at xs o === Just y + + +lastIndexOfImpliesAtTests :: Effect Unit +lastIndexOfImpliesAtTests = overAll lastIndexOfImpliesAt + where + lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result + lastIndexOfImpliesAt (WithOffset _ xs) = + case TA.at xs 0 of + Nothing -> Success + Just y -> case TA.lastIndexOf xs y Nothing of + Nothing -> Failed "no lastIndex of" + Just o -> TA.at xs o === Just y From 24ff49968d1170edf2021c0f70a1b74f6317a351 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 13:10:17 -0700 Subject: [PATCH 088/236] quickcheck combinators --- bower.json | 3 ++- test/Properties/TypedArray.purs | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bower.json b/bower.json index e604d3f..abc0bf8 100644 --- a/bower.json +++ b/bower.json @@ -26,6 +26,7 @@ "purescript-debug": "^4.0.0", "purescript-quickcheck": "^5.0.0", "purescript-partial": "^2.0.0", - "purescript-unicode": "^4.0.1" + "purescript-unicode": "^4.0.1", + "purescript-quickcheck-combinators": "^0.1.0" } } diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 43a93bc..88e6fed 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -17,7 +17,8 @@ import Data.Vec (head) as Vec import Data.Array as Array import Data.HeytingAlgebra (implies) import Type.Proxy (Proxy (..)) -import Test.QuickCheck (quickCheckGen, Result (..), (===), class Testable, class Arbitrary) +import Test.QuickCheck (quickCheckGen, Result (..), (===), class Testable, class Arbitrary, ()) +import Test.QuickCheck.Combinators ((&=&), (|=|), (==>)) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) @@ -55,6 +56,12 @@ typedArrayTests = do lastIndexOfImpliesAtTests +-- TODO: folding, traversals, mapping +-- copyWithin, reverse, sort, setTyped, slice, subArray +-- toString ~ join "," + + + type TestableArrayF a b n t q = Show t => Eq t @@ -139,7 +146,7 @@ allImpliesAnyTests = overAll allImpliesAny let pred x o = pure (x /= zero) all' = unsafePerformEffect (TA.all pred xs) any' = unsafePerformEffect (TA.any pred xs) - in (all' `implies` any') === true + in all' `implies` any' "All doesn't imply any" -- | Should work with any arbitrary predicate, but we can't generate them @@ -151,7 +158,7 @@ filterImpliesAllTests = overAll filterImpliesAll let pred x o = pure (x /= zero) ys = unsafePerformEffect (TA.filter pred xs) all' = unsafePerformEffect (TA.all pred ys) - in all' === true + in all' "Filter doesn't imply all" -- | Should work with any arbitrary predicate, but we can't generate them @@ -183,7 +190,7 @@ withOffsetHasIndexTests = overAll withOffsetHasIndex where withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result withOffsetHasIndex (WithOffset os xs) = - Array.all (\o -> TA.hasIndex xs o) os === true + Array.all (\o -> TA.hasIndex xs o) os "All doesn't have index of itself" withOffsetElemTests :: Effect Unit @@ -191,7 +198,8 @@ withOffsetElemTests = overAll withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = - Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt xs o)) Nothing xs) os === true + Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt xs o)) Nothing xs) os + "All doesn't have an elem of itself" -- | Should work with any arbitrary predicate, but we can't generate them @@ -207,7 +215,7 @@ anyImpliesFindTests = overAll anyImpliesFind case mzs of Nothing -> pure Nothing Just z -> Just <$> pred z 0 - in q `implies` (Just true == is) === true + in q `implies` (Just true == is) "Any imples find" -- | Should work with any arbitrary predicate, but we can't generate them @@ -222,7 +230,7 @@ findIndexImpliesAtTests = overAll findIndexImpliesAt Nothing -> Success Just o -> case TA.at xs o of Nothing -> Failed "No value at found index" - Just x -> unsafePerformEffect (pred x o) === true + Just x -> unsafePerformEffect (pred x o) "Find index implies at" From 7fb380cda4e4b7b22594cb2c1717a76d558ceaa1 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 14:13:06 -0700 Subject: [PATCH 089/236] cosmetic --- test/Properties/TypedArray.purs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 88e6fed..0b46c47 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -56,11 +56,6 @@ typedArrayTests = do lastIndexOfImpliesAtTests --- TODO: folding, traversals, mapping --- copyWithin, reverse, sort, setTyped, slice, subArray --- toString ~ join "," - - type TestableArrayF a b n t q = Show t @@ -256,3 +251,10 @@ lastIndexOfImpliesAtTests = overAll lastIndexOfImpliesAt Just y -> case TA.lastIndexOf xs y Nothing of Nothing -> Failed "no lastIndex of" Just o -> TA.at xs o === Just y + + +-- - traversal_: +-- push to the end of a new typed array, see if they're iso. Likewise, for folds? +-- TODO: folding, traversals, mapping +-- copyWithin, reverse, sort, setTyped, slice, subArray +-- toString ~ join "," From 94158776d940828f0743004741ac14eae2ac6288 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 14:34:13 -0700 Subject: [PATCH 090/236] sort is idempotent, reverse tests --- test/Properties/TypedArray.purs | 96 +++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 0b46c47..7b60761 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -22,6 +22,7 @@ import Test.QuickCheck.Combinators ((&=&), (|=|), (==>)) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) +import Effect.Ref as Ref typedArrayTests :: Effect Unit @@ -54,6 +55,20 @@ typedArrayTests = do indexOfImpliesAtTests log " - at x (lastIndexOf y x) == y" lastIndexOfImpliesAtTests + log " - foldr cons [] x == toArray x" + foldrConsIsToArrayTests + log " - foldl snoc [] x == toArray x" + foldlSnocIsToArrayTests + log " - map identity x == x" + mapIdentityIsIdentityTests + log " - traverse snoc x == toArray x" + traverseSnocIsToArrayTests + log " - reverse (reverse x) == x" + doubleReverseIsIdentityTests + log " - toArray (reverse x) == Array.reverse (toArray x)" + reverseIsArrayReverseTests + log " - sort (sort x) == sort x" + sortIsIdempotentTests @@ -253,8 +268,81 @@ lastIndexOfImpliesAtTests = overAll lastIndexOfImpliesAt Just o -> TA.at xs o === Just y --- - traversal_: --- push to the end of a new typed array, see if they're iso. Likewise, for folds? --- TODO: folding, traversals, mapping --- copyWithin, reverse, sort, setTyped, slice, subArray +foldrConsIsToArrayTests :: Effect Unit +foldrConsIsToArrayTests = overAll foldrConsIsToArray + where + foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result + foldrConsIsToArray (WithOffset _ xs) = + TA.foldr xs (\x acc _ -> Array.cons x acc) [] === TA.toArray xs + + +foldlSnocIsToArrayTests :: Effect Unit +foldlSnocIsToArrayTests = overAll foldlSnocIsToArray + where + foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result + foldlSnocIsToArray (WithOffset _ xs) = + TA.foldl xs (\acc x _ -> Array.snoc acc x) [] === TA.toArray xs + + +mapIdentityIsIdentityTests :: Effect Unit +mapIdentityIsIdentityTests = overAll mapIdentityIsIdentity + where + mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + mapIdentityIsIdentity (WithOffset _ xs) = + TA.toArray (TA.map (\x _ -> x) xs) === TA.toArray xs + + +traverseSnocIsToArrayTests :: Effect Unit +traverseSnocIsToArrayTests = overAll traverseSnocIsToArray + where + traverseSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result + traverseSnocIsToArray (WithOffset _ xs) = + let ys = unsafePerformEffect do + ref <- Ref.new [] + TA.traverse_ (\x _ -> void (Ref.modify (\xs -> Array.snoc xs x) ref)) xs + Ref.read ref + in TA.toArray xs === ys + + +doubleReverseIsIdentityTests :: Effect Unit +doubleReverseIsIdentityTests = overAll doubleReverseIsIdentity + where + doubleReverseIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + doubleReverseIsIdentity (WithOffset _ xs) = + let ys = TA.toArray xs + _ = unsafePerformEffect do + TA.reverse xs + TA.reverse xs + in TA.toArray xs === ys + + +reverseIsArrayReverseTests :: Effect Unit +reverseIsArrayReverseTests = overAll reverseIsArrayReverse + where + reverseIsArrayReverse :: forall a b t. TestableArrayF a b D0 t Result + reverseIsArrayReverse (WithOffset _ xs) = + let ys = Array.reverse (TA.toArray xs) + _ = unsafePerformEffect do + TA.reverse xs + in TA.toArray xs === ys + + +sortIsIdempotentTests :: Effect Unit +sortIsIdempotentTests = overAll sortIsIdempotent + where + sortIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result + sortIsIdempotent (WithOffset _ xs) = + let ys = unsafePerformEffect do + TA.sort xs + pure (TA.toArray xs) + zs = unsafePerformEffect do + TA.sort xs + pure (TA.toArray xs) + in zs === ys + + + + + +-- TODO: copyWithin, sort, setTyped, slice, subArray -- toString ~ join "," From 22326b1b30b46d3966a81bc02e725e29fdd3024f Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 14:57:52 -0700 Subject: [PATCH 091/236] sort is correct --- test/Properties/TypedArray.purs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 7b60761..7125f62 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -69,17 +69,20 @@ typedArrayTests = do reverseIsArrayReverseTests log " - sort (sort x) == sort x" sortIsIdempotentTests + log " - toArray (sort x) == Array.sort (toArray x)" + sortIsArraySortTests type TestableArrayF a b n t q = Show t => Eq t + => Ord t + => Semiring t + => Arbitrary t => TypedArray a t - => Nat b => BytesPerValue a b - => Arbitrary t - => Semiring t + => Nat b => WithOffset n a -> q @@ -341,8 +344,19 @@ sortIsIdempotentTests = overAll sortIsIdempotent in zs === ys +sortIsArraySortTests :: Effect Unit +sortIsArraySortTests = overAll sortIsArraySort + where + sortIsArraySort :: forall a b t. TestableArrayF a b D0 t Result + sortIsArraySort (WithOffset _ xs) = + let ys = Array.sort (TA.toArray xs) + _ = unsafePerformEffect do + TA.sort xs + in TA.toArray xs === ys + + --- TODO: copyWithin, sort, setTyped, slice, subArray +-- TODO: copyWithin, setTyped, slice, subArray -- toString ~ join "," From 1031fd284da150d9830f10f3ea3dc51dbe54762a Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 15:01:40 -0700 Subject: [PATCH 092/236] toString --- test/Properties/TypedArray.purs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 7125f62..2b71da2 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -71,6 +71,8 @@ typedArrayTests = do sortIsIdempotentTests log " - toArray (sort x) == Array.sort (toArray x)" sortIsArraySortTests + log " - toString' \",\" x == toString x" + toStringIsJoinWithCommaTests @@ -355,6 +357,14 @@ sortIsArraySortTests = overAll sortIsArraySort in TA.toArray xs === ys +toStringIsJoinWithCommaTests :: Effect Unit +toStringIsJoinWithCommaTests = overAll toStringIsJoinWithComma + where + toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result + toStringIsJoinWithComma (WithOffset _ xs) = + TA.toString' xs "," === TA.toString xs + + From 7482f4d546cf5c670a828eb5cd654b1a622c96ec Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 15:39:23 -0700 Subject: [PATCH 093/236] dual subArray and slice references --- test/Properties/TypedArray.purs | 89 +++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 2b71da2..efa483f 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -17,7 +17,7 @@ import Data.Vec (head) as Vec import Data.Array as Array import Data.HeytingAlgebra (implies) import Type.Proxy (Proxy (..)) -import Test.QuickCheck (quickCheckGen, Result (..), (===), class Testable, class Arbitrary, ()) +import Test.QuickCheck (quickCheckGen, Result (..), (===), (/==), class Testable, class Arbitrary, ()) import Test.QuickCheck.Combinators ((&=&), (|=|), (==>)) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) @@ -73,6 +73,14 @@ typedArrayTests = do sortIsArraySortTests log " - toString' \",\" x == toString x" toStringIsJoinWithCommaTests + log " - setTyped x (subArray x) == x" + setTypedOfSubArrayIsIdentityTests + log " - let z' = subArray o z; q = toArray z'; mutate z; pure q /= toArray z'" + modifyingOriginalMutatesSubArrayTests + log " - let z' = slice o z; q = toArray z'; mutate z; pure q == toArray z'" + modifyingOriginalDoesntMutateSliceTests + -- log " - take (o + 1) (copyWithin o x) == subArray o x" + -- copyWithinIsSubArrayTests @@ -137,7 +145,7 @@ allAreFilledTests = overAll allAreFilled Just y -> y TA.fill xs x Nothing b <- TA.all (\y o -> pure (y == x)) xs - pure (b === true) + pure (b "All aren't the filled value") setSingletonIsEqTests :: Effect Unit @@ -159,9 +167,9 @@ allImpliesAnyTests = overAll allImpliesAny allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result allImpliesAny (WithOffset _ xs) = let pred x o = pure (x /= zero) - all' = unsafePerformEffect (TA.all pred xs) - any' = unsafePerformEffect (TA.any pred xs) - in all' `implies` any' "All doesn't imply any" + all' = unsafePerformEffect (TA.all pred xs) "All don't satisfy the predicate" + any' = unsafePerformEffect (TA.any pred xs) "None satisfy the predicate" + in all' ==> any' -- | Should work with any arbitrary predicate, but we can't generate them @@ -224,13 +232,18 @@ anyImpliesFindTests = overAll anyImpliesFind anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result anyImpliesFind (WithOffset _ xs) = let pred x o = pure (x /= zero) - q = unsafePerformEffect (TA.any pred xs) - is = unsafePerformEffect do + p = unsafePerformEffect (TA.any pred xs) "All don't satisfy the predicate" + q = unsafePerformEffect do mzs <- TA.find xs pred case mzs of - Nothing -> pure Nothing - Just z -> Just <$> pred z 0 - in q `implies` (Just true == is) "Any imples find" + Nothing -> pure (Failed "Doesn't have a value satisfying the predicate") + Just z -> do + b <- pred z 0 + pure $ + if b + then Success + else Failed "Found value doesn't satisfy the predicate" + in p ==> q -- | Should work with any arbitrary predicate, but we can't generate them @@ -365,8 +378,62 @@ toStringIsJoinWithCommaTests = overAll toStringIsJoinWithComma TA.toString' xs "," === TA.toString xs +setTypedOfSubArrayIsIdentityTests :: Effect Unit +setTypedOfSubArrayIsIdentityTests = overAll setTypedOfSubArrayIsIdentity + where + setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + setTypedOfSubArrayIsIdentity (WithOffset _ xs) = + let ys = TA.toArray xs + zsSub = TA.subArray xs 0 Nothing + zs = unsafePerformEffect do + TA.setTyped xs Nothing zsSub + pure (TA.toArray xs) + in zs === ys + + +modifyingOriginalMutatesSubArrayTests :: Effect Unit +modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray + where + modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArray (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.subArray xs 0 Nothing + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs /== ys + + +modifyingOriginalDoesntMutateSliceTests :: Effect Unit +modifyingOriginalDoesntMutateSliceTests = overAll modifyingOriginalDoesntMutateSlice + where + modifyingOriginalDoesntMutateSlice :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalDoesntMutateSlice (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.slice xs Nothing + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs === ys + + +-- copyWithinIsSubArrayTests :: Effect Unit +-- copyWithinIsSubArrayTests = overAll copyWithinIsSubArray +-- where +-- copyWithinIsSubArray :: forall a b t. TestableArrayF a b D1 t Result +-- copyWithinIsSubArray (WithOffset os xs) = +-- let o = Vec.head os +-- ys = TA.subArray xs o Nothing +-- zs = unsafePerformEffect do +-- TA.copyWithin xs 0 o Nothing +-- pure $ Array.take ((o + 1)) $ TA.toArray xs +-- in zs === TA.toArray ys + -- TODO: copyWithin, setTyped, slice, subArray --- toString ~ join "," From 44cc05d4a463e23bce36db730e6e0a1708941013 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 15:52:52 -0700 Subject: [PATCH 094/236] really weird test results - subArray isn't mutable when supplied with a nonzero argument? --- test/Properties/TypedArray.purs | 68 +++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index efa483f..7cf9c21 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -75,12 +75,16 @@ typedArrayTests = do toStringIsJoinWithCommaTests log " - setTyped x (subArray x) == x" setTypedOfSubArrayIsIdentityTests - log " - let z' = subArray o z; q = toArray z'; mutate z; pure q /= toArray z'" + log " - let z' = subArray z; q = toArray z'; mutate z; pure q /= toArray z'" modifyingOriginalMutatesSubArrayTests - log " - let z' = slice o z; q = toArray z'; mutate z; pure q == toArray z'" + -- log " - let z' = subArray o z; q = toArray z'; mutate z; pure q /= toArray z'" + -- modifyingOriginalMutatesSubArrayPartTests + log " - let z' = slice z; q = toArray z'; mutate z; pure q == toArray z'" modifyingOriginalDoesntMutateSliceTests - -- log " - take (o + 1) (copyWithin o x) == subArray o x" - -- copyWithinIsSubArrayTests + log " - let z' = slice o z; q = toArray z'; mutate z; pure q == toArray z'" + modifyingOriginalDoesntMutateSlicePartTests + -- log " - take (o + 1) (copyWithin o x) == slice o x" + -- copyWithinIsSliceTests @@ -406,6 +410,23 @@ modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray in zs /== ys +modifyingOriginalMutatesSubArrayPartTests :: Effect Unit +modifyingOriginalMutatesSubArrayPartTests = overAll modifyingOriginalMutatesSubArrayPart + where + modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalMutatesSubArrayPart (WithOffset os xs) + | Array.all (eq zero) (TA.toArray (TA.subArray xs (Vec.head os) Nothing)) = Success + | TA.at xs (Vec.head os) == Just zero = Success + | otherwise = + let o = Vec.head os + zsSub = TA.subArray xs o Nothing + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs /== ys + + modifyingOriginalDoesntMutateSliceTests :: Effect Unit modifyingOriginalDoesntMutateSliceTests = overAll modifyingOriginalDoesntMutateSlice where @@ -421,17 +442,34 @@ modifyingOriginalDoesntMutateSliceTests = overAll modifyingOriginalDoesntMutateS in zs === ys --- copyWithinIsSubArrayTests :: Effect Unit --- copyWithinIsSubArrayTests = overAll copyWithinIsSubArray --- where --- copyWithinIsSubArray :: forall a b t. TestableArrayF a b D1 t Result --- copyWithinIsSubArray (WithOffset os xs) = --- let o = Vec.head os --- ys = TA.subArray xs o Nothing --- zs = unsafePerformEffect do --- TA.copyWithin xs 0 o Nothing --- pure $ Array.take ((o + 1)) $ TA.toArray xs --- in zs === TA.toArray ys +modifyingOriginalDoesntMutateSlicePartTests :: Effect Unit +modifyingOriginalDoesntMutateSlicePartTests = overAll modifyingOriginalDoesntMutateSlicePart + where + modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) + | Array.all (eq zero) (TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing)))) = Success + | TA.at xs (Vec.head os) == Just zero = Success + | otherwise = + let o = Vec.head os + zsSub = TA.slice xs (Just (Tuple o Nothing)) + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs === ys + + +copyWithinIsSliceTests :: Effect Unit +copyWithinIsSliceTests = overAll copyWithinIsSlice + where + copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result + copyWithinIsSlice (WithOffset os xs) = + let o = Vec.head os + ys = TA.toArray (TA.slice xs (Just (Tuple o Nothing))) + zs = unsafePerformEffect do + TA.copyWithin xs 0 o Nothing + pure $ Array.take (Array.length ys - o) $ TA.toArray xs + in zs === ys From d585392626db8eaa14f36e625603b56d69016306 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 16:13:50 -0700 Subject: [PATCH 095/236] peculiar behavior of subArray --- src/Data/ArrayBuffer/Typed.js | 10 ++++-- src/Data/ArrayBuffer/Typed.purs | 16 ++++++++-- test/Properties/TypedArray.purs | 56 +++++++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 169bd6a..eccb089 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -264,11 +264,15 @@ exports.sortImpl = function sortImpl (a) { }; -exports.subArrayImpl = function subArrayImpl (a,s,me) { +exports.subArrayImpl = function subArrayImpl (a,ms,me) { if (me === null) { - return a.subarray(s); + if (ms === null) { + return a.subarray(); + } else { + return a.subarray(ms); + } } else { - return a.subarray(s,me); + return a.subarray(ms,me); } }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index d2379d4..9316e31 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -496,11 +496,21 @@ sort :: forall a. ArrayView a -> Effect Unit sort = runEffectFn1 sortImpl -foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset (Nullable Offset) (ArrayView a) +foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a -subArray a o mo = runFn3 subArrayImpl a o (toNullable mo) +-- | +-- | **Note**: there is really peculiar behavior with `subArray` - if the first offset argument is omitted, or +-- | is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a +-- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. +-- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves +-- | purely. +subArray :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +subArray a mz = case mz of + Nothing -> runFn3 subArrayImpl a (toNullable Nothing) (toNullable Nothing) + Just (Tuple s me) -> case me of + Nothing -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable Nothing) + Just e -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable (Just e)) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 7cf9c21..4102ec6 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -77,8 +77,12 @@ typedArrayTests = do setTypedOfSubArrayIsIdentityTests log " - let z' = subArray z; q = toArray z'; mutate z; pure q /= toArray z'" modifyingOriginalMutatesSubArrayTests - -- log " - let z' = subArray o z; q = toArray z'; mutate z; pure q /= toArray z'" - -- modifyingOriginalMutatesSubArrayPartTests + log " - let z' = subArray 0 z; q = toArray z'; mutate z; pure q /= toArray z'" + modifyingOriginalMutatesSubArrayZeroTests + log " - let z' = subArray 0 (length z) z; q = toArray z'; mutate z; pure q /= toArray z'" + modifyingOriginalMutatesSubArrayAllTests + log " - let z' = subArray o z; q = toArray z'; mutate z; pure q == toArray z'" + modifyingOriginalDoesntMutateSubArrayPartTests log " - let z' = slice z; q = toArray z'; mutate z; pure q == toArray z'" modifyingOriginalDoesntMutateSliceTests log " - let z' = slice o z; q = toArray z'; mutate z; pure q == toArray z'" @@ -388,13 +392,16 @@ setTypedOfSubArrayIsIdentityTests = overAll setTypedOfSubArrayIsIdentity setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result setTypedOfSubArrayIsIdentity (WithOffset _ xs) = let ys = TA.toArray xs - zsSub = TA.subArray xs 0 Nothing + zsSub = TA.subArray xs Nothing zs = unsafePerformEffect do TA.setTyped xs Nothing zsSub pure (TA.toArray xs) in zs === ys +-- setTyped of subArray is copyWithin + + modifyingOriginalMutatesSubArrayTests :: Effect Unit modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray where @@ -402,7 +409,22 @@ modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray modifyingOriginalMutatesSubArray (WithOffset _ xs) | Array.all (eq zero) (TA.toArray xs) = Success | otherwise = - let zsSub = TA.subArray xs 0 Nothing + let zsSub = TA.subArray xs Nothing + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs /== ys + + +modifyingOriginalMutatesSubArrayZeroTests :: Effect Unit +modifyingOriginalMutatesSubArrayZeroTests = overAll modifyingOriginalMutatesSubArrayZero + where + modifyingOriginalMutatesSubArrayZero :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArrayZero (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) zs = TA.toArray zsSub ys = unsafePerformEffect do TA.fill xs zero Nothing @@ -410,21 +432,37 @@ modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray in zs /== ys -modifyingOriginalMutatesSubArrayPartTests :: Effect Unit -modifyingOriginalMutatesSubArrayPartTests = overAll modifyingOriginalMutatesSubArrayPart +modifyingOriginalMutatesSubArrayAllTests :: Effect Unit +modifyingOriginalMutatesSubArrayAllTests = overAll modifyingOriginalMutatesSubArrayAll + where + modifyingOriginalMutatesSubArrayAll :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArrayAll (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs /== ys + + +modifyingOriginalDoesntMutateSubArrayPartTests :: Effect Unit +modifyingOriginalDoesntMutateSubArrayPartTests = overAll modifyingOriginalMutatesSubArrayPart where modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalMutatesSubArrayPart (WithOffset os xs) - | Array.all (eq zero) (TA.toArray (TA.subArray xs (Vec.head os) Nothing)) = Success + | Vec.head os == 0 = Success + | Array.all (eq zero) (TA.toArray (TA.subArray xs Nothing)) = Success | TA.at xs (Vec.head os) == Just zero = Success | otherwise = let o = Vec.head os - zsSub = TA.subArray xs o Nothing + zsSub = TA.subArray xs (Just (Tuple o Nothing)) zs = TA.toArray zsSub ys = unsafePerformEffect do TA.fill xs zero Nothing pure (TA.toArray zsSub) - in zs /== ys + in zs === ys modifyingOriginalDoesntMutateSliceTests :: Effect Unit From 2233a4dd68456a68df636f1349b37e83354785f3 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 16:22:15 -0700 Subject: [PATCH 096/236] verifying the mutability of whole subArrays --- test/Properties/TypedArray.purs | 65 +++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 4102ec6..d7418f1 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -75,17 +75,23 @@ typedArrayTests = do toStringIsJoinWithCommaTests log " - setTyped x (subArray x) == x" setTypedOfSubArrayIsIdentityTests - log " - let z' = subArray z; q = toArray z'; mutate z; pure q /= toArray z'" + log " - let z' = subArray x; q = toArray z'; mutate x; pure q /= toArray z'" modifyingOriginalMutatesSubArrayTests - log " - let z' = subArray 0 z; q = toArray z'; mutate z; pure q /= toArray z'" + log " - let z' = subArray x; q = toArray x; mutate z'; pure q /= toArray x" + modifyingSubArrayMutatesOriginalTests + log " - let z' = subArray 0 x; q = toArray z'; mutate x; pure q /= toArray z'" modifyingOriginalMutatesSubArrayZeroTests - log " - let z' = subArray 0 (length z) z; q = toArray z'; mutate z; pure q /= toArray z'" + log " - let z' = subArray 0 x; q = toArray x; mutate z'; pure q /= toArray x" + modifyingSubArrayMutatesOriginalZeroTests + log " - let z' = subArray 0 (length x) x; q = toArray z'; mutate x; pure q /= toArray z'" modifyingOriginalMutatesSubArrayAllTests - log " - let z' = subArray o z; q = toArray z'; mutate z; pure q == toArray z'" + log " - let z' = subArray 0 (length x) x; q = toArray x; mutate z'; pure q /= toArray x" + modifyingSubArrayMutatesOriginalAllTests + log " - let z' = subArray o x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSubArrayPartTests - log " - let z' = slice z; q = toArray z'; mutate z; pure q == toArray z'" + log " - let z' = slice x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSliceTests - log " - let z' = slice o z; q = toArray z'; mutate z; pure q == toArray z'" + log " - let z' = slice o x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSlicePartTests -- log " - take (o + 1) (copyWithin o x) == slice o x" -- copyWithinIsSliceTests @@ -325,7 +331,7 @@ traverseSnocIsToArrayTests = overAll traverseSnocIsToArray traverseSnocIsToArray (WithOffset _ xs) = let ys = unsafePerformEffect do ref <- Ref.new [] - TA.traverse_ (\x _ -> void (Ref.modify (\xs -> Array.snoc xs x) ref)) xs + TA.traverse_ (\x _ -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs Ref.read ref in TA.toArray xs === ys @@ -417,6 +423,21 @@ modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray in zs /== ys +modifyingSubArrayMutatesOriginalTests :: Effect Unit +modifyingSubArrayMutatesOriginalTests = overAll modifyingOriginalMutatesSubArray + where + modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArray (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.subArray xs Nothing + zs = TA.toArray xs + ys = unsafePerformEffect do + TA.fill zsSub zero Nothing + pure (TA.toArray xs) + in zs /== ys + + modifyingOriginalMutatesSubArrayZeroTests :: Effect Unit modifyingOriginalMutatesSubArrayZeroTests = overAll modifyingOriginalMutatesSubArrayZero where @@ -432,6 +453,21 @@ modifyingOriginalMutatesSubArrayZeroTests = overAll modifyingOriginalMutatesSubA in zs /== ys +modifyingSubArrayMutatesOriginalZeroTests :: Effect Unit +modifyingSubArrayMutatesOriginalZeroTests = overAll modifyingSubArrayMutatesOriginalZero + where + modifyingSubArrayMutatesOriginalZero :: forall a b t. TestableArrayF a b D0 t Result + modifyingSubArrayMutatesOriginalZero (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) + zs = TA.toArray xs + ys = unsafePerformEffect do + TA.fill zsSub zero Nothing + pure (TA.toArray xs) + in zs /== ys + + modifyingOriginalMutatesSubArrayAllTests :: Effect Unit modifyingOriginalMutatesSubArrayAllTests = overAll modifyingOriginalMutatesSubArrayAll where @@ -447,6 +483,21 @@ modifyingOriginalMutatesSubArrayAllTests = overAll modifyingOriginalMutatesSubAr in zs /== ys +modifyingSubArrayMutatesOriginalAllTests :: Effect Unit +modifyingSubArrayMutatesOriginalAllTests = overAll modifyingSubArrayMutatesOriginalAll + where + modifyingSubArrayMutatesOriginalAll :: forall a b t. TestableArrayF a b D0 t Result + modifyingSubArrayMutatesOriginalAll (WithOffset _ xs) + | Array.all (eq zero) (TA.toArray xs) = Success + | otherwise = + let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) + zs = TA.toArray xs + ys = unsafePerformEffect do + TA.fill zsSub zero Nothing + pure (TA.toArray xs) + in zs /== ys + + modifyingOriginalDoesntMutateSubArrayPartTests :: Effect Unit modifyingOriginalDoesntMutateSubArrayPartTests = overAll modifyingOriginalMutatesSubArrayPart where From 19194712fbc2a8bdc339dbe25609f158ebbabfa5 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 16:31:53 -0700 Subject: [PATCH 097/236] better docs --- src/Data/ArrayBuffer/Typed.purs | 18 ++++++++++++++++++ test/Properties/TypedArray.purs | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 9316e31..f61574d 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -505,6 +505,24 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu -- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. -- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves -- | purely. +-- | +-- | **tl;dr**: if you want a duplicate reference of the same typed array, consider either of the following: +-- | +-- | ``` +-- | y :: ArrayView _ +-- | y = x +-- | +-- | y' :: ArrayView _ +-- | y' = subArray x Nothing +-- | +-- | y'' :: ArrayView _ +-- | y'' = subArray x (Just (Tuple 0 Nothing)) +-- | +-- | y''' :: ArrayView _ +-- | y''' = subArray x (Just (Tuple 0 (Just (length x)))) +-- | ``` +-- | +-- | Otherwise, you'll get an _image_ of the array at the moment, like `slice`. subArray :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a subArray a mz = case mz of Nothing -> runFn3 subArrayImpl a (toNullable Nothing) (toNullable Nothing) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index d7418f1..a4943fe 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -89,6 +89,8 @@ typedArrayTests = do modifyingSubArrayMutatesOriginalAllTests log " - let z' = subArray o x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSubArrayPartTests + log " - let z' = subArray 0 o x; q = toArray z'; mutate x; pure q == toArray z'" + modifyingOriginalDoesntMutateSubArrayPart2Tests log " - let z' = slice x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSliceTests log " - let z' = slice o x; q = toArray z'; mutate x; pure q == toArray z'" @@ -516,6 +518,24 @@ modifyingOriginalDoesntMutateSubArrayPartTests = overAll modifyingOriginalMutate in zs === ys +modifyingOriginalDoesntMutateSubArrayPart2Tests :: Effect Unit +modifyingOriginalDoesntMutateSubArrayPart2Tests = overAll modifyingOriginalMutatesSubArrayPart2 + where + modifyingOriginalMutatesSubArrayPart2 :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalMutatesSubArrayPart2 (WithOffset os xs) + | Vec.head os == 0 = Success + | Array.all (eq zero) (TA.toArray (TA.subArray xs Nothing)) = Success + | TA.at xs (Vec.head os) == Just zero = Success + | otherwise = + let o = Vec.head os + zsSub = TA.subArray xs (Just (Tuple 0 (Just o))) + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs === ys + + modifyingOriginalDoesntMutateSliceTests :: Effect Unit modifyingOriginalDoesntMutateSliceTests = overAll modifyingOriginalDoesntMutateSlice where From e892216d64817d566ce34af02d1456fc4a0fb4c0 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 16:34:33 -0700 Subject: [PATCH 098/236] misimplementation --- generated-docs/Data/ArrayBuffer/Typed.md | 26 +++++++++++++++++++++++- src/Data/ArrayBuffer/Typed.js | 2 +- test/Properties/TypedArray.purs | 19 +++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md index 1b2c56a..a1c5795 100644 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -230,11 +230,35 @@ Copy part of the contents of a typed array into a new buffer, between some start #### `subArray` ``` purescript -subArray :: forall a. ArrayView a -> Offset -> Maybe Offset -> ArrayView a +subArray :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a ``` Returns a new typed array view of the same buffer, beginning at the index and ending at the second. +**Note**: there is really peculiar behavior with `subArray` - if the first offset argument is omitted, or +is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a +mutable replica of the original array - the sub-array reference reflects mutations to the original array. +However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves +purely. + +**tl;dr**: if you want a duplicate reference of the same typed array, consider either of the following: + +``` +y :: ArrayView _ +y = x + +y' :: ArrayView _ +y' = subArray x Nothing + +y'' :: ArrayView _ +y'' = subArray x (Just (Tuple 0 Nothing)) + +y''' :: ArrayView _ +y''' = subArray x (Just (Tuple 0 (Just (length x)))) +``` + +Otherwise, you'll get an _image_ of the array at the moment, like `slice`. + #### `toString` ``` purescript diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index eccb089..603b524 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -254,7 +254,7 @@ exports.sliceImpl = function sliceImpl (a,ms,me) { return a.slice(ms); } } else { - return a.slice(s,e); + return a.slice(ms,me); } }; diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index a4943fe..d28fa42 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -95,6 +95,8 @@ typedArrayTests = do modifyingOriginalDoesntMutateSliceTests log " - let z' = slice o x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSlicePartTests + log " - let z' = slice 0 o x; q = toArray z'; mutate x; pure q == toArray z'" + modifyingOriginalDoesntMutateSlicePart2Tests -- log " - take (o + 1) (copyWithin o x) == slice o x" -- copyWithinIsSliceTests @@ -568,6 +570,23 @@ modifyingOriginalDoesntMutateSlicePartTests = overAll modifyingOriginalDoesntMut in zs === ys +modifyingOriginalDoesntMutateSlicePart2Tests :: Effect Unit +modifyingOriginalDoesntMutateSlicePart2Tests = overAll modifyingOriginalDoesntMutateSlicePart2 + where + modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) + | Array.all (eq zero) (TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing)))) = Success + | TA.at xs (Vec.head os) == Just zero = Success + | otherwise = + let o = Vec.head os + zsSub = TA.slice xs (Just (Tuple 0 (Just o))) + zs = TA.toArray zsSub + ys = unsafePerformEffect do + TA.fill xs zero Nothing + pure (TA.toArray zsSub) + in zs === ys + + copyWithinIsSliceTests :: Effect Unit copyWithinIsSliceTests = overAll copyWithinIsSlice where From 75d5c6c7aea8d0f6c1fe3f6ff4767797d4c247c5 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 16:39:00 -0700 Subject: [PATCH 099/236] copy within self --- test/Properties/TypedArray.purs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index d28fa42..dabb275 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -97,6 +97,8 @@ typedArrayTests = do modifyingOriginalDoesntMutateSlicePartTests log " - let z' = slice 0 o x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSlicePart2Tests + log " - copyWithin x 0 0 (length x) == x" + copyWithinSelfIsIdentityTests -- log " - take (o + 1) (copyWithin o x) == slice o x" -- copyWithinIsSliceTests @@ -587,6 +589,18 @@ modifyingOriginalDoesntMutateSlicePart2Tests = overAll modifyingOriginalDoesntMu in zs === ys +copyWithinSelfIsIdentityTests :: Effect Unit +copyWithinSelfIsIdentityTests = overAll copyWithinSelfIsIdentity + where + copyWithinSelfIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + copyWithinSelfIsIdentity (WithOffset _ xs) = + let ys = TA.toArray xs + zs = unsafePerformEffect do + TA.copyWithin xs 0 0 (Just (TA.length xs)) + pure (TA.toArray xs) + in zs === ys + + copyWithinIsSliceTests :: Effect Unit copyWithinIsSliceTests = overAll copyWithinIsSlice where From ca0b7d30c650d9bab2f6bab70e509173836cb407 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 17:11:25 -0700 Subject: [PATCH 100/236] copyWithin verified --- test/Properties/TypedArray.purs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index dabb275..7dee905 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -22,9 +22,14 @@ import Test.QuickCheck.Combinators ((&=&), (|=|), (==>)) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) +import Effect.Ref (Ref) import Effect.Ref as Ref +count :: Ref Int +count = unsafePerformEffect (Ref.new 0) + + typedArrayTests :: Effect Unit typedArrayTests = do log " - byteLength x / bytesPerValue === length x" @@ -99,8 +104,11 @@ typedArrayTests = do modifyingOriginalDoesntMutateSlicePart2Tests log " - copyWithin x 0 0 (length x) == x" copyWithinSelfIsIdentityTests - -- log " - take (o + 1) (copyWithin o x) == slice o x" - -- copyWithinIsSliceTests + log " - take (o + 1) (copyWithin o x) == slice o x" + copyWithinIsSliceTests + + c <- Ref.read count + log $ "Verified " <> show c <> " properties, generating " <> show (c * 900) <> " test cases." @@ -119,6 +127,7 @@ type TestableArrayF a b n t q = overAll :: forall q n. Testable q => Nat n => (forall a b t. TestableArrayF a b n t q) -> Effect Unit overAll f = do + void (Ref.modify (\x -> x + 1) count) log " - Uint8ClampedArray" quickCheckGen (f <$> genWithOffset genUint8ClampedArray) log " - Uint32Array" @@ -610,8 +619,8 @@ copyWithinIsSliceTests = overAll copyWithinIsSlice ys = TA.toArray (TA.slice xs (Just (Tuple o Nothing))) zs = unsafePerformEffect do TA.copyWithin xs 0 o Nothing - pure $ Array.take (Array.length ys - o) $ TA.toArray xs - in zs === ys + pure $ Array.drop (Array.length ys) $ TA.toArray xs + in TA.toArray xs === ys <> zs From 8bd3651d8f9a199842ac983e0086678dbae32e17 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 17:16:37 -0700 Subject: [PATCH 101/236] more verification for copyWithin --- test/Properties/TypedArray.purs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 7dee905..8700ca9 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -106,6 +106,8 @@ typedArrayTests = do copyWithinSelfIsIdentityTests log " - take (o + 1) (copyWithin o x) == slice o x" copyWithinIsSliceTests + log " - copyWithin o x == setTyped x (slice o x)" + copyWithinViaSetTypedTests c <- Ref.read count log $ "Verified " <> show c <> " properties, generating " <> show (c * 900) <> " test cases." @@ -623,6 +625,20 @@ copyWithinIsSliceTests = overAll copyWithinIsSlice in TA.toArray xs === ys <> zs +copyWithinViaSetTypedTests :: Effect Unit +copyWithinViaSetTypedTests = overAll copyWithinViaSetTyped + where + copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result + copyWithinViaSetTyped (WithOffset os xs) = + let o = Vec.head os + xs' = TA.fromArray (TA.toArray xs) :: ArrayView a + _ = unsafePerformEffect do + let ys = TA.slice xs' (Just (Tuple o Nothing)) + TA.setTyped xs' Nothing ys + TA.copyWithin xs 0 o Nothing + in TA.toArray xs === TA.toArray xs' + + -- TODO: copyWithin, setTyped, slice, subArray From b3c0a1f44b736357b537e67491d58fa488798864 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 19:31:33 -0700 Subject: [PATCH 102/236] gen arraybuffer and dataview --- src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 12 ++++++++++++ src/Data/ArrayBuffer/DataView/Gen.purs | 12 ++++++++++++ test/Properties/TypedArray.purs | 5 ----- 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 src/Data/ArrayBuffer/ArrayBuffer/Gen.purs create mode 100644 src/Data/ArrayBuffer/DataView/Gen.purs diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs new file mode 100644 index 0000000..959338e --- /dev/null +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -0,0 +1,12 @@ +module Data.ArrayBuffer.ArrayBuffer.Gen where + +import Data.ArrayBuffer.Typed.Gen (genUint8Array) +import Data.ArrayBuffer.Typed (buffer) +import Data.ArrayBuffer.Types (ArrayBuffer) + +import Prelude ((<$>)) +import Control.Monad.Gen.Class (class MonadGen) + + +genArrayBuffer :: forall m. MonadGen m => m ArrayBuffer +genArrayBuffer = buffer <$> genUint8Array diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs new file mode 100644 index 0000000..5e4c189 --- /dev/null +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -0,0 +1,12 @@ +module Data.ArrayBuffer.DataView.Gen where + +import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) +import Data.ArrayBuffer.DataView (whole) +import Data.ArrayBuffer.Types (DataView) + +import Prelude ((<$>)) +import Control.Monad.Gen.Class (class MonadGen) + + +genDataView :: forall m. MonadGen m => m DataView +genDataView = whole <$> genArrayBuffer diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 8700ca9..a6cdf65 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -422,9 +422,6 @@ setTypedOfSubArrayIsIdentityTests = overAll setTypedOfSubArrayIsIdentity in zs === ys --- setTyped of subArray is copyWithin - - modifyingOriginalMutatesSubArrayTests :: Effect Unit modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray where @@ -640,5 +637,3 @@ copyWithinViaSetTypedTests = overAll copyWithinViaSetTyped - --- TODO: copyWithin, setTyped, slice, subArray From 1a65829e9adf3d2b4c7974548336d8c3f44bbde9 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 19:35:18 -0700 Subject: [PATCH 103/236] more qualified imports --- src/Data/ArrayBuffer/DataView.purs | 2 +- src/Data/ArrayBuffer/Typed.purs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index e5e54df..b71d47e 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -40,7 +40,7 @@ module Data.ArrayBuffer.DataView , setFloat64le ) where -import Prelude +import Prelude (Unit, const, pure, (<$>)) import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) import Data.Maybe (Maybe(..)) import Effect (Effect) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f61574d..b8db8cc 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -19,7 +19,7 @@ module Data.ArrayBuffer.Typed , toString, toString', toArray ) where -import Prelude +import Prelude (Unit, pure, (<$>), (<<<), ($)) import Effect (Effect) import Effect.Uncurried ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 From daa72e07d29349cfc7f3d6c5a8b6150a01e53cac Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 19:40:35 -0700 Subject: [PATCH 104/236] using inline ternary op --- src/Data/ArrayBuffer/Typed.js | 117 +++++++++++----------------------- 1 file changed, 36 insertions(+), 81 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 603b524..db1db76 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -38,103 +38,58 @@ exports.lengthImpl = function lemgthImpl (v) { // Typed Arrays exports.newUint8ClampedArray = function newUint8ClampedArray (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Uint8ClampedArray(a); - } else { - return new Uint8ClampedArray(a,mb); - } - } else { - return new Uint8ClampedArray(a,mb,mc); - } + return mc === null ? ( mb === null ? new Uint8ClampedArray(a) + : new Uint8ClampedArray(a,mb) + ) + : new Uint8ClampedArray(a,mb,mc); }; exports.newUint32Array = function newUint32Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Uint32Array(a); - } else { - return new Uint32Array(a,mb); - } - } else { - return new Uint32Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Uint32Array(a) + : new Uint32Array(a,mb) + ) + : new Uint32Array(a,mb,mc); }; exports.newUint16Array = function newUint16Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Uint16Array(a); - } else { - return new Uint16Array(a,mb); - } - } else { - return new Uint16Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Uint16Array(a) + : new Uint16Array(a,mb) + ) + : new Uint16Array(a,mb,mc); }; exports.newUint8Array = function newUint8Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Uint8Array(a); - } else { - return new Uint8Array(a,mb); - } - } else { - return new Uint8Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Uint8Array(a) + : new Uint8Array(a,mb) + ) + : new Uint8Array(a,mb,mc); }; exports.newInt32Array = function newInt32Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Int32Array(a); - } else { - return new Int32Array(a,mb); - } - } else { - return new Int32Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Int32Array(a) + : new Int32Array(a,mb) + ) + : new Int32Array(a,mb,mc); }; exports.newInt16Array = function newInt16Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Int16Array(a); - } else { - return new Int16Array(a,mb); - } - } else { - return new Int16Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Int16Array(a) + : new Int16Array(a,mb) + ) + : new Int16Array(a,mb,mc); }; exports.newInt8Array = function newInt8Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Int8Array(a); - } else { - return new Int8Array(a,mb); - } - } else { - return new Int8Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Int8Array(a) + : new Int8Array(a,mb) + ) + : new Int8Array(a,mb,mc); }; exports.newFloat32Array = function newFloat32Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Float32Array(a); - } else { - return new Float32Array(a,mb); - } - } else { - return new Float32Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Float32Array(a) + : new Float32Array(a,mb) + ) + : new Float32Array(a,mb,mc); }; exports.newFloat64Array = function newFloat64Array (a,mb,mc) { - if (mc === null) { - if (mb === null) { - return new Float64Array(a); - } else { - return new Float64Array(a,mb); - } - } else { - return new Float64Array(a,mb,mc); - } + return mc === null ? ( mb === null ? new Float64Array(a) + : new Float64Array(a,mb) + ) + : new Float64Array(a,mb,mc); }; From c909bd4d24bece6a9fd770fa65257fdfc0cd5610 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 13 Dec 2018 20:23:21 -0700 Subject: [PATCH 105/236] different gen paradigm --- src/Data/ArrayBuffer/ArrayBuffer.js | 10 +-- src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 13 +++- src/Data/ArrayBuffer/DataView/Gen.purs | 11 ++- src/Data/ArrayBuffer/Typed.js | 94 +++++++---------------- src/Data/ArrayBuffer/Typed/Gen.purs | 50 ++++-------- test/Properties/TypedArray.purs | 29 +++---- 6 files changed, 75 insertions(+), 132 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index c7a1ea0..0952ca2 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -11,13 +11,5 @@ exports.byteLength = function byteLength (a) { }; exports.sliceImpl = function sliceImpl (a, ms, me) { - if (me === null) { - if (ms === null) { - return a.slice(); - } else { - return a.slice(ms); - } - } else { - return a.slice(ms,me); - } + return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me); }; diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index 959338e..686f503 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -1,12 +1,17 @@ module Data.ArrayBuffer.ArrayBuffer.Gen where -import Data.ArrayBuffer.Typed.Gen (genUint8Array) +import Data.ArrayBuffer.Typed.Gen (genUByte, genTypedArray) import Data.ArrayBuffer.Typed (buffer) -import Data.ArrayBuffer.Types (ArrayBuffer) +import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, Uint8Array) import Prelude ((<$>)) +import Data.Maybe (Maybe) import Control.Monad.Gen.Class (class MonadGen) -genArrayBuffer :: forall m. MonadGen m => m ArrayBuffer -genArrayBuffer = buffer <$> genUint8Array +genArrayBuffer :: forall m + . MonadGen m + => ByteLength + -> Maybe ByteLength + -> m ArrayBuffer +genArrayBuffer a b = buffer <$> (genTypedArray a b genUByte :: m Uint8Array) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 5e4c189..23f775e 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -2,11 +2,16 @@ module Data.ArrayBuffer.DataView.Gen where import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole) -import Data.ArrayBuffer.Types (DataView) +import Data.ArrayBuffer.Types (DataView, ByteLength) import Prelude ((<$>)) +import Data.Maybe (Maybe) import Control.Monad.Gen.Class (class MonadGen) -genDataView :: forall m. MonadGen m => m DataView -genDataView = whole <$> genArrayBuffer +genDataView :: forall m + . MonadGen m + => ByteLength + -> Maybe ByteLength + -> m DataView +genDataView a b = whole <$> genArrayBuffer a b diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index db1db76..cc38dc4 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -45,51 +45,51 @@ exports.newUint8ClampedArray = function newUint8ClampedArray (a,mb,mc) { }; exports.newUint32Array = function newUint32Array (a,mb,mc) { return mc === null ? ( mb === null ? new Uint32Array(a) - : new Uint32Array(a,mb) + : new Uint32Array(a,mb) ) - : new Uint32Array(a,mb,mc); + : new Uint32Array(a,mb,mc); }; exports.newUint16Array = function newUint16Array (a,mb,mc) { return mc === null ? ( mb === null ? new Uint16Array(a) - : new Uint16Array(a,mb) + : new Uint16Array(a,mb) ) - : new Uint16Array(a,mb,mc); + : new Uint16Array(a,mb,mc); }; exports.newUint8Array = function newUint8Array (a,mb,mc) { return mc === null ? ( mb === null ? new Uint8Array(a) - : new Uint8Array(a,mb) + : new Uint8Array(a,mb) ) - : new Uint8Array(a,mb,mc); + : new Uint8Array(a,mb,mc); }; exports.newInt32Array = function newInt32Array (a,mb,mc) { return mc === null ? ( mb === null ? new Int32Array(a) - : new Int32Array(a,mb) + : new Int32Array(a,mb) ) - : new Int32Array(a,mb,mc); + : new Int32Array(a,mb,mc); }; exports.newInt16Array = function newInt16Array (a,mb,mc) { return mc === null ? ( mb === null ? new Int16Array(a) - : new Int16Array(a,mb) + : new Int16Array(a,mb) ) - : new Int16Array(a,mb,mc); + : new Int16Array(a,mb,mc); }; exports.newInt8Array = function newInt8Array (a,mb,mc) { return mc === null ? ( mb === null ? new Int8Array(a) - : new Int8Array(a,mb) + : new Int8Array(a,mb) ) - : new Int8Array(a,mb,mc); + : new Int8Array(a,mb,mc); }; exports.newFloat32Array = function newFloat32Array (a,mb,mc) { return mc === null ? ( mb === null ? new Float32Array(a) - : new Float32Array(a,mb) + : new Float32Array(a,mb) ) - : new Float32Array(a,mb,mc); + : new Float32Array(a,mb,mc); }; exports.newFloat64Array = function newFloat64Array (a,mb,mc) { return mc === null ? ( mb === null ? new Float64Array(a) - : new Float64Array(a,mb) + : new Float64Array(a,mb) ) - : new Float64Array(a,mb,mc); + : new Float64Array(a,mb,mc); }; @@ -104,15 +104,7 @@ exports.someImpl = function someImpl (a,p) { exports.fillImpl = function fillImpl (a,x,ms,me) { - if (me === null) { - if (ms === null) { - return a.fill(x); - } else { - return a.fill(x,ms); - } - } else { - return a.fill(x,ms,me); - } + return me === null ? (ms === null ? a.fill(x) : a.fill(x,ms)) : a.fill(x,ms,me); }; @@ -129,11 +121,7 @@ exports.filterImpl = function filterImpl (a,p) { }; exports.includesImpl = function includesImpl (a,x,mo) { - if (mo === null) { - return a.includes(x); - } else { - return a.includes(x,mo); - } + return mo === null ? a.includes(x) : a.includes(x,mo); }; exports.reduceImpl = function reduceImpl (a,f,i) { @@ -158,21 +146,11 @@ exports.findIndexImpl = function findIndexImpl (a,f) { return (x === -1) ? null : x; }; exports.indexOfImpl = function indexOfImpl (a,x,mo) { - var r; - if (mo === null) { - r = a.indexOf(x); - } else { - r = a.indexOf(x,mo); - } + var r = mo === null ? a.indexOf(x) : a.indexOf(x,mo); return r === -1 ? null : r; }; exports.lastIndexOfImpl = function lastIndexOfImpl (a,x,mo) { - var r; - if (mo === null) { - r = a.lastIndexOf(x); - } else { - r = a.lastIndexOf(x,mo); - } + var r = mo === null ? a.lastIndexOf(x) : a.lastIndexOf(x,mo); return r === -1 ? null : r; }; @@ -202,15 +180,7 @@ exports.setImpl = function setImpl (a, off, b) { exports.sliceImpl = function sliceImpl (a,ms,me) { - if (me === null) { - if (ms === null) { - return a.slice(); - } else { - return a.slice(ms); - } - } else { - return a.slice(ms,me); - } + return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me); }; @@ -220,15 +190,7 @@ exports.sortImpl = function sortImpl (a) { exports.subArrayImpl = function subArrayImpl (a,ms,me) { - if (me === null) { - if (ms === null) { - return a.subarray(); - } else { - return a.subarray(ms); - } - } else { - return a.subarray(ms,me); - } + return me === null ? (ms === null ? a.subarray() : a.subarray(ms)) : a.subarray(ms,me); }; @@ -245,13 +207,13 @@ exports.unsafeAtImpl = function(a, i) { } exports.hasIndexImpl = function(a, i) { - return i in a; + return i in a; } exports.toArrayImpl = function(a) { - var l = a.length; - var ret = new Array(l); - for (var i = 0; i < l; i++) - ret[i] = a[i]; - return ret; + var l = a.length; + var ret = new Array(l); + for (var i = 0; i < l; i++) + ret[i] = a[i]; + return ret; } diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 5a88d10..75e6af5 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -12,7 +12,6 @@ import Data.ArrayBuffer.Typed as TA import Prelude import Math as M import Data.Maybe (Maybe (..)) -import Data.List.Lazy (replicateM) import Data.Int as I import Data.UInt (UInt) import Data.UInt as UInt @@ -31,42 +30,19 @@ import Partial.Unsafe (unsafePartial) -genUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray -genUint8ClampedArray = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genUByte - -genUint32Array :: forall m. MonadGen m => m Uint32Array -genUint32Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genUWord - -genUint16Array :: forall m. MonadGen m => m Uint16Array -genUint16Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genUChomp - -genUint8Array :: forall m. MonadGen m => m Uint8Array -genUint8Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genUByte - -genInt32Array :: forall m. MonadGen m => m Int32Array -genInt32Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genWord - -genInt16Array :: forall m. MonadGen m => m Int16Array -genInt16Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genChomp - -genInt8Array :: forall m. MonadGen m => m Int8Array -genInt8Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genByte - -genFloat32Array :: forall m. MonadGen m => m Float32Array -genFloat32Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genFloat32 - -genFloat64Array :: forall m. MonadGen m => m Float64Array -genFloat64Array = sized \s -> - TA.fromArray <<< Array.fromFoldable <$> replicateM s genFloat64 - +genTypedArray :: forall m a t + . MonadGen m + => TA.TypedArray a t + => TA.Length -- ^ Minimum length + -> Maybe TA.Length -- ^ Max length + -> m t + -> m (ArrayView a) +genTypedArray q1 mq2 gen = sized \s -> + let s'' = s `max` q1 + s' = case mq2 of + Nothing -> s'' + Just q2 -> s'' `min` q2 + in TA.fromArray <$> replicateA s' gen diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index a6cdf65..75472b5 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -1,13 +1,15 @@ module Test.Properties.TypedArray where -import Data.ArrayBuffer.Types (ArrayView) +import Data.ArrayBuffer.Types + (ArrayView, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array + , Float32Array, Float64Array) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed (class BytesPerValue, class TypedArray) import Data.ArrayBuffer.Typed.Gen - ( genUint8ClampedArray, genUint8Array, genUint16Array, genUint32Array - , genInt8Array, genInt16Array, genInt32Array - , genFloat32Array, genFloat64Array, WithOffset (..), genWithOffset) + ( genUByte, genUChomp, genUWord + , genByte, genChomp, genWord, genFloat32, genFloat64 + , WithOffset (..), genWithOffset, genTypedArray) import Prelude import Data.Maybe (Maybe (..)) @@ -18,6 +20,7 @@ import Data.Array as Array import Data.HeytingAlgebra (implies) import Type.Proxy (Proxy (..)) import Test.QuickCheck (quickCheckGen, Result (..), (===), (/==), class Testable, class Arbitrary, ()) +import Test.QuickCheck.Gen (Gen) import Test.QuickCheck.Combinators ((&=&), (|=|), (==>)) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) @@ -131,23 +134,23 @@ overAll :: forall q n. Testable q => Nat n => (forall a b t. TestableArrayF a b overAll f = do void (Ref.modify (\x -> x + 1) count) log " - Uint8ClampedArray" - quickCheckGen (f <$> genWithOffset genUint8ClampedArray) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUByte :: Gen Uint8ClampedArray)) log " - Uint32Array" - quickCheckGen (f <$> genWithOffset genUint32Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUWord :: Gen Uint32Array)) log " - Uint16Array" - quickCheckGen (f <$> genWithOffset genUint16Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUChomp :: Gen Uint16Array)) log " - Uint8Array" - quickCheckGen (f <$> genWithOffset genUint8Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUByte :: Gen Uint8Array)) log " - Int32Array" - quickCheckGen (f <$> genWithOffset genInt32Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genWord :: Gen Int32Array)) log " - Int16Array" - quickCheckGen (f <$> genWithOffset genInt16Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genChomp :: Gen Int16Array)) log " - Int8Array" - quickCheckGen (f <$> genWithOffset genInt8Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genByte :: Gen Int8Array)) log " - Float32Array" - quickCheckGen (f <$> genWithOffset genFloat32Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genFloat32 :: Gen Float32Array)) log " - Float64Array" - quickCheckGen (f <$> genWithOffset genFloat64Array) + quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genFloat64 :: Gen Float64Array)) byteLengthDivBytesPerValueTests :: Effect Unit From bcd08776e20c0be0a8b4e72bc990f5c45310709c Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 11:19:21 -0700 Subject: [PATCH 106/236] moving classes around - safe dataView modification --- src/Data/ArrayBuffer/Typed.purs | 31 +-- src/Data/ArrayBuffer/Typed/Gen.purs | 10 +- src/Data/ArrayBuffer/ValueMapping.purs | 34 ++++ test/Main.purs | 11 +- test/Properties.purs | 14 ++ test/Properties/DataView.purs | 25 +++ test/Properties/TypedArray.purs | 264 ++++++++++++------------- 7 files changed, 223 insertions(+), 166 deletions(-) create mode 100644 src/Data/ArrayBuffer/ValueMapping.purs diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b8db8cc..44445f9 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -5,7 +5,6 @@ module Data.ArrayBuffer.Typed ( polyFill , Offset, Length , buffer, byteOffset, byteLength, length - , class BytesPerValue , class TypedArray , whole, remainder, part, empty, fromArray , fill, set, setTyped, copyWithin @@ -19,6 +18,15 @@ module Data.ArrayBuffer.Typed , toString, toString', toArray ) where +import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) +import Data.ArrayBuffer.Types + ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength + , Float64Array, Float32Array + , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array + , Float64, Float32 + , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) + + import Prelude (Unit, pure, (<$>), (<<<), ($)) import Effect (Effect) import Effect.Uncurried @@ -31,14 +39,7 @@ import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, toNullable, toMaybe) import Data.UInt (UInt) import Data.UInt (fromNumber, toNumber) as UInt -import Data.ArrayBuffer.Types - ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength - , Float64Array, Float32Array - , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array - , Float64, Float32 - , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) -import Data.Typelevel.Num (D1,D2,D4,D8) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -55,18 +56,6 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength foreign import lengthImpl :: forall a. ArrayView a -> Length -class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b - -instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped D1 -instance bytesPerValueUint32 :: BytesPerValue Uint32 D4 -instance bytesPerValueUint16 :: BytesPerValue Uint16 D2 -instance bytesPerValueUint8 :: BytesPerValue Uint8 D1 -instance bytesPerValueInt32 :: BytesPerValue Int32 D4 -instance bytesPerValueInt16 :: BytesPerValue Int16 D2 -instance bytesPerValueInt8 :: BytesPerValue Int8 D1 -instance bytesPerValueFloat32 :: BytesPerValue Float32 D4 -instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 - length :: forall a b. BytesPerValue a b => ArrayView a -> Int length = lengthImpl @@ -140,7 +129,7 @@ type Length = Int -- | - `subArray` returns a new typed array with a separate array buffer -- | - `toString` prints to a CSV, `toString'` allows you to supply the delimiter -- | - `toArray` returns an array of numeric values -class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where +class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where -- | View mapping the whole `ArrayBuffer`. whole :: ArrayBuffer -> ArrayView a -- | View mapping the rest of an `ArrayBuffer` after an index. diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 75e6af5..2d8caad 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -2,12 +2,9 @@ module Data.ArrayBuffer.Typed.Gen where -import Data.ArrayBuffer.Types - ( Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array - , Int8Array, Int16Array, Int32Array - , Float32Array, Float64Array, ArrayView - ) +import Data.ArrayBuffer.Types (ArrayView) import Data.ArrayBuffer.Typed as TA +import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Prelude import Math as M @@ -17,7 +14,6 @@ import Data.UInt (UInt) import Data.UInt as UInt import Data.String.CodeUnits as S import Data.Float.Parse (parseFloat) -import Data.Array as Array import Data.Vec (Vec) import Data.Vec (fromArray) as Vec import Data.Generic.Rep (class Generic) @@ -97,7 +93,7 @@ derive instance genericWithOffset :: Generic (ArrayView a) a' => Generic (WithOf genWithOffset :: forall m n b a . MonadGen m => Nat n - => TA.BytesPerValue a b + => BytesPerValue a b => m (ArrayView a) -> m (WithOffset n a) genWithOffset genArrayView = do diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs new file mode 100644 index 0000000..73a0675 --- /dev/null +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -0,0 +1,34 @@ +module Data.ArrayBuffer.ValueMapping where + +import Data.ArrayBuffer.Types + ( kind ArrayViewType + , Float64, Float32 + , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) +import Data.Typelevel.Num (D1, D2, D4, D8) +import Data.UInt (UInt) + + +class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b + +instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped D1 +instance bytesPerValueUint32 :: BytesPerValue Uint32 D4 +instance bytesPerValueUint16 :: BytesPerValue Uint16 D2 +instance bytesPerValueUint8 :: BytesPerValue Uint8 D1 +instance bytesPerValueInt32 :: BytesPerValue Int32 D4 +instance bytesPerValueInt16 :: BytesPerValue Int16 D2 +instance bytesPerValueInt8 :: BytesPerValue Int8 D1 +instance bytesPerValueFloat32 :: BytesPerValue Float32 D4 +instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 + + +class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t + +instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped Int +instance binaryValueUint32 :: BinaryValue Uint32 UInt +instance binaryValueUint16 :: BinaryValue Uint16 Int +instance binaryValueUint8 :: BinaryValue Uint8 Int +instance binaryValueInt32 :: BinaryValue Int32 Int +instance binaryValueInt16 :: BinaryValue Int16 Int +instance binaryValueInt8 :: BinaryValue Int8 Int +instance binaryValueFloat32 :: BinaryValue Float32 Number +instance binaryValueFloat64 :: BinaryValue Float64 Number diff --git a/test/Main.purs b/test/Main.purs index 1925879..38cc552 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,19 +1,24 @@ module Test.Main where -import Test.Properties.TypedArray (typedArrayTests) +import Test.Properties (propertiesTests) import Prelude import Effect (Effect) import Effect.Console (log) +import Effect.Ref as Ref main :: Effect Unit main = do + count <- Ref.new 0 + log "Starting tests..." - log " - TypedArray Tests:" - typedArrayTests + propertiesTests count + + c <- Ref.read count + log $ "Verified " <> show c <> " properties, generating " <> show (c * 900) <> " test cases." -- import Data.ArrayBuffer.ArrayBuffer as AB diff --git a/test/Properties.purs b/test/Properties.purs index a82bbb7..22156bf 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -1,3 +1,17 @@ module Test.Properties where +import Test.Properties.TypedArray (typedArrayTests) +import Test.Properties.DataView (dataViewTests) +import Prelude (Unit, bind, discard) +import Effect (Effect) +import Effect.Ref (Ref) +import Effect.Console (log) + + +propertiesTests :: Ref Int -> Effect Unit +propertiesTests count = do + log " - TypedArray Tests:" + typedArrayTests count + log " - DataView Tests:" + dataViewTests count diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 71e7bd1..897a2af 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -1,2 +1,27 @@ module Test.Properties.DataView where + +import Data.ArrayBuffer.Types (DataView) +import Data.ArrayBuffer.DataView as DA +import Data.ArrayBuffer.DataView.Gen (genDataView) + +import Prelude +import Effect (Effect) +import Effect.Ref (Ref) +import Effect.Ref as Ref + + + +dataViewTests :: Ref Int -> Effect Unit +dataViewTests count = do + pure unit + + +type TestableViewF q = + DataView + -> q + + +overAll :: Ref Int -> Effect Unit +overAll count = do + void (Ref.modify (\x -> x + 1) count) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 75472b5..2576de5 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -5,7 +5,8 @@ import Data.ArrayBuffer.Types (ArrayView, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array , Float32Array, Float64Array) import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Typed (class BytesPerValue, class TypedArray) +import Data.ArrayBuffer.Typed (class TypedArray) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.ArrayBuffer.Typed.Gen ( genUByte, genUChomp, genUWord , genByte, genChomp, genWord, genFloat32, genFloat64 @@ -29,91 +30,84 @@ import Effect.Ref (Ref) import Effect.Ref as Ref -count :: Ref Int -count = unsafePerformEffect (Ref.new 0) - - -typedArrayTests :: Effect Unit -typedArrayTests = do +typedArrayTests :: Ref Int -> Effect Unit +typedArrayTests count = do log " - byteLength x / bytesPerValue === length x" - byteLengthDivBytesPerValueTests + byteLengthDivBytesPerValueTests count log " - fromArray (toArray x) === x" - fromArrayToArrayIsoTests + fromArrayToArrayIsoTests count log " - fill y x => all (== y) x" - allAreFilledTests + allAreFilledTests count log " - set x [y] o => (at x o == Just y)" - setSingletonIsEqTests + setSingletonIsEqTests count log " - all p x => any p x" - allImpliesAnyTests + allImpliesAnyTests count log " - all p (filter p x)" - filterImpliesAllTests + filterImpliesAllTests count log " - filter (not . p) (filter p x) == []" - filterIsTotalTests + filterIsTotalTests count log " - filter p (filter p x) == filter p x" - filterIsIdempotentTests + filterIsIdempotentTests count log " - forall os `in` xs. all (\\o -> hasIndex o xs)" - withOffsetHasIndexTests + withOffsetHasIndexTests count log " - forall os `in` xs. all (\\o -> elem (at o xs) xs)" - withOffsetElemTests + withOffsetElemTests count log " - any p x => p (find p x)" - anyImpliesFindTests + anyImpliesFindTests count log " - p (at x (findIndex p x))" - findIndexImpliesAtTests + findIndexImpliesAtTests count log " - at x (indexOf y x) == y" - indexOfImpliesAtTests + indexOfImpliesAtTests count log " - at x (lastIndexOf y x) == y" - lastIndexOfImpliesAtTests + lastIndexOfImpliesAtTests count log " - foldr cons [] x == toArray x" - foldrConsIsToArrayTests + foldrConsIsToArrayTests count log " - foldl snoc [] x == toArray x" - foldlSnocIsToArrayTests + foldlSnocIsToArrayTests count log " - map identity x == x" - mapIdentityIsIdentityTests + mapIdentityIsIdentityTests count log " - traverse snoc x == toArray x" - traverseSnocIsToArrayTests + traverseSnocIsToArrayTests count log " - reverse (reverse x) == x" - doubleReverseIsIdentityTests + doubleReverseIsIdentityTests count log " - toArray (reverse x) == Array.reverse (toArray x)" - reverseIsArrayReverseTests + reverseIsArrayReverseTests count log " - sort (sort x) == sort x" - sortIsIdempotentTests + sortIsIdempotentTests count log " - toArray (sort x) == Array.sort (toArray x)" - sortIsArraySortTests + sortIsArraySortTests count log " - toString' \",\" x == toString x" - toStringIsJoinWithCommaTests + toStringIsJoinWithCommaTests count log " - setTyped x (subArray x) == x" - setTypedOfSubArrayIsIdentityTests + setTypedOfSubArrayIsIdentityTests count log " - let z' = subArray x; q = toArray z'; mutate x; pure q /= toArray z'" - modifyingOriginalMutatesSubArrayTests + modifyingOriginalMutatesSubArrayTests count log " - let z' = subArray x; q = toArray x; mutate z'; pure q /= toArray x" - modifyingSubArrayMutatesOriginalTests + modifyingSubArrayMutatesOriginalTests count log " - let z' = subArray 0 x; q = toArray z'; mutate x; pure q /= toArray z'" - modifyingOriginalMutatesSubArrayZeroTests + modifyingOriginalMutatesSubArrayZeroTests count log " - let z' = subArray 0 x; q = toArray x; mutate z'; pure q /= toArray x" - modifyingSubArrayMutatesOriginalZeroTests + modifyingSubArrayMutatesOriginalZeroTests count log " - let z' = subArray 0 (length x) x; q = toArray z'; mutate x; pure q /= toArray z'" - modifyingOriginalMutatesSubArrayAllTests + modifyingOriginalMutatesSubArrayAllTests count log " - let z' = subArray 0 (length x) x; q = toArray x; mutate z'; pure q /= toArray x" - modifyingSubArrayMutatesOriginalAllTests + modifyingSubArrayMutatesOriginalAllTests count log " - let z' = subArray o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSubArrayPartTests + modifyingOriginalDoesntMutateSubArrayPartTests count log " - let z' = subArray 0 o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSubArrayPart2Tests + modifyingOriginalDoesntMutateSubArrayPart2Tests count log " - let z' = slice x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSliceTests + modifyingOriginalDoesntMutateSliceTests count log " - let z' = slice o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSlicePartTests + modifyingOriginalDoesntMutateSlicePartTests count log " - let z' = slice 0 o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSlicePart2Tests + modifyingOriginalDoesntMutateSlicePart2Tests count log " - copyWithin x 0 0 (length x) == x" - copyWithinSelfIsIdentityTests + copyWithinSelfIsIdentityTests count log " - take (o + 1) (copyWithin o x) == slice o x" - copyWithinIsSliceTests + copyWithinIsSliceTests count log " - copyWithin o x == setTyped x (slice o x)" - copyWithinViaSetTypedTests - - c <- Ref.read count - log $ "Verified " <> show c <> " properties, generating " <> show (c * 900) <> " test cases." + copyWithinViaSetTypedTests count @@ -130,47 +124,47 @@ type TestableArrayF a b n t q = -> q -overAll :: forall q n. Testable q => Nat n => (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll f = do +overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit +overAll count f = do void (Ref.modify (\x -> x + 1) count) log " - Uint8ClampedArray" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUByte :: Gen Uint8ClampedArray)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUByte :: Gen Uint8ClampedArray)) log " - Uint32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUWord :: Gen Uint32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUWord :: Gen Uint32Array)) log " - Uint16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUChomp :: Gen Uint16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUChomp :: Gen Uint16Array)) log " - Uint8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genUByte :: Gen Uint8Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUByte :: Gen Uint8Array)) log " - Int32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genWord :: Gen Int32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genWord :: Gen Int32Array)) log " - Int16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genChomp :: Gen Int16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genChomp :: Gen Int16Array)) log " - Int8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genByte :: Gen Int8Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genByte :: Gen Int8Array)) log " - Float32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genFloat32 :: Gen Float32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genFloat32 :: Gen Float32Array)) log " - Float64Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 0 Nothing genFloat64 :: Gen Float64Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genFloat64 :: Gen Float64Array)) -byteLengthDivBytesPerValueTests :: Effect Unit -byteLengthDivBytesPerValueTests = overAll byteLengthDivBytesPerValueEqLength +byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit +byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValueEqLength where byteLengthDivBytesPerValueEqLength :: forall a b t. TestableArrayF a b D0 t Result byteLengthDivBytesPerValueEqLength (WithOffset _ a) = let b = toInt' (Proxy :: Proxy b) in TA.length a === (TA.byteLength a `div` b) -fromArrayToArrayIsoTests :: Effect Unit -fromArrayToArrayIsoTests = overAll fromArrayToArrayIso +fromArrayToArrayIsoTests :: Ref Int -> Effect Unit +fromArrayToArrayIsoTests count = overAll count fromArrayToArrayIso where fromArrayToArrayIso :: forall a b t. TestableArrayF a b D0 t Result fromArrayToArrayIso (WithOffset _ x) = TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x -allAreFilledTests :: Effect Unit -allAreFilledTests = overAll allAreFilled +allAreFilledTests :: Ref Int -> Effect Unit +allAreFilledTests count = overAll count allAreFilled where allAreFilled :: forall a b t. TestableArrayF a b D0 t Result allAreFilled (WithOffset _ xs) = unsafePerformEffect do @@ -182,8 +176,8 @@ allAreFilledTests = overAll allAreFilled pure (b "All aren't the filled value") -setSingletonIsEqTests :: Effect Unit -setSingletonIsEqTests = overAll setSingletonIsEq +setSingletonIsEqTests :: Ref Int -> Effect Unit +setSingletonIsEqTests count = overAll count setSingletonIsEq where setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result setSingletonIsEq (WithOffset os xs) = unsafePerformEffect do @@ -195,8 +189,8 @@ setSingletonIsEqTests = overAll setSingletonIsEq -- | Should work with any arbitrary predicate, but we can't generate them -allImpliesAnyTests :: Effect Unit -allImpliesAnyTests = overAll allImpliesAny +allImpliesAnyTests :: Ref Int -> Effect Unit +allImpliesAnyTests count = overAll count allImpliesAny where allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result allImpliesAny (WithOffset _ xs) = @@ -207,8 +201,8 @@ allImpliesAnyTests = overAll allImpliesAny -- | Should work with any arbitrary predicate, but we can't generate them -filterImpliesAllTests :: Effect Unit -filterImpliesAllTests = overAll filterImpliesAll +filterImpliesAllTests :: Ref Int -> Effect Unit +filterImpliesAllTests count = overAll count filterImpliesAll where filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result filterImpliesAll (WithOffset _ xs) = @@ -219,8 +213,8 @@ filterImpliesAllTests = overAll filterImpliesAll -- | Should work with any arbitrary predicate, but we can't generate them -filterIsTotalTests :: Effect Unit -filterIsTotalTests = overAll filterIsTotal +filterIsTotalTests :: Ref Int -> Effect Unit +filterIsTotalTests count = overAll count filterIsTotal where filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result filterIsTotal (WithOffset _ xs) = @@ -231,8 +225,8 @@ filterIsTotalTests = overAll filterIsTotal -- | Should work with any arbitrary predicate, but we can't generate them -filterIsIdempotentTests :: Effect Unit -filterIsIdempotentTests = overAll filterIsIdempotent +filterIsIdempotentTests :: Ref Int -> Effect Unit +filterIsIdempotentTests count = overAll count filterIsIdempotent where filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result filterIsIdempotent (WithOffset _ xs) = @@ -242,16 +236,16 @@ filterIsIdempotentTests = overAll filterIsIdempotent in TA.toArray zs === TA.toArray ys -withOffsetHasIndexTests :: Effect Unit -withOffsetHasIndexTests = overAll withOffsetHasIndex +withOffsetHasIndexTests :: Ref Int -> Effect Unit +withOffsetHasIndexTests count = overAll count withOffsetHasIndex where withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result withOffsetHasIndex (WithOffset os xs) = Array.all (\o -> TA.hasIndex xs o) os "All doesn't have index of itself" -withOffsetElemTests :: Effect Unit -withOffsetElemTests = overAll withOffsetElem +withOffsetElemTests :: Ref Int -> Effect Unit +withOffsetElemTests count = overAll count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = @@ -260,8 +254,8 @@ withOffsetElemTests = overAll withOffsetElem -- | Should work with any arbitrary predicate, but we can't generate them -anyImpliesFindTests :: Effect Unit -anyImpliesFindTests = overAll anyImpliesFind +anyImpliesFindTests :: Ref Int -> Effect Unit +anyImpliesFindTests count = overAll count anyImpliesFind where anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result anyImpliesFind (WithOffset _ xs) = @@ -281,8 +275,8 @@ anyImpliesFindTests = overAll anyImpliesFind -- | Should work with any arbitrary predicate, but we can't generate them -findIndexImpliesAtTests :: Effect Unit -findIndexImpliesAtTests = overAll findIndexImpliesAt +findIndexImpliesAtTests :: Ref Int -> Effect Unit +findIndexImpliesAtTests count = overAll count findIndexImpliesAt where findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result findIndexImpliesAt (WithOffset _ xs) = @@ -296,8 +290,8 @@ findIndexImpliesAtTests = overAll findIndexImpliesAt -indexOfImpliesAtTests :: Effect Unit -indexOfImpliesAtTests = overAll indexOfImpliesAt +indexOfImpliesAtTests :: Ref Int -> Effect Unit +indexOfImpliesAtTests count = overAll count indexOfImpliesAt where indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result indexOfImpliesAt (WithOffset _ xs) = @@ -308,8 +302,8 @@ indexOfImpliesAtTests = overAll indexOfImpliesAt Just o -> TA.at xs o === Just y -lastIndexOfImpliesAtTests :: Effect Unit -lastIndexOfImpliesAtTests = overAll lastIndexOfImpliesAt +lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit +lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt where lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result lastIndexOfImpliesAt (WithOffset _ xs) = @@ -320,32 +314,32 @@ lastIndexOfImpliesAtTests = overAll lastIndexOfImpliesAt Just o -> TA.at xs o === Just y -foldrConsIsToArrayTests :: Effect Unit -foldrConsIsToArrayTests = overAll foldrConsIsToArray +foldrConsIsToArrayTests :: Ref Int -> Effect Unit +foldrConsIsToArrayTests count = overAll count foldrConsIsToArray where foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldrConsIsToArray (WithOffset _ xs) = TA.foldr xs (\x acc _ -> Array.cons x acc) [] === TA.toArray xs -foldlSnocIsToArrayTests :: Effect Unit -foldlSnocIsToArrayTests = overAll foldlSnocIsToArray +foldlSnocIsToArrayTests :: Ref Int -> Effect Unit +foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray where foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldlSnocIsToArray (WithOffset _ xs) = TA.foldl xs (\acc x _ -> Array.snoc acc x) [] === TA.toArray xs -mapIdentityIsIdentityTests :: Effect Unit -mapIdentityIsIdentityTests = overAll mapIdentityIsIdentity +mapIdentityIsIdentityTests :: Ref Int -> Effect Unit +mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity where mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result mapIdentityIsIdentity (WithOffset _ xs) = TA.toArray (TA.map (\x _ -> x) xs) === TA.toArray xs -traverseSnocIsToArrayTests :: Effect Unit -traverseSnocIsToArrayTests = overAll traverseSnocIsToArray +traverseSnocIsToArrayTests :: Ref Int -> Effect Unit +traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray where traverseSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result traverseSnocIsToArray (WithOffset _ xs) = @@ -356,8 +350,8 @@ traverseSnocIsToArrayTests = overAll traverseSnocIsToArray in TA.toArray xs === ys -doubleReverseIsIdentityTests :: Effect Unit -doubleReverseIsIdentityTests = overAll doubleReverseIsIdentity +doubleReverseIsIdentityTests :: Ref Int -> Effect Unit +doubleReverseIsIdentityTests count = overAll count doubleReverseIsIdentity where doubleReverseIsIdentity :: forall a b t. TestableArrayF a b D0 t Result doubleReverseIsIdentity (WithOffset _ xs) = @@ -368,8 +362,8 @@ doubleReverseIsIdentityTests = overAll doubleReverseIsIdentity in TA.toArray xs === ys -reverseIsArrayReverseTests :: Effect Unit -reverseIsArrayReverseTests = overAll reverseIsArrayReverse +reverseIsArrayReverseTests :: Ref Int -> Effect Unit +reverseIsArrayReverseTests count = overAll count reverseIsArrayReverse where reverseIsArrayReverse :: forall a b t. TestableArrayF a b D0 t Result reverseIsArrayReverse (WithOffset _ xs) = @@ -379,8 +373,8 @@ reverseIsArrayReverseTests = overAll reverseIsArrayReverse in TA.toArray xs === ys -sortIsIdempotentTests :: Effect Unit -sortIsIdempotentTests = overAll sortIsIdempotent +sortIsIdempotentTests :: Ref Int -> Effect Unit +sortIsIdempotentTests count = overAll count sortIsIdempotent where sortIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result sortIsIdempotent (WithOffset _ xs) = @@ -393,8 +387,8 @@ sortIsIdempotentTests = overAll sortIsIdempotent in zs === ys -sortIsArraySortTests :: Effect Unit -sortIsArraySortTests = overAll sortIsArraySort +sortIsArraySortTests :: Ref Int -> Effect Unit +sortIsArraySortTests count = overAll count sortIsArraySort where sortIsArraySort :: forall a b t. TestableArrayF a b D0 t Result sortIsArraySort (WithOffset _ xs) = @@ -404,16 +398,16 @@ sortIsArraySortTests = overAll sortIsArraySort in TA.toArray xs === ys -toStringIsJoinWithCommaTests :: Effect Unit -toStringIsJoinWithCommaTests = overAll toStringIsJoinWithComma +toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit +toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma where toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result toStringIsJoinWithComma (WithOffset _ xs) = TA.toString' xs "," === TA.toString xs -setTypedOfSubArrayIsIdentityTests :: Effect Unit -setTypedOfSubArrayIsIdentityTests = overAll setTypedOfSubArrayIsIdentity +setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit +setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIdentity where setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result setTypedOfSubArrayIsIdentity (WithOffset _ xs) = @@ -425,8 +419,8 @@ setTypedOfSubArrayIsIdentityTests = overAll setTypedOfSubArrayIsIdentity in zs === ys -modifyingOriginalMutatesSubArrayTests :: Effect Unit -modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray +modifyingOriginalMutatesSubArrayTests :: Ref Int -> Effect Unit +modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMutatesSubArray where modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result modifyingOriginalMutatesSubArray (WithOffset _ xs) @@ -440,8 +434,8 @@ modifyingOriginalMutatesSubArrayTests = overAll modifyingOriginalMutatesSubArray in zs /== ys -modifyingSubArrayMutatesOriginalTests :: Effect Unit -modifyingSubArrayMutatesOriginalTests = overAll modifyingOriginalMutatesSubArray +modifyingSubArrayMutatesOriginalTests :: Ref Int -> Effect Unit +modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMutatesSubArray where modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result modifyingOriginalMutatesSubArray (WithOffset _ xs) @@ -455,8 +449,8 @@ modifyingSubArrayMutatesOriginalTests = overAll modifyingOriginalMutatesSubArray in zs /== ys -modifyingOriginalMutatesSubArrayZeroTests :: Effect Unit -modifyingOriginalMutatesSubArrayZeroTests = overAll modifyingOriginalMutatesSubArrayZero +modifyingOriginalMutatesSubArrayZeroTests :: Ref Int -> Effect Unit +modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOriginalMutatesSubArrayZero where modifyingOriginalMutatesSubArrayZero :: forall a b t. TestableArrayF a b D0 t Result modifyingOriginalMutatesSubArrayZero (WithOffset _ xs) @@ -470,8 +464,8 @@ modifyingOriginalMutatesSubArrayZeroTests = overAll modifyingOriginalMutatesSubA in zs /== ys -modifyingSubArrayMutatesOriginalZeroTests :: Effect Unit -modifyingSubArrayMutatesOriginalZeroTests = overAll modifyingSubArrayMutatesOriginalZero +modifyingSubArrayMutatesOriginalZeroTests :: Ref Int -> Effect Unit +modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArrayMutatesOriginalZero where modifyingSubArrayMutatesOriginalZero :: forall a b t. TestableArrayF a b D0 t Result modifyingSubArrayMutatesOriginalZero (WithOffset _ xs) @@ -485,8 +479,8 @@ modifyingSubArrayMutatesOriginalZeroTests = overAll modifyingSubArrayMutatesOrig in zs /== ys -modifyingOriginalMutatesSubArrayAllTests :: Effect Unit -modifyingOriginalMutatesSubArrayAllTests = overAll modifyingOriginalMutatesSubArrayAll +modifyingOriginalMutatesSubArrayAllTests :: Ref Int -> Effect Unit +modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginalMutatesSubArrayAll where modifyingOriginalMutatesSubArrayAll :: forall a b t. TestableArrayF a b D0 t Result modifyingOriginalMutatesSubArrayAll (WithOffset _ xs) @@ -500,8 +494,8 @@ modifyingOriginalMutatesSubArrayAllTests = overAll modifyingOriginalMutatesSubAr in zs /== ys -modifyingSubArrayMutatesOriginalAllTests :: Effect Unit -modifyingSubArrayMutatesOriginalAllTests = overAll modifyingSubArrayMutatesOriginalAll +modifyingSubArrayMutatesOriginalAllTests :: Ref Int -> Effect Unit +modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArrayMutatesOriginalAll where modifyingSubArrayMutatesOriginalAll :: forall a b t. TestableArrayF a b D0 t Result modifyingSubArrayMutatesOriginalAll (WithOffset _ xs) @@ -515,8 +509,8 @@ modifyingSubArrayMutatesOriginalAllTests = overAll modifyingSubArrayMutatesOrigi in zs /== ys -modifyingOriginalDoesntMutateSubArrayPartTests :: Effect Unit -modifyingOriginalDoesntMutateSubArrayPartTests = overAll modifyingOriginalMutatesSubArrayPart +modifyingOriginalDoesntMutateSubArrayPartTests :: Ref Int -> Effect Unit +modifyingOriginalDoesntMutateSubArrayPartTests count = overAll count modifyingOriginalMutatesSubArrayPart where modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalMutatesSubArrayPart (WithOffset os xs) @@ -533,8 +527,8 @@ modifyingOriginalDoesntMutateSubArrayPartTests = overAll modifyingOriginalMutate in zs === ys -modifyingOriginalDoesntMutateSubArrayPart2Tests :: Effect Unit -modifyingOriginalDoesntMutateSubArrayPart2Tests = overAll modifyingOriginalMutatesSubArrayPart2 +modifyingOriginalDoesntMutateSubArrayPart2Tests :: Ref Int -> Effect Unit +modifyingOriginalDoesntMutateSubArrayPart2Tests count = overAll count modifyingOriginalMutatesSubArrayPart2 where modifyingOriginalMutatesSubArrayPart2 :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalMutatesSubArrayPart2 (WithOffset os xs) @@ -551,8 +545,8 @@ modifyingOriginalDoesntMutateSubArrayPart2Tests = overAll modifyingOriginalMutat in zs === ys -modifyingOriginalDoesntMutateSliceTests :: Effect Unit -modifyingOriginalDoesntMutateSliceTests = overAll modifyingOriginalDoesntMutateSlice +modifyingOriginalDoesntMutateSliceTests :: Ref Int -> Effect Unit +modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalDoesntMutateSlice where modifyingOriginalDoesntMutateSlice :: forall a b t. TestableArrayF a b D0 t Result modifyingOriginalDoesntMutateSlice (WithOffset _ xs) @@ -566,8 +560,8 @@ modifyingOriginalDoesntMutateSliceTests = overAll modifyingOriginalDoesntMutateS in zs === ys -modifyingOriginalDoesntMutateSlicePartTests :: Effect Unit -modifyingOriginalDoesntMutateSlicePartTests = overAll modifyingOriginalDoesntMutateSlicePart +modifyingOriginalDoesntMutateSlicePartTests :: Ref Int -> Effect Unit +modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOriginalDoesntMutateSlicePart where modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) @@ -583,8 +577,8 @@ modifyingOriginalDoesntMutateSlicePartTests = overAll modifyingOriginalDoesntMut in zs === ys -modifyingOriginalDoesntMutateSlicePart2Tests :: Effect Unit -modifyingOriginalDoesntMutateSlicePart2Tests = overAll modifyingOriginalDoesntMutateSlicePart2 +modifyingOriginalDoesntMutateSlicePart2Tests :: Ref Int -> Effect Unit +modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOriginalDoesntMutateSlicePart2 where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) @@ -600,8 +594,8 @@ modifyingOriginalDoesntMutateSlicePart2Tests = overAll modifyingOriginalDoesntMu in zs === ys -copyWithinSelfIsIdentityTests :: Effect Unit -copyWithinSelfIsIdentityTests = overAll copyWithinSelfIsIdentity +copyWithinSelfIsIdentityTests :: Ref Int -> Effect Unit +copyWithinSelfIsIdentityTests count = overAll count copyWithinSelfIsIdentity where copyWithinSelfIsIdentity :: forall a b t. TestableArrayF a b D0 t Result copyWithinSelfIsIdentity (WithOffset _ xs) = @@ -612,8 +606,8 @@ copyWithinSelfIsIdentityTests = overAll copyWithinSelfIsIdentity in zs === ys -copyWithinIsSliceTests :: Effect Unit -copyWithinIsSliceTests = overAll copyWithinIsSlice +copyWithinIsSliceTests :: Ref Int -> Effect Unit +copyWithinIsSliceTests count = overAll count copyWithinIsSlice where copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result copyWithinIsSlice (WithOffset os xs) = @@ -625,8 +619,8 @@ copyWithinIsSliceTests = overAll copyWithinIsSlice in TA.toArray xs === ys <> zs -copyWithinViaSetTypedTests :: Effect Unit -copyWithinViaSetTypedTests = overAll copyWithinViaSetTyped +copyWithinViaSetTypedTests :: Ref Int -> Effect Unit +copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped where copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result copyWithinViaSetTyped (WithOffset os xs) = From 7148150c8cc31f050d243da4a1ec2ad3e49ef923 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 11:59:20 -0700 Subject: [PATCH 107/236] generating a data view with an offset, for a particular value --- src/Data/ArrayBuffer/DataView/Gen.purs | 39 ++++++++++++++++++++++---- src/Data/ArrayBuffer/Typed/Gen.purs | 4 +-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 23f775e..0c90698 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,12 +1,19 @@ module Data.ArrayBuffer.DataView.Gen where import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) -import Data.ArrayBuffer.DataView (whole) -import Data.ArrayBuffer.Types (DataView, ByteLength) +import Data.ArrayBuffer.DataView (whole, byteLength) +import Data.ArrayBuffer.Types (DataView, ByteLength, ByteOffset, kind ArrayViewType) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Prelude ((<$>)) -import Data.Maybe (Maybe) -import Control.Monad.Gen.Class (class MonadGen) +import Prelude ((<$>), bind, pure, (-), ($)) +import Data.Maybe (Maybe (Just)) +import Data.Vec (Vec) +import Data.Vec (fromArray) as Vec +import Data.Typelevel.Num (class Nat, toInt') +import Data.Unfoldable (replicateA) +import Control.Monad.Gen.Class (class MonadGen, chooseInt) +import Type.Proxy (Proxy (..)) +import Partial.Unsafe (unsafePartial) genDataView :: forall m @@ -15,3 +22,25 @@ genDataView :: forall m -> Maybe ByteLength -> m DataView genDataView a b = whole <$> genArrayBuffer a b + + + +-- | For generating some set of offsets residing inside the generated array +data WithOffset n (a :: ArrayViewType) = WithOffset (Vec n ByteOffset) DataView + +genWithOffset :: forall m n a b + . MonadGen m + => Nat n + => BytesPerValue a b + => Nat b + => m DataView -- ^ Assumes generated length is at least the minimum length of one value + -> m (WithOffset n a) +genWithOffset gen = do + let n = toInt' (Proxy :: Proxy n) + b = toInt' (Proxy :: Proxy b) + xs <- gen + let l = byteLength xs + mos <- replicateA n (chooseInt 0 (l - b)) + let os = unsafePartial $ case Vec.fromArray mos of + Just q -> q + pure (WithOffset os xs) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 2d8caad..faa0886 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -96,9 +96,9 @@ genWithOffset :: forall m n b a => BytesPerValue a b => m (ArrayView a) -> m (WithOffset n a) -genWithOffset genArrayView = do +genWithOffset gen = do let n = toInt' (Proxy :: Proxy n) - xs <- genArrayView + xs <- gen let l = TA.length xs mos <- replicateA n (chooseInt 0 (l - 1)) let os = unsafePartial $ case Vec.fromArray mos of From 4f455d3a4af965e3014850ac2f29c4e5fe23a0a4 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 12:15:21 -0700 Subject: [PATCH 108/236] properly using uint --- src/Data/ArrayBuffer/DataView.purs | 81 ++++++++++++++------------ src/Data/ArrayBuffer/Typed.purs | 6 +- src/Data/ArrayBuffer/Typed/Gen.purs | 10 ++-- src/Data/ArrayBuffer/ValueMapping.purs | 6 +- 4 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index b71d47e..7bd3dd6 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -40,19 +40,26 @@ module Data.ArrayBuffer.DataView , setFloat64le ) where +import Data.ArrayBuffer.Types + ( ByteOffset, DataView, ByteLength, ArrayBuffer, kind ArrayViewType + , Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) +import Data.ArrayBuffer.ValueMapping (class BinaryValue) + import Prelude (Unit, const, pure, (<$>)) -import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer) import Data.Maybe (Maybe(..)) +import Data.UInt (UInt) import Effect (Effect) import Effect.Exception (catchException) import Effect.Uncurried (EffectFn5, EffectFn3, EffectFn2, runEffectFn5, runEffectFn3, runEffectFn2) -- | Type for all fetching functions. -type Getter r = DataView -> ByteOffset -> Effect (Maybe r) +newtype Getter (a :: ArrayViewType) t = + Getter (DataView -> ByteOffset -> Effect (Maybe t)) -- | Type for all storing functions. -type Setter r = DataView -> r -> ByteOffset -> Effect Unit +newtype Setter (a :: ArrayViewType) t = + Setter (DataView -> t -> ByteOffset -> Effect Unit) -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView @@ -81,116 +88,116 @@ foreign import byteLength :: DataView -> ByteLength type Endianness = Boolean -foreign import getterImpl :: forall r. EffectFn5 String ByteLength Endianness DataView ByteOffset r +foreign import getterImpl :: forall t. EffectFn5 String ByteLength Endianness DataView ByteOffset t -getter :: forall r. String -> ByteLength -> Endianness -> Getter r -getter p l e d o = +getter :: forall a t. BinaryValue a t => String -> ByteLength -> Endianness -> Getter a t +getter p l e = Getter \d o -> let x = runEffectFn5 getterImpl p l e d o in catchException (const (pure Nothing)) (Just <$> x) -foreign import setterImpl :: forall r. EffectFn5 String Endianness DataView r ByteOffset Unit +foreign import setterImpl :: forall t. EffectFn5 String Endianness DataView t ByteOffset Unit -setter :: forall r. String -> Endianness -> Setter r -setter p e d x o = +setter :: forall a t. BinaryValue a t => String -> Endianness -> Setter a t +setter p e = Setter \d x o -> runEffectFn5 setterImpl p e d x o -- | Fetch int8 value at a certain index in a `DataView`. -getInt8 :: Getter Int +getInt8 :: Getter Int8 Int getInt8 = getter "getInt8" 1 false -- | Fetch int16 value at a certain index in a `DataView`. -getInt16be :: Getter Int +getInt16be :: Getter Int16 Int getInt16be = getter "getInt16" 2 false -getInt16le :: Getter Int +getInt16le :: Getter Int16 Int getInt16le = getter "getInt16" 2 true -- | Fetch int32 value at a certain index in a `DataView`. -getInt32be :: Getter Int +getInt32be :: Getter Int32 Int getInt32be = getter "getInt32" 4 false -getInt32le :: Getter Int +getInt32le :: Getter Int32 Int getInt32le = getter "getInt32" 4 true -- | Fetch uint8 value at a certain index in a `DataView`. -getUint8 :: Getter Int +getUint8 :: Getter Uint8 UInt getUint8 = getter "getUint8" 1 false -- | Fetch uint16 value at a certain index in a `DataView`. -getUint16be :: Getter Int +getUint16be :: Getter Uint16 UInt getUint16be = getter "getUint16" 2 false -getUint16le :: Getter Int +getUint16le :: Getter Uint16 UInt getUint16le = getter "getUint16" 2 true -- | Fetch uint32 value at a certain index in a `DataView`. -getUint32be :: Getter Number +getUint32be :: Getter Uint32 UInt getUint32be = getter "getUint32" 4 false -getUint32le :: Getter Number +getUint32le :: Getter Uint32 UInt getUint32le = getter "getUint32" 4 true -- | Fetch float32 value at a certain index in a `DataView`. -getFloat32be :: Getter Number +getFloat32be :: Getter Float32 Number getFloat32be = getter "getFloat32" 4 false -getFloat32le :: Getter Number +getFloat32le :: Getter Float32 Number getFloat32le = getter "getFloat32" 4 true -- | Fetch float64 value at a certain index in a `DataView`. -getFloat64be :: Getter Number +getFloat64be :: Getter Float64 Number getFloat64be = getter "getFloat64" 8 false -getFloat64le :: Getter Number +getFloat64le :: Getter Float64 Number getFloat64le = getter "getFloat64" 8 true -- | Store int8 value at a certain index in a `DataView`. -setInt8 :: Setter Int +setInt8 :: Setter Int8 Int setInt8 = setter "setInt8" false -- | Store int16 value at a certain index in a `DataView`. -setInt16be :: Setter Int +setInt16be :: Setter Int16 Int setInt16be = setter "setInt16" false -setInt16le :: Setter Int +setInt16le :: Setter Int16 Int setInt16le = setter "setInt16" true -- | Store int32 value at a certain index in a `DataView`. -setInt32be :: Setter Int +setInt32be :: Setter Int32 Int setInt32be = setter "setInt32" false -setInt32le :: Setter Int +setInt32le :: Setter Int32 Int setInt32le = setter "setInt32" true -- | Store uint8 value at a certain index in a `DataView`. -setUint8 :: Setter Int +setUint8 :: Setter Uint8 UInt setUint8 = setter "setUint8" false -- | Store uint16 value at a certain index in a `DataView`. -setUint16be :: Setter Int +setUint16be :: Setter Uint16 UInt setUint16be = setter "setUint16" false -setUint16le :: Setter Int +setUint16le :: Setter Uint16 UInt setUint16le = setter "setUint16" true -- | Store uint32 value at a certain index in a `DataView`. -setUint32be :: Setter Number +setUint32be :: Setter Uint32 UInt setUint32be = setter "setUint32" false -setUint32le :: Setter Number +setUint32le :: Setter Uint32 UInt setUint32le = setter "setUint32" true -- | Store float32 value at a certain index in a `DataView`. -setFloat32be :: Setter Number +setFloat32be :: Setter Float32 Number setFloat32be = setter "setFloat32" false -setFloat32le :: Setter Number +setFloat32le :: Setter Float32 Number setFloat32le = setter "setFloat32" true -- | Store float64 value at a certain index in a `DataView`. -setFloat64be :: Setter Number +setFloat64be :: Setter Float64 Number setFloat64be = setter "setFloat64" false -setFloat64le :: Setter Number +setFloat64le :: Setter Float64 Number setFloat64le = setter "setFloat64" true diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 44445f9..72608cc 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -178,7 +178,7 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh lastIndexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset -instance typedArrayUint8Clamped :: TypedArray Uint8Clamped Int where +instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable Nothing) part a x y = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable (Just y)) @@ -235,7 +235,7 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -instance typedArrayUint16 :: TypedArray Uint16 Int where +instance typedArrayUint16 :: TypedArray Uint16 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable Nothing) part a x y = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable (Just y)) @@ -263,7 +263,7 @@ instance typedArrayUint16 :: TypedArray Uint16 Int where findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -instance typedArrayUint8 :: TypedArray Uint8 Int where +instance typedArrayUint8 :: TypedArray Uint8 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable Nothing) part a x y = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable (Just y)) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index faa0886..c51662d 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -11,7 +11,7 @@ import Math as M import Data.Maybe (Maybe (..)) import Data.Int as I import Data.UInt (UInt) -import Data.UInt as UInt +import Data.UInt (fromInt, fromNumber) as UInt import Data.String.CodeUnits as S import Data.Float.Parse (parseFloat) import Data.Vec (Vec) @@ -42,16 +42,16 @@ genTypedArray q1 mq2 gen = sized \s -> -genUByte :: forall m. MonadGen m => m Int -genUByte = chooseInt 0 ((I.pow 2 8) - 1) +genUByte :: forall m. MonadGen m => m UInt +genUByte = UInt.fromInt <$> chooseInt 0 ((I.pow 2 8) - 1) genByte :: forall m. MonadGen m => m Int genByte = let j = I.pow 2 4 in chooseInt (negate j) (j - 1) -genUChomp :: forall m. MonadGen m => m Int -genUChomp = chooseInt 0 ((I.pow 2 16) - 1) +genUChomp :: forall m. MonadGen m => m UInt +genUChomp = UInt.fromInt <$> chooseInt 0 ((I.pow 2 16) - 1) genChomp :: forall m. MonadGen m => m Int genChomp = diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 73a0675..4aeac7e 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -23,10 +23,10 @@ instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t -instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped Int +instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt instance binaryValueUint32 :: BinaryValue Uint32 UInt -instance binaryValueUint16 :: BinaryValue Uint16 Int -instance binaryValueUint8 :: BinaryValue Uint8 Int +instance binaryValueUint16 :: BinaryValue Uint16 UInt +instance binaryValueUint8 :: BinaryValue Uint8 UInt instance binaryValueInt32 :: BinaryValue Int32 Int instance binaryValueInt16 :: BinaryValue Int16 Int instance binaryValueInt8 :: BinaryValue Int8 Int From 1dcde72f33ee63f3257f6d186eaa8aaf317e0468 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 12:17:23 -0700 Subject: [PATCH 109/236] runners --- src/Data/ArrayBuffer/DataView.purs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 7bd3dd6..1900807 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -9,6 +9,7 @@ module Data.ArrayBuffer.DataView , byteOffset , byteLength , Getter() + , runGetter , getInt8 , getInt16be , getInt32be @@ -24,6 +25,7 @@ module Data.ArrayBuffer.DataView , getFloat32le , getFloat64le , Setter() + , runSetter , setInt8 , setInt16be , setInt32be @@ -57,10 +59,18 @@ import Effect.Uncurried (EffectFn5, EffectFn3, EffectFn2, runEffectFn5, runEffec newtype Getter (a :: ArrayViewType) t = Getter (DataView -> ByteOffset -> Effect (Maybe t)) +runGetter :: forall a t. BinaryValue a t => Getter a t -> DataView -> ByteOffset -> Effect (Maybe t) +runGetter (Getter f) = f + + -- | Type for all storing functions. newtype Setter (a :: ArrayViewType) t = Setter (DataView -> t -> ByteOffset -> Effect Unit) +runSetter :: forall a t. BinaryValue a t => Setter a t -> DataView -> t -> ByteOffset -> Effect Unit +runSetter (Setter f) = f + + -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView From 27764883bfb4e12cb34a368951787e72456e349c Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 13:17:17 -0700 Subject: [PATCH 110/236] using type-declared value sizes --- src/Data/ArrayBuffer/DataView.purs | 206 +++++++++---------------- src/Data/ArrayBuffer/DataView/Gen.purs | 27 ++-- test/Main.purs | 5 +- test/Properties.purs | 15 +- test/Properties/DataView.purs | 56 ++++++- 5 files changed, 146 insertions(+), 163 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 1900807..4d737d6 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -8,67 +8,26 @@ module Data.ArrayBuffer.DataView , buffer , byteOffset , byteLength - , Getter() - , runGetter - , getInt8 - , getInt16be - , getInt32be - , getUint8 - , getUint16be - , getUint32be - , getFloat32be - , getFloat64be - , getInt16le - , getInt32le - , getUint16le - , getUint32le - , getFloat32le - , getFloat64le - , Setter() - , runSetter - , setInt8 - , setInt16be - , setInt32be - , setUint8 - , setUint16be - , setUint32be - , setFloat32be - , setFloat64be - , setInt16le - , setInt32le - , setUint16le - , setUint32le - , setFloat32le - , setFloat64le + , DVProxy (..), kind Endianness, BE, LE + , class DataView + , get, set ) where import Data.ArrayBuffer.Types ( ByteOffset, DataView, ByteLength, ArrayBuffer, kind ArrayViewType , Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) -import Data.ArrayBuffer.ValueMapping (class BinaryValue) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) import Prelude (Unit, const, pure, (<$>)) import Data.Maybe (Maybe(..)) import Data.UInt (UInt) +import Data.Typelevel.Num (toInt', class Nat) +import Type.Proxy (Proxy (..)) import Effect (Effect) import Effect.Exception (catchException) import Effect.Uncurried (EffectFn5, EffectFn3, EffectFn2, runEffectFn5, runEffectFn3, runEffectFn2) --- | Type for all fetching functions. -newtype Getter (a :: ArrayViewType) t = - Getter (DataView -> ByteOffset -> Effect (Maybe t)) - -runGetter :: forall a t. BinaryValue a t => Getter a t -> DataView -> ByteOffset -> Effect (Maybe t) -runGetter (Getter f) = f - - --- | Type for all storing functions. -newtype Setter (a :: ArrayViewType) t = - Setter (DataView -> t -> ByteOffset -> Effect Unit) - -runSetter :: forall a t. BinaryValue a t => Setter a t -> DataView -> t -> ByteOffset -> Effect Unit -runSetter (Setter f) = f -- | View mapping the whole `ArrayBuffer`. @@ -96,118 +55,93 @@ foreign import byteOffset :: DataView -> ByteOffset foreign import byteLength :: DataView -> ByteLength -type Endianness = Boolean -foreign import getterImpl :: forall t. EffectFn5 String ByteLength Endianness DataView ByteOffset t +data DVProxy (a :: ArrayViewType) (e :: Endianness) = DVProxy -getter :: forall a t. BinaryValue a t => String -> ByteLength -> Endianness -> Getter a t -getter p l e = Getter \d o -> - let x = runEffectFn5 getterImpl p l e d o - in catchException (const (pure Nothing)) (Just <$> x) - -foreign import setterImpl :: forall t. EffectFn5 String Endianness DataView t ByteOffset Unit - -setter :: forall a t. BinaryValue a t => String -> Endianness -> Setter a t -setter p e = Setter \d x o -> - runEffectFn5 setterImpl p e d x o +foreign import kind Endianness +foreign import data BE :: Endianness +foreign import data LE :: Endianness --- | Fetch int8 value at a certain index in a `DataView`. -getInt8 :: Getter Int8 Int -getInt8 = getter "getInt8" 1 false +class BinaryValue a t <= DataView (a :: ArrayViewType) (e :: Endianness) t | a -> t where + get :: DVProxy a e -> DataView -> ByteOffset -> Effect (Maybe t) + set :: DVProxy a e -> DataView -> t -> ByteOffset -> Effect Unit --- | Fetch int16 value at a certain index in a `DataView`. -getInt16be :: Getter Int16 Int -getInt16be = getter "getInt16" 2 false -getInt16le :: Getter Int16 Int -getInt16le = getter "getInt16" 2 true +foreign import getterImpl :: forall t. EffectFn5 String ByteLength Boolean DataView ByteOffset t --- | Fetch int32 value at a certain index in a `DataView`. -getInt32be :: Getter Int32 Int -getInt32be = getter "getInt32" 4 false - -getInt32le :: Getter Int32 Int -getInt32le = getter "getInt32" 4 true - --- | Fetch uint8 value at a certain index in a `DataView`. -getUint8 :: Getter Uint8 UInt -getUint8 = getter "getUint8" 1 false - --- | Fetch uint16 value at a certain index in a `DataView`. -getUint16be :: Getter Uint16 UInt -getUint16be = getter "getUint16" 2 false - -getUint16le :: Getter Uint16 UInt -getUint16le = getter "getUint16" 2 true - --- | Fetch uint32 value at a certain index in a `DataView`. -getUint32be :: Getter Uint32 UInt -getUint32be = getter "getUint32" 4 false +getter :: forall t. String -> ByteLength -> Boolean -> DataView -> ByteOffset -> Effect (Maybe t) +getter p l e = \d o -> + let x = runEffectFn5 getterImpl p l e d o + in catchException (const (pure Nothing)) (Just <$> x) -getUint32le :: Getter Uint32 UInt -getUint32le = getter "getUint32" 4 true +foreign import setterImpl :: forall t. EffectFn5 String Boolean DataView t ByteOffset Unit --- | Fetch float32 value at a certain index in a `DataView`. -getFloat32be :: Getter Float32 Number -getFloat32be = getter "getFloat32" 4 false +setter :: forall t. String -> Boolean -> DataView -> t -> ByteOffset -> Effect Unit +setter p e = \d x o -> + runEffectFn5 setterImpl p e d x o -getFloat32le :: Getter Float32 Number -getFloat32le = getter "getFloat32" 4 true --- | Fetch float64 value at a certain index in a `DataView`. -getFloat64be :: Getter Float64 Number -getFloat64be = getter "getFloat64" 8 false +instance dataViewInt8BE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int where + get DVProxy = getter "getInt8" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setInt8" false -getFloat64le :: Getter Float64 Number -getFloat64le = getter "getFloat64" 8 true +instance dataViewInt8LE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int where + get DVProxy = getter "getInt8" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setInt8" true --- | Store int8 value at a certain index in a `DataView`. -setInt8 :: Setter Int8 Int -setInt8 = setter "setInt8" false +instance dataViewInt16BE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int where + get DVProxy = getter "getInt16" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setInt16" false --- | Store int16 value at a certain index in a `DataView`. -setInt16be :: Setter Int16 Int -setInt16be = setter "setInt16" false +instance dataViewInt16LE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int where + get DVProxy = getter "getInt16" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setInt16" true -setInt16le :: Setter Int16 Int -setInt16le = setter "setInt16" true +instance dataViewInt32BE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int where + get DVProxy = getter "getInt32" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setInt32" false --- | Store int32 value at a certain index in a `DataView`. -setInt32be :: Setter Int32 Int -setInt32be = setter "setInt32" false +instance dataViewInt32LE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int where + get DVProxy = getter "getInt32" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setInt32" true -setInt32le :: Setter Int32 Int -setInt32le = setter "setInt32" true +instance dataViewUint8BE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt where + get DVProxy = getter "getUint8" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setUint8" false --- | Store uint8 value at a certain index in a `DataView`. -setUint8 :: Setter Uint8 UInt -setUint8 = setter "setUint8" false +instance dataViewUint8LE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt where + get DVProxy = getter "getUint8" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setUint8" true --- | Store uint16 value at a certain index in a `DataView`. -setUint16be :: Setter Uint16 UInt -setUint16be = setter "setUint16" false +instance dataViewUint16BE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt where + get DVProxy = getter "getUint16" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setUint16" false -setUint16le :: Setter Uint16 UInt -setUint16le = setter "setUint16" true +instance dataViewUint16LE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt where + get DVProxy = getter "getUint16" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setUint16" true --- | Store uint32 value at a certain index in a `DataView`. -setUint32be :: Setter Uint32 UInt -setUint32be = setter "setUint32" false +instance dataViewUint32BE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt where + get DVProxy = getter "getUint32" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setUint32" false -setUint32le :: Setter Uint32 UInt -setUint32le = setter "setUint32" true +instance dataViewUint32LE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt where + get DVProxy = getter "getUint32" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setUint32" true --- | Store float32 value at a certain index in a `DataView`. -setFloat32be :: Setter Float32 Number -setFloat32be = setter "setFloat32" false +instance dataViewFloat32BE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number where + get DVProxy = getter "getFloat32" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setFloat32" false -setFloat32le :: Setter Float32 Number -setFloat32le = setter "setFloat32" true +instance dataViewFloat32LE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number where + get DVProxy = getter "getFloat32" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setFloat32" true --- | Store float64 value at a certain index in a `DataView`. -setFloat64be :: Setter Float64 Number -setFloat64be = setter "setFloat64" false +instance dataViewFloat64BE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number where + get DVProxy = getter "getFloat64" (toInt' (Proxy :: Proxy b)) false + set DVProxy = setter "setFloat64" false -setFloat64le :: Setter Float64 Number -setFloat64le = setter "setFloat64" true +instance dataViewFloat64LE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number where + get DVProxy = getter "getFloat64" (toInt' (Proxy :: Proxy b)) true + set DVProxy = setter "setFloat64" true diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 0c90698..4749324 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -3,7 +3,7 @@ module Data.ArrayBuffer.DataView.Gen where import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole, byteLength) import Data.ArrayBuffer.Types (DataView, ByteLength, ByteOffset, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) import Prelude ((<$>), bind, pure, (-), ($)) import Data.Maybe (Maybe (Just)) @@ -26,16 +26,18 @@ genDataView a b = whole <$> genArrayBuffer a b -- | For generating some set of offsets residing inside the generated array -data WithOffset n (a :: ArrayViewType) = WithOffset (Vec n ByteOffset) DataView - -genWithOffset :: forall m n a b - . MonadGen m - => Nat n - => BytesPerValue a b - => Nat b - => m DataView -- ^ Assumes generated length is at least the minimum length of one value - -> m (WithOffset n a) -genWithOffset gen = do +data WithOffsetAndValue n (a :: ArrayViewType) t = WithOffsetAndValue (Vec n ByteOffset) t DataView + +genWithOffsetAndValue :: forall m n a b t + . MonadGen m + => Nat n + => BytesPerValue a b + => BinaryValue a t + => Nat b + => m DataView -- ^ Assumes generated length is at least the minimum length of one value + -> m t + -> m (WithOffsetAndValue n a t) +genWithOffsetAndValue gen genT = do let n = toInt' (Proxy :: Proxy n) b = toInt' (Proxy :: Proxy b) xs <- gen @@ -43,4 +45,5 @@ genWithOffset gen = do mos <- replicateA n (chooseInt 0 (l - b)) let os = unsafePartial $ case Vec.fromArray mos of Just q -> q - pure (WithOffset os xs) + t <- genT + pure (WithOffsetAndValue os t xs) diff --git a/test/Main.purs b/test/Main.purs index 38cc552..8296991 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -12,13 +12,10 @@ import Effect.Ref as Ref main :: Effect Unit main = do - count <- Ref.new 0 log "Starting tests..." - propertiesTests count + propertiesTests - c <- Ref.read count - log $ "Verified " <> show c <> " properties, generating " <> show (c * 900) <> " test cases." -- import Data.ArrayBuffer.ArrayBuffer as AB diff --git a/test/Properties.purs b/test/Properties.purs index 22156bf..4b1b9bb 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -3,15 +3,22 @@ module Test.Properties where import Test.Properties.TypedArray (typedArrayTests) import Test.Properties.DataView (dataViewTests) -import Prelude (Unit, bind, discard) +import Prelude (Unit, bind, discard, ($), (<>), (*), show) import Effect (Effect) -import Effect.Ref (Ref) +import Effect.Ref (new, read) as Ref import Effect.Console (log) -propertiesTests :: Ref Int -> Effect Unit -propertiesTests count = do +propertiesTests :: Effect Unit +propertiesTests = do + count <- Ref.new 0 log " - TypedArray Tests:" typedArrayTests count + c <- Ref.read count + log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." + + count <- Ref.new 0 log " - DataView Tests:" dataViewTests count + c <- Ref.read count + log $ " - Verified " <> show c <> " properties, generating " <> show (c * 8 * 100) <> " test cases." diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 897a2af..603a4cd 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -1,14 +1,27 @@ module Test.Properties.DataView where -import Data.ArrayBuffer.Types (DataView) -import Data.ArrayBuffer.DataView as DA -import Data.ArrayBuffer.DataView.Gen (genDataView) +import Data.ArrayBuffer.Types + ( DataView + , Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) +import Data.ArrayBuffer.DataView as DV +import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue (..)) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) +import Data.ArrayBuffer.Typed.Gen + (genUWord, genWord, genUChomp, genChomp, genUByte, genByte, genFloat32, genFloat64) import Prelude +import Data.Vec (head) as Vec +import Data.UInt (UInt) +import Data.Maybe (Maybe (..)) +import Data.Typelevel.Num (class Nat, D1) import Effect (Effect) +import Effect.Unsafe (unsafePerformEffect) +import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref +import Test.QuickCheck (class Testable, quickCheckGen, class Arbitrary, arbitrary, Result, (===)) +import Test.QuickCheck.Gen (Gen) @@ -17,11 +30,40 @@ dataViewTests count = do pure unit -type TestableViewF q = - DataView +type TestableViewF a b n t q = + Show t + => Eq t + => Ord t + => Semiring t + => Arbitrary t + => BinaryValue a t + => BytesPerValue a b + => Nat b + => WithOffsetAndValue n a t -> q -overAll :: Ref Int -> Effect Unit -overAll count = do +overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableViewF a b n t q) -> Effect Unit +overAll count f = do void (Ref.modify (\x -> x + 1) count) + log " - Uint32" + quickCheckGen (f <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord :: Gen (WithOffsetAndValue n Uint32 UInt))) + log " - Uint16" + log " - Uint8" + log " - Int32" + log " - Int16" + log " - Int8" + log " - Float32" + log " - Float64" + + +placingAValueIsThereTests :: forall a t. Ref Int -> DV.Getter a t -> DV.Setter a t -> Effect Unit +placingAValueIsThereTests count getter setter = overAll count placingAValueIsThere + where + placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result + placingAValueIsThere (WithOffsetAndValue os t xs) = + let o = Vec.head os + in unsafePerformEffect do + DV.runSetter setter xs t o + my <- DV.runGetter getter xs o + pure (my === Just t) From 5ce319b9203287b2970bc25d4f95d69b7b8e647f Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 13:37:38 -0700 Subject: [PATCH 111/236] verified data views --- src/Data/ArrayBuffer/DataView/Gen.purs | 13 +-- test/Properties.purs | 2 +- test/Properties/DataView.purs | 112 ++++++++++++++++++++----- 3 files changed, 100 insertions(+), 27 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 4749324..a8bf099 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,9 +1,9 @@ module Data.ArrayBuffer.DataView.Gen where import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) -import Data.ArrayBuffer.DataView (whole, byteLength) +import Data.ArrayBuffer.DataView (whole, byteLength, class DataView, kind Endianness) import Data.ArrayBuffer.Types (DataView, ByteLength, ByteOffset, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Prelude ((<$>), bind, pure, (-), ($)) import Data.Maybe (Maybe (Just)) @@ -26,17 +26,18 @@ genDataView a b = whole <$> genArrayBuffer a b -- | For generating some set of offsets residing inside the generated array -data WithOffsetAndValue n (a :: ArrayViewType) t = WithOffsetAndValue (Vec n ByteOffset) t DataView +data WithOffsetAndValue n (a :: ArrayViewType) (e :: Endianness) t = + WithOffsetAndValue (Vec n ByteOffset) t DataView -genWithOffsetAndValue :: forall m n a b t +genWithOffsetAndValue :: forall m n a b e t . MonadGen m => Nat n => BytesPerValue a b - => BinaryValue a t + => DataView a e t => Nat b => m DataView -- ^ Assumes generated length is at least the minimum length of one value -> m t - -> m (WithOffsetAndValue n a t) + -> m (WithOffsetAndValue n a e t) genWithOffsetAndValue gen genT = do let n = toInt' (Proxy :: Proxy n) b = toInt' (Proxy :: Proxy b) diff --git a/test/Properties.purs b/test/Properties.purs index 4b1b9bb..441e3fb 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -21,4 +21,4 @@ propertiesTests = do log " - DataView Tests:" dataViewTests count c <- Ref.read count - log $ " - Verified " <> show c <> " properties, generating " <> show (c * 8 * 100) <> " test cases." + log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 603a4cd..c0e6ff3 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -14,7 +14,7 @@ import Prelude import Data.Vec (head) as Vec import Data.UInt (UInt) import Data.Maybe (Maybe (..)) -import Data.Typelevel.Num (class Nat, D1) +import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) @@ -27,43 +27,115 @@ import Test.QuickCheck.Gen (Gen) dataViewTests :: Ref Int -> Effect Unit dataViewTests count = do - pure unit + log " - set x o => get o === Just x" + placingAValueIsThereTests count -type TestableViewF a b n t q = +type TestableViewF a b n e t q = Show t => Eq t => Ord t => Semiring t => Arbitrary t - => BinaryValue a t => BytesPerValue a b => Nat b - => WithOffsetAndValue n a t + => DV.DataView a e t + => WithOffsetAndValue n a e t -> q -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableViewF a b n t q) -> Effect Unit +overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b e t. TestableViewF a b n e t q) -> Effect Unit overAll count f = do void (Ref.modify (\x -> x + 1) count) - log " - Uint32" - quickCheckGen (f <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord :: Gen (WithOffsetAndValue n Uint32 UInt))) - log " - Uint16" - log " - Uint8" - log " - Int32" - log " - Int16" - log " - Int8" - log " - Float32" - log " - Float64" + log " - Uint32 BE" + quickCheckGen $ + let f' :: TestableViewF Uint32 D4 n DV.BE UInt q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord)) + log " - Uint32 LE" + quickCheckGen $ + let f' :: TestableViewF Uint32 D4 n DV.LE UInt q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord)) + log " - Uint16 BE" + quickCheckGen $ + let f' :: TestableViewF Uint16 D2 n DV.BE UInt q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUChomp)) + log " - Uint16 LE" + quickCheckGen $ + let f' :: TestableViewF Uint16 D2 n DV.LE UInt q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUChomp)) + log " - Uint8 BE" + quickCheckGen $ + let f' :: TestableViewF Uint8 D1 n DV.BE UInt q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUByte)) + log " - Uint8 LE" + quickCheckGen $ + let f' :: TestableViewF Uint8 D1 n DV.LE UInt q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUByte)) + log " - Int32 BE" + quickCheckGen $ + let f' :: TestableViewF Int32 D4 n DV.BE Int q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genWord)) + log " - Int32 LE" + quickCheckGen $ + let f' :: TestableViewF Int32 D4 n DV.LE Int q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genWord)) + log " - Int16 BE" + quickCheckGen $ + let f' :: TestableViewF Int16 D2 n DV.BE Int q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genChomp)) + log " - Int16 LE" + quickCheckGen $ + let f' :: TestableViewF Int16 D2 n DV.LE Int q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genChomp)) + log " - Int8 BE" + quickCheckGen $ + let f' :: TestableViewF Int8 D1 n DV.BE Int q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genByte)) + log " - Int8 LE" + quickCheckGen $ + let f' :: TestableViewF Int8 D1 n DV.LE Int q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genByte)) + log " - Float32 BE" + quickCheckGen $ + let f' :: TestableViewF Float32 D4 n DV.BE Number q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat32)) + log " - Float32 LE" + quickCheckGen $ + let f' :: TestableViewF Float32 D4 n DV.LE Number q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat32)) + log " - Float64 BE" + quickCheckGen $ + let f' :: TestableViewF Float64 D8 n DV.BE Number q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat64)) + log " - Float64 LE" + quickCheckGen $ + let f' :: TestableViewF Float64 D8 n DV.LE Number q + f' = f + in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat64)) -placingAValueIsThereTests :: forall a t. Ref Int -> DV.Getter a t -> DV.Setter a t -> Effect Unit -placingAValueIsThereTests count getter setter = overAll count placingAValueIsThere +placingAValueIsThereTests :: Ref Int -> Effect Unit +placingAValueIsThereTests count = overAll count placingAValueIsThere where - placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result + placingAValueIsThere :: forall a b e t. TestableViewF a b D1 e t Result placingAValueIsThere (WithOffsetAndValue os t xs) = let o = Vec.head os in unsafePerformEffect do - DV.runSetter setter xs t o - my <- DV.runGetter getter xs o + DV.set (DV.DVProxy :: DV.DVProxy a e) xs t o + my <- DV.get (DV.DVProxy :: DV.DVProxy a e) xs o pure (my === Just t) From 2fe5c9a70a6b1decb47938a0ec6e9889e154730b Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 14:06:49 -0700 Subject: [PATCH 112/236] cosmetic --- src/Data/ArrayBuffer/Typed.purs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 72608cc..aa5309c 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -520,6 +520,9 @@ subArray a mz = case mz of Just e -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable (Just e)) +-- FIXME ^ deliberately just create a new typed array from the previous one's buffer? + + -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String From e4aed3a9901eb9cd4d1f654d142d2200e1e8827d Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 14:48:53 -0700 Subject: [PATCH 113/236] better docs, noting how subArray / new use Data.ArrayBuffer.ArrayBuffer.slice --- .../Data/ArrayBuffer/ArrayBuffer/Gen.md | 9 + generated-docs/Data/ArrayBuffer/DataView.md | 235 ++++-------------- .../Data/ArrayBuffer/DataView/Gen.md | 24 ++ generated-docs/Data/ArrayBuffer/Show.md | 9 - generated-docs/Data/ArrayBuffer/Typed.md | 56 +---- generated-docs/Data/ArrayBuffer/Typed/Gen.md | 56 +---- .../Data/ArrayBuffer/ValueMapping.md | 48 ++++ src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 4 +- src/Data/ArrayBuffer/DataView/Gen.purs | 6 +- src/Data/ArrayBuffer/Typed.purs | 32 +-- src/Data/ArrayBuffer/Typed/Gen.purs | 2 +- src/Data/ArrayBuffer/ValueMapping.purs | 5 + 12 files changed, 162 insertions(+), 324 deletions(-) create mode 100644 generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md create mode 100644 generated-docs/Data/ArrayBuffer/DataView/Gen.md delete mode 100644 generated-docs/Data/ArrayBuffer/Show.md create mode 100644 generated-docs/Data/ArrayBuffer/ValueMapping.md diff --git a/generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md b/generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md new file mode 100644 index 0000000..da93609 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md @@ -0,0 +1,9 @@ +## Module Data.ArrayBuffer.ArrayBuffer.Gen + +#### `genArrayBuffer` + +``` purescript +genArrayBuffer :: forall m. MonadGen m => ByteLength -> Maybe ByteLength -> m ArrayBuffer +``` + + diff --git a/generated-docs/Data/ArrayBuffer/DataView.md b/generated-docs/Data/ArrayBuffer/DataView.md index e6d3eb3..68fc377 100644 --- a/generated-docs/Data/ArrayBuffer/DataView.md +++ b/generated-docs/Data/ArrayBuffer/DataView.md @@ -51,220 +51,81 @@ byteLength :: DataView -> ByteLength Represents the length of this view. -#### `Getter` +#### `DVProxy` ``` purescript -type Getter r = DataView -> ByteOffset -> Effect (Maybe r) +data DVProxy (a :: ArrayViewType) (e :: Endianness) + = DVProxy ``` -Type for all fetching functions. - -#### `getInt8` - -``` purescript -getInt8 :: Getter Int -``` - -Fetch int8 value at a certain index in a `DataView`. - -#### `getInt16be` - -``` purescript -getInt16be :: Getter Int -``` - -Fetch int16 value at a certain index in a `DataView`. - -#### `getInt32be` - -``` purescript -getInt32be :: Getter Int -``` - -Fetch int32 value at a certain index in a `DataView`. - -#### `getUint8` - -``` purescript -getUint8 :: Getter Int -``` - -Fetch uint8 value at a certain index in a `DataView`. - -#### `getUint16be` +#### `Endianness` ``` purescript -getUint16be :: Getter Int +kind Endianness ``` -Fetch uint16 value at a certain index in a `DataView`. - -#### `getUint32be` +#### `BE` ``` purescript -getUint32be :: Getter Number +data BE :: Endianness ``` -Fetch uint32 value at a certain index in a `DataView`. - -#### `getFloat32be` - +##### Instances ``` purescript -getFloat32be :: Getter Number +(BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int +(BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int +(BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int +(BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt +(BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt +(BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt +(BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number +(BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number ``` -Fetch float32 value at a certain index in a `DataView`. - -#### `getFloat64be` +#### `LE` ``` purescript -getFloat64be :: Getter Number +data LE :: Endianness ``` -Fetch float64 value at a certain index in a `DataView`. - -#### `getInt16le` - +##### Instances ``` purescript -getInt16le :: Getter Int +(BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int +(BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int +(BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int +(BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt +(BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt +(BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt +(BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number +(BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number ``` -#### `getInt32le` +#### `DataView` ``` purescript -getInt32le :: Getter Int +class (BinaryValue a t) <= DataView (a :: ArrayViewType) (e :: Endianness) t | a -> t where + get :: DVProxy a e -> DataView -> ByteOffset -> Effect (Maybe t) + set :: DVProxy a e -> DataView -> t -> ByteOffset -> Effect Unit ``` -#### `getUint16le` - -``` purescript -getUint16le :: Getter Int -``` - -#### `getUint32le` - -``` purescript -getUint32le :: Getter Number -``` - -#### `getFloat32le` - -``` purescript -getFloat32le :: Getter Number -``` - -#### `getFloat64le` - -``` purescript -getFloat64le :: Getter Number -``` - -#### `Setter` - -``` purescript -type Setter r = DataView -> r -> ByteOffset -> Effect Unit -``` - -Type for all storing functions. - -#### `setInt8` - -``` purescript -setInt8 :: Setter Int -``` - -Store int8 value at a certain index in a `DataView`. - -#### `setInt16be` - -``` purescript -setInt16be :: Setter Int -``` - -Store int16 value at a certain index in a `DataView`. - -#### `setInt32be` - -``` purescript -setInt32be :: Setter Int -``` - -Store int32 value at a certain index in a `DataView`. - -#### `setUint8` - -``` purescript -setUint8 :: Setter Int -``` - -Store uint8 value at a certain index in a `DataView`. - -#### `setUint16be` - -``` purescript -setUint16be :: Setter Int -``` - -Store uint16 value at a certain index in a `DataView`. - -#### `setUint32be` - -``` purescript -setUint32be :: Setter Number -``` - -Store uint32 value at a certain index in a `DataView`. - -#### `setFloat32be` - -``` purescript -setFloat32be :: Setter Number -``` - -Store float32 value at a certain index in a `DataView`. - -#### `setFloat64be` - -``` purescript -setFloat64be :: Setter Number -``` - -Store float64 value at a certain index in a `DataView`. - -#### `setInt16le` - -``` purescript -setInt16le :: Setter Int -``` - -#### `setInt32le` - -``` purescript -setInt32le :: Setter Int -``` - -#### `setUint16le` - -``` purescript -setUint16le :: Setter Int -``` - -#### `setUint32le` - -``` purescript -setUint32le :: Setter Number -``` - -#### `setFloat32le` - -``` purescript -setFloat32le :: Setter Number -``` - -#### `setFloat64le` - +##### Instances ``` purescript -setFloat64le :: Setter Number +(BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int +(BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int +(BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int +(BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int +(BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int +(BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int +(BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt +(BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt +(BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt +(BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt +(BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt +(BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt +(BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number +(BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number +(BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number +(BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number ``` diff --git a/generated-docs/Data/ArrayBuffer/DataView/Gen.md b/generated-docs/Data/ArrayBuffer/DataView/Gen.md new file mode 100644 index 0000000..f55ffe0 --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/DataView/Gen.md @@ -0,0 +1,24 @@ +## Module Data.ArrayBuffer.DataView.Gen + +#### `genDataView` + +``` purescript +genDataView :: forall m. MonadGen m => ByteLength -> Maybe ByteLength -> m DataView +``` + +#### `WithOffsetAndValue` + +``` purescript +data WithOffsetAndValue n (a :: ArrayViewType) (e :: Endianness) t + = WithOffsetAndValue (Vec n ByteOffset) t DataView +``` + +For generating some set of offsets residing inside the generated array, with some computable value + +#### `genWithOffsetAndValue` + +``` purescript +genWithOffsetAndValue :: forall m n a b e t. MonadGen m => Nat n => BytesPerValue a b => DataView a e t => Nat b => m DataView -> m t -> m (WithOffsetAndValue n a e t) +``` + + diff --git a/generated-docs/Data/ArrayBuffer/Show.md b/generated-docs/Data/ArrayBuffer/Show.md deleted file mode 100644 index 88a5008..0000000 --- a/generated-docs/Data/ArrayBuffer/Show.md +++ /dev/null @@ -1,9 +0,0 @@ -## Module Data.ArrayBuffer.Show - -#### `showViaInspect` - -``` purescript -showViaInspect :: forall a. ArrayView a -> String -``` - - diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md index a1c5795..ec43161 100644 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -57,32 +57,13 @@ Represents the length of this typed array, in bytes. length :: forall a b. BytesPerValue a b => ArrayView a -> Int ``` -#### `BytesPerValue` - -``` purescript -class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b -``` - -##### Instances -``` purescript -BytesPerValue Uint8Clamped D1 -BytesPerValue Uint32 D4 -BytesPerValue Uint16 D2 -BytesPerValue Uint8 D1 -BytesPerValue Int32 D4 -BytesPerValue Int16 D2 -BytesPerValue Int8 D1 -BytesPerValue Float32 D4 -BytesPerValue Float64 D8 -``` - #### `TypedArray` ``` purescript -class TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where +class (BinaryValue a t) <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where whole :: ArrayBuffer -> ArrayView a - remainder :: ArrayBuffer -> Offset -> Effect (ArrayView a) - part :: ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) + remainder :: ArrayBuffer -> ByteOffset -> Effect (ArrayView a) + part :: ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) empty :: Length -> ArrayView a fromArray :: Array t -> ArrayView a fill :: ArrayView a -> t -> Maybe (Tuple Offset (Maybe Offset)) -> Effect Unit @@ -110,7 +91,10 @@ Typeclass that associates a measured user-level type with a typed array. #### Creation - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface - on top of an existing `ArrayBuffer`. + on top of an existing `ArrayBuffer` - Note, `part` and `remainder` may behave unintuitively - + when the operation is isomorphic to `whole`, the new TypedArray uses the same buffer as the input, + but not when the portion is a sub-array of the original buffer, a new one is made with + `Data.ArrayBuffer.ArrayBuffer.slice`. - `empty` and `fromArray` are methods for creating pure typed arrays #### Modification @@ -136,10 +120,10 @@ Typeclass that associates a measured user-level type with a typed array. ##### Instances ``` purescript -TypedArray Uint8Clamped Int +TypedArray Uint8Clamped UInt TypedArray Uint32 UInt -TypedArray Uint16 Int -TypedArray Uint8 Int +TypedArray Uint16 UInt +TypedArray Uint8 UInt TypedArray Int32 Int TypedArray Int16 Int TypedArray Int8 Int @@ -239,25 +223,7 @@ Returns a new typed array view of the same buffer, beginning at the index and en is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a mutable replica of the original array - the sub-array reference reflects mutations to the original array. However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves -purely. - -**tl;dr**: if you want a duplicate reference of the same typed array, consider either of the following: - -``` -y :: ArrayView _ -y = x - -y' :: ArrayView _ -y' = subArray x Nothing - -y'' :: ArrayView _ -y'' = subArray x (Just (Tuple 0 Nothing)) - -y''' :: ArrayView _ -y''' = subArray x (Just (Tuple 0 (Just (length x)))) -``` - -Otherwise, you'll get an _image_ of the array at the moment, like `slice`. +purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. #### `toString` diff --git a/generated-docs/Data/ArrayBuffer/Typed/Gen.md b/generated-docs/Data/ArrayBuffer/Typed/Gen.md index 45dabfd..82d6133 100644 --- a/generated-docs/Data/ArrayBuffer/Typed/Gen.md +++ b/generated-docs/Data/ArrayBuffer/Typed/Gen.md @@ -2,64 +2,16 @@ Functions for generating typed arrays and values. -#### `genUint8ClampedArray` +#### `genTypedArray` ``` purescript -genUint8ClampedArray :: forall m. MonadGen m => m Uint8ClampedArray -``` - -#### `genUint32Array` - -``` purescript -genUint32Array :: forall m. MonadGen m => m Uint32Array -``` - -#### `genUint16Array` - -``` purescript -genUint16Array :: forall m. MonadGen m => m Uint16Array -``` - -#### `genUint8Array` - -``` purescript -genUint8Array :: forall m. MonadGen m => m Uint8Array -``` - -#### `genInt32Array` - -``` purescript -genInt32Array :: forall m. MonadGen m => m Int32Array -``` - -#### `genInt16Array` - -``` purescript -genInt16Array :: forall m. MonadGen m => m Int16Array -``` - -#### `genInt8Array` - -``` purescript -genInt8Array :: forall m. MonadGen m => m Int8Array -``` - -#### `genFloat32Array` - -``` purescript -genFloat32Array :: forall m. MonadGen m => m Float32Array -``` - -#### `genFloat64Array` - -``` purescript -genFloat64Array :: forall m. MonadGen m => m Float64Array +genTypedArray :: forall m a t. MonadGen m => TypedArray a t => Length -> Maybe Length -> m t -> m (ArrayView a) ``` #### `genUByte` ``` purescript -genUByte :: forall m. MonadGen m => m Int +genUByte :: forall m. MonadGen m => m UInt ``` #### `genByte` @@ -71,7 +23,7 @@ genByte :: forall m. MonadGen m => m Int #### `genUChomp` ``` purescript -genUChomp :: forall m. MonadGen m => m Int +genUChomp :: forall m. MonadGen m => m UInt ``` #### `genChomp` diff --git a/generated-docs/Data/ArrayBuffer/ValueMapping.md b/generated-docs/Data/ArrayBuffer/ValueMapping.md new file mode 100644 index 0000000..631e73d --- /dev/null +++ b/generated-docs/Data/ArrayBuffer/ValueMapping.md @@ -0,0 +1,48 @@ +## Module Data.ArrayBuffer.ValueMapping + +This module represents type-level mappings between `ArrayViewType`s +and meaningful data. + +#### `BytesPerValue` + +``` purescript +class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b +``` + +Maps a `TypedArray`'s binary casted value, to the space occupied by that value, in bytes. + +##### Instances +``` purescript +BytesPerValue Uint8Clamped D1 +BytesPerValue Uint32 D4 +BytesPerValue Uint16 D2 +BytesPerValue Uint8 D1 +BytesPerValue Int32 D4 +BytesPerValue Int16 D2 +BytesPerValue Int8 D1 +BytesPerValue Float32 D4 +BytesPerValue Float64 D8 +``` + +#### `BinaryValue` + +``` purescript +class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t +``` + +Maps a `TypedArray`'s binary casted value, to its computable representation in JavaScript. + +##### Instances +``` purescript +BinaryValue Uint8Clamped UInt +BinaryValue Uint32 UInt +BinaryValue Uint16 UInt +BinaryValue Uint8 UInt +BinaryValue Int32 Int +BinaryValue Int16 Int +BinaryValue Int8 Int +BinaryValue Float32 Number +BinaryValue Float64 Number +``` + + diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index 686f503..125072a 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -11,7 +11,7 @@ import Control.Monad.Gen.Class (class MonadGen) genArrayBuffer :: forall m . MonadGen m - => ByteLength - -> Maybe ByteLength + => ByteLength -- ^ Min length + -> Maybe ByteLength -- ^ Max length -> m ArrayBuffer genArrayBuffer a b = buffer <$> (genTypedArray a b genUByte :: m Uint8Array) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index a8bf099..4ac9a18 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -18,14 +18,14 @@ import Partial.Unsafe (unsafePartial) genDataView :: forall m . MonadGen m - => ByteLength - -> Maybe ByteLength + => ByteLength -- ^ Min length + -> Maybe ByteLength -- ^ Max length -> m DataView genDataView a b = whole <$> genArrayBuffer a b --- | For generating some set of offsets residing inside the generated array +-- | For generating some set of offsets residing inside the generated array, with some computable value data WithOffsetAndValue n (a :: ArrayViewType) (e :: Endianness) t = WithOffsetAndValue (Vec n ByteOffset) t DataView diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index aa5309c..e4c8979 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -106,7 +106,10 @@ type Length = Int -- | #### Creation -- | -- | - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface --- | on top of an existing `ArrayBuffer`. +-- | on top of an existing `ArrayBuffer` - Note, `part` and `remainder` may behave unintuitively - +-- | when the operation is isomorphic to `whole`, the new TypedArray uses the same buffer as the input, +-- | but not when the portion is a sub-array of the original buffer, a new one is made with +-- | `Data.ArrayBuffer.ArrayBuffer.slice`. -- | - `empty` and `fromArray` are methods for creating pure typed arrays -- | -- | #### Modification @@ -133,9 +136,9 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | View mapping the whole `ArrayBuffer`. whole :: ArrayBuffer -> ArrayView a -- | View mapping the rest of an `ArrayBuffer` after an index. - remainder :: ArrayBuffer -> Offset -> Effect (ArrayView a) + remainder :: ArrayBuffer -> ByteOffset -> Effect (ArrayView a) -- | View mapping a region of the `ArrayBuffer`. - part :: ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) + part :: ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) -- | Creates an empty typed array, where each value is assigned 0 empty :: Length -> ArrayView a -- | Creates a typed array from an input array of values, to be binary serialized @@ -493,25 +496,7 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu -- | is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a -- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. -- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves --- | purely. --- | --- | **tl;dr**: if you want a duplicate reference of the same typed array, consider either of the following: --- | --- | ``` --- | y :: ArrayView _ --- | y = x --- | --- | y' :: ArrayView _ --- | y' = subArray x Nothing --- | --- | y'' :: ArrayView _ --- | y'' = subArray x (Just (Tuple 0 Nothing)) --- | --- | y''' :: ArrayView _ --- | y''' = subArray x (Just (Tuple 0 (Just (length x)))) --- | ``` --- | --- | Otherwise, you'll get an _image_ of the array at the moment, like `slice`. +-- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. subArray :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a subArray a mz = case mz of Nothing -> runFn3 subArrayImpl a (toNullable Nothing) (toNullable Nothing) @@ -520,9 +505,6 @@ subArray a mz = case mz of Just e -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable (Just e)) --- FIXME ^ deliberately just create a new typed array from the previous one's buffer? - - -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index c51662d..01beec4 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -29,7 +29,7 @@ import Partial.Unsafe (unsafePartial) genTypedArray :: forall m a t . MonadGen m => TA.TypedArray a t - => TA.Length -- ^ Minimum length + => TA.Length -- ^ Min length -> Maybe TA.Length -- ^ Max length -> m t -> m (ArrayView a) diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 4aeac7e..8085865 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -1,3 +1,6 @@ +-- | This module represents type-level mappings between `ArrayViewType`s +-- | and meaningful data. + module Data.ArrayBuffer.ValueMapping where import Data.ArrayBuffer.Types @@ -8,6 +11,7 @@ import Data.Typelevel.Num (D1, D2, D4, D8) import Data.UInt (UInt) +-- | Maps a `TypedArray`'s binary casted value, to the space occupied by that value, in bytes. class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped D1 @@ -21,6 +25,7 @@ instance bytesPerValueFloat32 :: BytesPerValue Float32 D4 instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 +-- | Maps a `TypedArray`'s binary casted value, to its computable representation in JavaScript. class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt From d00e77b5b3aea75bda52b06c73c4f1bde72b9822 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 15:12:30 -0700 Subject: [PATCH 114/236] reformatting arguments --- src/Data/ArrayBuffer/Typed.purs | 215 +++++++++++++++++--------------- test/Properties/TypedArray.purs | 14 +-- 2 files changed, 118 insertions(+), 111 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index e4c8979..158134c 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -3,7 +3,7 @@ module Data.ArrayBuffer.Typed ( polyFill - , Offset, Length + , Offset, Length, Range , buffer, byteOffset, byteLength, length , class TypedArray , whole, remainder, part, empty, fromArray @@ -11,7 +11,7 @@ module Data.ArrayBuffer.Typed , map, traverse, traverse_, filter , sort, reverse , elem, all, any - , unsafeAt, hasIndex, at + , unsafeAt, hasIndex, at, (!) , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 , find, findIndex, indexOf, lastIndexOf , slice, subArray @@ -99,6 +99,10 @@ type Offset = Int -- | Value-oriented array length type Length = Int +-- | Represents a range of indicies, where if omitted, it represents the whole span. +-- | If only the second argument is omitted, then it represents the remainder of the span after the first index. +type Range = Maybe (Tuple Offset (Maybe Offset)) + -- TODO use purescript-quotient -- | Typeclass that associates a measured user-level type with a typed array. @@ -144,7 +148,7 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: Array t -> ArrayView a -- | Fill the array with a value - fill :: ArrayView a -> t -> Maybe (Tuple Offset (Maybe Offset)) -> Effect Unit + fill :: ArrayView a -> t -> Range -> Effect Unit -- | Stores multiple values into the typed array set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit -- | Maps a new value over the typed array, creating a new buffer and typed array as well. @@ -162,23 +166,23 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | Tests if a value is an element of the typed array elem :: t -> Maybe Offset -> ArrayView a -> Boolean -- | Fetch element at index. - unsafeAt :: ArrayView a -> Offset -> Effect t + unsafeAt :: Offset -> ArrayView a -> Effect t -- | Folding from the left - foldlM :: forall b. ArrayView a -> (b -> t -> Offset -> Effect b) -> b -> Effect b + foldlM :: forall b. (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -- | Assumes the typed array is non-empty - foldl1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t + foldl1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -- | Folding from the right - foldrM :: forall b. ArrayView a -> (t -> b -> Offset -> Effect b) -> b -> Effect b + foldrM :: forall b. (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -- | Assumes the typed array is non-empty - foldr1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t + foldr1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -- | Returns the first value satisfying the predicate - find :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe t) + find :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe t) -- | Returns the first index of the value satisfying the predicate - findIndex :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe Offset) + findIndex :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe Offset) -- | Returns the first index of the element, if it exists, from the left - indexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset + indexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset -- | Returns the first index of the element, if it exists, from the right - lastIndexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset + lastIndexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where @@ -200,15 +204,15 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint32 :: TypedArray Uint32 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint32Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable Nothing) @@ -229,15 +233,15 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 (f <<< UInt.fromNumber)) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) elem x mo a = runFn3 includesImpl a (UInt.toNumber x) (toNullable mo) - unsafeAt o ms = UInt.fromNumber <$> runEffectFn2 unsafeAtImpl o ms - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = UInt.fromNumber <$> runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint16 :: TypedArray Uint16 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable Nothing) @@ -257,15 +261,15 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint8 :: TypedArray Uint8 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable Nothing) @@ -285,15 +289,15 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt32 :: TypedArray Int32 Int where whole a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable Nothing) @@ -313,15 +317,15 @@ instance typedArrayInt32 :: TypedArray Int32 Int where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt16 :: TypedArray Int16 Int where whole a = unsafePerformEffect (runEffectFn3 newInt16Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable Nothing) @@ -341,15 +345,15 @@ instance typedArrayInt16 :: TypedArray Int16 Int where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt8 :: TypedArray Int8 Int where whole a = unsafePerformEffect (runEffectFn3 newInt8Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable Nothing) @@ -369,15 +373,15 @@ instance typedArrayInt8 :: TypedArray Int8 Int where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat32 :: TypedArray Float32 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable Nothing) @@ -397,15 +401,15 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat64 :: TypedArray Float64 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a (toNullable Nothing) (toNullable Nothing)) remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable Nothing) @@ -425,28 +429,28 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt = runEffectFn2 unsafeAtImpl - foldlM a f = runEffectFn3 reduceImpl a (mkEffectFn3 f) - foldl1M a f = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM a f = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) - foldr1M a f = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find a f = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex a f = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) - indexOf a x mo = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf a x mo = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + unsafeAt o a = runEffectFn2 unsafeAtImpl a o + foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) + findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) + lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b -foldl a f i = unsafePerformEffect (foldlM a (\acc x o -> pure (f acc x o)) i) +foldl :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b +foldl f i a = unsafePerformEffect (foldlM (\acc x o -> pure (f acc x o)) i a) -foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) -> b -> b -foldr a f i = unsafePerformEffect (foldrM a (\x acc o -> pure (f x acc o)) i) +foldr :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b +foldr f i a = unsafePerformEffect (foldrM (\x acc o -> pure (f x acc o)) i a) -foldl1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t -foldl1 a f = unsafePerformEffect (foldl1M a (\acc x o -> pure (f acc x o))) +foldl1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t +foldl1 f a = unsafePerformEffect (foldl1M (\acc x o -> pure (f acc x o)) a) -foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t -foldr1 a f = unsafePerformEffect (foldr1M a (\x acc o -> pure (f x acc o))) +foldr1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t +foldr1 f a = unsafePerformEffect (foldr1M (\x acc o -> pure (f x acc o)) a) foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit @@ -473,7 +477,7 @@ setTyped a mo x = runEffectFn3 setImpl a (toNullable mo) x foreign import sliceImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +slice :: forall a. ArrayView a -> Range -> ArrayView a slice a mz = case mz of Nothing -> runFn3 sliceImpl a (toNullable Nothing) (toNullable Nothing) Just (Tuple s me) -> case me of @@ -497,7 +501,7 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu -- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. -- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves -- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. -subArray :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +subArray :: forall a. ArrayView a -> Range -> ArrayView a subArray a mz = case mz of Nothing -> runFn3 subArrayImpl a (toNullable Nothing) (toNullable Nothing) Just (Tuple s me) -> case me of @@ -527,9 +531,12 @@ hasIndex = runFn2 hasIndexImpl at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t at a n = do if a `hasIndex` n - then Just (unsafePerformEffect (unsafeAt a n)) + then Just (unsafePerformEffect (unsafeAt n a)) else Nothing +infixl 3 at as ! + + foreign import toArrayImpl :: forall a b. ArrayView a -> Array b -- | Turn typed array into an array. diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 2576de5..5f8c1a6 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -249,7 +249,7 @@ withOffsetElemTests count = overAll count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = - Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt xs o)) Nothing xs) os + Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt o xs)) Nothing xs) os "All doesn't have an elem of itself" @@ -262,7 +262,7 @@ anyImpliesFindTests count = overAll count anyImpliesFind let pred x o = pure (x /= zero) p = unsafePerformEffect (TA.any pred xs) "All don't satisfy the predicate" q = unsafePerformEffect do - mzs <- TA.find xs pred + mzs <- TA.find pred xs case mzs of Nothing -> pure (Failed "Doesn't have a value satisfying the predicate") Just z -> do @@ -281,7 +281,7 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result findIndexImpliesAt (WithOffset _ xs) = let pred x o = pure (x /= zero) - mo = unsafePerformEffect (TA.findIndex xs pred) + mo = unsafePerformEffect (TA.findIndex pred xs) in case mo of Nothing -> Success Just o -> case TA.at xs o of @@ -297,7 +297,7 @@ indexOfImpliesAtTests count = overAll count indexOfImpliesAt indexOfImpliesAt (WithOffset _ xs) = case TA.at xs 0 of Nothing -> Success - Just y -> case TA.indexOf xs y Nothing of + Just y -> case TA.indexOf y Nothing xs of Nothing -> Failed "no index of" Just o -> TA.at xs o === Just y @@ -309,7 +309,7 @@ lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt lastIndexOfImpliesAt (WithOffset _ xs) = case TA.at xs 0 of Nothing -> Success - Just y -> case TA.lastIndexOf xs y Nothing of + Just y -> case TA.lastIndexOf y Nothing xs of Nothing -> Failed "no lastIndex of" Just o -> TA.at xs o === Just y @@ -319,7 +319,7 @@ foldrConsIsToArrayTests count = overAll count foldrConsIsToArray where foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldrConsIsToArray (WithOffset _ xs) = - TA.foldr xs (\x acc _ -> Array.cons x acc) [] === TA.toArray xs + TA.foldr (\x acc _ -> Array.cons x acc) [] xs === TA.toArray xs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit @@ -327,7 +327,7 @@ foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray where foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldlSnocIsToArray (WithOffset _ xs) = - TA.foldl xs (\acc x _ -> Array.snoc acc x) [] === TA.toArray xs + TA.foldl (\acc x _ -> Array.snoc acc x) [] xs === TA.toArray xs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit From 2e5bf9f7c080bdc6da307a528707e8cccd413e1c Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 15:15:12 -0700 Subject: [PATCH 115/236] final touch-ups --- generated-docs/Data/ArrayBuffer/Typed.md | 47 ++++++++++++++++-------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md index ec43161..14393fa 100644 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ b/generated-docs/Data/ArrayBuffer/Typed.md @@ -27,6 +27,15 @@ type Length = Int Value-oriented array length +#### `Range` + +``` purescript +type Range = Maybe (Tuple Offset (Maybe Offset)) +``` + +Represents a range of indicies, where if omitted, it represents the whole span. +If only the second argument is omitted, then it represents the remainder of the span after the first index. + #### `buffer` ``` purescript @@ -66,7 +75,7 @@ class (BinaryValue a t) <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t part :: ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) empty :: Length -> ArrayView a fromArray :: Array t -> ArrayView a - fill :: ArrayView a -> t -> Maybe (Tuple Offset (Maybe Offset)) -> Effect Unit + fill :: ArrayView a -> t -> Range -> Effect Unit set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit map :: (t -> Offset -> t) -> ArrayView a -> ArrayView a traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) @@ -75,15 +84,15 @@ class (BinaryValue a t) <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) elem :: t -> Maybe Offset -> ArrayView a -> Boolean - unsafeAt :: ArrayView a -> Offset -> Effect t - foldlM :: forall b. ArrayView a -> (b -> t -> Offset -> Effect b) -> b -> Effect b - foldl1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t - foldrM :: forall b. ArrayView a -> (t -> b -> Offset -> Effect b) -> b -> Effect b - foldr1M :: ArrayView a -> (t -> t -> Offset -> Effect t) -> Effect t - find :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe t) - findIndex :: ArrayView a -> (t -> Offset -> Effect Boolean) -> Effect (Maybe Offset) - indexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset - lastIndexOf :: ArrayView a -> t -> Maybe Offset -> Maybe Offset + unsafeAt :: Offset -> ArrayView a -> Effect t + foldlM :: forall b. (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b + foldl1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t + foldrM :: forall b. (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b + foldr1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t + find :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe t) + findIndex :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe Offset) + indexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset + lastIndexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset ``` Typeclass that associates a measured user-level type with a typed array. @@ -179,34 +188,40 @@ at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t Fetch element at index. +#### `(!)` + +``` purescript +infixl 3 at as ! +``` + #### `foldl` ``` purescript -foldl :: forall a b t. TypedArray a t => ArrayView a -> (b -> t -> Offset -> b) -> b -> b +foldl :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b ``` #### `foldl1` ``` purescript -foldl1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +foldl1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t ``` #### `foldr` ``` purescript -foldr :: forall a b t. TypedArray a t => ArrayView a -> (t -> b -> Offset -> b) -> b -> b +foldr :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b ``` #### `foldr1` ``` purescript -foldr1 :: forall a t. TypedArray a t => ArrayView a -> (t -> t -> Offset -> t) -> t +foldr1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t ``` #### `slice` ``` purescript -slice :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +slice :: forall a. ArrayView a -> Range -> ArrayView a ``` Copy part of the contents of a typed array into a new buffer, between some start and end indices. @@ -214,7 +229,7 @@ Copy part of the contents of a typed array into a new buffer, between some start #### `subArray` ``` purescript -subArray :: forall a. ArrayView a -> Maybe (Tuple Offset (Maybe Offset)) -> ArrayView a +subArray :: forall a. ArrayView a -> Range -> ArrayView a ``` Returns a new typed array view of the same buffer, beginning at the index and ending at the second. From 95bf30e01e3d373a47499abc1461d858fe2a683c Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 15 Dec 2018 15:42:27 -0700 Subject: [PATCH 116/236] removing obsolete tests --- test/Input.purs | 44 ----------------------- test/Main.purs | 94 ------------------------------------------------- 2 files changed, 138 deletions(-) delete mode 100644 test/Input.purs diff --git a/test/Input.purs b/test/Input.purs deleted file mode 100644 index 926c66b..0000000 --- a/test/Input.purs +++ /dev/null @@ -1,44 +0,0 @@ --- Uses code originally found in the purescript-encoding library. -{- - Copyright 2018 Andreas Schacker - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --} -module Test.Input where - -import Data.Char.Unicode (isPrint) -import Prelude ((<$>), ($), (<<<)) -import Data.Array (filter) -import Data.String.CodeUnits (fromCharArray, toCharArray) -import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary) - - --- When UTF8-encoding a string, surrogate code points and other non-characters --- are simply replaced by the replacement character � (U+FFFD). --- This entails that the `encodeUtf8` function is not injective anymore and --- thus the desired property `decodeUtf8 <<< encodeUtf8 == id` does not hold --- in general. --- --- For well-formed input strings, however, we can expect the property to hold. - --- Use a newtype in order to define an `Arbitrary` instance. -newtype WellFormedInput = WellFormedInput String - --- The `Arbitrary` instance for `String` currently simply chooses characters --- out of the first 65536 unicode code points. --- See `charGen` in `purescript-strongcheck`. -instance arbWellFormedInput :: Arbitrary WellFormedInput where - arbitrary = WellFormedInput <<< filterString isPrint <$> arbitrary - -filterString :: (Char -> Boolean) -> String -> String -filterString f s = fromCharArray <<< filter f $ toCharArray s diff --git a/test/Main.purs b/test/Main.purs index 8296991..9375027 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -15,97 +15,3 @@ main = do log "Starting tests..." propertiesTests - - - --- import Data.ArrayBuffer.ArrayBuffer as AB --- import Data.ArrayBuffer.DataView as DV --- import Data.ArrayBuffer.Typed as TA --- import Data.Either (fromRight) --- import Data.Maybe (Maybe(..), isNothing) --- import Data.UInt (fromInt, pow) --- import Partial.Unsafe (unsafePartial) --- import Test.QuickCheck (quickCheck', (), quickCheck) --- import Test.Input (WellFormedInput(..)) - - --- assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit --- assertEffEquals expectedValue computation = do --- actualValue <- computation --- let msg = show expectedValue <> " /= " <> show actualValue --- quickCheck' 1 $ actualValue == expectedValue msg - --- assertEquals :: forall a. Eq a => Show a => a -> a -> Effect Unit --- assertEquals expected actual = do --- let msg = show expected <> " /= " <> show actual --- quickCheck' 1 $ expected == actual msg - --- main :: Effect Unit --- main = do --- assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") - --- quickCheck --- \(WellFormedInput s) -> --- let --- result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) --- in --- s == result --- "Isormorphic arraybuffer conversion with string failed for input\n" --- <> s --- <> " which, after the round trip, result in\n" --- <> result - --- ab4 <- AB.create 4 --- ab8 <- AB.create 8 --- assertEquals 4 $ AB.byteLength ab4 --- assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 0 2 ab4 --- assertEffEquals 2 $ pure <<< AB.byteLength =<< AB.slice 2 4 ab4 --- assertEffEquals 0 $ pure <<< AB.byteLength =<< AB.slice (-2) (-2) ab4 --- assertEffEquals 1 $ pure <<< AB.byteLength =<< (AB.slice (-2) (-1) ab4) --- assertEquals Nothing $ DV.byteLength <$> DV.slice 0 200 ab4 --- assertEquals (Just 2) $ DV.byteLength <$> DV.slice 0 2 ab4 --- assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 --- assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] --- assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] --- assertEquals 4 $ AB.byteLength $ AB.fromString "hola" --- assertEquals 5 $ AB.byteLength $ AB.fromString "hóla" --- assertEquals 7 $ AB.byteLength $ AB.fromString "hóla¡" --- assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 --- assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 - --- assertEquals (Just 8) $ DV.byteLength <$> DV.slice 0 8 ab8 --- assertEquals true $ isNothing $ DV.slice 0 40 ab8 - --- fourElementInt8Array <- pure <<< TA.asInt8Array <<< DV.whole $ AB.fromIntArray [1, 2, 3, 4] --- assertEffEquals (Just 2.0) $ TA.at fourElementInt8Array 1 --- assertEffEquals Nothing $ TA.at fourElementInt8Array 4 --- assertEffEquals Nothing $ TA.at fourElementInt8Array (-1) - --- assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0] - --- twoElementDataView <- do --- ab' <- AB.create 2 --- let dv = DV.whole ab' --- DV.setUint8 dv (fromInt 123) 0 --- DV.setUint8 dv (fromInt 0) 1 --- pure dv --- assertEffEquals (Just $ fromInt 123) $ DV.getUint16le twoElementDataView 0 --- assertEffEquals (Just $ fromInt 31488) $ DV.getUint16be twoElementDataView 0 --- assertEffEquals (Just $ fromInt 2 `pow` fromInt 32 - fromInt 1) $ do --- ab' <- AB.create 4 --- let dv = DV.whole ab' --- t = fromInt 255 --- DV.setUint8 dv t 0 --- DV.setUint8 dv t 1 --- DV.setUint8 dv t 2 --- DV.setUint8 dv t 3 --- DV.getUint32be dv 0 - --- let arr = DV.whole (AB.fromIntArray [0x4, 0x3, 0x2, 0x1]) - --- assertEffEquals (Just 0x04) (DV.getInt8 arr 0) --- assertEffEquals (Just 0x04) (DV.getInt8 (TA.dataView (TA.asInt8Array arr)) 0) --- assertEffEquals (Just 0x0304) (DV.getInt16le arr 0) --- assertEffEquals (Just 0x0304) (DV.getInt16le (TA.dataView (TA.asInt16Array arr)) 0) --- assertEffEquals (Just 0x01020304) (DV.getInt32le arr 0) --- assertEffEquals (Just 0x01020304) (DV.getInt32le (TA.dataView (TA.asInt32Array arr)) 0) From e1689dbd88c6b228d710507ec51623cb5bd9bbf5 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 19 Dec 2018 13:35:47 -0700 Subject: [PATCH 117/236] removed generated docs --- .gitignore | 1 + .../Data/ArrayBuffer/ArrayBuffer.md | 30 -- .../Data/ArrayBuffer/ArrayBuffer/Gen.md | 9 - generated-docs/Data/ArrayBuffer/DataView.md | 131 --------- .../Data/ArrayBuffer/DataView/Gen.md | 24 -- generated-docs/Data/ArrayBuffer/Typed.md | 267 ------------------ generated-docs/Data/ArrayBuffer/Typed/Gen.md | 79 ------ .../Data/ArrayBuffer/ValueMapping.md | 48 ---- 8 files changed, 1 insertion(+), 588 deletions(-) delete mode 100644 generated-docs/Data/ArrayBuffer/ArrayBuffer.md delete mode 100644 generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md delete mode 100644 generated-docs/Data/ArrayBuffer/DataView.md delete mode 100644 generated-docs/Data/ArrayBuffer/DataView/Gen.md delete mode 100644 generated-docs/Data/ArrayBuffer/Typed.md delete mode 100644 generated-docs/Data/ArrayBuffer/Typed/Gen.md delete mode 100644 generated-docs/Data/ArrayBuffer/ValueMapping.md diff --git a/.gitignore b/.gitignore index ff18e04..e4de85d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ node_modules/ .psci_modules/ yarn-error.log yarn.lock +generated-docs/ diff --git a/generated-docs/Data/ArrayBuffer/ArrayBuffer.md b/generated-docs/Data/ArrayBuffer/ArrayBuffer.md deleted file mode 100644 index 4e9eff0..0000000 --- a/generated-docs/Data/ArrayBuffer/ArrayBuffer.md +++ /dev/null @@ -1,30 +0,0 @@ -## Module Data.ArrayBuffer.ArrayBuffer - -This module represents the functional bindings to JavaScript's `ArrayBuffer` -objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) for details. - -#### `empty` - -``` purescript -empty :: ByteLength -> ArrayBuffer -``` - -Create an `ArrayBuffer` with the given capacity. - -#### `byteLength` - -``` purescript -byteLength :: ArrayBuffer -> ByteLength -``` - -Represents the length of an `ArrayBuffer` in bytes. - -#### `slice` - -``` purescript -slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer -``` - -Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. - - diff --git a/generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md b/generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md deleted file mode 100644 index da93609..0000000 --- a/generated-docs/Data/ArrayBuffer/ArrayBuffer/Gen.md +++ /dev/null @@ -1,9 +0,0 @@ -## Module Data.ArrayBuffer.ArrayBuffer.Gen - -#### `genArrayBuffer` - -``` purescript -genArrayBuffer :: forall m. MonadGen m => ByteLength -> Maybe ByteLength -> m ArrayBuffer -``` - - diff --git a/generated-docs/Data/ArrayBuffer/DataView.md b/generated-docs/Data/ArrayBuffer/DataView.md deleted file mode 100644 index 68fc377..0000000 --- a/generated-docs/Data/ArrayBuffer/DataView.md +++ /dev/null @@ -1,131 +0,0 @@ -## Module Data.ArrayBuffer.DataView - -This module represents the functional bindings to JavaScript's `DataView` -objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. - -#### `whole` - -``` purescript -whole :: ArrayBuffer -> DataView -``` - -View mapping the whole `ArrayBuffer`. - -#### `remainder` - -``` purescript -remainder :: ArrayBuffer -> ByteOffset -> Effect DataView -``` - -View mapping the rest of an `ArrayBuffer` after an index. - -#### `part` - -``` purescript -part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView -``` - -View mapping a region of the `ArrayBuffer`. - -#### `buffer` - -``` purescript -buffer :: DataView -> ArrayBuffer -``` - -`ArrayBuffer` being mapped by the view. - -#### `byteOffset` - -``` purescript -byteOffset :: DataView -> ByteOffset -``` - -Represents the offset of this view from the start of its `ArrayBuffer`. - -#### `byteLength` - -``` purescript -byteLength :: DataView -> ByteLength -``` - -Represents the length of this view. - -#### `DVProxy` - -``` purescript -data DVProxy (a :: ArrayViewType) (e :: Endianness) - = DVProxy -``` - -#### `Endianness` - -``` purescript -kind Endianness -``` - -#### `BE` - -``` purescript -data BE :: Endianness -``` - -##### Instances -``` purescript -(BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int -(BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int -(BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int -(BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt -(BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt -(BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt -(BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number -(BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number -``` - -#### `LE` - -``` purescript -data LE :: Endianness -``` - -##### Instances -``` purescript -(BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int -(BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int -(BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int -(BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt -(BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt -(BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt -(BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number -(BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number -``` - -#### `DataView` - -``` purescript -class (BinaryValue a t) <= DataView (a :: ArrayViewType) (e :: Endianness) t | a -> t where - get :: DVProxy a e -> DataView -> ByteOffset -> Effect (Maybe t) - set :: DVProxy a e -> DataView -> t -> ByteOffset -> Effect Unit -``` - -##### Instances -``` purescript -(BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int -(BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int -(BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int -(BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int -(BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int -(BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int -(BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt -(BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt -(BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt -(BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt -(BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt -(BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt -(BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number -(BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number -(BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number -(BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number -``` - - diff --git a/generated-docs/Data/ArrayBuffer/DataView/Gen.md b/generated-docs/Data/ArrayBuffer/DataView/Gen.md deleted file mode 100644 index f55ffe0..0000000 --- a/generated-docs/Data/ArrayBuffer/DataView/Gen.md +++ /dev/null @@ -1,24 +0,0 @@ -## Module Data.ArrayBuffer.DataView.Gen - -#### `genDataView` - -``` purescript -genDataView :: forall m. MonadGen m => ByteLength -> Maybe ByteLength -> m DataView -``` - -#### `WithOffsetAndValue` - -``` purescript -data WithOffsetAndValue n (a :: ArrayViewType) (e :: Endianness) t - = WithOffsetAndValue (Vec n ByteOffset) t DataView -``` - -For generating some set of offsets residing inside the generated array, with some computable value - -#### `genWithOffsetAndValue` - -``` purescript -genWithOffsetAndValue :: forall m n a b e t. MonadGen m => Nat n => BytesPerValue a b => DataView a e t => Nat b => m DataView -> m t -> m (WithOffsetAndValue n a e t) -``` - - diff --git a/generated-docs/Data/ArrayBuffer/Typed.md b/generated-docs/Data/ArrayBuffer/Typed.md deleted file mode 100644 index 14393fa..0000000 --- a/generated-docs/Data/ArrayBuffer/Typed.md +++ /dev/null @@ -1,267 +0,0 @@ -## Module Data.ArrayBuffer.Typed - -This module represents the functional bindings to JavaScript's `TypedArray` and other -objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) for details. - -#### `polyFill` - -``` purescript -polyFill :: Effect Unit -``` - -Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill - -#### `Offset` - -``` purescript -type Offset = Int -``` - -Value-oriented array offset - -#### `Length` - -``` purescript -type Length = Int -``` - -Value-oriented array length - -#### `Range` - -``` purescript -type Range = Maybe (Tuple Offset (Maybe Offset)) -``` - -Represents a range of indicies, where if omitted, it represents the whole span. -If only the second argument is omitted, then it represents the remainder of the span after the first index. - -#### `buffer` - -``` purescript -buffer :: forall a. ArrayView a -> ArrayBuffer -``` - -`ArrayBuffer` being mapped by the typed array. - -#### `byteOffset` - -``` purescript -byteOffset :: forall a. ArrayView a -> ByteOffset -``` - -Represents the offset of this view from the start of its `ArrayBuffer`. - -#### `byteLength` - -``` purescript -byteLength :: forall a. ArrayView a -> ByteLength -``` - -Represents the length of this typed array, in bytes. - -#### `length` - -``` purescript -length :: forall a b. BytesPerValue a b => ArrayView a -> Int -``` - -#### `TypedArray` - -``` purescript -class (BinaryValue a t) <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where - whole :: ArrayBuffer -> ArrayView a - remainder :: ArrayBuffer -> ByteOffset -> Effect (ArrayView a) - part :: ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) - empty :: Length -> ArrayView a - fromArray :: Array t -> ArrayView a - fill :: ArrayView a -> t -> Range -> Effect Unit - set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit - map :: (t -> Offset -> t) -> ArrayView a -> ArrayView a - traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) - traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit - all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean - any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean - filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) - elem :: t -> Maybe Offset -> ArrayView a -> Boolean - unsafeAt :: Offset -> ArrayView a -> Effect t - foldlM :: forall b. (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b - foldl1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t - foldrM :: forall b. (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b - foldr1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t - find :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe t) - findIndex :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe Offset) - indexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset - lastIndexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset -``` - -Typeclass that associates a measured user-level type with a typed array. - -#### Creation - -- `whole`, `remainder`, and `part` are methods for building a typed array accessible interface - on top of an existing `ArrayBuffer` - Note, `part` and `remainder` may behave unintuitively - - when the operation is isomorphic to `whole`, the new TypedArray uses the same buffer as the input, - but not when the portion is a sub-array of the original buffer, a new one is made with - `Data.ArrayBuffer.ArrayBuffer.slice`. -- `empty` and `fromArray` are methods for creating pure typed arrays - -#### Modification - -- `fill`, `set`, and `setTyped` are methods for assigning values from external sources -- `map` and `traverse` allow you to create a new array from the existing values in another -- `copyWithin` allows you to set values to the array that exist in other parts of the array -- `filter` creates a new array without the values that don't pass a predicate -- `reverse` modifies an existing array in-place, with all values reversed -- `sort` modifies an existing array in-place, with all values sorted - -#### Access - -- `elem`, `all`, and `any` are functions for testing the contents of an array -- `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset -- `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array -- `find` and `findIndex` are searching functions via a predicate -- `indexOf` and `lastIndexOf` are searching functions via equality -- `slice` returns a new typed array on the same array buffer content as the input -- `subArray` returns a new typed array with a separate array buffer -- `toString` prints to a CSV, `toString'` allows you to supply the delimiter -- `toArray` returns an array of numeric values - -##### Instances -``` purescript -TypedArray Uint8Clamped UInt -TypedArray Uint32 UInt -TypedArray Uint16 UInt -TypedArray Uint8 UInt -TypedArray Int32 Int -TypedArray Int16 Int -TypedArray Int8 Int -TypedArray Float32 Number -TypedArray Float64 Number -``` - -#### `setTyped` - -``` purescript -setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Unit -``` - -Stores multiple values in the typed array, reading input values from the second typed array. - -#### `copyWithin` - -``` purescript -copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit -``` - -Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. - -#### `sort` - -``` purescript -sort :: forall a. ArrayView a -> Effect Unit -``` - -Sorts the values in-place - -#### `reverse` - -``` purescript -reverse :: forall a. ArrayView a -> Effect Unit -``` - -Reverses a typed array in-place. - -#### `hasIndex` - -``` purescript -hasIndex :: forall a. ArrayView a -> Offset -> Boolean -``` - -Determine if a certain index is valid. - -#### `at` - -``` purescript -at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t -``` - -Fetch element at index. - -#### `(!)` - -``` purescript -infixl 3 at as ! -``` - -#### `foldl` - -``` purescript -foldl :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b -``` - -#### `foldl1` - -``` purescript -foldl1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t -``` - -#### `foldr` - -``` purescript -foldr :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b -``` - -#### `foldr1` - -``` purescript -foldr1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t -``` - -#### `slice` - -``` purescript -slice :: forall a. ArrayView a -> Range -> ArrayView a -``` - -Copy part of the contents of a typed array into a new buffer, between some start and end indices. - -#### `subArray` - -``` purescript -subArray :: forall a. ArrayView a -> Range -> ArrayView a -``` - -Returns a new typed array view of the same buffer, beginning at the index and ending at the second. - -**Note**: there is really peculiar behavior with `subArray` - if the first offset argument is omitted, or -is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a -mutable replica of the original array - the sub-array reference reflects mutations to the original array. -However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves -purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. - -#### `toString` - -``` purescript -toString :: forall a. ArrayView a -> String -``` - -Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. - -#### `toString'` - -``` purescript -toString' :: forall a. ArrayView a -> String -> String -``` - -Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. - -#### `toArray` - -``` purescript -toArray :: forall a t. TypedArray a t => ArrayView a -> Array t -``` - -Turn typed array into an array. - - diff --git a/generated-docs/Data/ArrayBuffer/Typed/Gen.md b/generated-docs/Data/ArrayBuffer/Typed/Gen.md deleted file mode 100644 index 82d6133..0000000 --- a/generated-docs/Data/ArrayBuffer/Typed/Gen.md +++ /dev/null @@ -1,79 +0,0 @@ -## Module Data.ArrayBuffer.Typed.Gen - -Functions for generating typed arrays and values. - -#### `genTypedArray` - -``` purescript -genTypedArray :: forall m a t. MonadGen m => TypedArray a t => Length -> Maybe Length -> m t -> m (ArrayView a) -``` - -#### `genUByte` - -``` purescript -genUByte :: forall m. MonadGen m => m UInt -``` - -#### `genByte` - -``` purescript -genByte :: forall m. MonadGen m => m Int -``` - -#### `genUChomp` - -``` purescript -genUChomp :: forall m. MonadGen m => m UInt -``` - -#### `genChomp` - -``` purescript -genChomp :: forall m. MonadGen m => m Int -``` - -#### `genUWord` - -``` purescript -genUWord :: forall m. MonadGen m => m UInt -``` - -#### `genWord` - -``` purescript -genWord :: forall m. MonadGen m => m Int -``` - -#### `genFloat32` - -``` purescript -genFloat32 :: forall m. MonadGen m => m Number -``` - -#### `genFloat64` - -``` purescript -genFloat64 :: forall m. MonadGen m => m Number -``` - -#### `WithOffset` - -``` purescript -data WithOffset n a - = WithOffset (Vec n Offset) (ArrayView a) -``` - -For generating some set of offsets residing inside the generated array - -##### Instances -``` purescript -(Generic (ArrayView a) a') => Generic (WithOffset n a) _ -``` - -#### `genWithOffset` - -``` purescript -genWithOffset :: forall m n b a. MonadGen m => Nat n => BytesPerValue a b => m (ArrayView a) -> m (WithOffset n a) -``` - - diff --git a/generated-docs/Data/ArrayBuffer/ValueMapping.md b/generated-docs/Data/ArrayBuffer/ValueMapping.md deleted file mode 100644 index 631e73d..0000000 --- a/generated-docs/Data/ArrayBuffer/ValueMapping.md +++ /dev/null @@ -1,48 +0,0 @@ -## Module Data.ArrayBuffer.ValueMapping - -This module represents type-level mappings between `ArrayViewType`s -and meaningful data. - -#### `BytesPerValue` - -``` purescript -class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b -``` - -Maps a `TypedArray`'s binary casted value, to the space occupied by that value, in bytes. - -##### Instances -``` purescript -BytesPerValue Uint8Clamped D1 -BytesPerValue Uint32 D4 -BytesPerValue Uint16 D2 -BytesPerValue Uint8 D1 -BytesPerValue Int32 D4 -BytesPerValue Int16 D2 -BytesPerValue Int8 D1 -BytesPerValue Float32 D4 -BytesPerValue Float64 D8 -``` - -#### `BinaryValue` - -``` purescript -class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t -``` - -Maps a `TypedArray`'s binary casted value, to its computable representation in JavaScript. - -##### Instances -``` purescript -BinaryValue Uint8Clamped UInt -BinaryValue Uint32 UInt -BinaryValue Uint16 UInt -BinaryValue Uint8 UInt -BinaryValue Int32 Int -BinaryValue Int16 Int -BinaryValue Int8 Int -BinaryValue Float32 Number -BinaryValue Float64 Number -``` - - From 29a14f8c055160f842a9d4449b3875316092d52a Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 3 Jan 2019 15:44:12 -0700 Subject: [PATCH 118/236] omitting `try` in DataView.js --- src/Data/ArrayBuffer/DataView.js | 4 ++-- src/Data/ArrayBuffer/DataView.purs | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 55b47f7..806ef9d 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -27,8 +27,8 @@ exports.byteLength = function byteLength (v) { return v.byteLength; }; -exports.getterImpl = function getterImpl (s, l, e, v, o) { - return v[s].call(v,o,e); +exports.getterImpl = function getterImpl (data, s, l, e, v, o) { + return ((o + 1) >>> 0) <= v.byteLength ? data.just (v[s].call(v,o,e)) : data.nothing; }; exports.setterImpl = function setterImpl (s,e,v,n,o) { diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 4d737d6..0e51915 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -18,14 +18,15 @@ import Data.ArrayBuffer.Types , Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) -import Prelude (Unit, const, pure, (<$>)) +import Prelude (Unit) import Data.Maybe (Maybe(..)) import Data.UInt (UInt) import Data.Typelevel.Num (toInt', class Nat) import Type.Proxy (Proxy (..)) import Effect (Effect) -import Effect.Exception (catchException) -import Effect.Uncurried (EffectFn5, EffectFn3, EffectFn2, runEffectFn5, runEffectFn3, runEffectFn2) +import Effect.Uncurried + ( EffectFn5, EffectFn6, EffectFn3, EffectFn2 + , runEffectFn5, runEffectFn6, runEffectFn3, runEffectFn2) @@ -68,12 +69,13 @@ class BinaryValue a t <= DataView (a :: ArrayViewType) (e :: Endianness) t | a - set :: DVProxy a e -> DataView -> t -> ByteOffset -> Effect Unit -foreign import getterImpl :: forall t. EffectFn5 String ByteLength Boolean DataView ByteOffset t +foreign import getterImpl :: forall t + . EffectFn6 { just :: t -> Maybe t + , nothing :: Maybe t + } String ByteLength Boolean DataView ByteOffset (Maybe t) getter :: forall t. String -> ByteLength -> Boolean -> DataView -> ByteOffset -> Effect (Maybe t) -getter p l e = \d o -> - let x = runEffectFn5 getterImpl p l e d o - in catchException (const (pure Nothing)) (Just <$> x) +getter p l e = \d o -> runEffectFn6 getterImpl {just: Just, nothing: Nothing} p l e d o foreign import setterImpl :: forall t. EffectFn5 String Boolean DataView t ByteOffset Unit From 289a9e9f47abef30ad2ef1c4ee27b85008052612 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 3 Jan 2019 15:53:43 -0700 Subject: [PATCH 119/236] better code re-use --- src/Data/ArrayBuffer/Typed.js | 73 +++++++++-------------------------- 1 file changed, 19 insertions(+), 54 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index cc38dc4..05e8dc0 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -37,60 +37,25 @@ exports.lengthImpl = function lemgthImpl (v) { // Typed Arrays -exports.newUint8ClampedArray = function newUint8ClampedArray (a,mb,mc) { - return mc === null ? ( mb === null ? new Uint8ClampedArray(a) - : new Uint8ClampedArray(a,mb) - ) - : new Uint8ClampedArray(a,mb,mc); -}; -exports.newUint32Array = function newUint32Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Uint32Array(a) - : new Uint32Array(a,mb) - ) - : new Uint32Array(a,mb,mc); -}; -exports.newUint16Array = function newUint16Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Uint16Array(a) - : new Uint16Array(a,mb) - ) - : new Uint16Array(a,mb,mc); -}; -exports.newUint8Array = function newUint8Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Uint8Array(a) - : new Uint8Array(a,mb) - ) - : new Uint8Array(a,mb,mc); -}; -exports.newInt32Array = function newInt32Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Int32Array(a) - : new Int32Array(a,mb) - ) - : new Int32Array(a,mb,mc); -}; -exports.newInt16Array = function newInt16Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Int16Array(a) - : new Int16Array(a,mb) - ) - : new Int16Array(a,mb,mc); -}; -exports.newInt8Array = function newInt8Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Int8Array(a) - : new Int8Array(a,mb) - ) - : new Int8Array(a,mb,mc); -}; -exports.newFloat32Array = function newFloat32Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Float32Array(a) - : new Float32Array(a,mb) - ) - : new Float32Array(a,mb,mc); -}; -exports.newFloat64Array = function newFloat64Array (a,mb,mc) { - return mc === null ? ( mb === null ? new Float64Array(a) - : new Float64Array(a,mb) - ) - : new Float64Array(a,mb,mc); -}; + +function newArray (f) { + return function newArray_ (a,mb,mc) { + return mc === null ? ( mb === null ? new f(a) + : new f(a,mb) + ) + : new f(a,mb,mc); + }; +} + +exports.newUint8ClampedArray = newArray(Uint8ClampedArray); +exports.newUint32Array = newArray(Uint32Array); +exports.newUint16Array = newArray(Uint16Array); +exports.newUint8Array = newArray(Uint8Array); +exports.newInt32Array = newArray(Int32Array); +exports.newInt16Array = newArray(Int16Array); +exports.newInt8Array = newArray(Int8Array); +exports.newFloat32Array = newArray(Float32Array); +exports.newFloat64Array = newArray(Float64Array); // ------ From 9e8b5671170b301acb9ef0e2ec4f5c07a593e21c Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sun, 6 Jan 2019 15:27:37 -0700 Subject: [PATCH 120/236] fixing implementation --- src/Data/ArrayBuffer/DataView.js | 6 ++- src/Data/ArrayBuffer/DataView.purs | 61 +++++++++++++++++++----------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 806ef9d..e76d84a 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -27,8 +27,10 @@ exports.byteLength = function byteLength (v) { return v.byteLength; }; -exports.getterImpl = function getterImpl (data, s, l, e, v, o) { - return ((o + 1) >>> 0) <= v.byteLength ? data.just (v[s].call(v,o,e)) : data.nothing; +exports.getterImpl = function getterImpl (data, v, o) { + return ((o + data.bytesPerValue) >>> 0) <= v.byteLength + ? data.just (v[data.functionName].call(v,o,data.endian)) + : data.nothing; }; exports.setterImpl = function setterImpl (s,e,v,n,o) { diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 0e51915..902aae8 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -25,8 +25,8 @@ import Data.Typelevel.Num (toInt', class Nat) import Type.Proxy (Proxy (..)) import Effect (Effect) import Effect.Uncurried - ( EffectFn5, EffectFn6, EffectFn3, EffectFn2 - , runEffectFn5, runEffectFn6, runEffectFn3, runEffectFn2) + ( EffectFn5, EffectFn3, EffectFn2 + , runEffectFn5, runEffectFn3, runEffectFn2) @@ -70,12 +70,27 @@ class BinaryValue a t <= DataView (a :: ArrayViewType) (e :: Endianness) t | a - foreign import getterImpl :: forall t - . EffectFn6 { just :: t -> Maybe t + . EffectFn3 { just :: t -> Maybe t , nothing :: Maybe t - } String ByteLength Boolean DataView ByteOffset (Maybe t) - -getter :: forall t. String -> ByteLength -> Boolean -> DataView -> ByteOffset -> Effect (Maybe t) -getter p l e = \d o -> runEffectFn6 getterImpl {just: Just, nothing: Nothing} p l e d o + , functionName :: String + , endian :: Boolean + , bytesPerValue :: ByteLength + } DataView ByteOffset (Maybe t) + +getter :: forall t + . { functionName :: String + , bytesPerValue :: ByteLength + , endian :: Boolean + } + -> DataView -> ByteOffset -> Effect (Maybe t) +getter data' = + runEffectFn3 getterImpl + { just: Just + , nothing: Nothing + , functionName: data'.functionName + , endian: data'.endian + , bytesPerValue: data'.bytesPerValue + } foreign import setterImpl :: forall t. EffectFn5 String Boolean DataView t ByteOffset Unit @@ -85,65 +100,65 @@ setter p e = \d x o -> instance dataViewInt8BE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int where - get DVProxy = getter "getInt8" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setInt8" false instance dataViewInt8LE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int where - get DVProxy = getter "getInt8" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setInt8" true instance dataViewInt16BE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int where - get DVProxy = getter "getInt16" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setInt16" false instance dataViewInt16LE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int where - get DVProxy = getter "getInt16" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setInt16" true instance dataViewInt32BE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int where - get DVProxy = getter "getInt32" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setInt32" false instance dataViewInt32LE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int where - get DVProxy = getter "getInt32" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setInt32" true instance dataViewUint8BE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt where - get DVProxy = getter "getUint8" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setUint8" false instance dataViewUint8LE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt where - get DVProxy = getter "getUint8" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setUint8" true instance dataViewUint16BE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt where - get DVProxy = getter "getUint16" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setUint16" false instance dataViewUint16LE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt where - get DVProxy = getter "getUint16" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setUint16" true instance dataViewUint32BE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt where - get DVProxy = getter "getUint32" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setUint32" false instance dataViewUint32LE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt where - get DVProxy = getter "getUint32" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setUint32" true instance dataViewFloat32BE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number where - get DVProxy = getter "getFloat32" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setFloat32" false instance dataViewFloat32LE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number where - get DVProxy = getter "getFloat32" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setFloat32" true instance dataViewFloat64BE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number where - get DVProxy = getter "getFloat64" (toInt' (Proxy :: Proxy b)) false + get DVProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} set DVProxy = setter "setFloat64" false instance dataViewFloat64LE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number where - get DVProxy = getter "getFloat64" (toInt' (Proxy :: Proxy b)) true + get DVProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} set DVProxy = setter "setFloat64" true From c5a1af4ecf9d61c1e1c4ea4a9b4dc2228f4f75ac Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sun, 6 Jan 2019 15:33:38 -0700 Subject: [PATCH 121/236] reorganized setter --- src/Data/ArrayBuffer/DataView.js | 6 ++--- src/Data/ArrayBuffer/DataView.purs | 43 +++++++++++++++--------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index e76d84a..85977ef 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -33,7 +33,7 @@ exports.getterImpl = function getterImpl (data, v, o) { : data.nothing; }; -exports.setterImpl = function setterImpl (s,e,v,n,o) { - var f = v[s]; - f.call(v,o,n,e); +exports.setterImpl = function setterImpl (data,v,n,o) { + var f = v[data.functionName]; + f.call(v,o,n,data.endian); }; diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 902aae8..7b577cb 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -25,8 +25,8 @@ import Data.Typelevel.Num (toInt', class Nat) import Type.Proxy (Proxy (..)) import Effect (Effect) import Effect.Uncurried - ( EffectFn5, EffectFn3, EffectFn2 - , runEffectFn5, runEffectFn3, runEffectFn2) + ( EffectFn4, EffectFn3, EffectFn2 + , runEffectFn4, runEffectFn3, runEffectFn2) @@ -92,73 +92,72 @@ getter data' = , bytesPerValue: data'.bytesPerValue } -foreign import setterImpl :: forall t. EffectFn5 String Boolean DataView t ByteOffset Unit +foreign import setterImpl :: forall t. EffectFn4 {functionName :: String, endian :: Boolean} DataView t ByteOffset Unit -setter :: forall t. String -> Boolean -> DataView -> t -> ByteOffset -> Effect Unit -setter p e = \d x o -> - runEffectFn5 setterImpl p e d x o +setter :: forall t. {functionName :: String, endian :: Boolean} -> DataView -> t -> ByteOffset -> Effect Unit +setter = runEffectFn4 setterImpl instance dataViewInt8BE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int where get DVProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setInt8" false + set DVProxy = setter {functionName: "setInt8", endian: false} instance dataViewInt8LE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int where get DVProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setInt8" true + set DVProxy = setter {functionName: "setInt8", endian: true} instance dataViewInt16BE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int where get DVProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setInt16" false + set DVProxy = setter {functionName: "setInt16", endian: false} instance dataViewInt16LE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int where get DVProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setInt16" true + set DVProxy = setter {functionName: "setInt16", endian: true} instance dataViewInt32BE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int where get DVProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setInt32" false + set DVProxy = setter {functionName: "setInt32", endian: false} instance dataViewInt32LE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int where get DVProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setInt32" true + set DVProxy = setter {functionName: "setInt32", endian: true} instance dataViewUint8BE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt where get DVProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setUint8" false + set DVProxy = setter {functionName: "setUint8", endian: false} instance dataViewUint8LE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt where get DVProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setUint8" true + set DVProxy = setter {functionName: "setUint8", endian: true} instance dataViewUint16BE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt where get DVProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setUint16" false + set DVProxy = setter {functionName: "setUint16", endian: false} instance dataViewUint16LE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt where get DVProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setUint16" true + set DVProxy = setter {functionName: "setUint16", endian: true} instance dataViewUint32BE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt where get DVProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setUint32" false + set DVProxy = setter {functionName: "setUint32", endian: false} instance dataViewUint32LE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt where get DVProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setUint32" true + set DVProxy = setter {functionName: "setUint32", endian: true} instance dataViewFloat32BE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number where get DVProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setFloat32" false + set DVProxy = setter {functionName: "setFloat32", endian: false} instance dataViewFloat32LE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number where get DVProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setFloat32" true + set DVProxy = setter {functionName: "setFloat32", endian: true} instance dataViewFloat64BE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number where get DVProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter "setFloat64" false + set DVProxy = setter {functionName: "setFloat64", endian: false} instance dataViewFloat64LE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number where get DVProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter "setFloat64" true + set DVProxy = setter {functionName: "setFloat64", endian: true} From 7f4819e687ec642ba2957113ea8670e5cfd42097 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sun, 6 Jan 2019 16:33:39 -0700 Subject: [PATCH 122/236] better design --- src/Data/ArrayBuffer/DataView.js | 10 +- src/Data/ArrayBuffer/DataView.purs | 147 ++++++++++++------------- src/Data/ArrayBuffer/DataView/Gen.purs | 10 +- test/Properties/DataView.purs | 108 +++++++----------- 4 files changed, 121 insertions(+), 154 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 85977ef..d51835c 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -29,11 +29,15 @@ exports.byteLength = function byteLength (v) { exports.getterImpl = function getterImpl (data, v, o) { return ((o + data.bytesPerValue) >>> 0) <= v.byteLength - ? data.just (v[data.functionName].call(v,o,data.endian)) + ? data.just (v[data.functionName].call(v,o,data.littleEndian)) : data.nothing; }; exports.setterImpl = function setterImpl (data,v,n,o) { - var f = v[data.functionName]; - f.call(v,o,n,data.endian); + if (((o + data.bytesPerValue) >>> 0) <= v.byteLength) { + v[data.functionName].call(v,o,n,data.littleEndian); + return true; + } else { + return false; + } }; diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 7b577cb..1766642 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -8,9 +8,9 @@ module Data.ArrayBuffer.DataView , buffer , byteOffset , byteLength - , DVProxy (..), kind Endianness, BE, LE + , AProxy (..) , class DataView - , get, set + , getBE, getLE, setBE, setLE ) where import Data.ArrayBuffer.Types @@ -56,31 +56,28 @@ foreign import byteOffset :: DataView -> ByteOffset foreign import byteLength :: DataView -> ByteLength +data AProxy (a :: ArrayViewType) = AProxy -data DVProxy (a :: ArrayViewType) (e :: Endianness) = DVProxy -foreign import kind Endianness -foreign import data BE :: Endianness -foreign import data LE :: Endianness - - -class BinaryValue a t <= DataView (a :: ArrayViewType) (e :: Endianness) t | a -> t where - get :: DVProxy a e -> DataView -> ByteOffset -> Effect (Maybe t) - set :: DVProxy a e -> DataView -> t -> ByteOffset -> Effect Unit +class BinaryValue a t <= DataView (a :: ArrayViewType) t | a -> t where + getLE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) + getBE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) + setBE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean + setLE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean foreign import getterImpl :: forall t . EffectFn3 { just :: t -> Maybe t , nothing :: Maybe t , functionName :: String - , endian :: Boolean + , littleEndian :: Boolean , bytesPerValue :: ByteLength } DataView ByteOffset (Maybe t) getter :: forall t . { functionName :: String , bytesPerValue :: ByteLength - , endian :: Boolean + , littleEndian :: Boolean } -> DataView -> ByteOffset -> Effect (Maybe t) getter data' = @@ -88,76 +85,68 @@ getter data' = { just: Just , nothing: Nothing , functionName: data'.functionName - , endian: data'.endian + , littleEndian: data'.littleEndian , bytesPerValue: data'.bytesPerValue } -foreign import setterImpl :: forall t. EffectFn4 {functionName :: String, endian :: Boolean} DataView t ByteOffset Unit +foreign import setterImpl :: forall t + . EffectFn4 { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } DataView t ByteOffset Boolean -setter :: forall t. {functionName :: String, endian :: Boolean} -> DataView -> t -> ByteOffset -> Effect Unit +setter :: forall t + . { functionName :: String + , bytesPerValue :: ByteLength + , littleEndian :: Boolean + } -> DataView -> t -> ByteOffset -> Effect Boolean setter = runEffectFn4 setterImpl -instance dataViewInt8BE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 BE Int where - get DVProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setInt8", endian: false} - -instance dataViewInt8LE :: (BytesPerValue Int8 b, Nat b) => DataView Int8 LE Int where - get DVProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setInt8", endian: true} - -instance dataViewInt16BE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 BE Int where - get DVProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setInt16", endian: false} - -instance dataViewInt16LE :: (BytesPerValue Int16 b, Nat b) => DataView Int16 LE Int where - get DVProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setInt16", endian: true} - -instance dataViewInt32BE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 BE Int where - get DVProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setInt32", endian: false} - -instance dataViewInt32LE :: (BytesPerValue Int32 b, Nat b) => DataView Int32 LE Int where - get DVProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setInt32", endian: true} - -instance dataViewUint8BE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 BE UInt where - get DVProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setUint8", endian: false} - -instance dataViewUint8LE :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 LE UInt where - get DVProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setUint8", endian: true} - -instance dataViewUint16BE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 BE UInt where - get DVProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setUint16", endian: false} - -instance dataViewUint16LE :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 LE UInt where - get DVProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setUint16", endian: true} - -instance dataViewUint32BE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 BE UInt where - get DVProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setUint32", endian: false} - -instance dataViewUint32LE :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 LE UInt where - get DVProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setUint32", endian: true} - -instance dataViewFloat32BE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 BE Number where - get DVProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setFloat32", endian: false} - -instance dataViewFloat32LE :: (BytesPerValue Float32 b, Nat b) => DataView Float32 LE Number where - get DVProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setFloat32", endian: true} - -instance dataViewFloat64BE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 BE Number where - get DVProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), endian: false} - set DVProxy = setter {functionName: "setFloat64", endian: false} - -instance dataViewFloat64LE :: (BytesPerValue Float64 b, Nat b) => DataView Float64 LE Number where - get DVProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), endian: true} - set DVProxy = setter {functionName: "setFloat64", endian: true} +instance dataViewInt8 :: (BytesPerValue Int8 b, Nat b) => DataView Int8 Int where + getBE AProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewInt16 :: (BytesPerValue Int16 b, Nat b) => DataView Int16 Int where + getBE AProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewInt32 :: (BytesPerValue Int32 b, Nat b) => DataView Int32 Int where + getBE AProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewUint8 :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 UInt where + getBE AProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewUint16 :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 UInt where + getBE AProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewUint32 :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 UInt where + getBE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewFloat32 :: (BytesPerValue Float32 b, Nat b) => DataView Float32 Number where + getBE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + +instance dataViewFloat64 :: (BytesPerValue Float64 b, Nat b) => DataView Float64 Number where + getBE AProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + setBE AProxy = setter {functionName: "setFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} + getLE AProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + setLE AProxy = setter {functionName: "setFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 4ac9a18..5ab9e05 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,7 +1,7 @@ module Data.ArrayBuffer.DataView.Gen where import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) -import Data.ArrayBuffer.DataView (whole, byteLength, class DataView, kind Endianness) +import Data.ArrayBuffer.DataView (whole, byteLength, class DataView) import Data.ArrayBuffer.Types (DataView, ByteLength, ByteOffset, kind ArrayViewType) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) @@ -26,18 +26,18 @@ genDataView a b = whole <$> genArrayBuffer a b -- | For generating some set of offsets residing inside the generated array, with some computable value -data WithOffsetAndValue n (a :: ArrayViewType) (e :: Endianness) t = +data WithOffsetAndValue n (a :: ArrayViewType) t = WithOffsetAndValue (Vec n ByteOffset) t DataView -genWithOffsetAndValue :: forall m n a b e t +genWithOffsetAndValue :: forall m n a b t . MonadGen m => Nat n => BytesPerValue a b - => DataView a e t + => DataView a t => Nat b => m DataView -- ^ Assumes generated length is at least the minimum length of one value -> m t - -> m (WithOffsetAndValue n a e t) + -> m (WithOffsetAndValue n a t) genWithOffsetAndValue gen genT = do let n = toInt' (Proxy :: Proxy n) b = toInt' (Proxy :: Proxy b) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index c0e6ff3..519af34 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -27,11 +27,13 @@ import Test.QuickCheck.Gen (Gen) dataViewTests :: Ref Int -> Effect Unit dataViewTests count = do - log " - set x o => get o === Just x" - placingAValueIsThereTests count + log " - setBE x o => getBE o === Just x" + placingAValueIsThereTestsBE count + log " - setLE x o => getLE o === Just x" + placingAValueIsThereTestsLE count -type TestableViewF a b n e t q = +type TestableViewF a b n t q = Show t => Eq t => Ord t @@ -39,103 +41,75 @@ type TestableViewF a b n e t q = => Arbitrary t => BytesPerValue a b => Nat b - => DV.DataView a e t - => WithOffsetAndValue n a e t + => DV.DataView a t + => WithOffsetAndValue n a t -> q -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b e t. TestableViewF a b n e t q) -> Effect Unit +overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableViewF a b n t q) -> Effect Unit overAll count f = do void (Ref.modify (\x -> x + 1) count) - log " - Uint32 BE" + log " - Uint32" quickCheckGen $ - let f' :: TestableViewF Uint32 D4 n DV.BE UInt q + let f' :: TestableViewF Uint32 D4 n UInt q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord)) - log " - Uint32 LE" + log " - Uint16" quickCheckGen $ - let f' :: TestableViewF Uint32 D4 n DV.LE UInt q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord)) - log " - Uint16 BE" - quickCheckGen $ - let f' :: TestableViewF Uint16 D2 n DV.BE UInt q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUChomp)) - log " - Uint16 LE" - quickCheckGen $ - let f' :: TestableViewF Uint16 D2 n DV.LE UInt q + let f' :: TestableViewF Uint16 D2 n UInt q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUChomp)) - log " - Uint8 BE" - quickCheckGen $ - let f' :: TestableViewF Uint8 D1 n DV.BE UInt q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUByte)) - log " - Uint8 LE" + log " - Uint8" quickCheckGen $ - let f' :: TestableViewF Uint8 D1 n DV.LE UInt q + let f' :: TestableViewF Uint8 D1 n UInt q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUByte)) - log " - Int32 BE" - quickCheckGen $ - let f' :: TestableViewF Int32 D4 n DV.BE Int q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genWord)) - log " - Int32 LE" + log " - Int32" quickCheckGen $ - let f' :: TestableViewF Int32 D4 n DV.LE Int q + let f' :: TestableViewF Int32 D4 n Int q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genWord)) - log " - Int16 BE" + log " - Int16" quickCheckGen $ - let f' :: TestableViewF Int16 D2 n DV.BE Int q + let f' :: TestableViewF Int16 D2 n Int q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genChomp)) - log " - Int16 LE" + log " - Int8" quickCheckGen $ - let f' :: TestableViewF Int16 D2 n DV.LE Int q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genChomp)) - log " - Int8 BE" - quickCheckGen $ - let f' :: TestableViewF Int8 D1 n DV.BE Int q + let f' :: TestableViewF Int8 D1 n Int q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genByte)) - log " - Int8 LE" + log " - Float32" quickCheckGen $ - let f' :: TestableViewF Int8 D1 n DV.LE Int q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genByte)) - log " - Float32 BE" - quickCheckGen $ - let f' :: TestableViewF Float32 D4 n DV.BE Number q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat32)) - log " - Float32 LE" - quickCheckGen $ - let f' :: TestableViewF Float32 D4 n DV.LE Number q + let f' :: TestableViewF Float32 D4 n Number q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat32)) - log " - Float64 BE" - quickCheckGen $ - let f' :: TestableViewF Float64 D8 n DV.BE Number q - f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat64)) - log " - Float64 LE" + log " - Float64" quickCheckGen $ - let f' :: TestableViewF Float64 D8 n DV.LE Number q + let f' :: TestableViewF Float64 D8 n Number q f' = f in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat64)) -placingAValueIsThereTests :: Ref Int -> Effect Unit -placingAValueIsThereTests count = overAll count placingAValueIsThere +placingAValueIsThereTestsBE :: Ref Int -> Effect Unit +placingAValueIsThereTestsBE count = overAll count placingAValueIsThere + where + placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result + placingAValueIsThere (WithOffsetAndValue os t xs) = + let o = Vec.head os + in unsafePerformEffect do + _ <- DV.setBE (DV.AProxy :: DV.AProxy a) xs t o + my <- DV.getBE (DV.AProxy :: DV.AProxy a) xs o + pure (my === Just t) + + +placingAValueIsThereTestsLE :: Ref Int -> Effect Unit +placingAValueIsThereTestsLE count = overAll count placingAValueIsThere where - placingAValueIsThere :: forall a b e t. TestableViewF a b D1 e t Result + placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result placingAValueIsThere (WithOffsetAndValue os t xs) = let o = Vec.head os in unsafePerformEffect do - DV.set (DV.DVProxy :: DV.DVProxy a e) xs t o - my <- DV.get (DV.DVProxy :: DV.DVProxy a e) xs o + _ <- DV.setLE (DV.AProxy :: DV.AProxy a) xs t o + my <- DV.getLE (DV.AProxy :: DV.AProxy a) xs o pure (my === Just t) From f9f9d2b1c4c4662ea3d4eb931938bf3bc2c44ce7 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 9 Jan 2019 15:57:37 -0700 Subject: [PATCH 123/236] omitting Arbitrary constraint --- bower.json | 2 +- src/Data/ArrayBuffer/DataView.purs | 1 - test/Main.purs | 1 - test/Properties.purs | 22 ++++++++++++---------- test/Properties/DataView.purs | 17 ++++++++--------- test/Properties/TypedArray.purs | 6 ++---- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/bower.json b/bower.json index abc0bf8..2e030e6 100644 --- a/bower.json +++ b/bower.json @@ -19,7 +19,7 @@ "purescript-nullable": "^4.1.0", "purescript-typelevel": "^4.0.0", "purescript-parseint": "^1.1.0", - "purescript-uint": "^4.1.0", + "purescript-uint": "^5.1.0", "purescript-sized-vectors": "^3.1.0" }, "devDependencies": { diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 1766642..2df4be6 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -18,7 +18,6 @@ import Data.ArrayBuffer.Types , Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) -import Prelude (Unit) import Data.Maybe (Maybe(..)) import Data.UInt (UInt) import Data.Typelevel.Num (toInt', class Nat) diff --git a/test/Main.purs b/test/Main.purs index 9375027..2d004f8 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -5,7 +5,6 @@ import Test.Properties (propertiesTests) import Prelude import Effect (Effect) import Effect.Console (log) -import Effect.Ref as Ref diff --git a/test/Properties.purs b/test/Properties.purs index 441e3fb..f71e596 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -11,14 +11,16 @@ import Effect.Console (log) propertiesTests :: Effect Unit propertiesTests = do - count <- Ref.new 0 - log " - TypedArray Tests:" - typedArrayTests count - c <- Ref.read count - log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." + do + count <- Ref.new 0 + log " - TypedArray Tests:" + typedArrayTests count + c <- Ref.read count + log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." - count <- Ref.new 0 - log " - DataView Tests:" - dataViewTests count - c <- Ref.read count - log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." + do + count <- Ref.new 0 + log " - DataView Tests:" + dataViewTests count + c <- Ref.read count + log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 519af34..4444104 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -38,7 +38,6 @@ type TestableViewF a b n t q = => Eq t => Ord t => Semiring t - => Arbitrary t => BytesPerValue a b => Nat b => DV.DataView a t @@ -53,42 +52,42 @@ overAll count f = do quickCheckGen $ let f' :: TestableViewF Uint32 D4 n UInt q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUWord)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUWord log " - Uint16" quickCheckGen $ let f' :: TestableViewF Uint16 D2 n UInt q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUChomp)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUChomp log " - Uint8" quickCheckGen $ let f' :: TestableViewF Uint8 D1 n UInt q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genUByte)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUByte log " - Int32" quickCheckGen $ let f' :: TestableViewF Int32 D4 n Int q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genWord)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genWord log " - Int16" quickCheckGen $ let f' :: TestableViewF Int16 D2 n Int q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genChomp)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genChomp log " - Int8" quickCheckGen $ let f' :: TestableViewF Int8 D1 n Int q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genByte)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genByte log " - Float32" quickCheckGen $ let f' :: TestableViewF Float32 D4 n Number q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat32)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genFloat32 log " - Float64" quickCheckGen $ let f' :: TestableViewF Float64 D8 n Number q f' = f - in (f' <$> (genWithOffsetAndValue (genDataView 20 Nothing) genFloat64)) + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genFloat64 placingAValueIsThereTestsBE :: Ref Int -> Effect Unit diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 5f8c1a6..ec2b01f 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -18,11 +18,10 @@ import Data.Tuple (Tuple (..)) import Data.Typelevel.Num (toInt', class Nat, D0, D1, D5) import Data.Vec (head) as Vec import Data.Array as Array -import Data.HeytingAlgebra (implies) import Type.Proxy (Proxy (..)) -import Test.QuickCheck (quickCheckGen, Result (..), (===), (/==), class Testable, class Arbitrary, ()) +import Test.QuickCheck (quickCheckGen, Result (..), (===), (/==), class Testable, ()) import Test.QuickCheck.Gen (Gen) -import Test.QuickCheck.Combinators ((&=&), (|=|), (==>)) +import Test.QuickCheck.Combinators ((==>)) import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) @@ -116,7 +115,6 @@ type TestableArrayF a b n t q = => Eq t => Ord t => Semiring t - => Arbitrary t => TypedArray a t => BytesPerValue a b => Nat b From b066a6c45dcae3c4f59bc5e53075bdbe6e1516bc Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 9 Jan 2019 15:59:38 -0700 Subject: [PATCH 124/236] cosmetic --- test/Properties/DataView.purs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 4444104..b88930f 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -2,11 +2,10 @@ module Test.Properties.DataView where import Data.ArrayBuffer.Types - ( DataView - , Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) + ( Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue (..)) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.ArrayBuffer.Typed.Gen (genUWord, genWord, genUChomp, genChomp, genUByte, genByte, genFloat32, genFloat64) @@ -20,8 +19,7 @@ import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref -import Test.QuickCheck (class Testable, quickCheckGen, class Arbitrary, arbitrary, Result, (===)) -import Test.QuickCheck.Gen (Gen) +import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) From fdccca3b4d56121f528d02ddf4f8ee186bb85d29 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 13:13:58 +0100 Subject: [PATCH 125/236] Some cosmetic changes and fixes. Fix limits in generators. Make limits more explicit. Simplify Float32 generator. --- src/Data/ArrayBuffer/Typed/Gen.js | 9 ++++ src/Data/ArrayBuffer/Typed/Gen.purs | 82 ++++++++++------------------- test/Properties/DataView.purs | 28 +++++----- test/Properties/TypedArray.purs | 42 ++++++--------- 4 files changed, 67 insertions(+), 94 deletions(-) create mode 100644 src/Data/ArrayBuffer/Typed/Gen.js diff --git a/src/Data/ArrayBuffer/Typed/Gen.js b/src/Data/ArrayBuffer/Typed/Gen.js new file mode 100644 index 0000000..a592186 --- /dev/null +++ b/src/Data/ArrayBuffer/Typed/Gen.js @@ -0,0 +1,9 @@ +"use strict"; + +// module Data.ArrayBuffer.Typed.Gen + +exports.toFloat32 = function toFloat32(s) { + var r = new Float32Array(1); + r[0] = s; + return r[0]; +} diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 01beec4..914e78d 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -2,28 +2,23 @@ module Data.ArrayBuffer.Typed.Gen where -import Data.ArrayBuffer.Types (ArrayView) +import Prelude + +import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed as TA +import Data.ArrayBuffer.Types (ArrayView) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) - -import Prelude -import Math as M -import Data.Maybe (Maybe (..)) -import Data.Int as I -import Data.UInt (UInt) -import Data.UInt (fromInt, fromNumber) as UInt -import Data.String.CodeUnits as S -import Data.Float.Parse (parseFloat) -import Data.Vec (Vec) -import Data.Vec (fromArray) as Vec import Data.Generic.Rep (class Generic) +import Data.Maybe (Maybe(..), fromMaybe) import Data.Typelevel.Num (class Nat, toInt') +import Data.UInt (UInt) +import Data.UInt (fromInt) as UInt +import Data.UInt.Gen (genUInt) as UInt import Data.Unfoldable (replicateA) -import Type.Proxy (Proxy (..)) -import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) +import Data.Vec (Vec) +import Data.Vec (fromArray) as Vec import Partial.Unsafe (unsafePartial) - - +import Type.Proxy (Proxy(..)) genTypedArray :: forall m a t @@ -33,58 +28,37 @@ genTypedArray :: forall m a t -> Maybe TA.Length -- ^ Max length -> m t -> m (ArrayView a) -genTypedArray q1 mq2 gen = sized \s -> - let s'' = s `max` q1 - s' = case mq2 of - Nothing -> s'' - Just q2 -> s'' `min` q2 +genTypedArray lo mhi gen = sized \s -> + let hi = fromMaybe s mhi + s' = clamp lo hi s in TA.fromArray <$> replicateA s' gen - genUByte :: forall m. MonadGen m => m UInt -genUByte = UInt.fromInt <$> chooseInt 0 ((I.pow 2 8) - 1) +genUByte = UInt.fromInt <$> chooseInt 0 255 genByte :: forall m. MonadGen m => m Int -genByte = - let j = I.pow 2 4 - in chooseInt (negate j) (j - 1) +genByte = chooseInt (-128) 127 + +genUShort :: forall m. MonadGen m => m UInt +genUShort = UInt.fromInt <$> chooseInt 0 65535 -genUChomp :: forall m. MonadGen m => m UInt -genUChomp = UInt.fromInt <$> chooseInt 0 ((I.pow 2 16) - 1) +genShort :: forall m. MonadGen m => m Int +genShort = chooseInt (-32768) 32767 -genChomp :: forall m. MonadGen m => m Int -genChomp = - let j = I.pow 2 8 - in chooseInt (negate j) (j - 1) +genUInt :: forall m. MonadGen m => m UInt +genUInt = UInt.genUInt bottom top -genUWord :: forall m. MonadGen m => m UInt -genUWord = UInt.fromNumber <$> chooseFloat 0.0 ((M.pow 2.0 32.0) - 1.0) +genInt :: forall m. MonadGen m => m Int +genInt = chooseInt bottom top -genWord :: forall m. MonadGen m => m Int -genWord = - let j = I.pow 2 16 - in chooseInt (negate j) (j - 1) +foreign import toFloat32 :: Number -> Number genFloat32 :: forall m. MonadGen m => m Number -genFloat32 = - let maxFloat32 = (1.0 - (M.pow 2.0 (-24.0))) * (M.pow 2.0 128.0) - minFloat32 = -maxFloat32 -- because of sign bit - reformat :: String -> String - reformat s = - let pre = S.takeWhile (\c -> c /= '.') s - suf = S.dropWhile (\c -> c /= '.') s - in pre <> "." <> S.take 6 suf - fix :: Number -> Number - fix x = unsafePartial $ case parseFloat (reformat (show x)) of - Just y -> y - in fix <$> chooseFloat minFloat32 maxFloat32 - -- roughly estimated because of variable precision between 6 and 9 digs +genFloat32 = toFloat32 <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 genFloat64 :: forall m. MonadGen m => m Number -genFloat64 = chooseFloat (-1.7e308) 1.7e308 - - +genFloat64 = chooseFloat (-1.7976931348623157e+308) 1.7976931348623157e+308 -- | For generating some set of offsets residing inside the generated array data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index b88930f..9908e36 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -1,24 +1,22 @@ module Test.Properties.DataView where -import Data.ArrayBuffer.Types - ( Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) +import Prelude + import Data.ArrayBuffer.DataView as DV -import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue (..)) +import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) +import Data.ArrayBuffer.Typed.Gen (genByte, genFloat32, genFloat64, genInt, genShort, genUByte, genUInt, genUShort) +import Data.ArrayBuffer.Types (Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Data.ArrayBuffer.Typed.Gen - (genUWord, genWord, genUChomp, genChomp, genUByte, genByte, genFloat32, genFloat64) - -import Prelude -import Data.Vec (head) as Vec -import Data.UInt (UInt) -import Data.Maybe (Maybe (..)) +import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) +import Data.UInt (UInt) +import Data.Vec (head) as Vec import Effect (Effect) -import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref +import Effect.Unsafe (unsafePerformEffect) import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) @@ -50,12 +48,12 @@ overAll count f = do quickCheckGen $ let f' :: TestableViewF Uint32 D4 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUWord + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUInt log " - Uint16" quickCheckGen $ let f' :: TestableViewF Uint16 D2 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUChomp + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUShort log " - Uint8" quickCheckGen $ let f' :: TestableViewF Uint8 D1 n UInt q @@ -65,12 +63,12 @@ overAll count f = do quickCheckGen $ let f' :: TestableViewF Int32 D4 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genWord + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt log " - Int16" quickCheckGen $ let f' :: TestableViewF Int16 D2 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genChomp + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genShort log " - Int8" quickCheckGen $ let f' :: TestableViewF Int8 D1 n Int q diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index ec2b01f..086136c 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -1,32 +1,27 @@ module Test.Properties.TypedArray where -import Data.ArrayBuffer.Types - (ArrayView, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array - , Float32Array, Float64Array) -import Data.ArrayBuffer.Typed as TA +import Prelude + +import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) +import Data.ArrayBuffer.Typed as TA +import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genByte, genFloat32, genFloat64, genInt, genShort, genTypedArray, genUByte, genUInt, genUShort, genWithOffset) +import Data.ArrayBuffer.Types (ArrayView, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float32Array, Float64Array) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Data.ArrayBuffer.Typed.Gen - ( genUByte, genUChomp, genUWord - , genByte, genChomp, genWord, genFloat32, genFloat64 - , WithOffset (..), genWithOffset, genTypedArray) - -import Prelude -import Data.Maybe (Maybe (..)) -import Data.Tuple (Tuple (..)) +import Data.Maybe (Maybe(..)) +import Data.Tuple (Tuple(..)) import Data.Typelevel.Num (toInt', class Nat, D0, D1, D5) import Data.Vec (head) as Vec -import Data.Array as Array -import Type.Proxy (Proxy (..)) -import Test.QuickCheck (quickCheckGen, Result (..), (===), (/==), class Testable, ()) -import Test.QuickCheck.Gen (Gen) -import Test.QuickCheck.Combinators ((==>)) import Effect (Effect) -import Effect.Unsafe (unsafePerformEffect) import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref +import Effect.Unsafe (unsafePerformEffect) +import Test.QuickCheck (quickCheckGen, Result(..), (===), (/==), class Testable, ()) +import Test.QuickCheck.Combinators ((==>)) +import Test.QuickCheck.Gen (Gen) +import Type.Proxy (Proxy(..)) typedArrayTests :: Ref Int -> Effect Unit @@ -128,15 +123,15 @@ overAll count f = do log " - Uint8ClampedArray" quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUByte :: Gen Uint8ClampedArray)) log " - Uint32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUWord :: Gen Uint32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUInt :: Gen Uint32Array)) log " - Uint16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUChomp :: Gen Uint16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUShort :: Gen Uint16Array)) log " - Uint8Array" quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUByte :: Gen Uint8Array)) log " - Int32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genWord :: Gen Int32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt :: Gen Int32Array)) log " - Int16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genChomp :: Gen Int16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genShort :: Gen Int16Array)) log " - Int8Array" quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genByte :: Gen Int8Array)) log " - Float32Array" @@ -629,6 +624,3 @@ copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped TA.setTyped xs' Nothing ys TA.copyWithin xs 0 o Nothing in TA.toArray xs === TA.toArray xs' - - - From 6576b17bf7fc6e8cbb38e22a7e4c4d82a6695a1f Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 13:18:58 +0100 Subject: [PATCH 126/236] Sort includes --- src/Data/ArrayBuffer/ArrayBuffer.purs | 4 ++-- src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 7 +++--- src/Data/ArrayBuffer/DataView.purs | 13 ++++-------- src/Data/ArrayBuffer/DataView/Gen.purs | 13 ++++++------ src/Data/ArrayBuffer/Typed.purs | 26 +++++++---------------- src/Data/ArrayBuffer/ValueMapping.purs | 5 +---- 6 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 931df9c..7ceec3f 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -9,9 +9,9 @@ module Data.ArrayBuffer.ArrayBuffer import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) +import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, toNullable) -import Data.Maybe (Maybe (..)) -import Data.Tuple (Tuple (..)) +import Data.Tuple (Tuple(..)) -- | Create an `ArrayBuffer` with the given capacity. diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index 125072a..ce5fd91 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -1,12 +1,11 @@ module Data.ArrayBuffer.ArrayBuffer.Gen where -import Data.ArrayBuffer.Typed.Gen (genUByte, genTypedArray) +import Control.Monad.Gen.Class (class MonadGen) import Data.ArrayBuffer.Typed (buffer) +import Data.ArrayBuffer.Typed.Gen (genUByte, genTypedArray) import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, Uint8Array) - -import Prelude ((<$>)) import Data.Maybe (Maybe) -import Control.Monad.Gen.Class (class MonadGen) +import Prelude ((<$>)) genArrayBuffer :: forall m diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 2df4be6..a568f40 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -13,19 +13,14 @@ module Data.ArrayBuffer.DataView , getBE, getLE, setBE, setLE ) where -import Data.ArrayBuffer.Types - ( ByteOffset, DataView, ByteLength, ArrayBuffer, kind ArrayViewType - , Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) +import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer, kind ArrayViewType, Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) - import Data.Maybe (Maybe(..)) -import Data.UInt (UInt) import Data.Typelevel.Num (toInt', class Nat) -import Type.Proxy (Proxy (..)) +import Data.UInt (UInt) import Effect (Effect) -import Effect.Uncurried - ( EffectFn4, EffectFn3, EffectFn2 - , runEffectFn4, runEffectFn3, runEffectFn2) +import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4) +import Type.Proxy (Proxy(..)) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 5ab9e05..608fbc9 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,19 +1,18 @@ module Data.ArrayBuffer.DataView.Gen where +import Control.Monad.Gen.Class (class MonadGen, chooseInt) import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole, byteLength, class DataView) import Data.ArrayBuffer.Types (DataView, ByteLength, ByteOffset, kind ArrayViewType) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) - -import Prelude ((<$>), bind, pure, (-), ($)) -import Data.Maybe (Maybe (Just)) -import Data.Vec (Vec) -import Data.Vec (fromArray) as Vec +import Data.Maybe (Maybe(Just)) import Data.Typelevel.Num (class Nat, toInt') import Data.Unfoldable (replicateA) -import Control.Monad.Gen.Class (class MonadGen, chooseInt) -import Type.Proxy (Proxy (..)) +import Data.Vec (Vec) +import Data.Vec (fromArray) as Vec import Partial.Unsafe (unsafePartial) +import Prelude ((<$>), bind, pure, (-), ($)) +import Type.Proxy (Proxy(..)) genDataView :: forall m diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 158134c..f74c150 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -18,28 +18,18 @@ module Data.ArrayBuffer.Typed , toString, toString', toArray ) where +import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) -import Data.ArrayBuffer.Types - ( ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength - , Float64Array, Float32Array - , Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array - , Float64, Float32 - , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) - - -import Prelude (Unit, pure, (<$>), (<<<), ($)) -import Effect (Effect) -import Effect.Uncurried - ( EffectFn4, EffectFn3, EffectFn2, EffectFn1 - , runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1 - , mkEffectFn2, mkEffectFn3) -import Effect.Unsafe (unsafePerformEffect) -import Data.Tuple (Tuple (..)) +import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, toNullable, toMaybe) +import Data.Nullable (Nullable, toMaybe, toNullable) +import Data.Tuple (Tuple(..)) import Data.UInt (UInt) import Data.UInt (fromNumber, toNumber) as UInt -import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) +import Effect (Effect) +import Effect.Uncurried (EffectFn4, EffectFn3, EffectFn2, EffectFn1, runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1, mkEffectFn2, mkEffectFn3) +import Effect.Unsafe (unsafePerformEffect) +import Prelude (Unit, pure, (<$>), (<<<), ($)) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 8085865..0ed8fb9 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -3,10 +3,7 @@ module Data.ArrayBuffer.ValueMapping where -import Data.ArrayBuffer.Types - ( kind ArrayViewType - , Float64, Float32 - , Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) +import Data.ArrayBuffer.Types (kind ArrayViewType, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.Typelevel.Num (D1, D2, D4, D8) import Data.UInt (UInt) From 970351c810e387add70fd74e83f437b409bce563 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 13:21:14 +0100 Subject: [PATCH 127/236] Sort includes --- test/Main.purs | 7 ++----- test/Properties.purs | 9 ++++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 2d004f8..40066b3 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,13 +1,10 @@ module Test.Main where -import Test.Properties (propertiesTests) - import Prelude + import Effect (Effect) import Effect.Console (log) - - - +import Test.Properties (propertiesTests) main :: Effect Unit main = do diff --git a/test/Properties.purs b/test/Properties.purs index f71e596..77f0d14 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -1,12 +1,11 @@ module Test.Properties where -import Test.Properties.TypedArray (typedArrayTests) -import Test.Properties.DataView (dataViewTests) - -import Prelude (Unit, bind, discard, ($), (<>), (*), show) import Effect (Effect) -import Effect.Ref (new, read) as Ref import Effect.Console (log) +import Effect.Ref (new, read) as Ref +import Prelude (Unit, bind, discard, ($), (<>), (*), show) +import Test.Properties.DataView (dataViewTests) +import Test.Properties.TypedArray (typedArrayTests) propertiesTests :: Effect Unit From a880ebb93ef72aefc4bb0b7f35b9caf345aec0d8 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 13:43:00 +0100 Subject: [PATCH 128/236] Still not happy with the names, renamed generators --- src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 4 ++-- src/Data/ArrayBuffer/Typed/Gen.purs | 24 +++++++++++----------- test/Properties/DataView.purs | 21 ++++++++++++------- test/Properties/TypedArray.purs | 25 +++++++++++++++-------- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index ce5fd91..8e6d607 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -2,7 +2,7 @@ module Data.ArrayBuffer.ArrayBuffer.Gen where import Control.Monad.Gen.Class (class MonadGen) import Data.ArrayBuffer.Typed (buffer) -import Data.ArrayBuffer.Typed.Gen (genUByte, genTypedArray) +import Data.ArrayBuffer.Typed.Gen (genTypedArray, genUint8) import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, Uint8Array) import Data.Maybe (Maybe) import Prelude ((<$>)) @@ -13,4 +13,4 @@ genArrayBuffer :: forall m => ByteLength -- ^ Min length -> Maybe ByteLength -- ^ Max length -> m ArrayBuffer -genArrayBuffer a b = buffer <$> (genTypedArray a b genUByte :: m Uint8Array) +genArrayBuffer a b = buffer <$> (genTypedArray a b genUint8 :: m Uint8Array) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 914e78d..2aa2474 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -34,23 +34,23 @@ genTypedArray lo mhi gen = sized \s -> in TA.fromArray <$> replicateA s' gen -genUByte :: forall m. MonadGen m => m UInt -genUByte = UInt.fromInt <$> chooseInt 0 255 +genUint8 :: forall m. MonadGen m => m UInt +genUint8 = UInt.fromInt <$> chooseInt 0 255 -genByte :: forall m. MonadGen m => m Int -genByte = chooseInt (-128) 127 +genInt8 :: forall m. MonadGen m => m Int +genInt8 = chooseInt (-128) 127 -genUShort :: forall m. MonadGen m => m UInt -genUShort = UInt.fromInt <$> chooseInt 0 65535 +genUint16 :: forall m. MonadGen m => m UInt +genUint16 = UInt.fromInt <$> chooseInt 0 65535 -genShort :: forall m. MonadGen m => m Int -genShort = chooseInt (-32768) 32767 +genInt16 :: forall m. MonadGen m => m Int +genInt16 = chooseInt (-32768) 32767 -genUInt :: forall m. MonadGen m => m UInt -genUInt = UInt.genUInt bottom top +genUint32 :: forall m. MonadGen m => m UInt +genUint32 = UInt.genUInt bottom top -genInt :: forall m. MonadGen m => m Int -genInt = chooseInt bottom top +genInt32 :: forall m. MonadGen m => m Int +genInt32 = chooseInt bottom top foreign import toFloat32 :: Number -> Number diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 9908e36..7ce16f8 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -5,7 +5,7 @@ import Prelude import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) -import Data.ArrayBuffer.Typed.Gen (genByte, genFloat32, genFloat64, genInt, genShort, genUByte, genUInt, genUShort) +import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genUint16, genUint32, genUint8) import Data.ArrayBuffer.Types (Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..)) @@ -48,37 +48,44 @@ overAll count f = do quickCheckGen $ let f' :: TestableViewF Uint32 D4 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUInt + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUint32 + log " - Uint16" quickCheckGen $ let f' :: TestableViewF Uint16 D2 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUShort + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUint16 + log " - Uint8" quickCheckGen $ let f' :: TestableViewF Uint8 D1 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUByte + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUint8 + log " - Int32" quickCheckGen $ let f' :: TestableViewF Int32 D4 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt32 + log " - Int16" quickCheckGen $ let f' :: TestableViewF Int16 D2 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genShort + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt16 + log " - Int8" quickCheckGen $ let f' :: TestableViewF Int8 D1 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genByte + in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt8 + log " - Float32" quickCheckGen $ let f' :: TestableViewF Float32 D4 n Number q f' = f in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genFloat32 + log " - Float64" quickCheckGen $ let f' :: TestableViewF Float64 D8 n Number q diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 086136c..32e2fae 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -6,7 +6,7 @@ import Prelude import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genByte, genFloat32, genFloat64, genInt, genShort, genTypedArray, genUByte, genUInt, genUShort, genWithOffset) +import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithOffset) import Data.ArrayBuffer.Types (ArrayView, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float32Array, Float64Array) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..)) @@ -120,22 +120,31 @@ type TestableArrayF a b n t q = overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit overAll count f = do void (Ref.modify (\x -> x + 1) count) + log " - Uint8ClampedArray" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUByte :: Gen Uint8ClampedArray)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint8 :: Gen Uint8ClampedArray)) + log " - Uint32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUInt :: Gen Uint32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint32 :: Gen Uint32Array)) + log " - Uint16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUShort :: Gen Uint16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint16 :: Gen Uint16Array)) + log " - Uint8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUByte :: Gen Uint8Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint8 :: Gen Uint8Array)) + log " - Int32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt :: Gen Int32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt32 :: Gen Int32Array)) + log " - Int16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genShort :: Gen Int16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt16 :: Gen Int16Array)) + log " - Int8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genByte :: Gen Int8Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt8 :: Gen Int8Array)) + log " - Float32Array" quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genFloat32 :: Gen Float32Array)) + log " - Float64Array" quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genFloat64 :: Gen Float64Array)) From 2a2795be14014fb2f887d0082717d9d28a86d639 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 13:58:05 +0100 Subject: [PATCH 129/236] toNullable Nothing -> null --- src/Data/ArrayBuffer/ArrayBuffer.purs | 6 +- src/Data/ArrayBuffer/Typed.purs | 118 +++++++++++++------------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 7ceec3f..3ae70a9 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -10,7 +10,7 @@ module Data.ArrayBuffer.ArrayBuffer import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, toNullable) +import Data.Nullable (Nullable, null, toNullable) import Data.Tuple (Tuple(..)) @@ -25,7 +25,7 @@ foreign import sliceImpl :: Fn3 ArrayBuffer (Nullable ByteOffset) (Nullable Byte -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer slice a mz = case mz of - Nothing -> runFn3 sliceImpl a (toNullable Nothing) (toNullable Nothing) + Nothing -> runFn3 sliceImpl a null null Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable Nothing) + Nothing -> runFn3 sliceImpl a (toNullable (Just s)) null Just e -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable (Just e)) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f74c150..086f8e1 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -22,7 +22,7 @@ import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteO import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, toMaybe, toNullable) +import Data.Nullable (Nullable, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.UInt (UInt) import Data.UInt (fromNumber, toNumber) as UInt @@ -176,17 +176,17 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) + remainder a x = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) null part a x y = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -204,17 +204,17 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint32 :: TypedArray Uint32 UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint32Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) + remainder a x = runEffectFn3 newUint32Array a (toNullable (Just x)) null part a x y = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newUint32Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newUint32Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) any p a = runEffectFn2 someImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) (UInt.toNumber <$> x) map f a = unsafePerformEffect $ runEffectFn2 mapImpl a $ @@ -233,17 +233,17 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint16 :: TypedArray Uint16 UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) + remainder a x = runEffectFn3 newUint16Array a (toNullable (Just x)) null part a x y = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newUint16Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newUint16Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -261,17 +261,17 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint8 :: TypedArray Uint8 UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) + remainder a x = runEffectFn3 newUint8Array a (toNullable (Just x)) null part a x y = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newUint8Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newUint8Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -289,17 +289,17 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt32 :: TypedArray Int32 Int where - whole a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) + remainder a x = runEffectFn3 newInt32Array a (toNullable (Just x)) null part a x y = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newInt32Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newInt32Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -317,17 +317,17 @@ instance typedArrayInt32 :: TypedArray Int32 Int where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt16 :: TypedArray Int16 Int where - whole a = unsafePerformEffect (runEffectFn3 newInt16Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) + remainder a x = runEffectFn3 newInt16Array a (toNullable (Just x)) null part a x y = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newInt16Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newInt16Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -345,17 +345,17 @@ instance typedArrayInt16 :: TypedArray Int16 Int where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt8 :: TypedArray Int8 Int where - whole a = unsafePerformEffect (runEffectFn3 newInt8Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) + remainder a x = runEffectFn3 newInt8Array a (toNullable (Just x)) null part a x y = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newInt8Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newInt8Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -373,17 +373,17 @@ instance typedArrayInt8 :: TypedArray Int8 Int where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat32 :: TypedArray Float32 Number where - whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) + remainder a x = runEffectFn3 newFloat32Array a (toNullable (Just x)) null part a x y = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -401,17 +401,17 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat64 :: TypedArray Float64 Number where - whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a (toNullable Nothing) (toNullable Nothing)) - remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable Nothing) + whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) + remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) null part a x y = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable (Just y)) - empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n (toNullable Nothing) (toNullable Nothing)) - fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a (toNullable Nothing) (toNullable Nothing)) + empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n null null) + fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) any p a = runEffectFn2 someImpl a (mkEffectFn2 p) fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable Nothing) + Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) @@ -469,9 +469,9 @@ foreign import sliceImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nulla -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. slice :: forall a. ArrayView a -> Range -> ArrayView a slice a mz = case mz of - Nothing -> runFn3 sliceImpl a (toNullable Nothing) (toNullable Nothing) + Nothing -> runFn3 sliceImpl a null null Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable Nothing) + Nothing -> runFn3 sliceImpl a (toNullable (Just s)) null Just e -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable (Just e)) @@ -493,9 +493,9 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu -- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. subArray :: forall a. ArrayView a -> Range -> ArrayView a subArray a mz = case mz of - Nothing -> runFn3 subArrayImpl a (toNullable Nothing) (toNullable Nothing) + Nothing -> runFn3 subArrayImpl a null null Just (Tuple s me) -> case me of - Nothing -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable Nothing) + Nothing -> runFn3 subArrayImpl a (toNullable (Just s)) null Just e -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable (Just e)) From 316f099cf9781936e9020a349b01ef1bbae763ec Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 14:20:45 +0100 Subject: [PATCH 130/236] toNullable . Just - > notNull --- src/Data/ArrayBuffer/ArrayBuffer.purs | 6 +- src/Data/ArrayBuffer/Typed.purs | 82 +++++++++++++-------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 3ae70a9..fe8a920 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -10,7 +10,7 @@ module Data.ArrayBuffer.ArrayBuffer import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, null, toNullable) +import Data.Nullable (Nullable, notNull, null) import Data.Tuple (Tuple(..)) @@ -27,5 +27,5 @@ slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuff slice a mz = case mz of Nothing -> runFn3 sliceImpl a null null Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (toNullable (Just s)) null - Just e -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runFn3 sliceImpl a (notNull s) null + Just e -> runFn3 sliceImpl a (notNull s) (notNull e) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 086f8e1..63aa51f 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -22,7 +22,7 @@ import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteO import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, null, toMaybe, toNullable) +import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.UInt (UInt) import Data.UInt (fromNumber, toNumber) as UInt @@ -177,8 +177,8 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) - remainder a x = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) null - part a x y = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newUint8ClampedArray a (notNull x) null + part a x y = runEffectFn3 newUint8ClampedArray a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -186,8 +186,8 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -205,8 +205,8 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint32 :: TypedArray Uint32 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) - remainder a x = runEffectFn3 newUint32Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newUint32Array a (notNull x) null + part a x y = runEffectFn3 newUint32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) @@ -214,8 +214,8 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (notNull s) null + Just e -> runEffectFn4 fillImpl a (UInt.toNumber x) (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) (UInt.toNumber <$> x) map f a = unsafePerformEffect $ runEffectFn2 mapImpl a $ mkEffectFn2 \x o -> pure $ UInt.toNumber $ f (UInt.fromNumber x) o @@ -234,8 +234,8 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint16 :: TypedArray Uint16 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) - remainder a x = runEffectFn3 newUint16Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newUint16Array a (notNull x) null + part a x y = runEffectFn3 newUint16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -243,8 +243,8 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -262,8 +262,8 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint8 :: TypedArray Uint8 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) - remainder a x = runEffectFn3 newUint8Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newUint8Array a (notNull x) null + part a x y = runEffectFn3 newUint8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -271,8 +271,8 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -290,8 +290,8 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt32 :: TypedArray Int32 Int where whole a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) - remainder a x = runEffectFn3 newInt32Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newInt32Array a (notNull x) null + part a x y = runEffectFn3 newInt32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -299,8 +299,8 @@ instance typedArrayInt32 :: TypedArray Int32 Int where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -318,8 +318,8 @@ instance typedArrayInt32 :: TypedArray Int32 Int where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt16 :: TypedArray Int16 Int where whole a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) - remainder a x = runEffectFn3 newInt16Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newInt16Array a (notNull x) null + part a x y = runEffectFn3 newInt16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -327,8 +327,8 @@ instance typedArrayInt16 :: TypedArray Int16 Int where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -346,8 +346,8 @@ instance typedArrayInt16 :: TypedArray Int16 Int where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt8 :: TypedArray Int8 Int where whole a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) - remainder a x = runEffectFn3 newInt8Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newInt8Array a (notNull x) null + part a x y = runEffectFn3 newInt8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -355,8 +355,8 @@ instance typedArrayInt8 :: TypedArray Int8 Int where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -374,8 +374,8 @@ instance typedArrayInt8 :: TypedArray Int8 Int where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat32 :: TypedArray Float32 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) - remainder a x = runEffectFn3 newFloat32Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newFloat32Array a (notNull x) null + part a x y = runEffectFn3 newFloat32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -383,8 +383,8 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -402,8 +402,8 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat64 :: TypedArray Float64 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) - remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable (Just y)) + remainder a x = runEffectFn3 newFloat64Array a (notNull x) null + part a x y = runEffectFn3 newFloat64Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) @@ -411,8 +411,8 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (toNullable (Just s)) null - Just e -> runEffectFn4 fillImpl a x (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) set a mo x = runEffectFn3 setImpl a (toNullable mo) x map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) @@ -471,8 +471,8 @@ slice :: forall a. ArrayView a -> Range -> ArrayView a slice a mz = case mz of Nothing -> runFn3 sliceImpl a null null Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (toNullable (Just s)) null - Just e -> runFn3 sliceImpl a (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runFn3 sliceImpl a (notNull s) null + Just e -> runFn3 sliceImpl a (notNull s) (notNull e) foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit @@ -495,8 +495,8 @@ subArray :: forall a. ArrayView a -> Range -> ArrayView a subArray a mz = case mz of Nothing -> runFn3 subArrayImpl a null null Just (Tuple s me) -> case me of - Nothing -> runFn3 subArrayImpl a (toNullable (Just s)) null - Just e -> runFn3 subArrayImpl a (toNullable (Just s)) (toNullable (Just e)) + Nothing -> runFn3 subArrayImpl a (notNull s) null + Just e -> runFn3 subArrayImpl a (notNull s) (notNull e) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. From cfd98c507a1ed261af1bdf4bd7ec91e1d6c2a800 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 17 Jan 2019 14:24:51 +0100 Subject: [PATCH 131/236] Typo --- src/Data/ArrayBuffer/Typed.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 63aa51f..aa16410 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -89,7 +89,7 @@ type Offset = Int -- | Value-oriented array length type Length = Int --- | Represents a range of indicies, where if omitted, it represents the whole span. +-- | Represents a range of indices, where if omitted, it represents the whole span. -- | If only the second argument is omitted, then it represents the remainder of the span after the first index. type Range = Maybe (Tuple Offset (Maybe Offset)) From 83370eb9f96aa1638f1c532d0d4c10bdad295d1c Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 17 Jan 2019 15:53:31 -0700 Subject: [PATCH 132/236] pure functions for filtering & querying --- src/Data/ArrayBuffer/Typed.purs | 112 ++++++++++++++++---------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 158134c..9ec54bb 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -39,7 +39,7 @@ import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, toNullable, toMaybe) import Data.UInt (UInt) import Data.UInt (fromNumber, toNumber) as UInt -import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) +import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3, mkFn2) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -75,21 +75,21 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N -- ---- -foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) Boolean -foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) Boolean +foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean +foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b (Nullable Offset) (Nullable Offset) Unit foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit -foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (ArrayView a) +foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) foreign import includesImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) Boolean foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (Nullable b) -foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (Nullable Offset) +foreign import findImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) +foreign import findIndexImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) foreign import indexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) foreign import lastIndexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) @@ -158,11 +158,11 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | Traverses over each value traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit -- | Test a predicate to pass on all values - all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + all :: (t -> Offset -> Boolean) -> ArrayView a -> Boolean -- | Test a predicate to pass on any value - any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + any :: (t -> Offset -> Boolean) -> ArrayView a -> Boolean -- | Returns a new typed array with all values that pass the predicate - filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) + filter :: (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a -- | Tests if a value is an element of the typed array elem :: t -> Maybe Offset -> ArrayView a -> Boolean -- | Fetch element at index. @@ -176,9 +176,9 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | Assumes the typed array is non-empty foldr1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -- | Returns the first value satisfying the predicate - find :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe t) + find :: (t -> Offset -> Boolean) -> ArrayView a -> Maybe t -- | Returns the first index of the value satisfying the predicate - findIndex :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe Offset) + findIndex :: (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset -- | Returns the first index of the element, if it exists, from the left indexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset -- | Returns the first index of the element, if it exists, from the right @@ -191,8 +191,8 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where part a x y = runEffectFn3 newUint8ClampedArray a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -202,15 +202,15 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint32 :: TypedArray Uint32 UInt where @@ -219,8 +219,8 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where part a x y = runEffectFn3 newUint32Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newUint32Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) - any p a = runEffectFn2 someImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) + all p a = runFn2 everyImpl a (mkFn2 (p <<< UInt.fromNumber)) + any p a = runFn2 someImpl a (mkFn2 (p <<< UInt.fromNumber)) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -231,15 +231,15 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where mkEffectFn2 \x o -> pure $ UInt.toNumber $ f (UInt.fromNumber x) o traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> UInt.toNumber <$> f (UInt.fromNumber x) o)) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 (f <<< UInt.fromNumber)) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) + filter p a = runFn2 filterImpl a (mkFn2 (p <<< UInt.fromNumber)) elem x mo a = runFn3 includesImpl a (UInt.toNumber x) (toNullable mo) unsafeAt o a = UInt.fromNumber <$> runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint16 :: TypedArray Uint16 UInt where @@ -248,8 +248,8 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where part a x y = runEffectFn3 newUint16Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newUint16Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -259,15 +259,15 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint8 :: TypedArray Uint8 UInt where @@ -276,8 +276,8 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where part a x y = runEffectFn3 newUint8Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newUint8Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -287,15 +287,15 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt32 :: TypedArray Int32 Int where @@ -304,8 +304,8 @@ instance typedArrayInt32 :: TypedArray Int32 Int where part a x y = runEffectFn3 newInt32Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newInt32Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -315,15 +315,15 @@ instance typedArrayInt32 :: TypedArray Int32 Int where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt16 :: TypedArray Int16 Int where @@ -332,8 +332,8 @@ instance typedArrayInt16 :: TypedArray Int16 Int where part a x y = runEffectFn3 newInt16Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newInt16Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -343,15 +343,15 @@ instance typedArrayInt16 :: TypedArray Int16 Int where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt8 :: TypedArray Int8 Int where @@ -360,8 +360,8 @@ instance typedArrayInt8 :: TypedArray Int8 Int where part a x y = runEffectFn3 newInt8Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newInt8Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -371,15 +371,15 @@ instance typedArrayInt8 :: TypedArray Int8 Int where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat32 :: TypedArray Float32 Number where @@ -388,8 +388,8 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where part a x y = runEffectFn3 newFloat32Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -399,15 +399,15 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat64 :: TypedArray Float64 Number where @@ -416,8 +416,8 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where part a x y = runEffectFn3 newFloat64Array a (toNullable (Just x)) (toNullable (Just y)) empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n (toNullable Nothing) (toNullable Nothing)) fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a (toNullable Nothing) (toNullable Nothing)) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x (toNullable Nothing) (toNullable Nothing) Just (Tuple s mq) -> case mq of @@ -427,15 +427,15 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) From 1b4aad4e05af31ac61537d44a22b762dfdffce1a Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Thu, 17 Jan 2019 15:53:31 -0700 Subject: [PATCH 133/236] Cherry-pick 83370eb9f96aa1638f1c532d0d4c10bdad295d1c, Uint32 instances no longer use toNumber/fromNumber --- src/Data/ArrayBuffer/Typed.purs | 117 ++++++++++++++++---------------- 1 file changed, 57 insertions(+), 60 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index aa16410..ffdd6dd 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -26,10 +26,7 @@ import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.UInt (UInt) import Data.UInt (fromNumber, toNumber) as UInt -import Effect (Effect) -import Effect.Uncurried (EffectFn4, EffectFn3, EffectFn2, EffectFn1, runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1, mkEffectFn2, mkEffectFn3) -import Effect.Unsafe (unsafePerformEffect) -import Prelude (Unit, pure, (<$>), (<<<), ($)) +import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3, mkFn2) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -65,21 +62,21 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N -- ---- -foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) Boolean -foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) Boolean +foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean +foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b (Nullable Offset) (Nullable Offset) Unit foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit -foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (ArrayView a) +foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) foreign import includesImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) Boolean foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (Nullable b) -foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Boolean) (Nullable Offset) +foreign import findImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) +foreign import findIndexImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) foreign import indexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) foreign import lastIndexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) @@ -148,11 +145,11 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | Traverses over each value traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit -- | Test a predicate to pass on all values - all :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + all :: (t -> Offset -> Boolean) -> ArrayView a -> Boolean -- | Test a predicate to pass on any value - any :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect Boolean + any :: (t -> Offset -> Boolean) -> ArrayView a -> Boolean -- | Returns a new typed array with all values that pass the predicate - filter :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (ArrayView a) + filter :: (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a -- | Tests if a value is an element of the typed array elem :: t -> Maybe Offset -> ArrayView a -> Boolean -- | Fetch element at index. @@ -166,9 +163,9 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh -- | Assumes the typed array is non-empty foldr1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -- | Returns the first value satisfying the predicate - find :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe t) + find :: (t -> Offset -> Boolean) -> ArrayView a -> Maybe t -- | Returns the first index of the value satisfying the predicate - findIndex :: (t -> Offset -> Effect Boolean) -> ArrayView a -> Effect (Maybe Offset) + findIndex :: (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset -- | Returns the first index of the element, if it exists, from the left indexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset -- | Returns the first index of the element, if it exists, from the right @@ -181,8 +178,8 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where part a x y = runEffectFn3 newUint8ClampedArray a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -192,15 +189,15 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint32 :: TypedArray Uint32 UInt where @@ -209,8 +206,8 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where part a x y = runEffectFn3 newUint32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) - any p a = runEffectFn2 someImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) + all p a = runFn2 everyImpl a (mkFn2 (p <<< UInt.fromNumber)) + any p a = runFn2 someImpl a (mkFn2 (p <<< UInt.fromNumber)) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) null null Just (Tuple s mq) -> case mq of @@ -221,15 +218,15 @@ instance typedArrayUint32 :: TypedArray Uint32 UInt where mkEffectFn2 \x o -> pure $ UInt.toNumber $ f (UInt.fromNumber x) o traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> UInt.toNumber <$> f (UInt.fromNumber x) o)) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 (f <<< UInt.fromNumber)) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 (p <<< UInt.fromNumber)) + filter p a = runFn2 filterImpl a (mkFn2 (p <<< UInt.fromNumber)) elem x mo a = runFn3 includesImpl a (UInt.toNumber x) (toNullable mo) unsafeAt o a = UInt.fromNumber <$> runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint16 :: TypedArray Uint16 UInt where @@ -238,8 +235,8 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where part a x y = runEffectFn3 newUint16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -249,15 +246,15 @@ instance typedArrayUint16 :: TypedArray Uint16 UInt where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayUint8 :: TypedArray Uint8 UInt where @@ -266,8 +263,8 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where part a x y = runEffectFn3 newUint8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -277,15 +274,15 @@ instance typedArrayUint8 :: TypedArray Uint8 UInt where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt32 :: TypedArray Int32 Int where @@ -294,8 +291,8 @@ instance typedArrayInt32 :: TypedArray Int32 Int where part a x y = runEffectFn3 newInt32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -305,15 +302,15 @@ instance typedArrayInt32 :: TypedArray Int32 Int where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt16 :: TypedArray Int16 Int where @@ -322,8 +319,8 @@ instance typedArrayInt16 :: TypedArray Int16 Int where part a x y = runEffectFn3 newInt16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -333,15 +330,15 @@ instance typedArrayInt16 :: TypedArray Int16 Int where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayInt8 :: TypedArray Int8 Int where @@ -350,8 +347,8 @@ instance typedArrayInt8 :: TypedArray Int8 Int where part a x y = runEffectFn3 newInt8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -361,15 +358,15 @@ instance typedArrayInt8 :: TypedArray Int8 Int where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat32 :: TypedArray Float32 Number where @@ -378,8 +375,8 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where part a x y = runEffectFn3 newFloat32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -389,25 +386,25 @@ instance typedArrayFloat32 :: TypedArray Float32 Number where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) instance typedArrayFloat64 :: TypedArray Float64 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) - remainder a x = runEffectFn3 newFloat64Array a (notNull x) null + remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) null part a x y = runEffectFn3 newFloat64Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) - all p a = runEffectFn2 everyImpl a (mkEffectFn2 p) - any p a = runEffectFn2 someImpl a (mkEffectFn2 p) + all p a = runFn2 everyImpl a (mkFn2 p) + any p a = runFn2 someImpl a (mkFn2 p) fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of @@ -417,15 +414,15 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runEffectFn2 filterImpl a (mkEffectFn2 p) + filter p a = runFn2 filterImpl a (mkFn2 p) elem x mo a = runFn3 includesImpl a x (toNullable mo) unsafeAt o a = runEffectFn2 unsafeAtImpl a o foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe <$> runEffectFn2 findImpl a (mkEffectFn2 f) - findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkEffectFn2 f) + find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) From d17d6c7a3da2ffe1f4bacf99a2371c1caf31b3b2 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 10:40:10 +0100 Subject: [PATCH 134/236] Fix the tests --- test/Properties/TypedArray.purs | 52 +++++++++++++++------------------ 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 32e2fae..ca51c34 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -174,7 +174,7 @@ allAreFilledTests count = overAll count allAreFilled Nothing -> zero Just y -> y TA.fill xs x Nothing - b <- TA.all (\y o -> pure (y == x)) xs + let b = TA.all (\y o -> y == x) xs pure (b "All aren't the filled value") @@ -196,9 +196,9 @@ allImpliesAnyTests count = overAll count allImpliesAny where allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result allImpliesAny (WithOffset _ xs) = - let pred x o = pure (x /= zero) - all' = unsafePerformEffect (TA.all pred xs) "All don't satisfy the predicate" - any' = unsafePerformEffect (TA.any pred xs) "None satisfy the predicate" + let pred x o = x /= zero + all' = TA.all pred xs "All don't satisfy the predicate" + any' = TA.any pred xs "None satisfy the predicate" in all' ==> any' @@ -208,9 +208,9 @@ filterImpliesAllTests count = overAll count filterImpliesAll where filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result filterImpliesAll (WithOffset _ xs) = - let pred x o = pure (x /= zero) - ys = unsafePerformEffect (TA.filter pred xs) - all' = unsafePerformEffect (TA.all pred ys) + let pred x o = x /= zero + ys = TA.filter pred xs + all' = TA.all pred ys in all' "Filter doesn't imply all" @@ -220,9 +220,9 @@ filterIsTotalTests count = overAll count filterIsTotal where filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result filterIsTotal (WithOffset _ xs) = - let pred x o = pure (x /= zero) - ys = unsafePerformEffect (TA.filter pred xs) - zs = unsafePerformEffect (TA.filter (\x o -> not <$> pred x o) ys) + let pred x o = x /= zero + ys = TA.filter pred xs + zs = TA.filter (\x o -> not pred x o) ys in TA.toArray zs === [] @@ -232,9 +232,9 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent where filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result filterIsIdempotent (WithOffset _ xs) = - let pred x o = pure (x /= zero) - ys = unsafePerformEffect (TA.filter pred xs) - zs = unsafePerformEffect (TA.filter pred ys) + let pred x o = x /= zero + ys = TA.filter pred xs + zs = TA.filter pred ys in TA.toArray zs === TA.toArray ys @@ -261,18 +261,14 @@ anyImpliesFindTests count = overAll count anyImpliesFind where anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result anyImpliesFind (WithOffset _ xs) = - let pred x o = pure (x /= zero) - p = unsafePerformEffect (TA.any pred xs) "All don't satisfy the predicate" - q = unsafePerformEffect do - mzs <- TA.find pred xs - case mzs of - Nothing -> pure (Failed "Doesn't have a value satisfying the predicate") - Just z -> do - b <- pred z 0 - pure $ - if b - then Success - else Failed "Found value doesn't satisfy the predicate" + let pred x o = x /= zero + p = TA.any pred xs "All don't satisfy the predicate" + q = + case TA.find pred xs of + Nothing -> Failed "Doesn't have a value satisfying the predicate" + Just z -> if pred z 0 + then Success + else Failed "Found value doesn't satisfy the predicate" in p ==> q @@ -282,13 +278,13 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt where findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result findIndexImpliesAt (WithOffset _ xs) = - let pred x o = pure (x /= zero) - mo = unsafePerformEffect (TA.findIndex pred xs) + let pred x o = x /= zero + mo = TA.findIndex pred xs in case mo of Nothing -> Success Just o -> case TA.at xs o of Nothing -> Failed "No value at found index" - Just x -> unsafePerformEffect (pred x o) "Find index implies at" + Just x -> pred x o "Find index implies at" From eac7856345ae49c88c1c2a7382d7dd84435cb4e4 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 10:41:00 +0100 Subject: [PATCH 135/236] These share the implementation... --- src/Data/ArrayBuffer/Typed.purs | 449 +++++++++++++++++--------------- 1 file changed, 245 insertions(+), 204 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index ffdd6dd..7b15b03 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -18,15 +18,18 @@ module Data.ArrayBuffer.Typed , toString, toString', toArray ) where +import Prelude + import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) -import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) +import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.UInt (UInt) -import Data.UInt (fromNumber, toNumber) as UInt -import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3, mkFn2) +import Effect (Effect) +import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) +import Effect.Unsafe (unsafePerformEffect) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -138,7 +141,7 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh fill :: ArrayView a -> t -> Range -> Effect Unit -- | Stores multiple values into the typed array set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit - -- | Maps a new value over the typed array, creating a new buffer and typed array as well. + -- | Maps a new value over the typed array, creating a new buffer and typed array aswell. map :: (t -> Offset -> t) -> ArrayView a -> ArrayView a -- | Traverses over each value, returning a new one traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) @@ -178,254 +181,292 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where part a x y = runEffectFn3 newUint8ClampedArray a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayUint32 :: TypedArray Uint32 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) remainder a x = runEffectFn3 newUint32Array a (notNull x) null part a x y = runEffectFn3 newUint32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint32Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array (UInt.toNumber <$> a) null null) - all p a = runFn2 everyImpl a (mkFn2 (p <<< UInt.fromNumber)) - any p a = runFn2 someImpl a (mkFn2 (p <<< UInt.fromNumber)) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a (UInt.toNumber x) (notNull s) null - Just e -> runEffectFn4 fillImpl a (UInt.toNumber x) (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) (UInt.toNumber <$> x) - map f a = unsafePerformEffect $ runEffectFn2 mapImpl a $ - mkEffectFn2 \x o -> pure $ UInt.toNumber $ f (UInt.fromNumber x) o - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> UInt.toNumber <$> f (UInt.fromNumber x) o)) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 (f <<< UInt.fromNumber)) - filter p a = runFn2 filterImpl a (mkFn2 (p <<< UInt.fromNumber)) - elem x mo a = runFn3 includesImpl a (UInt.toNumber x) (toNullable mo) - unsafeAt o a = UInt.fromNumber <$> runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayUint16 :: TypedArray Uint16 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) remainder a x = runEffectFn3 newUint16Array a (notNull x) null part a x y = runEffectFn3 newUint16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayUint8 :: TypedArray Uint8 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) remainder a x = runEffectFn3 newUint8Array a (notNull x) null part a x y = runEffectFn3 newUint8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayInt32 :: TypedArray Int32 Int where whole a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) remainder a x = runEffectFn3 newInt32Array a (notNull x) null part a x y = runEffectFn3 newInt32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayInt16 :: TypedArray Int16 Int where whole a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) remainder a x = runEffectFn3 newInt16Array a (notNull x) null part a x y = runEffectFn3 newInt16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayInt8 :: TypedArray Int8 Int where whole a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) remainder a x = runEffectFn3 newInt8Array a (notNull x) null part a x y = runEffectFn3 newInt8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayFloat32 :: TypedArray Float32 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) remainder a x = runEffectFn3 newFloat32Array a (notNull x) null part a x y = runEffectFn3 newFloat32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf instance typedArrayFloat64 :: TypedArray Float64 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) null part a x y = runEffectFn3 newFloat64Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) - all p a = runFn2 everyImpl a (mkFn2 p) - any p a = runFn2 someImpl a (mkFn2 p) - fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) - set a mo x = runEffectFn3 setImpl a (toNullable mo) x - map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) - traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) - traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) - filter p a = runFn2 filterImpl a (mkFn2 p) - elem x mo a = runFn3 includesImpl a x (toNullable mo) - unsafeAt o a = runEffectFn2 unsafeAtImpl a o - foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i - foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) - foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i - foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) - find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) - findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) - indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) - lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) + all = _all + any = _any + fill = _fill + set = _set + map = _map + traverse = _traverse + traverse_ = _traverse_ + filter = _filter + elem = _elem + unsafeAt = _unsafeAt + foldlM = _foldlM + foldl1M = _foldl1M + foldrM = _foldrM + foldr1M = _foldr1M + find = _find + findIndex = _findIndex + indexOf = _indexOf + lastIndexOf = _lastIndexOf + +-- | Fill the array with a value +_fill :: forall a t. ArrayView a -> t -> Range -> Effect Unit +_fill a x mz = case mz of + Nothing -> runEffectFn4 fillImpl a x null null + Just (Tuple s mq) -> case mq of + Nothing -> runEffectFn4 fillImpl a x (notNull s) null + Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) + +-- | Stores multiple values into the typed array +_set :: forall a t. ArrayView a -> Maybe Offset -> Array t -> Effect Unit +_set a mo x = runEffectFn3 setImpl a (toNullable mo) x + +-- | Maps a new value over the typed array, creating a new buffer and typed array as well. +_map :: forall a t. (t -> Offset -> t) -> ArrayView a -> ArrayView a +_map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + +-- | Traverses over each value, returning a new one +_traverse :: forall a t. (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) +_traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + +-- | Traverses over each value +_traverse_ :: forall a t. (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit +_traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + +-- | Test a predicate to pass on all values +_all :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Boolean +_all p a = runFn2 everyImpl a (mkFn2 p) + +-- | Test a predicate to pass on any value +_any :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Boolean +_any p a = runFn2 someImpl a (mkFn2 p) + +-- | Returns a new typed array with all values that pass the predicate +_filter :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a +_filter p a = runFn2 filterImpl a (mkFn2 p) + +-- | Tests if a value is an element of the typed array +_elem :: forall a t. t -> Maybe Offset -> ArrayView a -> Boolean +_elem x mo a = runFn3 includesImpl a x (toNullable mo) + +-- | Fetch element at index. +_unsafeAt :: forall a t. Offset -> ArrayView a -> Effect t +_unsafeAt o a = runEffectFn2 unsafeAtImpl a o + +-- | Folding from the left +_foldlM :: forall a t b. (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b +_foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + +-- | Assumes the typed array is non-empty +_foldl1M :: forall a t. (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t +_foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + +-- | Folding from the right +_foldrM :: forall a t b. (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b +_foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i + +-- | Assumes the typed array is non-empty +_foldr1M :: forall a t. (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t +_foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) + +-- | Returns the first value satisfying the predicate +_find :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Maybe t +_find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) + +-- | Returns the first index of the value satisfying the predicate +_findIndex :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset +_findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) + +-- | Returns the first index of the element, if it exists, from the left +_indexOf :: forall a t. t -> Maybe Offset -> ArrayView a -> Maybe Offset +_indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) +-- | Returns the first index of the element, if it exists, from the right +_lastIndexOf :: forall a t. t -> Maybe Offset -> ArrayView a -> Maybe Offset +_lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) foldl :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b foldl f i a = unsafePerformEffect (foldlM (\acc x o -> pure (f acc x o)) i a) From 8ae9e7c547dd77159c7b16dc2deef48d16f29dde Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 10:45:59 +0100 Subject: [PATCH 136/236] ... and don't need to be part of the type class --- src/Data/ArrayBuffer/Typed.purs | 271 +++++--------------------------- 1 file changed, 36 insertions(+), 235 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 7b15b03..4c9103d 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -137,43 +137,6 @@ class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t wh empty :: Length -> ArrayView a -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: Array t -> ArrayView a - -- | Fill the array with a value - fill :: ArrayView a -> t -> Range -> Effect Unit - -- | Stores multiple values into the typed array - set :: ArrayView a -> Maybe Offset -> Array t -> Effect Unit - -- | Maps a new value over the typed array, creating a new buffer and typed array aswell. - map :: (t -> Offset -> t) -> ArrayView a -> ArrayView a - -- | Traverses over each value, returning a new one - traverse :: (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) - -- | Traverses over each value - traverse_ :: (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit - -- | Test a predicate to pass on all values - all :: (t -> Offset -> Boolean) -> ArrayView a -> Boolean - -- | Test a predicate to pass on any value - any :: (t -> Offset -> Boolean) -> ArrayView a -> Boolean - -- | Returns a new typed array with all values that pass the predicate - filter :: (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a - -- | Tests if a value is an element of the typed array - elem :: t -> Maybe Offset -> ArrayView a -> Boolean - -- | Fetch element at index. - unsafeAt :: Offset -> ArrayView a -> Effect t - -- | Folding from the left - foldlM :: forall b. (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b - -- | Assumes the typed array is non-empty - foldl1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t - -- | Folding from the right - foldrM :: forall b. (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b - -- | Assumes the typed array is non-empty - foldr1M :: (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t - -- | Returns the first value satisfying the predicate - find :: (t -> Offset -> Boolean) -> ArrayView a -> Maybe t - -- | Returns the first index of the value satisfying the predicate - findIndex :: (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset - -- | Returns the first index of the element, if it exists, from the left - indexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset - -- | Returns the first index of the element, if it exists, from the right - lastIndexOf :: t -> Maybe Offset -> ArrayView a -> Maybe Offset - instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) @@ -181,292 +144,130 @@ instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where part a x y = runEffectFn3 newUint8ClampedArray a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayUint32 :: TypedArray Uint32 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) remainder a x = runEffectFn3 newUint32Array a (notNull x) null part a x y = runEffectFn3 newUint32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayUint16 :: TypedArray Uint16 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) remainder a x = runEffectFn3 newUint16Array a (notNull x) null part a x y = runEffectFn3 newUint16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayUint8 :: TypedArray Uint8 UInt where whole a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) remainder a x = runEffectFn3 newUint8Array a (notNull x) null part a x y = runEffectFn3 newUint8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newUint8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayInt32 :: TypedArray Int32 Int where whole a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) remainder a x = runEffectFn3 newInt32Array a (notNull x) null part a x y = runEffectFn3 newInt32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayInt16 :: TypedArray Int16 Int where whole a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) remainder a x = runEffectFn3 newInt16Array a (notNull x) null part a x y = runEffectFn3 newInt16Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt16Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayInt8 :: TypedArray Int8 Int where whole a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) remainder a x = runEffectFn3 newInt8Array a (notNull x) null part a x y = runEffectFn3 newInt8Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newInt8Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayFloat32 :: TypedArray Float32 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) remainder a x = runEffectFn3 newFloat32Array a (notNull x) null part a x y = runEffectFn3 newFloat32Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf instance typedArrayFloat64 :: TypedArray Float64 Number where whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) null part a x y = runEffectFn3 newFloat64Array a (notNull x) (notNull y) empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n null null) fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) - all = _all - any = _any - fill = _fill - set = _set - map = _map - traverse = _traverse - traverse_ = _traverse_ - filter = _filter - elem = _elem - unsafeAt = _unsafeAt - foldlM = _foldlM - foldl1M = _foldl1M - foldrM = _foldrM - foldr1M = _foldr1M - find = _find - findIndex = _findIndex - indexOf = _indexOf - lastIndexOf = _lastIndexOf -- | Fill the array with a value -_fill :: forall a t. ArrayView a -> t -> Range -> Effect Unit -_fill a x mz = case mz of +fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit +fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null Just (Tuple s mq) -> case mq of Nothing -> runEffectFn4 fillImpl a x (notNull s) null Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) -- | Stores multiple values into the typed array -_set :: forall a t. ArrayView a -> Maybe Offset -> Array t -> Effect Unit -_set a mo x = runEffectFn3 setImpl a (toNullable mo) x +set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Unit +set a mo x = runEffectFn3 setImpl a (toNullable mo) x -- | Maps a new value over the typed array, creating a new buffer and typed array as well. -_map :: forall a t. (t -> Offset -> t) -> ArrayView a -> ArrayView a -_map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) +map :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a +map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) -- | Traverses over each value, returning a new one -_traverse :: forall a t. (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) -_traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) +traverse :: forall a t. TypedArray a t => (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) -- | Traverses over each value -_traverse_ :: forall a t. (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit -_traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) +traverse_ :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit +traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) -- | Test a predicate to pass on all values -_all :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Boolean -_all p a = runFn2 everyImpl a (mkFn2 p) +all :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean +all p a = runFn2 everyImpl a (mkFn2 p) -- | Test a predicate to pass on any value -_any :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Boolean -_any p a = runFn2 someImpl a (mkFn2 p) +any :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean +any p a = runFn2 someImpl a (mkFn2 p) -- | Returns a new typed array with all values that pass the predicate -_filter :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a -_filter p a = runFn2 filterImpl a (mkFn2 p) +filter :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a +filter p a = runFn2 filterImpl a (mkFn2 p) -- | Tests if a value is an element of the typed array -_elem :: forall a t. t -> Maybe Offset -> ArrayView a -> Boolean -_elem x mo a = runFn3 includesImpl a x (toNullable mo) +elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolean +elem x mo a = runFn3 includesImpl a x (toNullable mo) -- | Fetch element at index. -_unsafeAt :: forall a t. Offset -> ArrayView a -> Effect t -_unsafeAt o a = runEffectFn2 unsafeAtImpl a o +unsafeAt :: forall a t. TypedArray a t => Offset -> ArrayView a -> Effect t +unsafeAt o a = runEffectFn2 unsafeAtImpl a o -- | Folding from the left -_foldlM :: forall a t b. (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -_foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i +foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b +foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i -- | Assumes the typed array is non-empty -_foldl1M :: forall a t. (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -_foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) +foldl1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t +foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) -- | Folding from the right -_foldrM :: forall a t b. (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -_foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i +foldrM :: forall a t b. TypedArray a t => (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b +foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i -- | Assumes the typed array is non-empty -_foldr1M :: forall a t. (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -_foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) +foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t +foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) -- | Returns the first value satisfying the predicate -_find :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Maybe t -_find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) +find :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe t +find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) -- | Returns the first index of the value satisfying the predicate -_findIndex :: forall a t. (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset -_findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) +findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset +findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) -- | Returns the first index of the element, if it exists, from the left -_indexOf :: forall a t. t -> Maybe Offset -> ArrayView a -> Maybe Offset -_indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) +indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset +indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) -- | Returns the first index of the element, if it exists, from the right -_lastIndexOf :: forall a t. t -> Maybe Offset -> ArrayView a -> Maybe Offset -_lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset +lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) foldl :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b foldl f i a = unsafePerformEffect (foldlM (\acc x o -> pure (f acc x o)) i a) From c11a9c00970f99fde16203a577279b8760571d41 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 11:06:13 +0100 Subject: [PATCH 137/236] These can also be removed from the type class --- src/Data/ArrayBuffer/Typed.purs | 88 ++++++++++++--------------------- 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 4c9103d..dbec4db 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -6,7 +6,7 @@ module Data.ArrayBuffer.Typed , Offset, Length, Range , buffer, byteOffset, byteLength, length , class TypedArray - , whole, remainder, part, empty, fromArray + , create, whole, remainder, part, empty, fromArray , fill, set, setTyped, copyWithin , map, traverse, traverse_, filter , sort, reverse @@ -126,72 +126,48 @@ type Range = Maybe (Tuple Offset (Maybe Offset)) -- | - `subArray` returns a new typed array with a separate array buffer -- | - `toString` prints to a CSV, `toString'` allows you to supply the delimiter -- | - `toArray` returns an array of numeric values + class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where - -- | View mapping the whole `ArrayBuffer`. - whole :: ArrayBuffer -> ArrayView a - -- | View mapping the rest of an `ArrayBuffer` after an index. - remainder :: ArrayBuffer -> ByteOffset -> Effect (ArrayView a) - -- | View mapping a region of the `ArrayBuffer`. - part :: ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) - -- | Creates an empty typed array, where each value is assigned 0 - empty :: Length -> ArrayView a - -- | Creates a typed array from an input array of values, to be binary serialized - fromArray :: Array t -> ArrayView a + create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) - remainder a x = runEffectFn3 newUint8ClampedArray a (notNull x) null - part a x y = runEffectFn3 newUint8ClampedArray a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newUint8ClampedArray n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newUint8ClampedArray a null null) + create = newUint8ClampedArray instance typedArrayUint32 :: TypedArray Uint32 UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) - remainder a x = runEffectFn3 newUint32Array a (notNull x) null - part a x y = runEffectFn3 newUint32Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newUint32Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newUint32Array a null null) + create = newUint32Array instance typedArrayUint16 :: TypedArray Uint16 UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) - remainder a x = runEffectFn3 newUint16Array a (notNull x) null - part a x y = runEffectFn3 newUint16Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newUint16Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newUint16Array a null null) + create = newUint16Array instance typedArrayUint8 :: TypedArray Uint8 UInt where - whole a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) - remainder a x = runEffectFn3 newUint8Array a (notNull x) null - part a x y = runEffectFn3 newUint8Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newUint8Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newUint8Array a null null) + create = newUint8Array instance typedArrayInt32 :: TypedArray Int32 Int where - whole a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) - remainder a x = runEffectFn3 newInt32Array a (notNull x) null - part a x y = runEffectFn3 newInt32Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newInt32Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newInt32Array a null null) + create = newInt32Array instance typedArrayInt16 :: TypedArray Int16 Int where - whole a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) - remainder a x = runEffectFn3 newInt16Array a (notNull x) null - part a x y = runEffectFn3 newInt16Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newInt16Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newInt16Array a null null) + create = newInt16Array instance typedArrayInt8 :: TypedArray Int8 Int where - whole a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) - remainder a x = runEffectFn3 newInt8Array a (notNull x) null - part a x y = runEffectFn3 newInt8Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newInt8Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newInt8Array a null null) + create = newInt8Array instance typedArrayFloat32 :: TypedArray Float32 Number where - whole a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) - remainder a x = runEffectFn3 newFloat32Array a (notNull x) null - part a x y = runEffectFn3 newFloat32Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newFloat32Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newFloat32Array a null null) + create = newFloat32Array instance typedArrayFloat64 :: TypedArray Float64 Number where - whole a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) - remainder a x = runEffectFn3 newFloat64Array a (toNullable (Just x)) null - part a x y = runEffectFn3 newFloat64Array a (notNull x) (notNull y) - empty n = unsafePerformEffect (runEffectFn3 newFloat64Array n null null) - fromArray a = unsafePerformEffect (runEffectFn3 newFloat64Array a null null) + create = newFloat64Array + +-- | View mapping the whole `ArrayBuffer`. +whole :: forall a t. TypedArray a t => ArrayBuffer -> ArrayView a +whole a = unsafePerformEffect (runEffectFn3 create a null null) + +-- | View mapping the rest of an `ArrayBuffer` after an index. +remainder :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) +remainder a x = runEffectFn3 create a (toNullable (Just x)) null + +-- | View mapping a region of the `ArrayBuffer`. +part :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) +part a x y = runEffectFn3 create a (notNull x) (notNull y) + +-- | Creates an empty typed array, where each value is assigned 0 +empty :: forall a t. TypedArray a t => Length -> ArrayView a +empty n = unsafePerformEffect (runEffectFn3 create n null null) + +-- | Creates a typed array from an input array of values, to be binary serialized +fromArray :: forall a t. TypedArray a t => Array t -> ArrayView a +fromArray a = unsafePerformEffect (runEffectFn3 create a null null) -- | Fill the array with a value fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit From be68a82b89e8a50f10053be00343f86695944ef1 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 11:20:16 +0100 Subject: [PATCH 138/236] Simplify slice --- src/Data/ArrayBuffer/Typed.purs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index dbec4db..68e79b3 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -285,10 +285,7 @@ foreign import sliceImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nulla slice :: forall a. ArrayView a -> Range -> ArrayView a slice a mz = case mz of Nothing -> runFn3 sliceImpl a null null - Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (notNull s) null - Just e -> runFn3 sliceImpl a (notNull s) (notNull e) - + Just (Tuple s me) -> runFn3 sliceImpl a (notNull s) (toNullable me) foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit From ab94c97f3811bd38f28997e55f06a6a3414d8190 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 11:21:19 +0100 Subject: [PATCH 139/236] Simplify subArray --- src/Data/ArrayBuffer/Typed.purs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 68e79b3..acfab38 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -306,10 +306,7 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu subArray :: forall a. ArrayView a -> Range -> ArrayView a subArray a mz = case mz of Nothing -> runFn3 subArrayImpl a null null - Just (Tuple s me) -> case me of - Nothing -> runFn3 subArrayImpl a (notNull s) null - Just e -> runFn3 subArrayImpl a (notNull s) (notNull e) - + Just (Tuple s me) -> runFn3 subArrayImpl a (notNull s) (toNullable me) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String From 48cff7aa5cf3044f41481e48260ca59616ef3be2 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 13:37:19 +0100 Subject: [PATCH 140/236] unsafeAt should be like unsafeIndex in arrays --- src/Data/ArrayBuffer/Typed.purs | 14 +++++++------- test/Properties/TypedArray.purs | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index acfab38..aae2440 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -30,6 +30,7 @@ import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) +import Partial.Unsafe (unsafePartial) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -210,8 +211,8 @@ elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolea elem x mo a = runFn3 includesImpl a x (toNullable mo) -- | Fetch element at index. -unsafeAt :: forall a t. TypedArray a t => Offset -> ArrayView a -> Effect t -unsafeAt o a = runEffectFn2 unsafeAtImpl a o +unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> t +unsafeAt = runFn2 unsafeAtImpl -- | Folding from the left foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b @@ -318,7 +319,7 @@ toString' :: forall a. ArrayView a -> String -> String toString' = runFn2 joinImpl -foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b +foreign import unsafeAtImpl :: forall a b. Fn2 (ArrayView a) Offset b foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean @@ -328,10 +329,9 @@ hasIndex = runFn2 hasIndexImpl -- | Fetch element at index. at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t -at a n = do - if a `hasIndex` n - then Just (unsafePerformEffect (unsafeAt n a)) - else Nothing +at a n = if a `hasIndex` n + then Just (unsafePartial (unsafeAt a n)) + else Nothing infixl 3 at as ! diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index ca51c34..07cb3c3 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -18,6 +18,7 @@ import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) +import Partial.Unsafe (unsafePartial) import Test.QuickCheck (quickCheckGen, Result(..), (===), (/==), class Testable, ()) import Test.QuickCheck.Combinators ((==>)) import Test.QuickCheck.Gen (Gen) @@ -251,7 +252,7 @@ withOffsetElemTests count = overAll count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = - Array.all (\o -> TA.elem (unsafePerformEffect (TA.unsafeAt o xs)) Nothing xs) os + Array.all (\o -> TA.elem (unsafePartial (TA.unsafeAt xs o)) Nothing xs) os "All doesn't have an elem of itself" From 48d5826a5dc25109ea93a240c3f7d85429389794 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 13:40:21 +0100 Subject: [PATCH 141/236] Simplify fill --- src/Data/ArrayBuffer/Typed.purs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index aae2440..01bf228 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -174,9 +174,7 @@ fromArray a = unsafePerformEffect (runEffectFn3 create a null null) fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> case mq of - Nothing -> runEffectFn4 fillImpl a x (notNull s) null - Just e -> runEffectFn4 fillImpl a x (notNull s) (notNull e) + Just (Tuple s mq) -> runEffectFn4 fillImpl a x (notNull s) (toNullable mq) -- | Stores multiple values into the typed array set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Unit From e08e284ad05cf32f3651af3866771bb692195d19 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 13:40:56 +0100 Subject: [PATCH 142/236] Better name --- src/Data/ArrayBuffer/Typed.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 01bf228..ab459f0 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -174,7 +174,7 @@ fromArray a = unsafePerformEffect (runEffectFn3 create a null null) fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit fill a x mz = case mz of Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s mq) -> runEffectFn4 fillImpl a x (notNull s) (toNullable mq) + Just (Tuple s me) -> runEffectFn4 fillImpl a x (notNull s) (toNullable me) -- | Stores multiple values into the typed array set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Unit From 17cf8816c124b62fb418924b672ac053cfe31f0a Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 16:37:28 +0100 Subject: [PATCH 143/236] Use notNull --- src/Data/ArrayBuffer/Typed.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index ab459f0..070ba5e 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -156,7 +156,7 @@ whole a = unsafePerformEffect (runEffectFn3 create a null null) -- | View mapping the rest of an `ArrayBuffer` after an index. remainder :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) -remainder a x = runEffectFn3 create a (toNullable (Just x)) null +remainder a x = runEffectFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. part :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) From 2b7b502de19c2a5379058f61bd710adb6c8b1ec3 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 18:47:30 +0100 Subject: [PATCH 144/236] WIP, generate arrays with the size provided by QC, split functions that take ByteOffset --- src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 6 +-- src/Data/ArrayBuffer/DataView/Gen.purs | 6 +-- src/Data/ArrayBuffer/Typed.js | 17 +++++--- src/Data/ArrayBuffer/Typed.purs | 48 ++++++++++++++--------- src/Data/ArrayBuffer/Typed/Gen.purs | 19 ++++----- test/Properties/DataView.purs | 16 ++++---- test/Properties/TypedArray.purs | 35 ++++++++++++----- 7 files changed, 87 insertions(+), 60 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index 8e6d607..f07b227 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -10,7 +10,5 @@ import Prelude ((<$>)) genArrayBuffer :: forall m . MonadGen m - => ByteLength -- ^ Min length - -> Maybe ByteLength -- ^ Max length - -> m ArrayBuffer -genArrayBuffer a b = buffer <$> (genTypedArray a b genUint8 :: m Uint8Array) + => m ArrayBuffer +genArrayBuffer = buffer <$> (genTypedArray genUint8 :: m Uint8Array) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 608fbc9..487d641 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -17,10 +17,8 @@ import Type.Proxy (Proxy(..)) genDataView :: forall m . MonadGen m - => ByteLength -- ^ Min length - -> Maybe ByteLength -- ^ Max length - -> m DataView -genDataView a b = whole <$> genArrayBuffer a b + => m DataView +genDataView = whole <$> genArrayBuffer diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 05e8dc0..cbadfb4 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -39,12 +39,17 @@ exports.lengthImpl = function lemgthImpl (v) { function newArray (f) { - return function newArray_ (a,mb,mc) { - return mc === null ? ( mb === null ? new f(a) - : new f(a,mb) - ) - : new f(a,mb,mc); - }; + return function newArray_ (a,mb,mc) { + if (mb === null) + return new f(a); + var l = a.byteLength; + var eb = f.BYTES_PER_ELEMENT; + var off = Math.min(l, mb|0); + if (mc === null) + return new f(a,off); + var len = Math.min((l - off) / eb, mc); + return new f(a,off,len); + }; } exports.newUint8ClampedArray = newArray(Uint8ClampedArray); diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 070ba5e..3fea2da 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -21,16 +21,18 @@ module Data.ArrayBuffer.Typed import Prelude import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) +import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) +import Type.Proxy (Proxy(..)) -- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill @@ -47,21 +49,21 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength foreign import lengthImpl :: forall a. ArrayView a -> Length -length :: forall a b. BytesPerValue a b => ArrayView a -> Int +length :: forall a. ArrayView a -> Int length = lengthImpl -- object creator implementations for each typed array -foreign import newUint8ClampedArray :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray -foreign import newUint32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array -foreign import newUint16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array -foreign import newUint8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array -foreign import newInt32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array -foreign import newInt16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array -foreign import newInt8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array -foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array -foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array +foreign import newUint8ClampedArray :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray +foreign import newUint32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array +foreign import newUint16Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array +foreign import newUint8Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array +foreign import newInt32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array +foreign import newInt16Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array +foreign import newInt8Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array +foreign import newFloat32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array +foreign import newFloat64Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array -- ---- @@ -129,7 +131,7 @@ type Range = Maybe (Tuple Offset (Maybe Offset)) -- | - `toArray` returns an array of numeric values class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where - create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) + create :: forall x. Fn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where create = newUint8ClampedArray @@ -152,23 +154,31 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where -- | View mapping the whole `ArrayBuffer`. whole :: forall a t. TypedArray a t => ArrayBuffer -> ArrayView a -whole a = unsafePerformEffect (runEffectFn3 create a null null) +whole a = runFn3 create a null null -- | View mapping the rest of an `ArrayBuffer` after an index. -remainder :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) -remainder a x = runEffectFn3 create a (notNull x) null +remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> ArrayView a +remainder a x = remainder' a o + where o = x * toInt' (Proxy :: Proxy b) + +remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> ArrayView a +remainder' a x = runFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. -part :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) -part a x y = runEffectFn3 create a (notNull x) (notNull y) +part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Length -> ArrayView a +part a x y = part' a o y + where o = x * toInt' (Proxy :: Proxy b) + +part' :: forall a b t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> ArrayView a +part' a x y = runFn3 create a (notNull x) (notNull y) -- | Creates an empty typed array, where each value is assigned 0 empty :: forall a t. TypedArray a t => Length -> ArrayView a -empty n = unsafePerformEffect (runEffectFn3 create n null null) +empty n = runFn3 create n null null -- | Creates a typed array from an input array of values, to be binary serialized fromArray :: forall a t. TypedArray a t => Array t -> ArrayView a -fromArray a = unsafePerformEffect (runEffectFn3 create a null null) +fromArray a = runFn3 create a null null -- | Fill the array with a value fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 2aa2474..acb4985 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -9,7 +9,7 @@ import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Generic.Rep (class Generic) -import Data.Maybe (Maybe(..), fromMaybe) +import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Data.UInt (fromInt) as UInt @@ -24,14 +24,14 @@ import Type.Proxy (Proxy(..)) genTypedArray :: forall m a t . MonadGen m => TA.TypedArray a t - => TA.Length -- ^ Min length - -> Maybe TA.Length -- ^ Max length - -> m t + => m t -> m (ArrayView a) -genTypedArray lo mhi gen = sized \s -> - let hi = fromMaybe s mhi - s' = clamp lo hi s - in TA.fromArray <$> replicateA s' gen +genTypedArray gen = sized \s -> do + n <- chooseInt 0 s + a <- replicateA n gen + pure $ TA.fromArray a +-- chooseInt 0 s >>= flip replicateA gen >>= TA.fromArray + --TA.fromArray <$> flip replicateA gen <*> chooseInt 0 s genUint8 :: forall m. MonadGen m => m UInt @@ -58,7 +58,8 @@ genFloat32 :: forall m. MonadGen m => m Number genFloat32 = toFloat32 <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 genFloat64 :: forall m. MonadGen m => m Number -genFloat64 = chooseFloat (-1.7976931348623157e+308) 1.7976931348623157e+308 +genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+308/div) + where div = 4.0 -- | For generating some set of offsets residing inside the generated array data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 7ce16f8..d399e1b 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -48,49 +48,49 @@ overAll count f = do quickCheckGen $ let f' :: TestableViewF Uint32 D4 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUint32 + in f' <$> genWithOffsetAndValue genDataView genUint32 log " - Uint16" quickCheckGen $ let f' :: TestableViewF Uint16 D2 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUint16 + in f' <$> genWithOffsetAndValue genDataView genUint16 log " - Uint8" quickCheckGen $ let f' :: TestableViewF Uint8 D1 n UInt q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genUint8 + in f' <$> genWithOffsetAndValue genDataView genUint8 log " - Int32" quickCheckGen $ let f' :: TestableViewF Int32 D4 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt32 + in f' <$> genWithOffsetAndValue genDataView genInt32 log " - Int16" quickCheckGen $ let f' :: TestableViewF Int16 D2 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt16 + in f' <$> genWithOffsetAndValue genDataView genInt16 log " - Int8" quickCheckGen $ let f' :: TestableViewF Int8 D1 n Int q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genInt8 + in f' <$> genWithOffsetAndValue genDataView genInt8 log " - Float32" quickCheckGen $ let f' :: TestableViewF Float32 D4 n Number q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genFloat32 + in f' <$> genWithOffsetAndValue genDataView genFloat32 log " - Float64" quickCheckGen $ let f' :: TestableViewF Float64 D8 n Number q f' = f - in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genFloat64 + in f' <$> genWithOffsetAndValue genDataView genFloat64 placingAValueIsThereTestsBE :: Ref Int -> Effect Unit diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 07cb3c3..935fbfb 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -2,7 +2,8 @@ module Test.Properties.TypedArray where import Prelude - +import Debug.Trace(spy) +import Data.Array (drop, take) import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA @@ -27,6 +28,8 @@ import Type.Proxy (Proxy(..)) typedArrayTests :: Ref Int -> Effect Unit typedArrayTests count = do + log "XXXXXX" + partBehavesLikeTakeDrop count log " - byteLength x / bytesPerValue === length x" byteLengthDivBytesPerValueTests count log " - fromArray (toArray x) === x" @@ -123,32 +126,44 @@ overAll count f = do void (Ref.modify (\x -> x + 1) count) log " - Uint8ClampedArray" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint8 :: Gen Uint8ClampedArray)) + quickCheckGen (f <$> genWithOffset (genTypedArray genUint8 :: Gen Uint8ClampedArray)) log " - Uint32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint32 :: Gen Uint32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genUint32 :: Gen Uint32Array)) log " - Uint16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint16 :: Gen Uint16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genUint16 :: Gen Uint16Array)) log " - Uint8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genUint8 :: Gen Uint8Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genUint8 :: Gen Uint8Array)) log " - Int32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt32 :: Gen Int32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genInt32 :: Gen Int32Array)) log " - Int16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt16 :: Gen Int16Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genInt16 :: Gen Int16Array)) log " - Int8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genInt8 :: Gen Int8Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genInt8 :: Gen Int8Array)) log " - Float32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genFloat32 :: Gen Float32Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genFloat32 :: Gen Float32Array)) log " - Float64Array" - quickCheckGen (f <$> genWithOffset (genTypedArray 10 Nothing genFloat64 :: Gen Float64Array)) + quickCheckGen (f <$> genWithOffset (genTypedArray genFloat64 :: Gen Float64Array)) + +partBehavesLikeTakeDrop :: Ref Int -> Effect Unit +partBehavesLikeTakeDrop count = overAll count f + where + f :: forall a b t. TestableArrayF a b D0 t Result + f (WithOffset _ a) = + let n = 2 + na = TA.toArray a + ba = TA.buffer (spy "arr" a) + pa :: ArrayView a + pa = TA.part ba n n + in take n (drop n na) === TA.toArray pa byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValueEqLength From b4a9b19ef75eb36b7977327179c84a927718ccb5 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 18:48:44 +0100 Subject: [PATCH 145/236] Cleanup --- src/Data/ArrayBuffer/Typed/Gen.purs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index acb4985..09c88e8 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -30,9 +30,6 @@ genTypedArray gen = sized \s -> do n <- chooseInt 0 s a <- replicateA n gen pure $ TA.fromArray a --- chooseInt 0 s >>= flip replicateA gen >>= TA.fromArray - --TA.fromArray <$> flip replicateA gen <*> chooseInt 0 s - genUint8 :: forall m. MonadGen m => m UInt genUint8 = UInt.fromInt <$> chooseInt 0 255 From a30e60283d4731f1f768e3d33b3b6edf981b64c9 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 19:39:02 +0100 Subject: [PATCH 146/236] Fix set/setTyped --- src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 3 +-- src/Data/ArrayBuffer/DataView/Gen.purs | 2 +- src/Data/ArrayBuffer/Typed.js | 7 ++----- src/Data/ArrayBuffer/Typed.purs | 24 +++++++++++++++-------- test/Properties/TypedArray.purs | 18 ++++++++--------- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index f07b227..dbe909a 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -3,8 +3,7 @@ module Data.ArrayBuffer.ArrayBuffer.Gen where import Control.Monad.Gen.Class (class MonadGen) import Data.ArrayBuffer.Typed (buffer) import Data.ArrayBuffer.Typed.Gen (genTypedArray, genUint8) -import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, Uint8Array) -import Data.Maybe (Maybe) +import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array) import Prelude ((<$>)) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 487d641..c77ba0a 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -3,7 +3,7 @@ module Data.ArrayBuffer.DataView.Gen where import Control.Monad.Gen.Class (class MonadGen, chooseInt) import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole, byteLength, class DataView) -import Data.ArrayBuffer.Types (DataView, ByteLength, ByteOffset, kind ArrayViewType) +import Data.ArrayBuffer.Types (DataView, ByteOffset, kind ArrayViewType) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(Just)) import Data.Typelevel.Num (class Nat, toInt') diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index cbadfb4..f5f6b67 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -141,11 +141,8 @@ exports.reverseImpl = function reverseImpl (a) { exports.setImpl = function setImpl (a, off, b) { - if (off === null) { - a.set(b); - } else { - a.set(b,off); - } + console.log(a, b, off); + a.set(b,off); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 3fea2da..b166483 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -20,10 +20,11 @@ module Data.ArrayBuffer.Typed import Prelude +import Data.Array (length) as A import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) -import Data.Maybe (Maybe(..)) +import Data.Maybe (Maybe(..), fromMaybe) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.Typelevel.Num (class Nat, toInt') @@ -49,7 +50,7 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength foreign import lengthImpl :: forall a. ArrayView a -> Length -length :: forall a. ArrayView a -> Int +length :: forall a. ArrayView a -> Length length = lengthImpl @@ -169,7 +170,7 @@ part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffe part a x y = part' a o y where o = x * toInt' (Proxy :: Proxy b) -part' :: forall a b t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> ArrayView a +part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> ArrayView a part' a x y = runFn3 create a (notNull x) (notNull y) -- | Creates an empty typed array, where each value is assigned 0 @@ -187,8 +188,8 @@ fill a x mz = case mz of Just (Tuple s me) -> runEffectFn4 fillImpl a x (notNull s) (toNullable me) -- | Stores multiple values into the typed array -set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Unit -set a mo x = runEffectFn3 setImpl a (toNullable mo) x +set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean +set = setInternal A.length -- | Maps a new value over the typed array, creating a new buffer and typed array as well. map :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a @@ -279,12 +280,19 @@ foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit reverse :: forall a. ArrayView a -> Effect Unit reverse = runEffectFn1 reverseImpl -foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) (Nullable Offset) b Unit +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit + +setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Offset -> b -> Effect Boolean +setInternal lenfn a mo b = + let o = fromMaybe 0 mo + in if o >= 0 && lenfn b <= length a - o + then runEffectFn3 setImpl a o b *> pure true + else pure false -- | Stores multiple values in the typed array, reading input values from the second typed array. -setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Unit -setTyped a mo x = runEffectFn3 setImpl a (toNullable mo) x +setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Boolean +setTyped = setInternal length -- | Copy the entire contents of the typed array into a new buffer. diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 935fbfb..756da84 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -2,7 +2,7 @@ module Test.Properties.TypedArray where import Prelude -import Debug.Trace(spy) + import Data.Array (drop, take) import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) @@ -160,7 +160,7 @@ partBehavesLikeTakeDrop count = overAll count f f (WithOffset _ a) = let n = 2 na = TA.toArray a - ba = TA.buffer (spy "arr" a) + ba = TA.buffer a pa :: ArrayView a pa = TA.part ba n n in take n (drop n na) === TA.toArray pa @@ -199,11 +199,11 @@ setSingletonIsEqTests count = overAll count setSingletonIsEq where setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result setSingletonIsEq (WithOffset os xs) = unsafePerformEffect do - let x = case TA.at xs 0 of - Nothing -> zero - Just y -> y - TA.set xs (Just (Vec.head os)) [x] - pure (TA.at xs (Vec.head os) === Just x) + case TA.at xs 0 of + Nothing -> pure Success + Just x -> do + _ <- TA.set xs (Just (Vec.head os)) [x] + pure (TA.at xs (Vec.head os) === Just x) -- | Should work with any arbitrary predicate, but we can't generate them @@ -428,7 +428,7 @@ setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIden let ys = TA.toArray xs zsSub = TA.subArray xs Nothing zs = unsafePerformEffect do - TA.setTyped xs Nothing zsSub + _ <- TA.setTyped xs Nothing zsSub pure (TA.toArray xs) in zs === ys @@ -642,6 +642,6 @@ copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped xs' = TA.fromArray (TA.toArray xs) :: ArrayView a _ = unsafePerformEffect do let ys = TA.slice xs' (Just (Tuple o Nothing)) - TA.setTyped xs' Nothing ys + _ <- TA.setTyped xs' Nothing ys TA.copyWithin xs 0 o Nothing in TA.toArray xs === TA.toArray xs' From 42f588febadca61f7e0fa510d183eff763812866 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 19:40:26 +0100 Subject: [PATCH 147/236] Cleanup --- src/Data/ArrayBuffer/Typed.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index f5f6b67..e7e643e 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -141,7 +141,6 @@ exports.reverseImpl = function reverseImpl (a) { exports.setImpl = function setImpl (a, off, b) { - console.log(a, b, off); a.set(b,off); }; From 8ee12638a02eff5e4d1b4b259959a7b12c16227d Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 18 Jan 2019 19:54:38 +0100 Subject: [PATCH 148/236] Fix allImpliesAnyTests --- test/Properties/TypedArray.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 756da84..11e322e 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -21,7 +21,7 @@ import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) import Test.QuickCheck (quickCheckGen, Result(..), (===), (/==), class Testable, ()) -import Test.QuickCheck.Combinators ((==>)) +import Test.QuickCheck.Combinators ((==>), (|=|)) import Test.QuickCheck.Gen (Gen) import Type.Proxy (Proxy(..)) @@ -215,7 +215,7 @@ allImpliesAnyTests count = overAll count allImpliesAny let pred x o = x /= zero all' = TA.all pred xs "All don't satisfy the predicate" any' = TA.any pred xs "None satisfy the predicate" - in all' ==> any' + in (TA.length xs === zero) |=| all' ==> any' -- | Should work with any arbitrary predicate, but we can't generate them From 7cde0262ee153828209a21a6c740e6c8b6c9c716 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 19 Jan 2019 13:37:56 +0100 Subject: [PATCH 149/236] Use unsigned --- src/Data/ArrayBuffer/Typed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index e7e643e..fb56808 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -44,7 +44,7 @@ function newArray (f) { return new f(a); var l = a.byteLength; var eb = f.BYTES_PER_ELEMENT; - var off = Math.min(l, mb|0); + var off = Math.min(l, mb>>>0); if (mc === null) return new f(a,off); var len = Math.min((l - off) / eb, mc); From 95c4abc1e1c5cdc1cb0b9a2e4ca998d8899b48f4 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 19 Jan 2019 13:41:43 +0100 Subject: [PATCH 150/236] Disable DataView tests --- test/Properties.purs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/Properties.purs b/test/Properties.purs index 77f0d14..33679ca 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -17,9 +17,9 @@ propertiesTests = do c <- Ref.read count log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." - do - count <- Ref.new 0 - log " - DataView Tests:" - dataViewTests count - c <- Ref.read count - log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." + -- do + -- count <- Ref.new 0 + -- log " - DataView Tests:" + -- dataViewTests count + -- c <- Ref.read count + -- log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." From 01c657e59b817ccdc95abec36baec0ef73b9470a Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 19 Jan 2019 13:42:19 +0100 Subject: [PATCH 151/236] Simplifying --- src/Data/ArrayBuffer/Typed/Gen.purs | 7 ++--- test/Properties/TypedArray.purs | 43 ++++++++++++++--------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 09c88e8..e617f96 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -5,9 +5,9 @@ module Data.ArrayBuffer.Typed.Gen where import Prelude import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) +import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Generic.Rep (class Generic) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, toInt') @@ -23,7 +23,7 @@ import Type.Proxy (Proxy(..)) genTypedArray :: forall m a t . MonadGen m - => TA.TypedArray a t + => TypedArray a t => m t -> m (ArrayView a) genTypedArray gen = sized \s -> do @@ -62,10 +62,9 @@ genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+3 data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) derive instance genericWithOffset :: Generic (ArrayView a) a' => Generic (WithOffset n a) _ -genWithOffset :: forall m n b a +genWithOffset :: forall m n a . MonadGen m => Nat n - => BytesPerValue a b => m (ArrayView a) -> m (WithOffset n a) genWithOffset gen = do diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 11e322e..b2862d8 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -3,16 +3,17 @@ module Test.Properties.TypedArray where import Prelude +import Control.Monad.Gen (suchThat) import Data.Array (drop, take) import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithOffset) -import Data.ArrayBuffer.Types (ArrayView, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float32Array, Float64Array) +import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint8Array, Uint8ClampedArray, Uint32Array) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..)) import Data.Tuple (Tuple(..)) -import Data.Typelevel.Num (toInt', class Nat, D0, D1, D5) +import Data.Typelevel.Num (class Nat, D0, D1, D5, toInt') import Data.Vec (head) as Vec import Effect (Effect) import Effect.Console (log) @@ -20,7 +21,7 @@ import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) -import Test.QuickCheck (quickCheckGen, Result(..), (===), (/==), class Testable, ()) +import Test.QuickCheck (class Testable, Result(..), quickCheckGen, (/==), (), (===)) import Test.QuickCheck.Combinators ((==>), (|=|)) import Test.QuickCheck.Gen (Gen) import Type.Proxy (Proxy(..)) @@ -28,7 +29,7 @@ import Type.Proxy (Proxy(..)) typedArrayTests :: Ref Int -> Effect Unit typedArrayTests count = do - log "XXXXXX" + log " - partBehavesLikeTakeDrop" partBehavesLikeTakeDrop count log " - byteLength x / bytesPerValue === length x" byteLengthDivBytesPerValueTests count @@ -125,32 +126,30 @@ overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. Testable overAll count f = do void (Ref.modify (\x -> x + 1) count) - log " - Uint8ClampedArray" - quickCheckGen (f <$> genWithOffset (genTypedArray genUint8 :: Gen Uint8ClampedArray)) + let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit + chk s _ gen = do + log $ " - " <> s + quickCheckGen $ f <$> genWithOffset arr + where arr :: Gen (ArrayView a) + arr = genTypedArray gen `suchThat` \xs -> TA.length xs > 0 - log " - Uint32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genUint32 :: Gen Uint32Array)) + chk "Uint8ClampedArray" (Proxy :: Proxy Uint8ClampedArray) genUint8 - log " - Uint16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genUint16 :: Gen Uint16Array)) + chk "Uint32Array" (Proxy :: Proxy Uint32Array) genUint32 - log " - Uint8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genUint8 :: Gen Uint8Array)) + chk "Uint16Array" (Proxy :: Proxy Uint16Array) genUint16 - log " - Int32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genInt32 :: Gen Int32Array)) + chk "Uint8Array" (Proxy :: Proxy Uint8Array) genUint8 - log " - Int16Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genInt16 :: Gen Int16Array)) + chk "Int32Array" (Proxy :: Proxy Int32Array) genInt32 - log " - Int8Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genInt8 :: Gen Int8Array)) + chk "Int16Array" (Proxy :: Proxy Int16Array) genInt16 - log " - Float32Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genFloat32 :: Gen Float32Array)) + chk "Int8Array" (Proxy :: Proxy Int8Array) genInt8 - log " - Float64Array" - quickCheckGen (f <$> genWithOffset (genTypedArray genFloat64 :: Gen Float64Array)) + chk "Float32Array" (Proxy :: Proxy Float32Array) genFloat32 + + chk "Float64Array" (Proxy :: Proxy Float64Array) genFloat64 partBehavesLikeTakeDrop :: Ref Int -> Effect Unit From 491c92e8d1cbc77e9edfd334529cd2edaec48383 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sat, 19 Jan 2019 13:51:04 +0100 Subject: [PATCH 152/236] Introduce overAll1 to test over non-empty arrays --- test/Properties/TypedArray.purs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index b2862d8..4731a97 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -122,8 +122,8 @@ type TestableArrayF a b n t q = -> q -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll count f = do +overAll' :: forall q n. Testable q => Nat n => Int -> Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit +overAll' mn count f = do void (Ref.modify (\x -> x + 1) count) let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit @@ -131,27 +131,25 @@ overAll count f = do log $ " - " <> s quickCheckGen $ f <$> genWithOffset arr where arr :: Gen (ArrayView a) - arr = genTypedArray gen `suchThat` \xs -> TA.length xs > 0 + arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs chk "Uint8ClampedArray" (Proxy :: Proxy Uint8ClampedArray) genUint8 - chk "Uint32Array" (Proxy :: Proxy Uint32Array) genUint32 - chk "Uint16Array" (Proxy :: Proxy Uint16Array) genUint16 - chk "Uint8Array" (Proxy :: Proxy Uint8Array) genUint8 - chk "Int32Array" (Proxy :: Proxy Int32Array) genInt32 - chk "Int16Array" (Proxy :: Proxy Int16Array) genInt16 - chk "Int8Array" (Proxy :: Proxy Int8Array) genInt8 - chk "Float32Array" (Proxy :: Proxy Float32Array) genFloat32 - chk "Float64Array" (Proxy :: Proxy Float64Array) genFloat64 +overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit +overAll count f = overAll' 0 count f + +overAll1 :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit +overAll1 count f = overAll' 1 count f + partBehavesLikeTakeDrop :: Ref Int -> Effect Unit partBehavesLikeTakeDrop count = overAll count f where @@ -254,7 +252,7 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent withOffsetHasIndexTests :: Ref Int -> Effect Unit -withOffsetHasIndexTests count = overAll count withOffsetHasIndex +withOffsetHasIndexTests count = overAll1 count withOffsetHasIndex where withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result withOffsetHasIndex (WithOffset os xs) = @@ -262,7 +260,7 @@ withOffsetHasIndexTests count = overAll count withOffsetHasIndex withOffsetElemTests :: Ref Int -> Effect Unit -withOffsetElemTests count = overAll count withOffsetElem +withOffsetElemTests count = overAll1 count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = From fa85219ee19cbb502add34bf8bea35e353a0eaca Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 19 Jan 2019 06:24:42 -0700 Subject: [PATCH 153/236] using external float32 library --- bower.json | 3 ++- src/Data/ArrayBuffer/DataView.purs | 3 ++- src/Data/ArrayBuffer/Typed.purs | 3 ++- src/Data/ArrayBuffer/Typed/Gen.js | 9 --------- src/Data/ArrayBuffer/Typed/Gen.purs | 7 +++---- src/Data/ArrayBuffer/ValueMapping.purs | 3 ++- test/Properties/DataView.purs | 3 ++- 7 files changed, 13 insertions(+), 18 deletions(-) delete mode 100644 src/Data/ArrayBuffer/Typed/Gen.js diff --git a/bower.json b/bower.json index 2e030e6..c47dff8 100644 --- a/bower.json +++ b/bower.json @@ -20,7 +20,8 @@ "purescript-typelevel": "^4.0.0", "purescript-parseint": "^1.1.0", "purescript-uint": "^5.1.0", - "purescript-sized-vectors": "^3.1.0" + "purescript-sized-vectors": "^3.1.0", + "purescript-float32": "^0.0.1" }, "devDependencies": { "purescript-debug": "^4.0.0", diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index a568f40..09e2bc2 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -18,6 +18,7 @@ import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (toInt', class Nat) import Data.UInt (UInt) +import Data.Float32 (Float32) as F import Effect (Effect) import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4) import Type.Proxy (Proxy(..)) @@ -133,7 +134,7 @@ instance dataViewUint32 :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 UI getLE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} setLE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} -instance dataViewFloat32 :: (BytesPerValue Float32 b, Nat b) => DataView Float32 Number where +instance dataViewFloat32 :: (BytesPerValue Float32 b, Nat b) => DataView Float32 F.Float32 where getBE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} setBE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} getLE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 46a0dd8..2b0880e 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -27,6 +27,7 @@ import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.UInt (UInt) +import Data.Float32 (Float32) as F import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) @@ -143,7 +144,7 @@ instance typedArrayInt16 :: TypedArray Int16 Int where create = newInt16Array instance typedArrayInt8 :: TypedArray Int8 Int where create = newInt8Array -instance typedArrayFloat32 :: TypedArray Float32 Number where +instance typedArrayFloat32 :: TypedArray Float32 F.Float32 where create = newFloat32Array instance typedArrayFloat64 :: TypedArray Float64 Number where create = newFloat64Array diff --git a/src/Data/ArrayBuffer/Typed/Gen.js b/src/Data/ArrayBuffer/Typed/Gen.js deleted file mode 100644 index a592186..0000000 --- a/src/Data/ArrayBuffer/Typed/Gen.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -// module Data.ArrayBuffer.Typed.Gen - -exports.toFloat32 = function toFloat32(s) { - var r = new Float32Array(1); - r[0] = s; - return r[0]; -} diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 2aa2474..47a419e 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -13,6 +13,7 @@ import Data.Maybe (Maybe(..), fromMaybe) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Data.UInt (fromInt) as UInt +import Data.Float32 (Float32, fromNumber) as F import Data.UInt.Gen (genUInt) as UInt import Data.Unfoldable (replicateA) import Data.Vec (Vec) @@ -52,10 +53,8 @@ genUint32 = UInt.genUInt bottom top genInt32 :: forall m. MonadGen m => m Int genInt32 = chooseInt bottom top -foreign import toFloat32 :: Number -> Number - -genFloat32 :: forall m. MonadGen m => m Number -genFloat32 = toFloat32 <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 +genFloat32 :: forall m. MonadGen m => m F.Float32 +genFloat32 = F.fromNumber <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 genFloat64 :: forall m. MonadGen m => m Number genFloat64 = chooseFloat (-1.7976931348623157e+308) 1.7976931348623157e+308 diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 0ed8fb9..c17d677 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -6,6 +6,7 @@ module Data.ArrayBuffer.ValueMapping where import Data.ArrayBuffer.Types (kind ArrayViewType, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.Typelevel.Num (D1, D2, D4, D8) import Data.UInt (UInt) +import Data.Float32 (Float32) as F -- | Maps a `TypedArray`'s binary casted value, to the space occupied by that value, in bytes. @@ -32,5 +33,5 @@ instance binaryValueUint8 :: BinaryValue Uint8 UInt instance binaryValueInt32 :: BinaryValue Int32 Int instance binaryValueInt16 :: BinaryValue Int16 Int instance binaryValueInt8 :: BinaryValue Int8 Int -instance binaryValueFloat32 :: BinaryValue Float32 Number +instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 7ce16f8..5cbca65 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -11,6 +11,7 @@ import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) import Data.UInt (UInt) +import Data.Float32 (Float32) as F import Data.Vec (head) as Vec import Effect (Effect) import Effect.Console (log) @@ -82,7 +83,7 @@ overAll count f = do log " - Float32" quickCheckGen $ - let f' :: TestableViewF Float32 D4 n Number q + let f' :: TestableViewF Float32 D4 n F.Float32 q f' = f in f' <$> genWithOffsetAndValue (genDataView 20 Nothing) genFloat32 From 58cd006e773d3ebbc652b3ccfafe77ef11afd019 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 20 Jan 2019 20:54:58 +0100 Subject: [PATCH 154/236] WIP, making API more consistent with foldable-traversable methods --- src/Data/ArrayBuffer/Typed.purs | 120 +++++++++++++++++++++++++------- test/Properties/TypedArray.purs | 24 +++---- 2 files changed, 108 insertions(+), 36 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b166483..f9bae86 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -9,8 +9,11 @@ module Data.ArrayBuffer.Typed , create, whole, remainder, part, empty, fromArray , fill, set, setTyped, copyWithin , map, traverse, traverse_, filter + , mapWithIndex, traverseWithIndex, traverseWithIndex_, filterWithIndex , sort, reverse - , elem, all, any + , elem + , all, any + , allWithIndex, anyWithIndex , unsafeAt, hasIndex, at, (!) , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 , find, findIndex, indexOf, lastIndexOf @@ -191,29 +194,75 @@ fill a x mz = case mz of set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean set = setInternal A.length --- | Maps a new value over the typed array, creating a new buffer and typed array as well. -map :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a -map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) +ap1 :: forall a b c. (a -> c) -> (a -> b -> c) +ap1 f = \x _ -> f x + + +-- | Maps a new value over the typed array, creating a new buffer and +-- | typed array as well. +map :: forall a t. TypedArray a t => (t -> t) -> ArrayView a -> ArrayView a +map = mapWithIndex' <<< ap1 + +-- | Apply a function to each element in an array, supplying a +-- | generated zero-based index integer along with the element, +-- | creating a typed array with the new elements +mapWithIndex :: forall a t. TypedArray a t => (Offset -> t -> t) -> ArrayView a -> ArrayView a +mapWithIndex = mapWithIndex' <<< flip + +mapWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a +mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) + +-- | Traverses over each value, returning a new one +traverse :: forall a t. TypedArray a t => (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverse = traverseWithIndex' <<< ap1 -- | Traverses over each value, returning a new one -traverse :: forall a t. TypedArray a t => (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) -traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f) +traverseWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverseWithIndex = traverseWithIndex' <<< flip + +traverseWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverseWithIndex' f a = runEffectFn2 mapImpl a (mkEffectFn2 f) + +-- | Traverses over each value +traverse_ :: forall a t. TypedArray a t => (t -> Effect Unit) -> ArrayView a -> Effect Unit +traverse_ = traverseWithIndex_' <<< ap1 -- | Traverses over each value -traverse_ :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit -traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) +traverseWithIndex_ :: forall a t. TypedArray a t => (Offset -> t -> Effect Unit) -> ArrayView a -> Effect Unit +traverseWithIndex_ = traverseWithIndex_' <<< flip + +traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit +traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) -- | Test a predicate to pass on all values -all :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean -all p a = runFn2 everyImpl a (mkFn2 p) +all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean +all = every <<< ap1 + +allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean +allWithIndex = every <<< flip + +every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean +every p a = runFn2 everyImpl a (mkFn2 p) -- | Test a predicate to pass on any value -any :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean -any p a = runFn2 someImpl a (mkFn2 p) +any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean +any = some <<< ap1 + +anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean +anyWithIndex = some <<< flip + +some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean +some p a = runFn2 someImpl a (mkFn2 p) -- | Returns a new typed array with all values that pass the predicate -filter :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a -filter p a = runFn2 filterImpl a (mkFn2 p) +filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> ArrayView a +filter = filterWithIndex' <<< ap1 + +filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> ArrayView a +filterWithIndex = filterWithIndex' <<< flip + +filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a +filterWithIndex' p a = runFn2 filterImpl a (mkFn2 p) -- | Tests if a value is an element of the typed array elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolean @@ -240,8 +289,14 @@ foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> Array foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) -- | Returns the first value satisfying the predicate -find :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe t -find f a = toMaybe (runFn2 findImpl a (mkFn2 f)) +find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Maybe t +find = findWithIndex' <<< ap1 + +findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Maybe t +findWithIndex = findWithIndex' <<< flip + +findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe t +findWithIndex' f a = toMaybe (runFn2 findImpl a (mkFn2 f)) -- | Returns the first index of the value satisfying the predicate findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset @@ -255,18 +310,35 @@ indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) -foldl :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b -foldl f i a = unsafePerformEffect (foldlM (\acc x o -> pure (f acc x o)) i a) +foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> b +foldl f = foldlWithIndex' (\a x _ -> f a x) + +foldlWithIndex :: forall a b t. TypedArray a t => (Offset -> b -> t -> b) -> b -> ArrayView a -> b +foldlWithIndex f = foldlWithIndex' (\a x o -> f o a x) + +foldlWithIndex' :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b +foldlWithIndex' f i = unsafePerformEffect <<< foldlM (\a x o -> pure (f a x o)) i + +foldr :: forall a b t. TypedArray a t => (t -> b -> b) -> b -> ArrayView a -> b +foldr f = foldrWithIndex' (\a x _ -> f a x) + +foldrWithIndex :: forall a b t. TypedArray a t => (Offset -> t -> b -> b) -> b -> ArrayView a -> b +foldrWithIndex f = foldrWithIndex' (\a x o -> f o a x) + +foldrWithIndex' :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b +foldrWithIndex' f i = unsafePerformEffect <<< foldrM (\x a o -> pure (f x a o)) i -foldr :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b -foldr f i a = unsafePerformEffect (foldrM (\x acc o -> pure (f x acc o)) i a) +foldl1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> t +foldl1 f = foldl1WithIndex (\_ a x -> f a x) -foldl1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t -foldl1 f a = unsafePerformEffect (foldl1M (\acc x o -> pure (f acc x o)) a) +foldl1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> t +foldl1WithIndex f = unsafePerformEffect <<< foldl1M (\acc x o -> pure (f o acc x)) -foldr1 :: forall a t. TypedArray a t => (t -> t -> Offset -> t) -> ArrayView a -> t -foldr1 f a = unsafePerformEffect (foldr1M (\x acc o -> pure (f x acc o)) a) +foldr1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> t +foldr1 f = foldr1WithIndex (\_ a x -> f a x) +foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> t +foldr1WithIndex f = unsafePerformEffect <<< foldr1M (\x a o -> pure (f o x a)) foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 4731a97..e570952 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -187,7 +187,7 @@ allAreFilledTests count = overAll count allAreFilled Nothing -> zero Just y -> y TA.fill xs x Nothing - let b = TA.all (\y o -> y == x) xs + let b = TA.all (\y -> y == x) xs pure (b "All aren't the filled value") @@ -209,7 +209,7 @@ allImpliesAnyTests count = overAll count allImpliesAny where allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result allImpliesAny (WithOffset _ xs) = - let pred x o = x /= zero + let pred x = x /= zero all' = TA.all pred xs "All don't satisfy the predicate" any' = TA.any pred xs "None satisfy the predicate" in (TA.length xs === zero) |=| all' ==> any' @@ -221,7 +221,7 @@ filterImpliesAllTests count = overAll count filterImpliesAll where filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result filterImpliesAll (WithOffset _ xs) = - let pred x o = x /= zero + let pred x = x /= zero ys = TA.filter pred xs all' = TA.all pred ys in all' "Filter doesn't imply all" @@ -233,9 +233,9 @@ filterIsTotalTests count = overAll count filterIsTotal where filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result filterIsTotal (WithOffset _ xs) = - let pred x o = x /= zero + let pred x = x /= zero ys = TA.filter pred xs - zs = TA.filter (\x o -> not pred x o) ys + zs = TA.filter (\x -> not pred x) ys in TA.toArray zs === [] @@ -245,7 +245,7 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent where filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result filterIsIdempotent (WithOffset _ xs) = - let pred x o = x /= zero + let pred x = x /= zero ys = TA.filter pred xs zs = TA.filter pred ys in TA.toArray zs === TA.toArray ys @@ -274,12 +274,12 @@ anyImpliesFindTests count = overAll count anyImpliesFind where anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result anyImpliesFind (WithOffset _ xs) = - let pred x o = x /= zero + let pred x = x /= zero p = TA.any pred xs "All don't satisfy the predicate" q = case TA.find pred xs of Nothing -> Failed "Doesn't have a value satisfying the predicate" - Just z -> if pred z 0 + Just z -> if pred z then Success else Failed "Found value doesn't satisfy the predicate" in p ==> q @@ -330,7 +330,7 @@ foldrConsIsToArrayTests count = overAll count foldrConsIsToArray where foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldrConsIsToArray (WithOffset _ xs) = - TA.foldr (\x acc _ -> Array.cons x acc) [] xs === TA.toArray xs + TA.foldr (\x acc -> Array.cons x acc) [] xs === TA.toArray xs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit @@ -338,7 +338,7 @@ foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray where foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldlSnocIsToArray (WithOffset _ xs) = - TA.foldl (\acc x _ -> Array.snoc acc x) [] xs === TA.toArray xs + TA.foldl (\acc x -> Array.snoc acc x) [] xs === TA.toArray xs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit @@ -346,7 +346,7 @@ mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity where mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result mapIdentityIsIdentity (WithOffset _ xs) = - TA.toArray (TA.map (\x _ -> x) xs) === TA.toArray xs + TA.toArray (TA.map identity xs) === TA.toArray xs traverseSnocIsToArrayTests :: Ref Int -> Effect Unit @@ -356,7 +356,7 @@ traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray traverseSnocIsToArray (WithOffset _ xs) = let ys = unsafePerformEffect do ref <- Ref.new [] - TA.traverse_ (\x _ -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs + TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs Ref.read ref in TA.toArray xs === ys From 45b4b623d288c941e960f8b1c4fc2c87639f8878 Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Thu, 6 Dec 2018 13:59:32 +0100 Subject: [PATCH 155/236] Remove text encoding dependency --- bower.json | 5 ++- package.json | 9 ++---- src/Data/ArrayBuffer/ArrayBuffer.purs | 25 +-------------- test/Input.purs | 44 --------------------------- test/Main.purs | 21 +------------ 5 files changed, 7 insertions(+), 97 deletions(-) delete mode 100644 test/Input.purs diff --git a/bower.json b/bower.json index b50facd..f2e0abf 100644 --- a/bower.json +++ b/bower.json @@ -16,8 +16,7 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0", - "purescript-text-encoding": "^0.0.9" + "purescript-uint": "^4.0.0" }, "devDependencies": { "purescript-debug": "^4.0.0", @@ -25,4 +24,4 @@ "purescript-partial": "^2.0.0", "purescript-unicode": "^4.0.1" } -} +} \ No newline at end of file diff --git a/package.json b/package.json index ffc2434..110d9f1 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,9 @@ { "name": "purescript-arraybuffer", - "version": "7.0.0", + "version": "9.0.0", "main": "index.js", "repository": "git@github.com:jacereda/purescript-arraybuffer.git", "author": "https://github.com/jacereda", "license": "MIT", - "devDependencies": { - "text-encoding": "^0.6.4" - } - -} + "devDependencies": {} +} \ No newline at end of file diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index d501ab8..539a766 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -3,20 +3,11 @@ module Data.ArrayBuffer.ArrayBuffer ( create , slice , fromArray , fromIntArray - , fromString - , decodeToString ) where -import Data.ArrayBuffer.DataView (whole, buffer) -import Data.ArrayBuffer.Typed (asUint8Array, dataView) import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) -import Data.Either (Either) import Data.Function.Uncurried (Fn3, runFn3) -import Data.TextDecoder (decodeUtf8) -import Data.TextEncoder (encodeUtf8) import Effect (Effect) -import Effect.Exception (Error) -import Prelude ((<<<)) -- | Create an `ArrayBuffer` with the given capacity. @@ -35,18 +26,4 @@ slice = runFn3 sliceImpl foreign import fromArray :: Array Number -> ArrayBuffer -- | Convert an array into an `ArrayBuffer` representation. -foreign import fromIntArray :: Array Int -> ArrayBuffer - --- | Convert a UTF-8 encoded `ArrayBuffer` into a `String`. --- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) --- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" -decodeToString :: ArrayBuffer -> Either Error String -decodeToString = decodeUtf8 <<< asUint8Array <<< whole - --- | Convert a `String` into a UTF-8 encoded `ArrayBuffer`. --- | Serves as a quick utility function for a common use-case. For more use-cases, --- | see: [purescript-text-encoding](https://pursuit.purescript.org/packages/purescript-text-encoding/0.0.8) --- | Requires the TextDecoder class available. A polyfill can be found in the npm package "text-encoding" -fromString :: String -> ArrayBuffer -fromString = buffer <<< dataView <<< encodeUtf8 +foreign import fromIntArray :: Array Int -> ArrayBuffer \ No newline at end of file diff --git a/test/Input.purs b/test/Input.purs deleted file mode 100644 index 926c66b..0000000 --- a/test/Input.purs +++ /dev/null @@ -1,44 +0,0 @@ --- Uses code originally found in the purescript-encoding library. -{- - Copyright 2018 Andreas Schacker - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --} -module Test.Input where - -import Data.Char.Unicode (isPrint) -import Prelude ((<$>), ($), (<<<)) -import Data.Array (filter) -import Data.String.CodeUnits (fromCharArray, toCharArray) -import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary) - - --- When UTF8-encoding a string, surrogate code points and other non-characters --- are simply replaced by the replacement character � (U+FFFD). --- This entails that the `encodeUtf8` function is not injective anymore and --- thus the desired property `decodeUtf8 <<< encodeUtf8 == id` does not hold --- in general. --- --- For well-formed input strings, however, we can expect the property to hold. - --- Use a newtype in order to define an `Arbitrary` instance. -newtype WellFormedInput = WellFormedInput String - --- The `Arbitrary` instance for `String` currently simply chooses characters --- out of the first 65536 unicode code points. --- See `charGen` in `purescript-strongcheck`. -instance arbWellFormedInput :: Arbitrary WellFormedInput where - arbitrary = WellFormedInput <<< filterString isPrint <$> arbitrary - -filterString :: (Char -> Boolean) -> String -> String -filterString f s = fromCharArray <<< filter f $ toCharArray s diff --git a/test/Main.purs b/test/Main.purs index 19ad101..017bc05 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,12 +6,9 @@ import Effect (Effect) import Data.ArrayBuffer.ArrayBuffer as AB import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.Typed as TA -import Data.Either (fromRight) import Data.Maybe (Maybe(..), isNothing) import Data.UInt (fromInt, pow) -import Partial.Unsafe (unsafePartial) -import Test.QuickCheck (quickCheck', (), quickCheck) -import Test.Input (WellFormedInput(..)) +import Test.QuickCheck (quickCheck', ()) assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit @@ -27,19 +24,6 @@ assertEquals expected actual = do main :: Effect Unit main = do - assertEquals "釺椱�밸造ə㊡癥闗" (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString "釺椱�밸造ə㊡癥闗") - - quickCheck - \(WellFormedInput s) -> - let - result = (unsafePartial $ fromRight $ AB.decodeToString $ AB.fromString s) - in - s == result - "Isormorphic arraybuffer conversion with string failed for input\n" - <> s - <> " which, after the round trip, result in\n" - <> result - ab4 <- AB.create 4 ab8 <- AB.create 8 assertEquals 4 $ AB.byteLength ab4 @@ -52,9 +36,6 @@ main = do assertEquals (Just 2) $ DV.byteLength <$> DV.slice 2 2 ab4 assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0] assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4] - assertEquals 4 $ AB.byteLength $ AB.fromString "hola" - assertEquals 5 $ AB.byteLength $ AB.fromString "hóla" - assertEquals 7 $ AB.byteLength $ AB.fromString "hóla¡" assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8 assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8 From 8a8e0a6bb68b6921de394aebba4ab7c5e96c55f3 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 22 Jan 2019 11:35:24 +0100 Subject: [PATCH 156/236] Remove unused dependencies, bump uint --- bower.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bower.json b/bower.json index f2e0abf..ad45920 100644 --- a/bower.json +++ b/bower.json @@ -16,12 +16,10 @@ "purescript-arraybuffer-types": "^2.0.0", "purescript-maybe": "^4.0.0", "purescript-effect": "^2.0.0", - "purescript-uint": "^4.0.0" + "purescript-uint": "^5.0.0" }, "devDependencies": { "purescript-debug": "^4.0.0", - "purescript-quickcheck": "^5.0.0", - "purescript-partial": "^2.0.0", - "purescript-unicode": "^4.0.1" + "purescript-quickcheck": "^5.0.0" } -} \ No newline at end of file +} From 49ca0b6ee9f9dd8b127c600ecc4a71440ef5a1fe Mon Sep 17 00:00:00 2001 From: Alexandra DeWit Date: Thu, 6 Dec 2018 14:01:04 +0100 Subject: [PATCH 157/236] Remove show and update readme --- README.md | 5 ----- src/Data/ArrayBuffer/Show.js | 7 ------- src/Data/ArrayBuffer/Show.purs | 18 ------------------ 3 files changed, 30 deletions(-) delete mode 100644 src/Data/ArrayBuffer/Show.js delete mode 100644 src/Data/ArrayBuffer/Show.purs diff --git a/README.md b/README.md index 7bd53a2..bbdcf1e 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,3 @@ ArrayBuffer bindings for PureScript. ## Documentation Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). - - -## Important Usage Notes - -- Usage of the ArrayBuffer<->String conversion functions requires the import of the NPM package 'text-encoding'. diff --git a/src/Data/ArrayBuffer/Show.js b/src/Data/ArrayBuffer/Show.js deleted file mode 100644 index 44eab29..0000000 --- a/src/Data/ArrayBuffer/Show.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -// module Show - -exports.showImpl = function(a) { - return require('util').inspect(a); -} diff --git a/src/Data/ArrayBuffer/Show.purs b/src/Data/ArrayBuffer/Show.purs deleted file mode 100644 index c22eccf..0000000 --- a/src/Data/ArrayBuffer/Show.purs +++ /dev/null @@ -1,18 +0,0 @@ -module Data.ArrayBuffer.Show where - --- import Prelude -import Data.ArrayBuffer.Types (ArrayView) --- import Data.ArrayBuffer.ArrayBuffer as AB --- import Data.ArrayBuffer.DataView as DV --- import Data.ArrayBuffer.Typed as T - --- instance showArrayView :: Show (ArrayView a) where --- show = showImpl --- --- instance showDataView :: Show DataView where --- show = show <<< T.asInt8Array --- --- instance showArrayBuffer :: Show ArrayBuffer where --- show = show <<< DV.whole --- -foreign import showImpl :: forall a. ArrayView a -> String From cbeffc5884d2ec734f2459fb644a898b0c1b60f0 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 22 Jan 2019 11:45:41 +0100 Subject: [PATCH 158/236] Mention purescript-arraybuffer-codecs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bbdcf1e..e318957 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,5 @@ ArrayBuffer bindings for PureScript. ## Documentation Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). + +See https://github.com/AlexaDeWit/purescript-arraybuffer-codecs if you need text/base64 encoding/decoding functions. From 07a501f7e133ee64217cb8e9336fa06e53547b7a Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 24 Jan 2019 18:57:59 +0100 Subject: [PATCH 159/236] Enabled DataView tests --- src/Data/ArrayBuffer/DataView/Gen.purs | 8 ++++++-- test/Properties.purs | 12 ++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index c77ba0a..46ba9ed 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,6 +1,10 @@ module Data.ArrayBuffer.DataView.Gen where +import Prelude + +import Control.Monad.Gen (suchThat) import Control.Monad.Gen.Class (class MonadGen, chooseInt) +import Control.Monad.Rec.Class (class MonadRec) import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole, byteLength, class DataView) import Data.ArrayBuffer.Types (DataView, ByteOffset, kind ArrayViewType) @@ -11,7 +15,6 @@ import Data.Unfoldable (replicateA) import Data.Vec (Vec) import Data.Vec (fromArray) as Vec import Partial.Unsafe (unsafePartial) -import Prelude ((<$>), bind, pure, (-), ($)) import Type.Proxy (Proxy(..)) @@ -28,6 +31,7 @@ data WithOffsetAndValue n (a :: ArrayViewType) t = genWithOffsetAndValue :: forall m n a b t . MonadGen m + => MonadRec m => Nat n => BytesPerValue a b => DataView a t @@ -38,7 +42,7 @@ genWithOffsetAndValue :: forall m n a b t genWithOffsetAndValue gen genT = do let n = toInt' (Proxy :: Proxy n) b = toInt' (Proxy :: Proxy b) - xs <- gen + xs <- gen `suchThat` \xs -> b <= byteLength xs let l = byteLength xs mos <- replicateA n (chooseInt 0 (l - b)) let os = unsafePartial $ case Vec.fromArray mos of diff --git a/test/Properties.purs b/test/Properties.purs index 33679ca..77f0d14 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -17,9 +17,9 @@ propertiesTests = do c <- Ref.read count log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." - -- do - -- count <- Ref.new 0 - -- log " - DataView Tests:" - -- dataViewTests count - -- c <- Ref.read count - -- log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." + do + count <- Ref.new 0 + log " - DataView Tests:" + dataViewTests count + c <- Ref.read count + log $ " - Verified " <> show c <> " properties, generating " <> show (c * 16 * 100) <> " test cases." From fde881bcbfcde3df5a79e4994234b9799fb8fc37 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 24 Jan 2019 19:31:00 +0100 Subject: [PATCH 160/236] Saturate arguments for runEffectFnX --- src/Data/ArrayBuffer/DataView.purs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index a568f40..966b13a 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -32,13 +32,13 @@ foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView -- | View mapping the rest of an `ArrayBuffer` after an index. remainder :: ArrayBuffer -> ByteOffset -> Effect DataView -remainder = runEffectFn2 remainderImpl +remainder a o = runEffectFn2 remainderImpl a o foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView -- | View mapping a region of the `ArrayBuffer`. part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView -part = runEffectFn3 partImpl +part a o l = runEffectFn3 partImpl a o l -- | `ArrayBuffer` being mapped by the view. foreign import buffer :: DataView -> ArrayBuffer @@ -74,14 +74,14 @@ getter :: forall t , littleEndian :: Boolean } -> DataView -> ByteOffset -> Effect (Maybe t) -getter data' = +getter data' d o = runEffectFn3 getterImpl { just: Just , nothing: Nothing , functionName: data'.functionName , littleEndian: data'.littleEndian , bytesPerValue: data'.bytesPerValue - } + } d o foreign import setterImpl :: forall t . EffectFn4 { functionName :: String @@ -94,7 +94,7 @@ setter :: forall t , bytesPerValue :: ByteLength , littleEndian :: Boolean } -> DataView -> t -> ByteOffset -> Effect Boolean -setter = runEffectFn4 setterImpl +setter d t o = runEffectFn4 setterImpl d t o instance dataViewInt8 :: (BytesPerValue Int8 b, Nat b) => DataView Int8 Int where From 4968226496605886d0cdbca1077e19f858235d8c Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 24 Jan 2019 19:43:25 +0100 Subject: [PATCH 161/236] Saturate arguments for run(Effect)FnX --- src/Data/ArrayBuffer/Typed.purs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f9bae86..98df98c 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -270,7 +270,7 @@ elem x mo a = runFn3 includesImpl a x (toNullable mo) -- | Fetch element at index. unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> t -unsafeAt = runFn2 unsafeAtImpl +unsafeAt a o = runFn2 unsafeAtImpl a o -- | Folding from the left foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b @@ -350,7 +350,7 @@ foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Reverses a typed array in-place. reverse :: forall a. ArrayView a -> Effect Unit -reverse = runEffectFn1 reverseImpl +reverse a = runEffectFn1 reverseImpl a foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit @@ -380,7 +380,7 @@ foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Sorts the values in-place sort :: forall a. ArrayView a -> Effect Unit -sort = runEffectFn1 sortImpl +sort a = runEffectFn1 sortImpl a foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) @@ -404,7 +404,7 @@ foreign import joinImpl :: forall a. Fn2 (ArrayView a) String String -- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. toString' :: forall a. ArrayView a -> String -> String -toString' = runFn2 joinImpl +toString' a s = runFn2 joinImpl a s foreign import unsafeAtImpl :: forall a b. Fn2 (ArrayView a) Offset b @@ -413,7 +413,7 @@ foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean -- | Determine if a certain index is valid. hasIndex :: forall a. ArrayView a -> Offset -> Boolean -hasIndex = runFn2 hasIndexImpl +hasIndex a o = runFn2 hasIndexImpl a o -- | Fetch element at index. at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t From 23c5135bea8c4e643134eb75f9a544c431b9e4c6 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 25 Jan 2019 21:27:01 +0100 Subject: [PATCH 162/236] Make empty effectful --- src/Data/ArrayBuffer/ArrayBuffer.js | 2 +- src/Data/ArrayBuffer/ArrayBuffer.purs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 0952ca2..656d576 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -2,7 +2,7 @@ // module Data.ArrayBuffer.ArrayBuffer -exports.empty = function empty (s) { +exports.emptyImpl = function empty (s) { return new ArrayBuffer(s); }; diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index fe8a920..4bd11a0 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -12,10 +12,16 @@ import Data.Function.Uncurried (Fn3, runFn3) import Data.Maybe (Maybe(..)) import Data.Nullable (Nullable, notNull, null) import Data.Tuple (Tuple(..)) +import Effect (Effect) +import Effect.Uncurried (EffectFn1, runEffectFn1) + + +foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer -- | Create an `ArrayBuffer` with the given capacity. -foreign import empty :: ByteLength -> ArrayBuffer +empty :: ByteLength -> Effect ArrayBuffer +empty l = runEffectFn1 emptyImpl l -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength From 86fd6e84c1324089a46a18fc2245d3f492e64845 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 25 Jan 2019 21:28:16 +0100 Subject: [PATCH 163/236] Restore specific setters/getters in the API, changed implementation --- src/Data/ArrayBuffer/DataView.js | 2 +- src/Data/ArrayBuffer/DataView.purs | 319 ++++++++++++++++++++++------- test/Properties/DataView.purs | 27 +-- 3 files changed, 254 insertions(+), 94 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index d51835c..44ad1b9 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -33,7 +33,7 @@ exports.getterImpl = function getterImpl (data, v, o) { : data.nothing; }; -exports.setterImpl = function setterImpl (data,v,n,o) { +exports.setterImpl = function setterImpl (data, v, o, n) { if (((o + data.bytesPerValue) >>> 0) <= v.byteLength) { v[data.functionName].call(v,o,n,data.littleEndian); return true; diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 966b13a..0c0e235 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -2,18 +2,56 @@ -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. module Data.ArrayBuffer.DataView - ( whole - , remainder - , part - , buffer - , byteOffset - , byteLength - , AProxy (..) - , class DataView - , getBE, getLE, setBE, setLE - ) where - -import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer, kind ArrayViewType, Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) + ( AProxy (..) + , Endian (..) + , buffer + , byteLength + , byteOffset + , class DataView + , class ViewTypeName + , get + , getBE + , getFloat32be + , getFloat32le + , getFloat64be + , getFloat64le + , getInt16be + , getInt16le + , getInt32be + , getInt32le + , getInt8 + , getLE + , getUint16be + , getUint16le + , getUint32be + , getUint32le + , getUint8 + , nm + , part + , remainder + , set + , setBE + , setFloat32be + , setFloat32le + , setFloat64be + , setFloat64le + , setInt16be + , setInt16le + , setInt32be + , setInt32le + , setInt8 + , setLE + , setUint16be + , setUint16le + , setUint32be + , setUint32le + , setUint8 + , whole + ) where + +import Prelude + +import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, kind ArrayViewType) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (toInt', class Nat) @@ -24,7 +62,6 @@ import Type.Proxy (Proxy(..)) - -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView @@ -52,13 +89,47 @@ foreign import byteLength :: DataView -> ByteLength data AProxy (a :: ArrayViewType) = AProxy +data Endian = LE | BE + +instance eqEndian :: Eq Endian where + eq LE LE = true + eq BE BE = true + eq _ _ = false + +class (BinaryValue a t, ViewTypeName a) <= DataView (a :: ArrayViewType) t | a -> t + +instance dataViewUint8Clamped :: DataView Uint8Clamped UInt +instance dataViewUint32 :: DataView Uint32 UInt +instance dataViewUint16 :: DataView Uint16 UInt +instance dataViewUint8 :: DataView Uint8 UInt +instance dataViewInt32 :: DataView Int32 Int +instance dataViewInt16 :: DataView Int16 Int +instance dataViewInt8 :: DataView Int8 Int +instance dataViewFloat32 :: DataView Float32 Number +instance dataViewFloat64 :: DataView Float64 Number -class BinaryValue a t <= DataView (a :: ArrayViewType) t | a -> t where - getLE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) - getBE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) - setBE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean - setLE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean +class ViewTypeName vty where + nm :: AProxy vty -> String + +instance viewTypeNameUint8Clamped :: ViewTypeName Uint8Clamped where + nm _ = "Uint8Clamped" +instance viewTypeNameViewUint32 :: ViewTypeName Uint32 where + nm _ = "Uint32" +instance viewTypeNameViewUint16 :: ViewTypeName Uint16 where + nm _ = "Uint16" +instance viewTypeNameViewUint8 :: ViewTypeName Uint8 where + nm _ = "Uint8" +instance viewTypeNameViewInt32 :: ViewTypeName Int32 where + nm _ = "Int32" +instance viewTypeNameViewInt16 :: ViewTypeName Int16 where + nm _ = "Int16" +instance viewTypeNameViewInt8 :: ViewTypeName Int8 where + nm _ = "Int8" +instance viewTypeNameViewFloat32 :: ViewTypeName Float32 where + nm _ = "Float32" +instance viewTypeNameViewFloat64 :: ViewTypeName Float64 where + nm _ = "Float64" foreign import getterImpl :: forall t . EffectFn3 { just :: t -> Maybe t @@ -68,8 +139,8 @@ foreign import getterImpl :: forall t , bytesPerValue :: ByteLength } DataView ByteOffset (Maybe t) -getter :: forall t - . { functionName :: String +getter :: forall t. + { functionName :: String , bytesPerValue :: ByteLength , littleEndian :: Boolean } @@ -83,64 +154,164 @@ getter data' d o = , bytesPerValue: data'.bytesPerValue } d o + +get :: forall a t b. DataView a t => BytesPerValue a b => Nat b => Endian -> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) +get endian prx = + let le = endian == LE + pnm = "get" <> nm prx + bpv = toInt' (Proxy :: Proxy b) + in getter { functionName: pnm + , bytesPerValue: bpv + , littleEndian: le + } + +getBE :: forall a t b. DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) +getBE = get BE + +getLE :: forall a t b. DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) +getLE = get LE + foreign import setterImpl :: forall t . EffectFn4 { functionName :: String , littleEndian :: Boolean , bytesPerValue :: ByteLength - } DataView t ByteOffset Boolean + } DataView ByteOffset t Boolean -setter :: forall t - . { functionName :: String +setter :: forall t. + { functionName :: String , bytesPerValue :: ByteLength , littleEndian :: Boolean - } -> DataView -> t -> ByteOffset -> Effect Boolean -setter d t o = runEffectFn4 setterImpl d t o - - -instance dataViewInt8 :: (BytesPerValue Int8 b, Nat b) => DataView Int8 Int where - getBE AProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewInt16 :: (BytesPerValue Int16 b, Nat b) => DataView Int16 Int where - getBE AProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewInt32 :: (BytesPerValue Int32 b, Nat b) => DataView Int32 Int where - getBE AProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewUint8 :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 UInt where - getBE AProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewUint16 :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 UInt where - getBE AProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewUint32 :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 UInt where - getBE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewFloat32 :: (BytesPerValue Float32 b, Nat b) => DataView Float32 Number where - getBE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewFloat64 :: (BytesPerValue Float64 b, Nat b) => DataView Float64 Number where - getBE AProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} + } -> DataView -> ByteOffset -> t -> Effect Boolean +setter d o t = runEffectFn4 setterImpl d o t + +set :: forall a t b. DataView a t => BytesPerValue a b => Nat b => Endian -> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean +set endian prx = + let le = endian == LE + pnm = "set" <> nm prx + bpv = toInt' (Proxy :: Proxy b) + in setter { functionName: pnm + , bytesPerValue: bpv + , littleEndian: le + } + +-- | Fetch int8 value at a certain index in a `DataView`. +getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt8 = getLE (AProxy :: AProxy Int8) + +-- | Fetch big-endian int16 value at a certain index in a `DataView`. +getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt16be = getBE (AProxy :: AProxy Int16) + +-- | Fetch little-endian int16 value at a certain index in a `DataView`. +getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt16le = getLE (AProxy :: AProxy Int16) + +-- | Fetch big-endian int32 value at a certain index in a `DataView`. +getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt32be = getBE (AProxy :: AProxy Int32) + +-- | Fetch little-endian int32 value at a certain index in a `DataView`. +getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt32le = getLE (AProxy :: AProxy Int32) + +-- | Fetch uint8 value at a certain index in a `DataView`. +getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint8 = getLE (AProxy :: AProxy Uint8) + +-- | Fetch big-endian uint16 value at a certain index in a `DataView`. +getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint16be = getBE (AProxy :: AProxy Uint16) + +-- | Fetch little-endian uint16 value at a certain index in a `DataView`. +getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint16le = getLE (AProxy :: AProxy Uint16) + +-- | Fetch big-endian uint32 value at a certain index in a `DataView`. +getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint32be = getBE (AProxy :: AProxy Uint32) + +-- | Fetch little-endian uint32 value at a certain index in a `DataView`. +getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint32le = getLE (AProxy :: AProxy Uint32) + +-- | Fetch big-endian float32 value at a certain index in a `DataView`. +getFloat32be :: DataView -> ByteOffset -> Effect (Maybe Number) +getFloat32be = getBE (AProxy :: AProxy Float32) + +-- | Fetch little-endian float32 value at a certain index in a `DataView`. +getFloat32le :: DataView -> ByteOffset -> Effect (Maybe Number) +getFloat32le = getLE (AProxy :: AProxy Float32) + +-- | Fetch big-endian float64 value at a certain index in a `DataView`. +getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number) +getFloat64be = getBE (AProxy :: AProxy Float64) + +-- | Fetch little-endian float64 value at a certain index in a `DataView`. +getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number) +getFloat64le = getLE (AProxy :: AProxy Float64) + + +-- | Store big-endian value at a certain index in a `DataView`. +setBE :: forall a t b. DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean +setBE = set BE + +-- | Store little-endian value at a certain index in a `DataView`. +setLE :: forall a t b. DataView a t => BytesPerValue a b => Nat b => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean +setLE = set LE + +-- | Store int8 value at a certain index in a `DataView`. +setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt8 = setLE (AProxy :: AProxy Int8) + +-- | Store big-endian int16 value at a certain index in a `DataView`. +setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt16be = setBE (AProxy :: AProxy Int16) + +-- | Store little-endian int16 value at a certain index in a `DataView`. +setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt16le = setLE (AProxy :: AProxy Int16) + +-- | Store big-endian int32 value at a certain index in a `DataView`. +setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt32be = setBE (AProxy :: AProxy Int32) + +-- | Store little-endian int32 value at a certain index in a `DataView`. +setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt32le = setLE (AProxy :: AProxy Int32) + +-- | Store uint8 value at a certain index in a `DataView`. +setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint8 = setLE (AProxy :: AProxy Uint8) + + +-- | Store big-endian uint16 value at a certain index in a `DataView`. +setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint16be = setBE (AProxy :: AProxy Uint16) + +-- | Store little-endian uint16 value at a certain index in a `DataView`. +setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint16le = setLE (AProxy :: AProxy Uint16) + +-- | Store big-endian uint32 value at a certain index in a `DataView`. +setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint32be = setBE (AProxy :: AProxy Uint32) + +-- | Store little-endian uint32 value at a certain index in a `DataView`. +setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint32le = setLE (AProxy :: AProxy Uint32) + +-- | Store big-endian float32 value at a certain index in a `DataView`. +setFloat32be :: DataView -> ByteOffset -> Number -> Effect Boolean +setFloat32be = setBE (AProxy :: AProxy Float32) + +-- | Store little-endian float32 value at a certain index in a `DataView`. +setFloat32le :: DataView -> ByteOffset -> Number -> Effect Boolean +setFloat32le = setLE (AProxy :: AProxy Float32) + +-- | Store big-endian float64 value at a certain index in a `DataView`. +setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean +setFloat64be = setBE (AProxy :: AProxy Float64) + +-- | Store little-endian float64 value at a certain index in a `DataView`. +setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean +setFloat64le = setLE (AProxy :: AProxy Float64) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index d399e1b..0664453 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -6,7 +6,7 @@ import Prelude import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genUint16, genUint32, genUint8) -import Data.ArrayBuffer.Types (Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) +import Data.ArrayBuffer.Types (Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) @@ -24,9 +24,9 @@ import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) dataViewTests :: Ref Int -> Effect Unit dataViewTests count = do log " - setBE x o => getBE o === Just x" - placingAValueIsThereTestsBE count + placingAValueIsThereTests DV.BE count log " - setLE x o => getLE o === Just x" - placingAValueIsThereTestsLE count + placingAValueIsThereTests DV.LE count type TestableViewF a b n t q = @@ -93,25 +93,14 @@ overAll count f = do in f' <$> genWithOffsetAndValue genDataView genFloat64 -placingAValueIsThereTestsBE :: Ref Int -> Effect Unit -placingAValueIsThereTestsBE count = overAll count placingAValueIsThere +placingAValueIsThereTests :: DV.Endian -> Ref Int -> Effect Unit +placingAValueIsThereTests endian count = overAll count placingAValueIsThere where placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result placingAValueIsThere (WithOffsetAndValue os t xs) = let o = Vec.head os + prx = DV.AProxy :: DV.AProxy a in unsafePerformEffect do - _ <- DV.setBE (DV.AProxy :: DV.AProxy a) xs t o - my <- DV.getBE (DV.AProxy :: DV.AProxy a) xs o - pure (my === Just t) - - -placingAValueIsThereTestsLE :: Ref Int -> Effect Unit -placingAValueIsThereTestsLE count = overAll count placingAValueIsThere - where - placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result - placingAValueIsThere (WithOffsetAndValue os t xs) = - let o = Vec.head os - in unsafePerformEffect do - _ <- DV.setLE (DV.AProxy :: DV.AProxy a) xs t o - my <- DV.getLE (DV.AProxy :: DV.AProxy a) xs o + _ <- DV.set endian prx xs o t + my <- DV.get endian prx xs o pure (my === Just t) From 0fa0d7a21d591d4dd25eb7a41195caccf0098300 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Fri, 25 Jan 2019 14:19:07 -0700 Subject: [PATCH 164/236] fixing tests --- test/Properties/DataView.purs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 9758f8c..3a13210 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -13,6 +13,7 @@ import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) import Data.UInt (UInt) import Data.Float32 (Float32) as F import Data.Vec (head) as Vec +import Data.Symbol (class IsSymbol) import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) @@ -30,66 +31,68 @@ dataViewTests count = do placingAValueIsThereTests DV.LE count -type TestableViewF a b n t q = +type TestableViewF a name b n t q = Show t => Eq t => Ord t => Semiring t => BytesPerValue a b + => DV.ShowArrayViewType a name + => IsSymbol name => Nat b => DV.DataView a t => WithOffsetAndValue n a t -> q -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableViewF a b n t q) -> Effect Unit +overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a name b t. TestableViewF a name b n t q) -> Effect Unit overAll count f = do void (Ref.modify (\x -> x + 1) count) log " - Uint32" quickCheckGen $ - let f' :: TestableViewF Uint32 D4 n UInt q + let f' :: TestableViewF Uint32 "Uint32" D4 n UInt q f' = f in f' <$> genWithOffsetAndValue genDataView genUint32 log " - Uint16" quickCheckGen $ - let f' :: TestableViewF Uint16 D2 n UInt q + let f' :: TestableViewF Uint16 "Uint16" D2 n UInt q f' = f in f' <$> genWithOffsetAndValue genDataView genUint16 log " - Uint8" quickCheckGen $ - let f' :: TestableViewF Uint8 D1 n UInt q + let f' :: TestableViewF Uint8 "Uint8" D1 n UInt q f' = f in f' <$> genWithOffsetAndValue genDataView genUint8 log " - Int32" quickCheckGen $ - let f' :: TestableViewF Int32 D4 n Int q + let f' :: TestableViewF Int32 "Int32" D4 n Int q f' = f in f' <$> genWithOffsetAndValue genDataView genInt32 log " - Int16" quickCheckGen $ - let f' :: TestableViewF Int16 D2 n Int q + let f' :: TestableViewF Int16 "Int16" D2 n Int q f' = f in f' <$> genWithOffsetAndValue genDataView genInt16 log " - Int8" quickCheckGen $ - let f' :: TestableViewF Int8 D1 n Int q + let f' :: TestableViewF Int8 "Int8" D1 n Int q f' = f in f' <$> genWithOffsetAndValue genDataView genInt8 log " - Float32" quickCheckGen $ - let f' :: TestableViewF Float32 D4 n F.Float32 q + let f' :: TestableViewF Float32 "Float32" D4 n F.Float32 q f' = f in f' <$> genWithOffsetAndValue genDataView genFloat32 log " - Float64" quickCheckGen $ - let f' :: TestableViewF Float64 D8 n Number q + let f' :: TestableViewF Float64 "Float64" D8 n Number q f' = f in f' <$> genWithOffsetAndValue genDataView genFloat64 @@ -97,7 +100,7 @@ overAll count f = do placingAValueIsThereTests :: DV.Endian -> Ref Int -> Effect Unit placingAValueIsThereTests endian count = overAll count placingAValueIsThere where - placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result + placingAValueIsThere :: forall a name b t. TestableViewF a name b D1 t Result placingAValueIsThere (WithOffsetAndValue os t xs) = let o = Vec.head os prx = DV.AProxy :: DV.AProxy a From 45f310a8b32874f280023ab904c07e6db08ad1cd Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Fri, 25 Jan 2019 14:24:41 -0700 Subject: [PATCH 165/236] restricted imports --- src/Data/ArrayBuffer/DataView/Gen.purs | 2 +- src/Data/ArrayBuffer/Typed.purs | 2 +- src/Data/ArrayBuffer/Typed/Gen.purs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 46ba9ed..3423d5f 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,6 +1,6 @@ module Data.ArrayBuffer.DataView.Gen where -import Prelude +import Prelude ((<$>), bind, ($), (<=), (-), pure) import Control.Monad.Gen (suchThat) import Control.Monad.Gen.Class (class MonadGen, chooseInt) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 226d452..c7e04f3 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -21,7 +21,7 @@ module Data.ArrayBuffer.Typed , toString, toString', toArray ) where -import Prelude +import Prelude (Unit, (>=), (&&), (<<<), (<=), pure, (-), flip, (*), (*>)) import Data.Array (length) as A import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 82daa80..ffe3dd8 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -2,7 +2,7 @@ module Data.ArrayBuffer.Typed.Gen where -import Prelude +import Prelude ((<$>), bind, (/), (-), negate, ($), bottom, pure, top) import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed (class TypedArray) @@ -30,7 +30,7 @@ genTypedArray :: forall m a t genTypedArray gen = sized \s -> do n <- chooseInt 0 s a <- replicateA n gen - pure $ TA.fromArray a + pure (TA.fromArray a) genUint8 :: forall m. MonadGen m => m UInt genUint8 = UInt.fromInt <$> chooseInt 0 255 From 167edadeea52600d3638aac6d8483f926bde0c63 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 27 Jan 2019 22:46:48 +0100 Subject: [PATCH 166/236] Changing some functions to be effectful --- src/Data/ArrayBuffer/Typed.purs | 55 ++-- src/Data/ArrayBuffer/Typed/Gen.purs | 8 +- test/Properties/TypedArray.purs | 390 ++++++++++++++-------------- 3 files changed, 222 insertions(+), 231 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index c7e04f3..0a376e6 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -21,22 +21,21 @@ module Data.ArrayBuffer.Typed , toString, toString', toArray ) where -import Prelude (Unit, (>=), (&&), (<<<), (<=), pure, (-), flip, (*), (*>)) - import Data.Array (length) as A import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) +import Data.Float32 (Float32) as F import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) import Data.Maybe (Maybe(..), fromMaybe) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Tuple (Tuple(..)) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) -import Data.Float32 (Float32) as F import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) +import Prelude (Unit, (>=), (&&), (<<<), (<=), pure, (-), flip, (*), (*>)) import Type.Proxy (Proxy(..)) @@ -60,15 +59,15 @@ length = lengthImpl -- object creator implementations for each typed array -foreign import newUint8ClampedArray :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray -foreign import newUint32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array -foreign import newUint16Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array -foreign import newUint8Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array -foreign import newInt32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array -foreign import newInt16Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array -foreign import newInt8Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array -foreign import newFloat32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array -foreign import newFloat64Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array +foreign import newUint8ClampedArray :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray +foreign import newUint32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array +foreign import newUint16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array +foreign import newUint8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array +foreign import newInt32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array +foreign import newInt16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array +foreign import newInt8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array +foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array +foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array -- ---- @@ -135,7 +134,7 @@ type Range = Maybe (Tuple Offset (Maybe Offset)) -- | - `toString` prints to a CSV, `toString'` allows you to supply the delimiter -- | - `toArray` returns an array of numeric values class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where - create :: forall x. Fn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) + create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where create = newUint8ClampedArray @@ -157,32 +156,32 @@ instance typedArrayFloat64 :: TypedArray Float64 Number where create = newFloat64Array -- | View mapping the whole `ArrayBuffer`. -whole :: forall a t. TypedArray a t => ArrayBuffer -> ArrayView a -whole a = runFn3 create a null null +whole :: forall a t. TypedArray a t => ArrayBuffer -> Effect (ArrayView a) +whole a = runEffectFn3 create a null null -- | View mapping the rest of an `ArrayBuffer` after an index. -remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> ArrayView a +remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Effect (ArrayView a) remainder a x = remainder' a o where o = x * toInt' (Proxy :: Proxy b) -remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> ArrayView a -remainder' a x = runFn3 create a (notNull x) null +remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) +remainder' a x = runEffectFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. -part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Length -> ArrayView a +part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) part a x y = part' a o y where o = x * toInt' (Proxy :: Proxy b) -part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> ArrayView a -part' a x y = runFn3 create a (notNull x) (notNull y) +part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) +part' a x y = runEffectFn3 create a (notNull x) (notNull y) -- | Creates an empty typed array, where each value is assigned 0 -empty :: forall a t. TypedArray a t => Length -> ArrayView a -empty n = runFn3 create n null null +empty :: forall a t. TypedArray a t => Length -> Effect (ArrayView a) +empty n = runEffectFn3 create n null null -- | Creates a typed array from an input array of values, to be binary serialized -fromArray :: forall a t. TypedArray a t => Array t -> ArrayView a -fromArray a = runFn3 create a null null +fromArray :: forall a t. TypedArray a t => Array t -> Effect (ArrayView a) +fromArray a = runEffectFn3 create a null null -- | Fill the array with a value fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit @@ -424,8 +423,8 @@ at a n = if a `hasIndex` n infixl 3 at as ! -foreign import toArrayImpl :: forall a b. ArrayView a -> Array b +foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b) -- | Turn typed array into an array. -toArray :: forall a t. TypedArray a t => ArrayView a -> Array t -toArray = toArrayImpl +toArray :: forall a t. TypedArray a t => ArrayView a -> Effect (Array t) +toArray a = runEffectFn1 toArrayImpl a diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index ffe3dd8..ebc7db0 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -2,23 +2,23 @@ module Data.ArrayBuffer.Typed.Gen where -import Prelude ((<$>), bind, (/), (-), negate, ($), bottom, pure, top) - import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) +import Data.Float32 (Float32, fromNumber) as F import Data.Generic.Rep (class Generic) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Data.UInt (fromInt) as UInt -import Data.Float32 (Float32, fromNumber) as F import Data.UInt.Gen (genUInt) as UInt import Data.Unfoldable (replicateA) import Data.Vec (Vec) import Data.Vec (fromArray) as Vec +import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) +import Prelude ((<$>), bind, (/), (-), negate, ($), bottom, pure, top) import Type.Proxy (Proxy(..)) @@ -30,7 +30,7 @@ genTypedArray :: forall m a t genTypedArray gen = sized \s -> do n <- chooseInt 0 s a <- replicateA n gen - pure (TA.fromArray a) + pure (unsafePerformEffect $ TA.fromArray a) genUint8 :: forall m. MonadGen m => m UInt genUint8 = UInt.fromInt <$> chooseInt 0 255 diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index e570952..b9cbfbd 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -4,7 +4,6 @@ module Test.Properties.TypedArray where import Prelude import Control.Monad.Gen (suchThat) -import Data.Array (drop, take) import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA @@ -92,9 +91,7 @@ typedArrayTests count = do log " - let z' = subArray 0 (length x) x; q = toArray x; mutate z'; pure q /= toArray x" modifyingSubArrayMutatesOriginalAllTests count log " - let z' = subArray o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSubArrayPartTests count - log " - let z' = subArray 0 o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSubArrayPart2Tests count + modifyingOriginalMutatesSubArrayPartTests count log " - let z' = slice x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSliceTests count log " - let z' = slice o x; q = toArray z'; mutate x; pure q == toArray z'" @@ -119,7 +116,7 @@ type TestableArrayF a b n t q = => BytesPerValue a b => Nat b => WithOffset n a - -> q + -> Effect q overAll' :: forall q n. Testable q => Nat n => Int -> Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit @@ -129,7 +126,7 @@ overAll' mn count f = do let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit chk s _ gen = do log $ " - " <> s - quickCheckGen $ f <$> genWithOffset arr + quickCheckGen $ unsafePerformEffect <<< f <$> genWithOffset arr where arr :: Gen (ArrayView a) arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs @@ -154,13 +151,12 @@ partBehavesLikeTakeDrop :: Ref Int -> Effect Unit partBehavesLikeTakeDrop count = overAll count f where f :: forall a b t. TestableArrayF a b D0 t Result - f (WithOffset _ a) = + f (WithOffset _ xs) = do let n = 2 - na = TA.toArray a - ba = TA.buffer a - pa :: ArrayView a - pa = TA.part ba n n - in take n (drop n na) === TA.toArray pa + axs <- TA.toArray xs + pxs <- TA.part (TA.buffer xs) n n :: Effect (ArrayView a) + aps <- TA.toArray pxs + pure $ Array.take n (Array.drop n axs) === aps byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValueEqLength @@ -168,21 +164,24 @@ byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValue byteLengthDivBytesPerValueEqLength :: forall a b t. TestableArrayF a b D0 t Result byteLengthDivBytesPerValueEqLength (WithOffset _ a) = let b = toInt' (Proxy :: Proxy b) - in TA.length a === (TA.byteLength a `div` b) + in pure $ TA.length a === (TA.byteLength a `div` b) fromArrayToArrayIsoTests :: Ref Int -> Effect Unit fromArrayToArrayIsoTests count = overAll count fromArrayToArrayIso where fromArrayToArrayIso :: forall a b t. TestableArrayF a b D0 t Result - fromArrayToArrayIso (WithOffset _ x) = - TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x + fromArrayToArrayIso (WithOffset _ xs) = do + axs <- TA.toArray xs + xs' <- TA.fromArray axs :: Effect (ArrayView a) + axs' <- TA.toArray xs' + pure $ axs' === axs allAreFilledTests :: Ref Int -> Effect Unit allAreFilledTests count = overAll count allAreFilled where allAreFilled :: forall a b t. TestableArrayF a b D0 t Result - allAreFilled (WithOffset _ xs) = unsafePerformEffect do + allAreFilled (WithOffset _ xs) = do let x = case TA.at xs 0 of Nothing -> zero Just y -> y @@ -195,7 +194,7 @@ setSingletonIsEqTests :: Ref Int -> Effect Unit setSingletonIsEqTests count = overAll count setSingletonIsEq where setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result - setSingletonIsEq (WithOffset os xs) = unsafePerformEffect do + setSingletonIsEq (WithOffset os xs) = do case TA.at xs 0 of Nothing -> pure Success Just x -> do @@ -212,7 +211,7 @@ allImpliesAnyTests count = overAll count allImpliesAny let pred x = x /= zero all' = TA.all pred xs "All don't satisfy the predicate" any' = TA.any pred xs "None satisfy the predicate" - in (TA.length xs === zero) |=| all' ==> any' + in pure $ (TA.length xs === zero) |=| all' ==> any' -- | Should work with any arbitrary predicate, but we can't generate them @@ -224,7 +223,7 @@ filterImpliesAllTests count = overAll count filterImpliesAll let pred x = x /= zero ys = TA.filter pred xs all' = TA.all pred ys - in all' "Filter doesn't imply all" + in pure $ all' "Filter doesn't imply all" -- | Should work with any arbitrary predicate, but we can't generate them @@ -232,11 +231,12 @@ filterIsTotalTests :: Ref Int -> Effect Unit filterIsTotalTests count = overAll count filterIsTotal where filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result - filterIsTotal (WithOffset _ xs) = + filterIsTotal (WithOffset _ xs) = do let pred x = x /= zero ys = TA.filter pred xs zs = TA.filter (\x -> not pred x) ys - in TA.toArray zs === [] + azs <- TA.toArray zs + pure $ azs === [] -- | Should work with any arbitrary predicate, but we can't generate them @@ -244,26 +244,28 @@ filterIsIdempotentTests :: Ref Int -> Effect Unit filterIsIdempotentTests count = overAll count filterIsIdempotent where filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result - filterIsIdempotent (WithOffset _ xs) = + filterIsIdempotent (WithOffset _ xs) = do let pred x = x /= zero ys = TA.filter pred xs zs = TA.filter pred ys - in TA.toArray zs === TA.toArray ys + azs <- TA.toArray zs + ays <- TA.toArray ys + pure $ azs === ays withOffsetHasIndexTests :: Ref Int -> Effect Unit withOffsetHasIndexTests count = overAll1 count withOffsetHasIndex where withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result - withOffsetHasIndex (WithOffset os xs) = - Array.all (\o -> TA.hasIndex xs o) os "All doesn't have index of itself" + withOffsetHasIndex (WithOffset os xs) = pure $ + Array.all (TA.hasIndex xs) os "All doesn't have index of itself" withOffsetElemTests :: Ref Int -> Effect Unit withOffsetElemTests count = overAll1 count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result - withOffsetElem (WithOffset os xs) = + withOffsetElem (WithOffset os xs) = pure $ Array.all (\o -> TA.elem (unsafePartial (TA.unsafeAt xs o)) Nothing xs) os "All doesn't have an elem of itself" @@ -282,7 +284,7 @@ anyImpliesFindTests count = overAll count anyImpliesFind Just z -> if pred z then Success else Failed "Found value doesn't satisfy the predicate" - in p ==> q + in pure $ p ==> q -- | Should work with any arbitrary predicate, but we can't generate them @@ -293,7 +295,7 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt findIndexImpliesAt (WithOffset _ xs) = let pred x o = x /= zero mo = TA.findIndex pred xs - in case mo of + in pure case mo of Nothing -> Success Just o -> case TA.at xs o of Nothing -> Failed "No value at found index" @@ -305,7 +307,7 @@ indexOfImpliesAtTests :: Ref Int -> Effect Unit indexOfImpliesAtTests count = overAll count indexOfImpliesAt where indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - indexOfImpliesAt (WithOffset _ xs) = + indexOfImpliesAt (WithOffset _ xs) = pure case TA.at xs 0 of Nothing -> Success Just y -> case TA.indexOf y Nothing xs of @@ -317,7 +319,7 @@ lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt where lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - lastIndexOfImpliesAt (WithOffset _ xs) = + lastIndexOfImpliesAt (WithOffset _ xs) = pure case TA.at xs 0 of Nothing -> Success Just y -> case TA.lastIndexOf y Nothing xs of @@ -329,84 +331,87 @@ foldrConsIsToArrayTests :: Ref Int -> Effect Unit foldrConsIsToArrayTests count = overAll count foldrConsIsToArray where foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result - foldrConsIsToArray (WithOffset _ xs) = - TA.foldr (\x acc -> Array.cons x acc) [] xs === TA.toArray xs + foldrConsIsToArray (WithOffset _ xs) = do + axs <- TA.toArray xs + pure $ TA.foldr (\x acc -> Array.cons x acc) [] xs === axs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray where foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result - foldlSnocIsToArray (WithOffset _ xs) = - TA.foldl (\acc x -> Array.snoc acc x) [] xs === TA.toArray xs + foldlSnocIsToArray (WithOffset _ xs) = do + axs <- TA.toArray xs + pure $ TA.foldl (\acc x -> Array.snoc acc x) [] xs === axs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity where mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - mapIdentityIsIdentity (WithOffset _ xs) = - TA.toArray (TA.map identity xs) === TA.toArray xs + mapIdentityIsIdentity (WithOffset _ xs) = do + axs <- TA.toArray xs + mxs <- TA.toArray (TA.map identity xs) + pure $ axs === mxs traverseSnocIsToArrayTests :: Ref Int -> Effect Unit traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray where traverseSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result - traverseSnocIsToArray (WithOffset _ xs) = - let ys = unsafePerformEffect do - ref <- Ref.new [] - TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs - Ref.read ref - in TA.toArray xs === ys + traverseSnocIsToArray (WithOffset _ xs) = do + ref <- Ref.new [] + TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs + ys <- Ref.read ref + axs <- TA.toArray xs + pure $ axs === ys doubleReverseIsIdentityTests :: Ref Int -> Effect Unit doubleReverseIsIdentityTests count = overAll count doubleReverseIsIdentity where doubleReverseIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - doubleReverseIsIdentity (WithOffset _ xs) = - let ys = TA.toArray xs - _ = unsafePerformEffect do - TA.reverse xs - TA.reverse xs - in TA.toArray xs === ys + doubleReverseIsIdentity (WithOffset _ xs) = do + axs <- TA.toArray xs + TA.reverse xs + TA.reverse xs + axs' <- TA.toArray xs + pure $ axs === axs' reverseIsArrayReverseTests :: Ref Int -> Effect Unit reverseIsArrayReverseTests count = overAll count reverseIsArrayReverse where reverseIsArrayReverse :: forall a b t. TestableArrayF a b D0 t Result - reverseIsArrayReverse (WithOffset _ xs) = - let ys = Array.reverse (TA.toArray xs) - _ = unsafePerformEffect do - TA.reverse xs - in TA.toArray xs === ys + reverseIsArrayReverse (WithOffset _ xs) = do + axs <- TA.toArray xs + TA.reverse xs + rxs <- TA.toArray xs + pure $ Array.reverse axs === rxs sortIsIdempotentTests :: Ref Int -> Effect Unit sortIsIdempotentTests count = overAll count sortIsIdempotent where sortIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result - sortIsIdempotent (WithOffset _ xs) = - let ys = unsafePerformEffect do - TA.sort xs - pure (TA.toArray xs) - zs = unsafePerformEffect do - TA.sort xs - pure (TA.toArray xs) - in zs === ys + sortIsIdempotent (WithOffset _ xs) = do + TA.sort xs + ys <- TA.toArray xs + TA.sort xs + zs <- TA.toArray xs + pure $ zs === ys sortIsArraySortTests :: Ref Int -> Effect Unit sortIsArraySortTests count = overAll count sortIsArraySort where sortIsArraySort :: forall a b t. TestableArrayF a b D0 t Result - sortIsArraySort (WithOffset _ xs) = - let ys = Array.sort (TA.toArray xs) - _ = unsafePerformEffect do - TA.sort xs - in TA.toArray xs === ys + sortIsArraySort (WithOffset _ xs) = do + axs <- TA.toArray xs + let ys = Array.sort axs + TA.sort xs + sxs <- TA.toArray xs + pure $ sxs === ys toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit @@ -414,231 +419,218 @@ toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma where toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result toStringIsJoinWithComma (WithOffset _ xs) = - TA.toString' xs "," === TA.toString xs + pure $ TA.toString' xs "," === TA.toString xs setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIdentity where setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - setTypedOfSubArrayIsIdentity (WithOffset _ xs) = - let ys = TA.toArray xs - zsSub = TA.subArray xs Nothing - zs = unsafePerformEffect do - _ <- TA.setTyped xs Nothing zsSub - pure (TA.toArray xs) - in zs === ys + setTypedOfSubArrayIsIdentity (WithOffset _ xs) = do + ys <- TA.toArray xs + let zsSub = TA.subArray xs Nothing + _ <- TA.setTyped xs Nothing zsSub + zs <- TA.toArray xs + pure $ zs === ys modifyingOriginalMutatesSubArrayTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMutatesSubArray where modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArray (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingOriginalMutatesSubArray (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.subArray xs Nothing - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs /== ys + zs <- TA.toArray zsSub + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMutatesSubArray where modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArray (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingOriginalMutatesSubArray (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.subArray xs Nothing - zs = TA.toArray xs - ys = unsafePerformEffect do - TA.fill zsSub zero Nothing - pure (TA.toArray xs) - in zs /== ys + zs <- TA.toArray xs + TA.fill zsSub zero Nothing + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayZeroTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOriginalMutatesSubArrayZero where modifyingOriginalMutatesSubArrayZero :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArrayZero (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingOriginalMutatesSubArrayZero (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs /== ys + zs <- TA.toArray zsSub + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalZeroTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArrayMutatesOriginalZero where modifyingSubArrayMutatesOriginalZero :: forall a b t. TestableArrayF a b D0 t Result - modifyingSubArrayMutatesOriginalZero (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingSubArrayMutatesOriginalZero (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) - zs = TA.toArray xs - ys = unsafePerformEffect do - TA.fill zsSub zero Nothing - pure (TA.toArray xs) - in zs /== ys + zs <- TA.toArray xs + TA.fill zsSub zero Nothing + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayAllTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginalMutatesSubArrayAll where modifyingOriginalMutatesSubArrayAll :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArrayAll (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingOriginalMutatesSubArrayAll (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs /== ys + zs <- TA.toArray zsSub + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalAllTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArrayMutatesOriginalAll where modifyingSubArrayMutatesOriginalAll :: forall a b t. TestableArrayF a b D0 t Result - modifyingSubArrayMutatesOriginalAll (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingSubArrayMutatesOriginalAll (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) - zs = TA.toArray xs - ys = unsafePerformEffect do - TA.fill zsSub zero Nothing - pure (TA.toArray xs) - in zs /== ys + zs <- TA.toArray xs + TA.fill zsSub zero Nothing + ys <- TA.toArray xs + pure $ zs /== ys -modifyingOriginalDoesntMutateSubArrayPartTests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSubArrayPartTests count = overAll count modifyingOriginalMutatesSubArrayPart +modifyingOriginalMutatesSubArrayPartTests :: Ref Int -> Effect Unit +modifyingOriginalMutatesSubArrayPartTests count = overAll count modifyingOriginalMutatesSubArrayPart where modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalMutatesSubArrayPart (WithOffset os xs) - | Vec.head os == 0 = Success - | Array.all (eq zero) (TA.toArray (TA.subArray xs Nothing)) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = - let o = Vec.head os - zsSub = TA.subArray xs (Just (Tuple o Nothing)) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys - - -modifyingOriginalDoesntMutateSubArrayPart2Tests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSubArrayPart2Tests count = overAll count modifyingOriginalMutatesSubArrayPart2 - where - modifyingOriginalMutatesSubArrayPart2 :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalMutatesSubArrayPart2 (WithOffset os xs) - | Vec.head os == 0 = Success - | Array.all (eq zero) (TA.toArray (TA.subArray xs Nothing)) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = - let o = Vec.head os - zsSub = TA.subArray xs (Just (Tuple 0 (Just o))) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys + modifyingOriginalMutatesSubArrayPart (WithOffset os xs) = do + let o = Vec.head os + zsSub = TA.subArray xs (Just (Tuple o Nothing)) + zs <- TA.toArray zsSub + if o == 0 || Array.all (eq zero) zs + then pure Success + else do + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingOriginalDoesntMutateSliceTests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalDoesntMutateSlice where modifyingOriginalDoesntMutateSlice :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalDoesntMutateSlice (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = + modifyingOriginalDoesntMutateSlice (WithOffset _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs + then pure Success + else do let zsSub = TA.slice xs Nothing - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys + zs <- TA.toArray zsSub + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs === ys modifyingOriginalDoesntMutateSlicePartTests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOriginalDoesntMutateSlicePart where modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) - | Array.all (eq zero) (TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing)))) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = + modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) = do + axs <- TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing))) + if Array.all (eq zero) axs || TA.at xs (Vec.head os) == Just zero + then pure Success + else do let o = Vec.head os zsSub = TA.slice xs (Just (Tuple o Nothing)) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys + zs <- TA.toArray zsSub + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs === ys modifyingOriginalDoesntMutateSlicePart2Tests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOriginalDoesntMutateSlicePart2 where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) - | Array.all (eq zero) (TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing)))) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = + modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) = do + axs <- TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing))) + if Array.all (eq zero) axs || TA.at xs (Vec.head os) == Just zero + then pure Success + else do let o = Vec.head os zsSub = TA.slice xs (Just (Tuple 0 (Just o))) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys + zs <- TA.toArray zsSub + TA.fill xs zero Nothing + ys <- TA.toArray zsSub + pure $ zs === ys copyWithinSelfIsIdentityTests :: Ref Int -> Effect Unit copyWithinSelfIsIdentityTests count = overAll count copyWithinSelfIsIdentity where copyWithinSelfIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - copyWithinSelfIsIdentity (WithOffset _ xs) = - let ys = TA.toArray xs - zs = unsafePerformEffect do - TA.copyWithin xs 0 0 (Just (TA.length xs)) - pure (TA.toArray xs) - in zs === ys + copyWithinSelfIsIdentity (WithOffset _ xs) = do + ys <- TA.toArray xs + TA.copyWithin xs 0 0 (Just (TA.length xs)) + zs <- TA.toArray xs + pure $ zs === ys copyWithinIsSliceTests :: Ref Int -> Effect Unit copyWithinIsSliceTests count = overAll count copyWithinIsSlice where copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result - copyWithinIsSlice (WithOffset os xs) = + copyWithinIsSlice (WithOffset os xs) = do let o = Vec.head os - ys = TA.toArray (TA.slice xs (Just (Tuple o Nothing))) - zs = unsafePerformEffect do - TA.copyWithin xs 0 o Nothing - pure $ Array.drop (Array.length ys) $ TA.toArray xs - in TA.toArray xs === ys <> zs + ys <- TA.toArray (TA.slice xs (Just (Tuple o Nothing))) + TA.copyWithin xs 0 o Nothing + axs <- TA.toArray xs + zs <- pure $ Array.drop (Array.length ys) axs + pure $ axs === ys <> zs copyWithinViaSetTypedTests :: Ref Int -> Effect Unit copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped where copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result - copyWithinViaSetTyped (WithOffset os xs) = + copyWithinViaSetTyped (WithOffset os xs) = do let o = Vec.head os - xs' = TA.fromArray (TA.toArray xs) :: ArrayView a - _ = unsafePerformEffect do - let ys = TA.slice xs' (Just (Tuple o Nothing)) - _ <- TA.setTyped xs' Nothing ys - TA.copyWithin xs 0 o Nothing - in TA.toArray xs === TA.toArray xs' + txs <- TA.toArray xs + xs' <- TA.fromArray txs :: Effect (ArrayView a) + let ys = TA.slice xs' (Just (Tuple o Nothing)) + _ <- TA.setTyped xs' Nothing ys + TA.copyWithin xs 0 o Nothing + axs <- TA.toArray xs + axs' <- TA.toArray xs' + pure $ axs === axs' From a76f4c874536836358e1693972f96c9ca2b6554b Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 27 Jan 2019 23:23:52 +0100 Subject: [PATCH 167/236] Effectful slice --- src/Data/ArrayBuffer/Typed.purs | 8 ++++---- test/Properties/TypedArray.purs | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 0a376e6..f6bb84c 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -367,13 +367,13 @@ setTyped = setInternal length -- | Copy the entire contents of the typed array into a new buffer. -foreign import sliceImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) +foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. ArrayView a -> Range -> ArrayView a +slice :: forall a. ArrayView a -> Range -> Effect (ArrayView a) slice a mz = case mz of - Nothing -> runFn3 sliceImpl a null null - Just (Tuple s me) -> runFn3 sliceImpl a (notNull s) (toNullable me) + Nothing -> runEffectFn3 sliceImpl a null null + Just (Tuple s me) -> runEffectFn3 sliceImpl a (notNull s) (toNullable me) foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index b9cbfbd..978ff24 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -555,7 +555,7 @@ modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalD if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.slice xs Nothing + zsSub <- TA.slice xs Nothing zs <- TA.toArray zsSub TA.fill xs zero Nothing ys <- TA.toArray zsSub @@ -567,12 +567,13 @@ modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOrigi where modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) = do - axs <- TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing))) - if Array.all (eq zero) axs || TA.at xs (Vec.head os) == Just zero + sl <- TA.slice xs (Just (Tuple (Vec.head os) Nothing)) + axs <- TA.toArray sl + let o = Vec.head os + if Array.all (eq zero) axs || TA.at xs o == Just zero then pure Success else do - let o = Vec.head os - zsSub = TA.slice xs (Just (Tuple o Nothing)) + zsSub <- TA.slice xs (Just (Tuple o Nothing)) zs <- TA.toArray zsSub TA.fill xs zero Nothing ys <- TA.toArray zsSub @@ -584,12 +585,13 @@ modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOrig where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) = do - axs <- TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing))) - if Array.all (eq zero) axs || TA.at xs (Vec.head os) == Just zero - then pure Success + sl <- TA.slice xs (Just (Tuple (Vec.head os) Nothing)) + axs <- TA.toArray sl + let o = Vec.head os + if Array.all (eq zero) axs || TA.at xs o == Just zero + then pure Success else do - let o = Vec.head os - zsSub = TA.slice xs (Just (Tuple 0 (Just o))) + zsSub <- TA.slice xs (Just (Tuple 0 (Just o))) zs <- TA.toArray zsSub TA.fill xs zero Nothing ys <- TA.toArray zsSub @@ -613,7 +615,8 @@ copyWithinIsSliceTests count = overAll count copyWithinIsSlice copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result copyWithinIsSlice (WithOffset os xs) = do let o = Vec.head os - ys <- TA.toArray (TA.slice xs (Just (Tuple o Nothing))) + sl <- TA.slice xs (Just (Tuple o Nothing)) + ys <- TA.toArray sl TA.copyWithin xs 0 o Nothing axs <- TA.toArray xs zs <- pure $ Array.drop (Array.length ys) axs @@ -628,7 +631,7 @@ copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped let o = Vec.head os txs <- TA.toArray xs xs' <- TA.fromArray txs :: Effect (ArrayView a) - let ys = TA.slice xs' (Just (Tuple o Nothing)) + ys <- TA.slice xs' (Just (Tuple o Nothing)) _ <- TA.setTyped xs' Nothing ys TA.copyWithin xs 0 o Nothing axs <- TA.toArray xs From e50d73e9746f19b83aff18f6026d336ab7ddc6e4 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Sun, 27 Jan 2019 23:34:17 +0100 Subject: [PATCH 168/236] Simplify some lambdas, remove some unneeded bindings --- test/Properties/TypedArray.purs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 978ff24..6c17127 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -121,7 +121,7 @@ type TestableArrayF a b n t q = overAll' :: forall q n. Testable q => Nat n => Int -> Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit overAll' mn count f = do - void (Ref.modify (\x -> x + 1) count) + void (Ref.modify (_ + 1) count) let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit chk s _ gen = do @@ -186,7 +186,7 @@ allAreFilledTests count = overAll count allAreFilled Nothing -> zero Just y -> y TA.fill xs x Nothing - let b = TA.all (\y -> y == x) xs + let b = TA.all (_ == x) xs pure (b "All aren't the filled value") @@ -234,7 +234,7 @@ filterIsTotalTests count = overAll count filterIsTotal filterIsTotal (WithOffset _ xs) = do let pred x = x /= zero ys = TA.filter pred xs - zs = TA.filter (\x -> not pred x) ys + zs = TA.filter (not pred) ys azs <- TA.toArray zs pure $ azs === [] @@ -333,7 +333,7 @@ foldrConsIsToArrayTests count = overAll count foldrConsIsToArray foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldrConsIsToArray (WithOffset _ xs) = do axs <- TA.toArray xs - pure $ TA.foldr (\x acc -> Array.cons x acc) [] xs === axs + pure $ TA.foldr Array.cons [] xs === axs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit @@ -342,7 +342,7 @@ foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldlSnocIsToArray (WithOffset _ xs) = do axs <- TA.toArray xs - pure $ TA.foldl (\acc x -> Array.snoc acc x) [] xs === axs + pure $ TA.foldl Array.snoc [] xs === axs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit @@ -567,8 +567,7 @@ modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOrigi where modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) = do - sl <- TA.slice xs (Just (Tuple (Vec.head os) Nothing)) - axs <- TA.toArray sl + axs <- TA.toArray =<< TA.slice xs (Just (Tuple (Vec.head os) Nothing)) let o = Vec.head os if Array.all (eq zero) axs || TA.at xs o == Just zero then pure Success @@ -585,8 +584,7 @@ modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOrig where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) = do - sl <- TA.slice xs (Just (Tuple (Vec.head os) Nothing)) - axs <- TA.toArray sl + axs <- TA.toArray =<< TA.slice xs (Just (Tuple (Vec.head os) Nothing)) let o = Vec.head os if Array.all (eq zero) axs || TA.at xs o == Just zero then pure Success @@ -615,8 +613,7 @@ copyWithinIsSliceTests count = overAll count copyWithinIsSlice copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result copyWithinIsSlice (WithOffset os xs) = do let o = Vec.head os - sl <- TA.slice xs (Just (Tuple o Nothing)) - ys <- TA.toArray sl + ys <- TA.toArray =<< TA.slice xs (Just (Tuple o Nothing)) TA.copyWithin xs 0 o Nothing axs <- TA.toArray xs zs <- pure $ Array.drop (Array.length ys) axs From f6caddc4077aec9d0cad13ee3246be46d5d8fa3b Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Mon, 28 Jan 2019 20:48:08 +0100 Subject: [PATCH 169/236] Changed at to be effectful --- src/Data/ArrayBuffer/Typed.purs | 14 ++++---- test/Properties/TypedArray.purs | 62 ++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f6bb84c..04720e2 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -35,7 +35,7 @@ import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) -import Prelude (Unit, (>=), (&&), (<<<), (<=), pure, (-), flip, (*), (*>)) +import Prelude (Unit, flip, pure, (&&), (*), (*>), (-), (<$>), (<<<), (<=), (>=)) import Type.Proxy (Proxy(..)) @@ -268,8 +268,8 @@ elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolea elem x mo a = runFn3 includesImpl a x (toNullable mo) -- | Fetch element at index. -unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> t -unsafeAt a o = runFn2 unsafeAtImpl a o +unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t +unsafeAt a o = runEffectFn2 unsafeAtImpl a o -- | Folding from the left foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b @@ -406,7 +406,7 @@ toString' :: forall a. ArrayView a -> String -> String toString' a s = runFn2 joinImpl a s -foreign import unsafeAtImpl :: forall a b. Fn2 (ArrayView a) Offset b +foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean @@ -415,10 +415,10 @@ hasIndex :: forall a. ArrayView a -> Offset -> Boolean hasIndex a o = runFn2 hasIndexImpl a o -- | Fetch element at index. -at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t +at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Effect (Maybe t) at a n = if a `hasIndex` n - then Just (unsafePartial (unsafeAt a n)) - else Nothing + then Just <$> unsafePartial (unsafeAt a n) + else pure Nothing infixl 3 at as ! diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 6c17127..782fe9b 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -182,7 +182,8 @@ allAreFilledTests count = overAll count allAreFilled where allAreFilled :: forall a b t. TestableArrayF a b D0 t Result allAreFilled (WithOffset _ xs) = do - let x = case TA.at xs 0 of + e <- TA.at xs 0 + let x = case e of Nothing -> zero Just y -> y TA.fill xs x Nothing @@ -195,11 +196,14 @@ setSingletonIsEqTests count = overAll count setSingletonIsEq where setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result setSingletonIsEq (WithOffset os xs) = do - case TA.at xs 0 of + e <- TA.at xs 0 + case e of Nothing -> pure Success Just x -> do - _ <- TA.set xs (Just (Vec.head os)) [x] - pure (TA.at xs (Vec.head os) === Just x) + let o = Vec.head os + _ <- TA.set xs (Just o) [x] + e' <- TA.at xs o + pure $ e' === Just x -- | Should work with any arbitrary predicate, but we can't generate them @@ -266,7 +270,7 @@ withOffsetElemTests count = overAll1 count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = pure $ - Array.all (\o -> TA.elem (unsafePartial (TA.unsafeAt xs o)) Nothing xs) os + Array.all (\o -> TA.elem (unsafePartial $ unsafePerformEffect $ TA.unsafeAt xs o) Nothing xs) os "All doesn't have an elem of itself" @@ -292,14 +296,16 @@ findIndexImpliesAtTests :: Ref Int -> Effect Unit findIndexImpliesAtTests count = overAll count findIndexImpliesAt where findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - findIndexImpliesAt (WithOffset _ xs) = - let pred x o = x /= zero + findIndexImpliesAt (WithOffset _ xs) = do + let pred x _ = x /= zero mo = TA.findIndex pred xs - in pure case mo of - Nothing -> Success - Just o -> case TA.at xs o of - Nothing -> Failed "No value at found index" - Just x -> pred x o "Find index implies at" + case mo of + Nothing -> pure Success + Just o -> do + e <- TA.at xs o + case e of + Nothing -> pure $ Failed "No value at found index" + Just x -> pure $ pred x o "Find index implies at" @@ -307,24 +313,30 @@ indexOfImpliesAtTests :: Ref Int -> Effect Unit indexOfImpliesAtTests count = overAll count indexOfImpliesAt where indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - indexOfImpliesAt (WithOffset _ xs) = pure - case TA.at xs 0 of - Nothing -> Success + indexOfImpliesAt (WithOffset _ xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success Just y -> case TA.indexOf y Nothing xs of - Nothing -> Failed "no index of" - Just o -> TA.at xs o === Just y + Nothing -> pure $ Failed "no index of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt where lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - lastIndexOfImpliesAt (WithOffset _ xs) = pure - case TA.at xs 0 of - Nothing -> Success + lastIndexOfImpliesAt (WithOffset _ xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success Just y -> case TA.lastIndexOf y Nothing xs of - Nothing -> Failed "no lastIndex of" - Just o -> TA.at xs o === Just y + Nothing -> pure $ Failed "no lastIndex of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y foldrConsIsToArrayTests :: Ref Int -> Effect Unit @@ -569,7 +581,8 @@ modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOrigi modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) = do axs <- TA.toArray =<< TA.slice xs (Just (Tuple (Vec.head os) Nothing)) let o = Vec.head os - if Array.all (eq zero) axs || TA.at xs o == Just zero + e <- TA.at xs o + if Array.all (eq zero) axs || e == Just zero then pure Success else do zsSub <- TA.slice xs (Just (Tuple o Nothing)) @@ -586,7 +599,8 @@ modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOrig modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) = do axs <- TA.toArray =<< TA.slice xs (Just (Tuple (Vec.head os) Nothing)) let o = Vec.head os - if Array.all (eq zero) axs || TA.at xs o == Just zero + e <- TA.at xs o + if Array.all (eq zero) axs || e == Just zero then pure Success else do zsSub <- TA.slice xs (Just (Tuple 0 (Just o))) From 8c59b1d6cff29323ef3ca3ef38fff736f77d1dbf Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Mon, 28 Jan 2019 23:46:25 +0100 Subject: [PATCH 170/236] Changed fill/slice/subArray to be consistent with Array/Node.Buffer --- src/Data/ArrayBuffer/ArrayBuffer.js | 4 +- src/Data/ArrayBuffer/ArrayBuffer.purs | 13 +-- src/Data/ArrayBuffer/Typed.js | 9 +-- src/Data/ArrayBuffer/Typed.purs | 31 +++---- test/Properties/TypedArray.purs | 111 +++++++++++++++++--------- 5 files changed, 94 insertions(+), 74 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 656d576..b341921 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -10,6 +10,6 @@ exports.byteLength = function byteLength (a) { return a.byteLength; }; -exports.sliceImpl = function sliceImpl (a, ms, me) { - return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me); +exports.sliceImpl = function sliceImpl (a, s, e) { + return a.slice(s, e); }; diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 4bd11a0..6dd5b4e 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -9,9 +9,6 @@ module Data.ArrayBuffer.ArrayBuffer import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) -import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, notNull, null) -import Data.Tuple (Tuple(..)) import Effect (Effect) import Effect.Uncurried (EffectFn1, runEffectFn1) @@ -26,12 +23,8 @@ empty l = runEffectFn1 emptyImpl l -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength -foreign import sliceImpl :: Fn3 ArrayBuffer (Nullable ByteOffset) (Nullable ByteOffset) ArrayBuffer +foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. -slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer -slice a mz = case mz of - Nothing -> runFn3 sliceImpl a null null - Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (notNull s) null - Just e -> runFn3 sliceImpl a (notNull s) (notNull e) +slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer +slice s e a = runFn3 sliceImpl a s e diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index fb56808..3f52aa7 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -145,18 +145,17 @@ exports.setImpl = function setImpl (a, off, b) { }; -exports.sliceImpl = function sliceImpl (a,ms,me) { - return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me); +exports.sliceImpl = function sliceImpl (a, s, e) { + return a.slice(s,e); }; - exports.sortImpl = function sortImpl (a) { a.sort(); }; -exports.subArrayImpl = function subArrayImpl (a,ms,me) { - return me === null ? (ms === null ? a.subarray() : a.subarray(ms)) : a.subarray(ms,me); +exports.subArrayImpl = function subArrayImpl (a, s, e) { + return a.subarray(s, e); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 04720e2..04d724b 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -3,7 +3,7 @@ module Data.ArrayBuffer.Typed ( polyFill - , Offset, Length, Range + , Offset, Length , buffer, byteOffset, byteLength, length , class TypedArray , create, whole, remainder, part, empty, fromArray @@ -28,7 +28,6 @@ import Data.Float32 (Float32) as F import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) import Data.Maybe (Maybe(..), fromMaybe) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) -import Data.Tuple (Tuple(..)) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Effect (Effect) @@ -75,7 +74,7 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b (Nullable Offset) (Nullable Offset) Unit +foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit @@ -96,10 +95,6 @@ type Offset = Int -- | Value-oriented array length type Length = Int --- | Represents a range of indices, where if omitted, it represents the whole span. --- | If only the second argument is omitted, then it represents the remainder of the span after the first index. -type Range = Maybe (Tuple Offset (Maybe Offset)) - -- TODO use purescript-quotient -- | Typeclass that associates a measured user-level type with a typed array. @@ -184,10 +179,8 @@ fromArray :: forall a t. TypedArray a t => Array t -> Effect (ArrayView a) fromArray a = runEffectFn3 create a null null -- | Fill the array with a value -fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit -fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s me) -> runEffectFn4 fillImpl a x (notNull s) (toNullable me) +fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit +fill x s e a = runEffectFn4 fillImpl a x s e -- | Stores multiple values into the typed array set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean @@ -367,13 +360,11 @@ setTyped = setInternal length -- | Copy the entire contents of the typed array into a new buffer. -foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) +foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a) -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. ArrayView a -> Range -> Effect (ArrayView a) -slice a mz = case mz of - Nothing -> runEffectFn3 sliceImpl a null null - Just (Tuple s me) -> runEffectFn3 sliceImpl a (notNull s) (toNullable me) +slice :: forall a. Offset -> Offset -> ArrayView a -> Effect (ArrayView a) +slice s e a = runEffectFn3 sliceImpl a s e foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit @@ -382,7 +373,7 @@ sort :: forall a. ArrayView a -> Effect Unit sort a = runEffectFn1 sortImpl a -foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) +foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -- | @@ -391,10 +382,8 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu -- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. -- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves -- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. -subArray :: forall a. ArrayView a -> Range -> ArrayView a -subArray a mz = case mz of - Nothing -> runFn3 subArrayImpl a null null - Just (Tuple s me) -> runFn3 subArrayImpl a (notNull s) (toNullable me) +subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a +subArray s e a = runFn3 subArrayImpl a s e -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. foreign import toString :: forall a. ArrayView a -> String diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 782fe9b..af84bc2 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -10,10 +10,9 @@ import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithOffset) import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint8Array, Uint8ClampedArray, Uint32Array) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Data.Maybe (Maybe(..)) -import Data.Tuple (Tuple(..)) -import Data.Typelevel.Num (class Nat, D0, D1, D5, toInt') -import Data.Vec (head) as Vec +import Data.Maybe (Maybe(..), fromMaybe) +import Data.Typelevel.Num (class Nat, D0, D1, D2, D5, d0, d1, toInt') +import Data.Vec (head, index) as Vec import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) @@ -28,8 +27,12 @@ import Type.Proxy (Proxy(..)) typedArrayTests :: Ref Int -> Effect Unit typedArrayTests count = do + log " - subarrayBehavesLikeArraySlice" + subarrayBehavesLikeArraySliceTests count + log " - sliceBehavesLikeArraySlice" + sliceBehavesLikeArraySliceTests count log " - partBehavesLikeTakeDrop" - partBehavesLikeTakeDrop count + partBehavesLikeTakeDropTests count log " - byteLength x / bytesPerValue === length x" byteLengthDivBytesPerValueTests count log " - fromArray (toArray x) === x" @@ -147,8 +150,32 @@ overAll count f = overAll' 0 count f overAll1 :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit overAll1 count f = overAll' 1 count f -partBehavesLikeTakeDrop :: Ref Int -> Effect Unit -partBehavesLikeTakeDrop count = overAll count f +subarrayBehavesLikeArraySliceTests :: Ref Int -> Effect Unit +subarrayBehavesLikeArraySliceTests count = overAll count f + where + f :: forall a b t. TestableArrayF a b D2 t Result + f (WithOffset os xs) = do + let s = os `Vec.index` d0 + e = os `Vec.index` d1 + axs <- TA.toArray xs + let sxs = TA.subArray s e xs + a <- TA.toArray sxs + pure $ Array.slice s e axs === a + +sliceBehavesLikeArraySliceTests :: Ref Int -> Effect Unit +sliceBehavesLikeArraySliceTests count = overAll count f + where + f :: forall a b t. TestableArrayF a b D2 t Result + f (WithOffset os xs) = do + let s = os `Vec.index` d0 + e = os `Vec.index` d1 + axs <- TA.toArray xs + sxs <- TA.slice s e xs + a <- TA.toArray sxs + pure $ Array.slice s e axs === a + +partBehavesLikeTakeDropTests :: Ref Int -> Effect Unit +partBehavesLikeTakeDropTests count = overAll count f where f :: forall a b t. TestableArrayF a b D0 t Result f (WithOffset _ xs) = do @@ -183,10 +210,9 @@ allAreFilledTests count = overAll count allAreFilled allAreFilled :: forall a b t. TestableArrayF a b D0 t Result allAreFilled (WithOffset _ xs) = do e <- TA.at xs 0 - let x = case e of - Nothing -> zero - Just y -> y - TA.fill xs x Nothing + let x = fromMaybe zero e + l = TA.length xs + TA.fill x 0 l xs let b = TA.all (_ == x) xs pure (b "All aren't the filled value") @@ -440,7 +466,8 @@ setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIden setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result setTypedOfSubArrayIsIdentity (WithOffset _ xs) = do ys <- TA.toArray xs - let zsSub = TA.subArray xs Nothing + let l = TA.length xs + zsSub = TA.subArray 0 l xs _ <- TA.setTyped xs Nothing zsSub zs <- TA.toArray xs pure $ zs === ys @@ -455,9 +482,10 @@ modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMut if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.subArray xs Nothing + let l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray zsSub - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs /== ys @@ -471,9 +499,10 @@ modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMut if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.subArray xs Nothing + let l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray xs - TA.fill zsSub zero Nothing + TA.fill zero 0 l zsSub ys <- TA.toArray xs pure $ zs /== ys @@ -487,9 +516,10 @@ modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOrigina if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) + let l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray zsSub - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs /== ys @@ -503,9 +533,10 @@ modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArra if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) + let l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray xs - TA.fill zsSub zero Nothing + TA.fill zero 0 l zsSub ys <- TA.toArray xs pure $ zs /== ys @@ -519,9 +550,10 @@ modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginal if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) + let l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray zsSub - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs /== ys @@ -535,9 +567,10 @@ modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArray if Array.all (eq zero) axs then pure Success else do - let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) + let l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray xs - TA.fill zsSub zero Nothing + TA.fill zero 0 l zsSub ys <- TA.toArray xs pure $ zs /== ys @@ -548,12 +581,13 @@ modifyingOriginalMutatesSubArrayPartTests count = overAll count modifyingOrigina modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalMutatesSubArrayPart (WithOffset os xs) = do let o = Vec.head os - zsSub = TA.subArray xs (Just (Tuple o Nothing)) + l = TA.length xs + zsSub = TA.subArray 0 l xs zs <- TA.toArray zsSub if o == 0 || Array.all (eq zero) zs then pure Success else do - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs /== ys @@ -567,9 +601,10 @@ modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalD if Array.all (eq zero) axs then pure Success else do - zsSub <- TA.slice xs Nothing + let l = TA.length xs + zsSub <- TA.slice 0 l xs zs <- TA.toArray zsSub - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs === ys @@ -579,15 +614,16 @@ modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOrigi where modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) = do - axs <- TA.toArray =<< TA.slice xs (Just (Tuple (Vec.head os) Nothing)) + let l = TA.length xs + axs <- TA.toArray =<< TA.slice 0 l xs let o = Vec.head os e <- TA.at xs o if Array.all (eq zero) axs || e == Just zero then pure Success else do - zsSub <- TA.slice xs (Just (Tuple o Nothing)) + zsSub <- TA.slice o l xs zs <- TA.toArray zsSub - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs === ys @@ -597,15 +633,16 @@ modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOrig where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) = do - axs <- TA.toArray =<< TA.slice xs (Just (Tuple (Vec.head os) Nothing)) let o = Vec.head os + l = TA.length xs + axs <- TA.toArray =<< TA.slice o l xs e <- TA.at xs o if Array.all (eq zero) axs || e == Just zero then pure Success else do - zsSub <- TA.slice xs (Just (Tuple 0 (Just o))) + zsSub <- TA.slice o l xs zs <- TA.toArray zsSub - TA.fill xs zero Nothing + TA.fill zero 0 l xs ys <- TA.toArray zsSub pure $ zs === ys @@ -627,7 +664,8 @@ copyWithinIsSliceTests count = overAll count copyWithinIsSlice copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result copyWithinIsSlice (WithOffset os xs) = do let o = Vec.head os - ys <- TA.toArray =<< TA.slice xs (Just (Tuple o Nothing)) + l = TA.length xs + ys <- TA.toArray =<< TA.slice o l xs TA.copyWithin xs 0 o Nothing axs <- TA.toArray xs zs <- pure $ Array.drop (Array.length ys) axs @@ -642,7 +680,8 @@ copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped let o = Vec.head os txs <- TA.toArray xs xs' <- TA.fromArray txs :: Effect (ArrayView a) - ys <- TA.slice xs' (Just (Tuple o Nothing)) + let l = TA.length xs' + ys <- TA.slice o l xs' _ <- TA.setTyped xs' Nothing ys TA.copyWithin xs 0 o Nothing axs <- TA.toArray xs From 3cfed24f76b26d528dda3115f5e4db045cc52b40 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Mon, 28 Jan 2019 23:53:45 +0100 Subject: [PATCH 171/236] Remove unused dependency --- bower.json | 1 - 1 file changed, 1 deletion(-) diff --git a/bower.json b/bower.json index f05f8d1..64e11d0 100644 --- a/bower.json +++ b/bower.json @@ -18,7 +18,6 @@ "purescript-effect": "^2.0.0", "purescript-nullable": "^4.1.0", "purescript-typelevel": "^4.0.0", - "purescript-parseint": "^1.1.0", "purescript-uint": "^5.1.0", "purescript-sized-vectors": "^3.1.0", "purescript-float32": "^0.0.1" From d1c5a2bb7df6015ce5cedaff9d2b48aab2e540c8 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Mon, 28 Jan 2019 23:57:19 +0100 Subject: [PATCH 172/236] Removed note about subArray, the behaviour seems correct --- src/Data/ArrayBuffer/Typed.purs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 04d724b..f9981f6 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -376,12 +376,6 @@ sort a = runEffectFn1 sortImpl a foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. --- | --- | **Note**: there is really peculiar behavior with `subArray` - if the first offset argument is omitted, or --- | is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a --- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. --- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves --- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a subArray s e a = runFn3 subArrayImpl a s e From 5e7ac0dc493560494ecc20a5eff7a35dca004430 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 22:25:17 +0100 Subject: [PATCH 173/236] Use Nullable --- src/Data/ArrayBuffer/DataView.js | 4 ++-- src/Data/ArrayBuffer/DataView.purs | 22 +++++++++------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 44ad1b9..6bfee81 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -29,8 +29,8 @@ exports.byteLength = function byteLength (v) { exports.getterImpl = function getterImpl (data, v, o) { return ((o + data.bytesPerValue) >>> 0) <= v.byteLength - ? data.just (v[data.functionName].call(v,o,data.littleEndian)) - : data.nothing; + ? v[data.functionName].call(v,o,data.littleEndian) + : null; }; exports.setterImpl = function setterImpl (data, v, o, n) { diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index cffe5ec..001f37b 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -48,17 +48,17 @@ module Data.ArrayBuffer.DataView , whole ) where -import Prelude (class Eq, (==), (<>)) - import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, kind ArrayViewType) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) -import Data.Maybe (Maybe(..)) +import Data.Float32 (Float32) as F +import Data.Maybe (Maybe) +import Data.Nullable (Nullable, toMaybe) +import Data.Symbol (SProxy(..), class IsSymbol, reflectSymbol) import Data.Typelevel.Num (toInt', class Nat) import Data.UInt (UInt) -import Data.Float32 (Float32) as F -import Data.Symbol (SProxy (..), class IsSymbol, reflectSymbol) import Effect (Effect) import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4) +import Prelude (class Eq, (<$>), (<>), (==)) import Type.Proxy (Proxy(..)) @@ -122,12 +122,10 @@ instance showArrayViewTypeViewFloat32 :: ShowArrayViewType Float32 "Float32" instance showArrayViewTypeViewFloat64 :: ShowArrayViewType Float64 "Float64" foreign import getterImpl :: forall t - . EffectFn3 { just :: t -> Maybe t - , nothing :: Maybe t - , functionName :: String + . EffectFn3 { functionName :: String , littleEndian :: Boolean , bytesPerValue :: ByteLength - } DataView ByteOffset (Maybe t) + } DataView ByteOffset (Nullable t) getter :: forall t. { functionName :: String @@ -135,11 +133,9 @@ getter :: forall t. , littleEndian :: Boolean } -> DataView -> ByteOffset -> Effect (Maybe t) -getter data' d o = +getter data' d o = toMaybe <$> runEffectFn3 getterImpl - { just: Just - , nothing: Nothing - , functionName: data'.functionName + { functionName: data'.functionName , littleEndian: data'.littleEndian , bytesPerValue: data'.bytesPerValue } d o From d7771c251299a8b9fdfb58374d95c681babe01ed Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 22:36:03 +0100 Subject: [PATCH 174/236] Effectful finds --- src/Data/ArrayBuffer/Typed.purs | 28 ++++++++++++------------- test/Properties/TypedArray.purs | 36 ++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f9981f6..dd58d3e 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -84,10 +84,10 @@ foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import findImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) -foreign import findIndexImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) -foreign import indexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) -foreign import lastIndexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) +foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) +foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) +foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) +foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) -- | Value-oriented array offset @@ -281,26 +281,26 @@ foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> Array foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) -- | Returns the first value satisfying the predicate -find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Maybe t +find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t) find = findWithIndex' <<< ap1 -findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Maybe t +findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex = findWithIndex' <<< flip -findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe t -findWithIndex' f a = toMaybe (runFn2 findImpl a (mkFn2 f)) +findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe t) +findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f) -- | Returns the first index of the value satisfying the predicate -findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset -findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) +findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe Offset) +findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f) -- | Returns the first index of the element, if it exists, from the left -indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset -indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) +indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) +indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) -- | Returns the first index of the element, if it exists, from the right -lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset -lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) +lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> b foldl f = foldlWithIndex' (\a x _ -> f a x) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index af84bc2..6faf4f9 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -305,16 +305,16 @@ anyImpliesFindTests :: Ref Int -> Effect Unit anyImpliesFindTests count = overAll count anyImpliesFind where anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result - anyImpliesFind (WithOffset _ xs) = + anyImpliesFind (WithOffset _ xs) = do let pred x = x /= zero p = TA.any pred xs "All don't satisfy the predicate" - q = - case TA.find pred xs of + idx <- TA.find pred xs + let q = case idx of Nothing -> Failed "Doesn't have a value satisfying the predicate" Just z -> if pred z then Success else Failed "Found value doesn't satisfy the predicate" - in pure $ p ==> q + pure $ p ==> q -- | Should work with any arbitrary predicate, but we can't generate them @@ -324,7 +324,7 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result findIndexImpliesAt (WithOffset _ xs) = do let pred x _ = x /= zero - mo = TA.findIndex pred xs + mo <- TA.findIndex pred xs case mo of Nothing -> pure Success Just o -> do @@ -338,16 +338,18 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt indexOfImpliesAtTests :: Ref Int -> Effect Unit indexOfImpliesAtTests count = overAll count indexOfImpliesAt where - indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result + indexOfImpliesAt :: forall a b t. TestableArrayF a b D1 t Result indexOfImpliesAt (WithOffset _ xs) = do e <- TA.at xs 0 case e of Nothing -> pure Success - Just y -> case TA.indexOf y Nothing xs of - Nothing -> pure $ Failed "no index of" - Just o -> do - e' <- TA.at xs o - pure $ e' === Just y + Just y -> do + idx <- TA.indexOf y Nothing xs + case idx of + Nothing -> pure $ Failed "no index of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit @@ -358,11 +360,13 @@ lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt e <- TA.at xs 0 case e of Nothing -> pure Success - Just y -> case TA.lastIndexOf y Nothing xs of - Nothing -> pure $ Failed "no lastIndex of" - Just o -> do - e' <- TA.at xs o - pure $ e' === Just y + Just y -> do + idx <- TA.lastIndexOf y Nothing xs + case idx of + Nothing -> pure $ Failed "no lastIndex of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y foldrConsIsToArrayTests :: Ref Int -> Effect Unit From e1aead144a3c022f1822d10742eb6777044c71d6 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 22:46:14 +0100 Subject: [PATCH 175/236] Effectful folds --- src/Data/ArrayBuffer/Typed.purs | 28 ++++++++++++++-------------- test/Properties/TypedArray.purs | 6 ++++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index dd58d3e..8332e41 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -302,35 +302,35 @@ indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) -foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> b +foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> Effect b foldl f = foldlWithIndex' (\a x _ -> f a x) -foldlWithIndex :: forall a b t. TypedArray a t => (Offset -> b -> t -> b) -> b -> ArrayView a -> b +foldlWithIndex :: forall a b t. TypedArray a t => (Offset -> b -> t -> b) -> b -> ArrayView a -> Effect b foldlWithIndex f = foldlWithIndex' (\a x o -> f o a x) -foldlWithIndex' :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b -foldlWithIndex' f i = unsafePerformEffect <<< foldlM (\a x o -> pure (f a x o)) i +foldlWithIndex' :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> Effect b +foldlWithIndex' f i = foldlM (\a x o -> pure (f a x o)) i -foldr :: forall a b t. TypedArray a t => (t -> b -> b) -> b -> ArrayView a -> b +foldr :: forall a b t. TypedArray a t => (t -> b -> b) -> b -> ArrayView a -> Effect b foldr f = foldrWithIndex' (\a x _ -> f a x) -foldrWithIndex :: forall a b t. TypedArray a t => (Offset -> t -> b -> b) -> b -> ArrayView a -> b +foldrWithIndex :: forall a b t. TypedArray a t => (Offset -> t -> b -> b) -> b -> ArrayView a -> Effect b foldrWithIndex f = foldrWithIndex' (\a x o -> f o a x) -foldrWithIndex' :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b -foldrWithIndex' f i = unsafePerformEffect <<< foldrM (\x a o -> pure (f x a o)) i +foldrWithIndex' :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> Effect b +foldrWithIndex' f i = foldrM (\x a o -> pure (f x a o)) i -foldl1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> t +foldl1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t foldl1 f = foldl1WithIndex (\_ a x -> f a x) -foldl1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> t -foldl1WithIndex f = unsafePerformEffect <<< foldl1M (\acc x o -> pure (f o acc x)) +foldl1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> Effect t +foldl1WithIndex f = foldl1M (\acc x o -> pure (f o acc x)) -foldr1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> t +foldr1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t foldr1 f = foldr1WithIndex (\_ a x -> f a x) -foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> t -foldr1WithIndex f = unsafePerformEffect <<< foldr1M (\x a o -> pure (f o x a)) +foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> Effect t +foldr1WithIndex f = foldr1M (\x a o -> pure (f o x a)) foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 6faf4f9..49517d3 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -375,7 +375,8 @@ foldrConsIsToArrayTests count = overAll count foldrConsIsToArray foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldrConsIsToArray (WithOffset _ xs) = do axs <- TA.toArray xs - pure $ TA.foldr Array.cons [] xs === axs + rxs <- TA.foldr Array.cons [] xs + pure $ rxs === axs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit @@ -384,7 +385,8 @@ foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result foldlSnocIsToArray (WithOffset _ xs) = do axs <- TA.toArray xs - pure $ TA.foldl Array.snoc [] xs === axs + rxs <- TA.foldl Array.snoc [] xs + pure $ rxs === axs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit From aa126bcc39e5b0bbb1e44b3281c73960b03533d9 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 23:07:26 +0100 Subject: [PATCH 176/236] Made some filters/reducers effectful --- src/Data/ArrayBuffer/Typed.purs | 36 ++++++++++++++--------------- test/Properties/TypedArray.purs | 41 +++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 8332e41..a0a4ac1 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -71,15 +71,15 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N -- ---- -foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean +foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean +foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit -foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) -foreign import includesImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) Boolean +foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) +foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c @@ -227,38 +227,38 @@ traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) -- | Test a predicate to pass on all values -all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean +all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean all = every <<< ap1 -allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean +allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean allWithIndex = every <<< flip -every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean -every p a = runFn2 everyImpl a (mkFn2 p) +every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean +every p a = runEffectFn2 everyImpl a (mkFn2 p) -- | Test a predicate to pass on any value -any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean +any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean any = some <<< ap1 -anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean +anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean anyWithIndex = some <<< flip -some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean -some p a = runFn2 someImpl a (mkFn2 p) +some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean +some p a = runEffectFn2 someImpl a (mkFn2 p) -- | Returns a new typed array with all values that pass the predicate -filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> ArrayView a +filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filter = filterWithIndex' <<< ap1 -filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> ArrayView a +filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex = filterWithIndex' <<< flip -filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a -filterWithIndex' p a = runFn2 filterImpl a (mkFn2 p) +filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (ArrayView a) +filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p) -- | Tests if a value is an element of the typed array -elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolean -elem x mo a = runFn3 includesImpl a x (toNullable mo) +elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect Boolean +elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) -- | Fetch element at index. unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 49517d3..aee4451 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -213,7 +213,7 @@ allAreFilledTests count = overAll count allAreFilled let x = fromMaybe zero e l = TA.length xs TA.fill x 0 l xs - let b = TA.all (_ == x) xs + b <- TA.all (_ == x) xs pure (b "All aren't the filled value") @@ -237,11 +237,13 @@ allImpliesAnyTests :: Ref Int -> Effect Unit allImpliesAnyTests count = overAll count allImpliesAny where allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result - allImpliesAny (WithOffset _ xs) = + allImpliesAny (WithOffset _ xs) = do let pred x = x /= zero - all' = TA.all pred xs "All don't satisfy the predicate" - any' = TA.any pred xs "None satisfy the predicate" - in pure $ (TA.length xs === zero) |=| all' ==> any' + all'' <- TA.all pred xs + let all' = all'' "All don't satisfy the predicate" + any'' <- TA.any pred xs + let any' = any'' "None satisfy the predicate" + pure $ (TA.length xs === zero) |=| all' ==> any' -- | Should work with any arbitrary predicate, but we can't generate them @@ -249,11 +251,11 @@ filterImpliesAllTests :: Ref Int -> Effect Unit filterImpliesAllTests count = overAll count filterImpliesAll where filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result - filterImpliesAll (WithOffset _ xs) = + filterImpliesAll (WithOffset _ xs) = do let pred x = x /= zero - ys = TA.filter pred xs - all' = TA.all pred ys - in pure $ all' "Filter doesn't imply all" + ys <- TA.filter pred xs + all' <- TA.all pred ys + pure $ all' "Filter doesn't imply all" -- | Should work with any arbitrary predicate, but we can't generate them @@ -263,8 +265,8 @@ filterIsTotalTests count = overAll count filterIsTotal filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result filterIsTotal (WithOffset _ xs) = do let pred x = x /= zero - ys = TA.filter pred xs - zs = TA.filter (not pred) ys + ys <- TA.filter pred xs + zs <- TA.filter (not pred) ys azs <- TA.toArray zs pure $ azs === [] @@ -276,8 +278,8 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result filterIsIdempotent (WithOffset _ xs) = do let pred x = x /= zero - ys = TA.filter pred xs - zs = TA.filter pred ys + ys <- TA.filter pred xs + zs <- TA.filter pred ys azs <- TA.toArray zs ays <- TA.toArray ys pure $ azs === ays @@ -295,9 +297,13 @@ withOffsetElemTests :: Ref Int -> Effect Unit withOffsetElemTests count = overAll1 count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result - withOffsetElem (WithOffset os xs) = pure $ - Array.all (\o -> TA.elem (unsafePartial $ unsafePerformEffect $ TA.unsafeAt xs o) Nothing xs) os - "All doesn't have an elem of itself" + withOffsetElem (WithOffset os xs) = do + let valid :: TA.Offset -> Boolean + valid o = unsafePerformEffect do + e <- unsafePartial $ TA.unsafeAt xs o + b <- TA.elem e Nothing xs + pure b + pure $ Array.all valid os "All doesn't have an elem of itself" -- | Should work with any arbitrary predicate, but we can't generate them @@ -307,7 +313,8 @@ anyImpliesFindTests count = overAll count anyImpliesFind anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result anyImpliesFind (WithOffset _ xs) = do let pred x = x /= zero - p = TA.any pred xs "All don't satisfy the predicate" + a <- TA.any pred xs + let p = a "All don't satisfy the predicate" idx <- TA.find pred xs let q = case idx of Nothing -> Failed "Doesn't have a value satisfying the predicate" From 52693b76d6bd21325fb9e330d8316c9be1ec56c9 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 23:26:53 +0100 Subject: [PATCH 177/236] Simplify --- test/Properties/TypedArray.purs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index aee4451..fd0f099 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -10,7 +10,8 @@ import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithOffset) import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint8Array, Uint8ClampedArray, Uint32Array) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Data.Maybe (Maybe(..), fromMaybe) +import Data.Maybe (Maybe(..), fromMaybe, isJust) +import Data.Traversable (traverse) import Data.Typelevel.Num (class Nat, D0, D1, D2, D5, d0, d1, toInt') import Data.Vec (head, index) as Vec import Effect (Effect) @@ -18,7 +19,6 @@ import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) -import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Testable, Result(..), quickCheckGen, (/==), (), (===)) import Test.QuickCheck.Combinators ((==>), (|=|)) import Test.QuickCheck.Gen (Gen) @@ -298,12 +298,9 @@ withOffsetElemTests count = overAll1 count withOffsetElem where withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result withOffsetElem (WithOffset os xs) = do - let valid :: TA.Offset -> Boolean - valid o = unsafePerformEffect do - e <- unsafePartial $ TA.unsafeAt xs o - b <- TA.elem e Nothing xs - pure b - pure $ Array.all valid os "All doesn't have an elem of itself" + let fetch o = TA.at xs o + exs <- traverse fetch os + pure $ Array.all isJust exs "All doesn't have an elem of itself" -- | Should work with any arbitrary predicate, but we can't generate them From ea63698cef557beb31629f7aa159241c4996b0b3 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 23:35:25 +0100 Subject: [PATCH 178/236] Effectful toString --- src/Data/ArrayBuffer/Typed.js | 2 +- src/Data/ArrayBuffer/Typed.purs | 11 +++++++---- test/Properties/TypedArray.purs | 6 ++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 3f52aa7..3b536e7 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -159,7 +159,7 @@ exports.subArrayImpl = function subArrayImpl (a, s, e) { }; -exports.toString = function toString (a) { +exports.toStringImpl = function toStringImpl (a) { return a.toString(); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index a0a4ac1..d411fa4 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -379,14 +379,17 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayV subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a subArray s e a = runFn3 subArrayImpl a s e +foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String + -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. -foreign import toString :: forall a. ArrayView a -> String +toString :: forall a. ArrayView a -> Effect String +toString a = runEffectFn1 toStringImpl a -foreign import joinImpl :: forall a. Fn2 (ArrayView a) String String +foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String -- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. -toString' :: forall a. ArrayView a -> String -> String -toString' a s = runFn2 joinImpl a s +toString' :: forall a. ArrayView a -> String -> Effect String +toString' a s = runEffectFn2 joinImpl a s foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index fd0f099..0ecaebc 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -466,8 +466,10 @@ toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma where toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result - toStringIsJoinWithComma (WithOffset _ xs) = - pure $ TA.toString' xs "," === TA.toString xs + toStringIsJoinWithComma (WithOffset _ xs) = do + s1 <- TA.toString' xs "," + s2 <- TA.toString xs + pure $ s1 === s2 setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit From 355f17e97f6d2f9f4f40d6f49102f752d81c322e Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 29 Jan 2019 23:48:35 +0100 Subject: [PATCH 179/236] toString' -> join, changed args order --- src/Data/ArrayBuffer/Typed.purs | 13 +++++++------ test/Properties/TypedArray.purs | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index d411fa4..3c7758e 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -18,7 +18,7 @@ module Data.ArrayBuffer.Typed , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 , find, findIndex, indexOf, lastIndexOf , slice, subArray - , toString, toString', toArray + , toString, join, toArray ) where import Data.Array (length) as A @@ -126,7 +126,7 @@ type Length = Int -- | - `indexOf` and `lastIndexOf` are searching functions via equality -- | - `slice` returns a new typed array on the same array buffer content as the input -- | - `subArray` returns a new typed array with a separate array buffer --- | - `toString` prints to a CSV, `toString'` allows you to supply the delimiter +-- | - `toString` prints to a CSV, `join` allows you to supply the delimiter -- | - `toArray` returns an array of numeric values class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) @@ -388,18 +388,19 @@ toString a = runEffectFn1 toStringImpl a foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String -- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. -toString' :: forall a. ArrayView a -> String -> Effect String -toString' a s = runEffectFn2 joinImpl a s +join :: forall a. String -> ArrayView a -> Effect String +join s a = runEffectFn2 joinImpl a s -foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b - foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean -- | Determine if a certain index is valid. hasIndex :: forall a. ArrayView a -> Offset -> Boolean hasIndex a o = runFn2 hasIndexImpl a o + +foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b + -- | Fetch element at index. at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Effect (Maybe t) at a n = if a `hasIndex` n diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 0ecaebc..ec4d811 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -77,7 +77,7 @@ typedArrayTests count = do sortIsIdempotentTests count log " - toArray (sort x) == Array.sort (toArray x)" sortIsArraySortTests count - log " - toString' \",\" x == toString x" + log " - join \",\" x == toString x" toStringIsJoinWithCommaTests count log " - setTyped x (subArray x) == x" setTypedOfSubArrayIsIdentityTests count @@ -467,7 +467,7 @@ toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma where toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result toStringIsJoinWithComma (WithOffset _ xs) = do - s1 <- TA.toString' xs "," + s1 <- TA.join "," xs s2 <- TA.toString xs pure $ s1 === s2 From 85dcb57e842614fa2000abb4bfcad7347afc737e Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:09:00 +0100 Subject: [PATCH 180/236] toNullable accepts undefined, no need to check --- src/Data/ArrayBuffer/Typed.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 3b536e7..bb4e21a 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -108,12 +108,12 @@ exports.reduceRight1Impl = function reduceRight1Impl (a,f) { }; exports.findImpl = function findImpl (a,f) { - var x = a.find(f); - return (x === undefined) ? null : x; + return a.find(f); }; + exports.findIndexImpl = function findIndexImpl (a,f) { - var x = a.findIndex(f); - return (x === -1) ? null : x; + var r = a.findIndex(f); + return r === -1 ? null : r; }; exports.indexOfImpl = function indexOfImpl (a,x,mo) { var r = mo === null ? a.indexOf(x) : a.indexOf(x,mo); From e8ebe3929e0a6e06d09c5c378082e819d8d95506 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:10:24 +0100 Subject: [PATCH 181/236] Simplify fill --- src/Data/ArrayBuffer/Typed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index bb4e21a..1969c21 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -73,8 +73,8 @@ exports.someImpl = function someImpl (a,p) { }; -exports.fillImpl = function fillImpl (a,x,ms,me) { - return me === null ? (ms === null ? a.fill(x) : a.fill(x,ms)) : a.fill(x,ms,me); +exports.fillImpl = function fillImpl (a,x,s,e) { + return a.fill(x,s,e); }; From bdc0c0dd3e9c8173857aefc01c1688814a8456b5 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:13:48 +0100 Subject: [PATCH 182/236] JS args order --- src/Data/ArrayBuffer/Typed.js | 2 +- src/Data/ArrayBuffer/Typed.purs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 1969c21..d7e2785 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -73,7 +73,7 @@ exports.someImpl = function someImpl (a,p) { }; -exports.fillImpl = function fillImpl (a,x,s,e) { +exports.fillImpl = function fillImpl (x, s, e, a) { return a.fill(x,s,e); }; diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 3c7758e..2b8b10b 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -74,7 +74,7 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit +foreign import fillImpl :: forall a b. EffectFn4 b Offset Offset (ArrayView a) Unit foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit @@ -180,7 +180,7 @@ fromArray a = runEffectFn3 create a null null -- | Fill the array with a value fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit -fill x s e a = runEffectFn4 fillImpl a x s e +fill x s e a = runEffectFn4 fillImpl x s e a -- | Stores multiple values into the typed array set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean From 6602ee480a5583d7d5972c671bb1c895e386c0ea Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:22:15 +0100 Subject: [PATCH 183/236] Simplify at --- src/Data/ArrayBuffer/Typed.purs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 2b8b10b..1d0f699 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -403,9 +403,7 @@ foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b -- | Fetch element at index. at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Effect (Maybe t) -at a n = if a `hasIndex` n - then Just <$> unsafePartial (unsafeAt a n) - else pure Nothing +at a n = toMaybe <$> runEffectFn2 unsafeAtImpl a n infixl 3 at as ! From 24ee28c11ff1427a98f5f3173d995021c846fe56 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:27:31 +0100 Subject: [PATCH 184/236] Moved to module doc --- src/Data/ArrayBuffer/Typed.purs | 60 +++++++++++++++------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 1d0f699..f00d3f4 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -1,5 +1,33 @@ -- | This module represents the functional bindings to JavaScript's `TypedArray` and other -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) for details. +-- | +-- | #### Creation +-- | +-- | - `whole`, `remainder`, and `part` are functions for building a typed array accessible interface +-- | on top of an existing `ArrayBuffer` +-- | - `empty` and `fromArray` are functions for creating pure typed arrays +-- | +-- | #### Modification +-- | +-- | - `fill`, `set`, and `setTyped` are functions for assigning values from external sources +-- | - `map` and `traverse` allow you to create a new array from the existing values in another +-- | - `copyWithin` allows you to set values to the array that exist in other parts of the array +-- | - `filter` creates a new array without the values that don't pass a predicate +-- | - `reverse` modifies an existing array in-place, with all values reversed +-- | - `sort` modifies an existing array in-place, with all values sorted +-- | +-- | #### Access +-- | +-- | - `elem`, `all`, and `any` are functions for testing the contents of an array +-- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset +-- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array +-- | - `find` and `findIndex` are searching functions via a predicate +-- | - `indexOf` and `lastIndexOf` are searching functions via equality +-- | - `slice` returns a new typed array on the same array buffer content as the input +-- | - `subArray` returns a new typed array with a separate array buffer +-- | - `toString` prints to a CSV, `join` allows you to supply the delimiter +-- | - `toArray` returns an array of numeric values + module Data.ArrayBuffer.Typed ( polyFill @@ -96,38 +124,6 @@ type Offset = Int type Length = Int --- TODO use purescript-quotient --- | Typeclass that associates a measured user-level type with a typed array. --- | --- | #### Creation --- | --- | - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface --- | on top of an existing `ArrayBuffer` - Note, `part` and `remainder` may behave unintuitively - --- | when the operation is isomorphic to `whole`, the new TypedArray uses the same buffer as the input, --- | but not when the portion is a sub-array of the original buffer, a new one is made with --- | `Data.ArrayBuffer.ArrayBuffer.slice`. --- | - `empty` and `fromArray` are methods for creating pure typed arrays --- | --- | #### Modification --- | --- | - `fill`, `set`, and `setTyped` are methods for assigning values from external sources --- | - `map` and `traverse` allow you to create a new array from the existing values in another --- | - `copyWithin` allows you to set values to the array that exist in other parts of the array --- | - `filter` creates a new array without the values that don't pass a predicate --- | - `reverse` modifies an existing array in-place, with all values reversed --- | - `sort` modifies an existing array in-place, with all values sorted --- | --- | #### Access --- | --- | - `elem`, `all`, and `any` are functions for testing the contents of an array --- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset --- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array --- | - `find` and `findIndex` are searching functions via a predicate --- | - `indexOf` and `lastIndexOf` are searching functions via equality --- | - `slice` returns a new typed array on the same array buffer content as the input --- | - `subArray` returns a new typed array with a separate array buffer --- | - `toString` prints to a CSV, `join` allows you to supply the delimiter --- | - `toArray` returns an array of numeric values class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) From 881787c0fe070ffdb009089cb0c7b0272ec09ac4 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:31:32 +0100 Subject: [PATCH 185/236] Document length --- src/Data/ArrayBuffer/Typed.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index f00d3f4..5d20531 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -54,14 +54,13 @@ import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteO import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) import Data.Float32 (Float32) as F import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) -import Data.Maybe (Maybe(..), fromMaybe) +import Data.Maybe (Maybe, fromMaybe) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) -import Partial.Unsafe (unsafePartial) import Prelude (Unit, flip, pure, (&&), (*), (*>), (-), (<$>), (<<<), (<=), (>=)) import Type.Proxy (Proxy(..)) @@ -80,6 +79,7 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength foreign import lengthImpl :: forall a. ArrayView a -> Length +-- | Represents the number of elements in this typed array. length :: forall a. ArrayView a -> Length length = lengthImpl From bf9c6821a74fa973ca6f038ea67d21d2337375e9 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 00:47:55 +0100 Subject: [PATCH 186/236] Docs --- src/Data/ArrayBuffer/Typed.purs | 51 +++++++++++++++++---------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 5d20531..6218b7f 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -118,9 +118,9 @@ foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Of foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) --- | Value-oriented array offset +-- | Value-oriented array offset. type Offset = Int --- | Value-oriented array length +-- | Value-oriented array length. type Length = Int @@ -166,19 +166,19 @@ part a x y = part' a o y part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) part' a x y = runEffectFn3 create a (notNull x) (notNull y) --- | Creates an empty typed array, where each value is assigned 0 +-- | Creates an empty typed array, where each value is assigned 0. empty :: forall a t. TypedArray a t => Length -> Effect (ArrayView a) empty n = runEffectFn3 create n null null --- | Creates a typed array from an input array of values, to be binary serialized +-- | Creates a typed array from an input array of values, to be binary serialized. fromArray :: forall a t. TypedArray a t => Array t -> Effect (ArrayView a) fromArray a = runEffectFn3 create a null null --- | Fill the array with a value +-- | Fill the array with a value. fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit fill x s e a = runEffectFn4 fillImpl x s e a --- | Stores multiple values into the typed array +-- | Stores multiple values into the typed array. set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean set = setInternal A.length @@ -193,56 +193,58 @@ map = mapWithIndex' <<< ap1 -- | Apply a function to each element in an array, supplying a -- | generated zero-based index integer along with the element, --- | creating a typed array with the new elements +-- | creating a typed array with the new elements. mapWithIndex :: forall a t. TypedArray a t => (Offset -> t -> t) -> ArrayView a -> ArrayView a mapWithIndex = mapWithIndex' <<< flip mapWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) --- | Traverses over each value, returning a new one +-- | Traverses over each value, returning a new one. traverse :: forall a t. TypedArray a t => (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverse = traverseWithIndex' <<< ap1 --- | Traverses over each value, returning a new one +-- | Traverses over each value, returning a new one. traverseWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverseWithIndex = traverseWithIndex' <<< flip traverseWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverseWithIndex' f a = runEffectFn2 mapImpl a (mkEffectFn2 f) --- | Traverses over each value +-- | Traverses over each value. traverse_ :: forall a t. TypedArray a t => (t -> Effect Unit) -> ArrayView a -> Effect Unit traverse_ = traverseWithIndex_' <<< ap1 --- | Traverses over each value +-- | Traverses over each value. traverseWithIndex_ :: forall a t. TypedArray a t => (Offset -> t -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_ = traverseWithIndex_' <<< flip traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) --- | Test a predicate to pass on all values +-- | Test a predicate to pass on all values. all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean all = every <<< ap1 +-- | Test a predicate (that receives also an index) to pass on all values. allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean allWithIndex = every <<< flip every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean every p a = runEffectFn2 everyImpl a (mkFn2 p) --- | Test a predicate to pass on any value +-- | Test a predicate to pass on any value. any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean any = some <<< ap1 +-- | Test a predicate (that receives also an index) to pass on any value. anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean anyWithIndex = some <<< flip some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean some p a = runEffectFn2 someImpl a (mkFn2 p) --- | Returns a new typed array with all values that pass the predicate +-- | Returns a new typed array with all values that pass the predicate. filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filter = filterWithIndex' <<< ap1 @@ -252,7 +254,7 @@ filterWithIndex = filterWithIndex' <<< flip filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p) --- | Tests if a value is an element of the typed array +-- | Tests if a value is an element of the typed array. elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect Boolean elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) @@ -260,23 +262,23 @@ elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t unsafeAt a o = runEffectFn2 unsafeAtImpl a o --- | Folding from the left +-- | Folding from the left. foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i --- | Assumes the typed array is non-empty +-- | Folding from the left. Assumes the typed array is non-empty. foldl1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) --- | Folding from the right +-- | Folding from the right. foldrM :: forall a t b. TypedArray a t => (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i --- | Assumes the typed array is non-empty +-- | Folding from the right. Assumes the typed array is non-empty. foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) --- | Returns the first value satisfying the predicate +-- | Returns the first value satisfying the predicate. find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t) find = findWithIndex' <<< ap1 @@ -286,18 +288,19 @@ findWithIndex = findWithIndex' <<< flip findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f) --- | Returns the first index of the value satisfying the predicate +-- | Returns the first index of the value satisfying the predicate. findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe Offset) findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f) --- | Returns the first index of the element, if it exists, from the left +-- | Returns the first index of the element, if it exists, from the left. indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) --- | Returns the first index of the element, if it exists, from the right +-- | Returns the first index of the element, if it exists, from the right. lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) + foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> Effect b foldl f = foldlWithIndex' (\a x _ -> f a x) @@ -364,7 +367,7 @@ slice s e a = runEffectFn3 sliceImpl a s e foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit --- | Sorts the values in-place +-- | Sorts the values in-place. sort :: forall a. ArrayView a -> Effect Unit sort a = runEffectFn1 sortImpl a From f68f7cdc515f72db486aadf0c0c218677870999f Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 22:53:45 +0100 Subject: [PATCH 187/236] Moved *Impl functions near their call site --- src/Data/ArrayBuffer/ArrayBuffer.purs | 8 +-- src/Data/ArrayBuffer/DataView.purs | 29 +++++----- src/Data/ArrayBuffer/Typed.purs | 77 ++++++++++----------------- 3 files changed, 44 insertions(+), 70 deletions(-) diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 6dd5b4e..1206040 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -12,19 +12,15 @@ import Data.Function.Uncurried (Fn3, runFn3) import Effect (Effect) import Effect.Uncurried (EffectFn1, runEffectFn1) - -foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer - - -- | Create an `ArrayBuffer` with the given capacity. empty :: ByteLength -> Effect ArrayBuffer empty l = runEffectFn1 emptyImpl l +foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength -foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer - -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer slice s e a = runFn3 sliceImpl a s e +foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 001f37b..2637003 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -66,17 +66,16 @@ import Type.Proxy (Proxy(..)) -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView -foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView -- | View mapping the rest of an `ArrayBuffer` after an index. remainder :: ArrayBuffer -> ByteOffset -> Effect DataView remainder a o = runEffectFn2 remainderImpl a o - -foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView +foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView -- | View mapping a region of the `ArrayBuffer`. part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView part a o l = runEffectFn3 partImpl a o l +foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView -- | `ArrayBuffer` being mapped by the view. foreign import buffer :: DataView -> ArrayBuffer @@ -121,12 +120,6 @@ instance showArrayViewTypeViewInt8 :: ShowArrayViewType Int8 "Int8" instance showArrayViewTypeViewFloat32 :: ShowArrayViewType Float32 "Float32" instance showArrayViewTypeViewFloat64 :: ShowArrayViewType Float64 "Float64" -foreign import getterImpl :: forall t - . EffectFn3 { functionName :: String - , littleEndian :: Boolean - , bytesPerValue :: ByteLength - } DataView ByteOffset (Nullable t) - getter :: forall t. { functionName :: String , bytesPerValue :: ByteLength @@ -139,6 +132,12 @@ getter data' d o = toMaybe <$> , littleEndian: data'.littleEndian , bytesPerValue: data'.bytesPerValue } d o +foreign import getterImpl :: forall t + . EffectFn3 { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } DataView ByteOffset (Nullable t) + get :: forall a name t b @@ -175,18 +174,18 @@ getLE :: forall a name t b => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) getLE = get LE -foreign import setterImpl :: forall t - . EffectFn4 { functionName :: String - , littleEndian :: Boolean - , bytesPerValue :: ByteLength - } DataView ByteOffset t Boolean - setter :: forall t. { functionName :: String , bytesPerValue :: ByteLength , littleEndian :: Boolean } -> DataView -> ByteOffset -> t -> Effect Boolean setter d o t = runEffectFn4 setterImpl d o t +foreign import setterImpl :: forall t + . EffectFn4 { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } DataView ByteOffset t Boolean + set :: forall a name t b . DataView a t diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 6218b7f..0d8c504 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -77,12 +77,10 @@ foreign import byteOffset :: forall a. ArrayView a -> ByteOffset -- | Represents the length of this typed array, in bytes. foreign import byteLength :: forall a. ArrayView a -> ByteLength -foreign import lengthImpl :: forall a. ArrayView a -> Length - -- | Represents the number of elements in this typed array. length :: forall a. ArrayView a -> Length length = lengthImpl - +foreign import lengthImpl :: forall a. ArrayView a -> Length -- object creator implementations for each typed array @@ -99,24 +97,6 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N -- ---- -foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean - -foreign import fillImpl :: forall a b. EffectFn4 b Offset Offset (ArrayView a) Unit - -foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) -foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit -foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) -foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean -foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c -foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c -foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) -foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) -foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) -foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) - -- | Value-oriented array offset. type Offset = Int @@ -177,6 +157,7 @@ fromArray a = runEffectFn3 create a null null -- | Fill the array with a value. fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit fill x s e a = runEffectFn4 fillImpl x s e a +foreign import fillImpl :: forall a b. EffectFn4 b Offset Offset (ArrayView a) Unit -- | Stores multiple values into the typed array. set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean @@ -199,6 +180,7 @@ mapWithIndex = mapWithIndex' <<< flip mapWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) +foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) -- | Traverses over each value, returning a new one. traverse :: forall a t. TypedArray a t => (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) @@ -221,6 +203,7 @@ traverseWithIndex_ = traverseWithIndex_' <<< flip traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) +foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit -- | Test a predicate to pass on all values. all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean @@ -232,6 +215,7 @@ allWithIndex = every <<< flip every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean every p a = runEffectFn2 everyImpl a (mkFn2 p) +foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -- | Test a predicate to pass on any value. any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean @@ -243,6 +227,7 @@ anyWithIndex = some <<< flip some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean some p a = runEffectFn2 someImpl a (mkFn2 p) +foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -- | Returns a new typed array with all values that pass the predicate. filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a) @@ -253,10 +238,12 @@ filterWithIndex = filterWithIndex' <<< flip filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p) +foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) -- | Tests if a value is an element of the typed array. elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect Boolean elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) +foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean -- | Fetch element at index. unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t @@ -265,18 +252,22 @@ unsafeAt a o = runEffectFn2 unsafeAtImpl a o -- | Folding from the left. foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i +foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c -- | Folding from the left. Assumes the typed array is non-empty. foldl1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) +foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -- | Folding from the right. foldrM :: forall a t b. TypedArray a t => (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i +foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c -- | Folding from the right. Assumes the typed array is non-empty. foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) +foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -- | Returns the first value satisfying the predicate. find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t) @@ -287,19 +278,22 @@ findWithIndex = findWithIndex' <<< flip findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f) +foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) -- | Returns the first index of the value satisfying the predicate. findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe Offset) findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f) +foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) -- | Returns the first index of the element, if it exists, from the left. indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) +foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) -- | Returns the first index of the element, if it exists, from the right. lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) - +foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> Effect b foldl f = foldlWithIndex' (\a x _ -> f a x) @@ -331,19 +325,16 @@ foldr1 f = foldr1WithIndex (\_ a x -> f a x) foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> Effect t foldr1WithIndex f = foldr1M (\x a o -> pure (f o x a)) -foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit - -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit copyWithin a t s me = runEffectFn4 copyWithinImpl a t s (toNullable me) - -foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit +foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit -- | Reverses a typed array in-place. reverse :: forall a. ArrayView a -> Effect Unit reverse a = runEffectFn1 reverseImpl a +foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit -foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Offset -> b -> Effect Boolean setInternal lenfn a mo b = @@ -351,64 +342,52 @@ setInternal lenfn a mo b = in if o >= 0 && lenfn b <= length a - o then runEffectFn3 setImpl a o b *> pure true else pure false +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit + -- | Stores multiple values in the typed array, reading input values from the second typed array. setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Boolean setTyped = setInternal length - --- | Copy the entire contents of the typed array into a new buffer. -foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a) - -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. slice :: forall a. Offset -> Offset -> ArrayView a -> Effect (ArrayView a) slice s e a = runEffectFn3 sliceImpl a s e - -foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit +foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a) -- | Sorts the values in-place. sort :: forall a. ArrayView a -> Effect Unit sort a = runEffectFn1 sortImpl a - - -foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) +foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a subArray s e a = runFn3 subArrayImpl a s e - -foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String +foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. toString :: forall a. ArrayView a -> Effect String toString a = runEffectFn1 toStringImpl a - -foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String +foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String -- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. join :: forall a. String -> ArrayView a -> Effect String join s a = runEffectFn2 joinImpl a s - - -foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean +foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String -- | Determine if a certain index is valid. hasIndex :: forall a. ArrayView a -> Offset -> Boolean hasIndex a o = runFn2 hasIndexImpl a o - - -foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b +foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean -- | Fetch element at index. at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Effect (Maybe t) at a n = toMaybe <$> runEffectFn2 unsafeAtImpl a n +foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b infixl 3 at as ! - -foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b) - -- | Turn typed array into an array. toArray :: forall a t. TypedArray a t => ArrayView a -> Effect (Array t) toArray a = runEffectFn1 toArrayImpl a +foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b) From 7e5febce607fd17b1bdc7f2aad34c00d8a23119d Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Wed, 30 Jan 2019 23:02:53 +0100 Subject: [PATCH 188/236] Docs --- src/Data/ArrayBuffer/Typed.purs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 0d8c504..8cc7cec 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -95,9 +95,6 @@ foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array --- ---- - - -- | Value-oriented array offset. type Offset = Int -- | Value-oriented array length. @@ -209,7 +206,8 @@ foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b O all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean all = every <<< ap1 --- | Test a predicate (that receives also an index) to pass on all values. +-- | Test a predicate to pass on all values. The predicate function +-- | receives the offset and the element. allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean allWithIndex = every <<< flip @@ -233,6 +231,9 @@ foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boo filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filter = filterWithIndex' <<< ap1 +-- | Returns a new typed array with all values that pass the +-- | predicate. The predicate function receives the offset and the +-- | element. filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex = filterWithIndex' <<< flip From 87efb53ae22075c84c43cb19bde1fd027b930142 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 31 Jan 2019 00:06:34 +0100 Subject: [PATCH 189/236] Renamed Offset->Index, removed foldr1WithIndex/foldl1WithIndex, docs --- src/Data/ArrayBuffer/Typed.purs | 182 ++++++++++++++-------------- src/Data/ArrayBuffer/Typed/Gen.purs | 12 +- test/Properties/TypedArray.purs | 102 ++++++++-------- 3 files changed, 149 insertions(+), 147 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 8cc7cec..9dd6b15 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -19,7 +19,7 @@ -- | #### Access -- | -- | - `elem`, `all`, and `any` are functions for testing the contents of an array --- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset +-- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an index -- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array -- | - `find` and `findIndex` are searching functions via a predicate -- | - `indexOf` and `lastIndexOf` are searching functions via equality @@ -31,7 +31,7 @@ module Data.ArrayBuffer.Typed ( polyFill - , Offset, Length + , Index, Length , buffer, byteOffset, byteLength, length , class TypedArray , create, whole, remainder, part, empty, fromArray @@ -43,7 +43,7 @@ module Data.ArrayBuffer.Typed , all, any , allWithIndex, anyWithIndex , unsafeAt, hasIndex, at, (!) - , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 + , reduce, reduce1, foldl, foldl1, reduceRight, reduceRight1, foldr, foldr1 , find, findIndex, indexOf, lastIndexOf , slice, subArray , toString, join, toArray @@ -61,7 +61,7 @@ import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) -import Prelude (Unit, flip, pure, (&&), (*), (*>), (-), (<$>), (<<<), (<=), (>=)) +import Prelude (Unit, flip, pure, ($), (&&), (*), (*>), (-), (<$>), (<<<), (<=), (>=)) import Type.Proxy (Proxy(..)) @@ -95,8 +95,8 @@ foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array --- | Value-oriented array offset. -type Offset = Int +-- | Value-oriented array index. +type Index = Int -- | Value-oriented array length. type Length = Int @@ -128,7 +128,7 @@ whole :: forall a t. TypedArray a t => ArrayBuffer -> Effect (ArrayView a) whole a = runEffectFn3 create a null null -- | View mapping the rest of an `ArrayBuffer` after an index. -remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Effect (ArrayView a) +remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Index -> Effect (ArrayView a) remainder a x = remainder' a o where o = x * toInt' (Proxy :: Proxy b) @@ -136,7 +136,7 @@ remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect remainder' a x = runEffectFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. -part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Length -> Effect (ArrayView a) +part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Index -> Length -> Effect (ArrayView a) part a x y = part' a o y where o = x * toInt' (Proxy :: Proxy b) @@ -152,12 +152,12 @@ fromArray :: forall a t. TypedArray a t => Array t -> Effect (ArrayView a) fromArray a = runEffectFn3 create a null null -- | Fill the array with a value. -fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit +fill :: forall a t. TypedArray a t => t -> Index -> Index -> ArrayView a -> Effect Unit fill x s e a = runEffectFn4 fillImpl x s e a -foreign import fillImpl :: forall a b. EffectFn4 b Offset Offset (ArrayView a) Unit +foreign import fillImpl :: forall a b. EffectFn4 b Index Index (ArrayView a) Unit -- | Stores multiple values into the typed array. -set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean +set :: forall a t. TypedArray a t => ArrayView a -> Maybe Index -> Array t -> Effect Boolean set = setInternal A.length ap1 :: forall a b c. (a -> c) -> (a -> b -> c) @@ -172,22 +172,22 @@ map = mapWithIndex' <<< ap1 -- | Apply a function to each element in an array, supplying a -- | generated zero-based index integer along with the element, -- | creating a typed array with the new elements. -mapWithIndex :: forall a t. TypedArray a t => (Offset -> t -> t) -> ArrayView a -> ArrayView a +mapWithIndex :: forall a t. TypedArray a t => (Index -> t -> t) -> ArrayView a -> ArrayView a mapWithIndex = mapWithIndex' <<< flip -mapWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a -mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) -foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) +mapWithIndex' :: forall a t. TypedArray a t => (t -> Index -> t) -> ArrayView a -> ArrayView a +mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 \x o -> pure (f x o))) +foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Index b) (ArrayView a) -- | Traverses over each value, returning a new one. traverse :: forall a t. TypedArray a t => (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverse = traverseWithIndex' <<< ap1 -- | Traverses over each value, returning a new one. -traverseWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverseWithIndex :: forall a t. TypedArray a t => (Index -> t -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverseWithIndex = traverseWithIndex' <<< flip -traverseWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverseWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverseWithIndex' f a = runEffectFn2 mapImpl a (mkEffectFn2 f) -- | Traverses over each value. @@ -195,141 +195,143 @@ traverse_ :: forall a t. TypedArray a t => (t -> Effect Unit) -> ArrayView a -> traverse_ = traverseWithIndex_' <<< ap1 -- | Traverses over each value. -traverseWithIndex_ :: forall a t. TypedArray a t => (Offset -> t -> Effect Unit) -> ArrayView a -> Effect Unit +traverseWithIndex_ :: forall a t. TypedArray a t => (Index -> t -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_ = traverseWithIndex_' <<< flip -traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit +traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Index -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) -foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit +foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Index Unit) Unit -- | Test a predicate to pass on all values. all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean all = every <<< ap1 -- | Test a predicate to pass on all values. The predicate function --- | receives the offset and the element. -allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean +-- | receives the index and the element. +allWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect Boolean allWithIndex = every <<< flip -every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean +every :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect Boolean every p a = runEffectFn2 everyImpl a (mkFn2 p) -foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean +foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) Boolean -- | Test a predicate to pass on any value. any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean any = some <<< ap1 -- | Test a predicate (that receives also an index) to pass on any value. -anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean +anyWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect Boolean anyWithIndex = some <<< flip -some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean +some :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect Boolean some p a = runEffectFn2 someImpl a (mkFn2 p) -foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean +foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) Boolean -- | Returns a new typed array with all values that pass the predicate. filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filter = filterWithIndex' <<< ap1 -- | Returns a new typed array with all values that pass the --- | predicate. The predicate function receives the offset and the +-- | predicate. The predicate function receives the index and the -- | element. -filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (ArrayView a) +filterWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex = filterWithIndex' <<< flip -filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (ArrayView a) +filterWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p) -foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) +foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (ArrayView a) -- | Tests if a value is an element of the typed array. -elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect Boolean +elem :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect Boolean elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) -foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean +foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) Boolean -- | Fetch element at index. -unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t +unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Index -> Effect t unsafeAt a o = runEffectFn2 unsafeAtImpl a o -- | Folding from the left. -foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i -foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c +reduce :: forall a t b. TypedArray a t => (b -> t -> Index -> Effect b) -> b -> ArrayView a -> Effect b +reduce f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i +foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Index c) c c -- | Folding from the left. Assumes the typed array is non-empty. -foldl1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) -foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b +reduce1 :: forall a t. Partial => TypedArray a t => (t -> t -> Index -> Effect t) -> ArrayView a -> Effect t +reduce1 f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) +foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Index b) b -- | Folding from the right. -foldrM :: forall a t b. TypedArray a t => (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i -foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c +reduceRight :: forall a t b. TypedArray a t => (t -> b -> Index -> Effect b) -> b -> ArrayView a -> Effect b +reduceRight f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 \acc x o -> f x acc o) i +foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Index c) c c -- | Folding from the right. Assumes the typed array is non-empty. -foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) -foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b +reduceRight1 :: forall a t. Partial => TypedArray a t => (t -> t -> Index -> Effect t) -> ArrayView a -> Effect t +reduceRight1 f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 \acc x o -> f x acc o) +foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Index b) b -- | Returns the first value satisfying the predicate. find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t) find = findWithIndex' <<< ap1 -findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (Maybe t) +-- | Returns the first value satisfying the predicate. The predicate +-- | receives the index and the element at that index. +findWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex = findWithIndex' <<< flip -findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe t) +findWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f) -foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) +foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (Nullable b) -- | Returns the first index of the value satisfying the predicate. -findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe Offset) +findIndex :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (Maybe Index) findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f) -foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) +foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (Nullable Index) -- | Returns the first index of the element, if it exists, from the left. -indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) +indexOf :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect (Maybe Index) indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) -foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) +foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) (Nullable Index) -- | Returns the first index of the element, if it exists, from the right. -lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset) +lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect (Maybe Index) lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) -foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) +foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) (Nullable Index) +-- | Fold a list from the left, accumulating the result using the +-- | specified function. foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> Effect b -foldl f = foldlWithIndex' (\a x _ -> f a x) +foldl f = reduce \a x _ -> pure $ f a x -foldlWithIndex :: forall a b t. TypedArray a t => (Offset -> b -> t -> b) -> b -> ArrayView a -> Effect b -foldlWithIndex f = foldlWithIndex' (\a x o -> f o a x) - -foldlWithIndex' :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> Effect b -foldlWithIndex' f i = foldlM (\a x o -> pure (f a x o)) i +-- | Fold a list from the left, accumulating the result using the +-- | supplied function. The accumulating function receives the index, +-- | the accumulated value and the element. +foldlWithIndex :: forall a b t. TypedArray a t => (Index -> b -> t -> b) -> b -> ArrayView a -> Effect b +foldlWithIndex f = reduce \a x o -> pure $ f o a x +-- | Fold a list from the right, accumulating the result using the +-- | specified function. foldr :: forall a b t. TypedArray a t => (t -> b -> b) -> b -> ArrayView a -> Effect b -foldr f = foldrWithIndex' (\a x _ -> f a x) - -foldrWithIndex :: forall a b t. TypedArray a t => (Offset -> t -> b -> b) -> b -> ArrayView a -> Effect b -foldrWithIndex f = foldrWithIndex' (\a x o -> f o a x) - -foldrWithIndex' :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> Effect b -foldrWithIndex' f i = foldrM (\x a o -> pure (f x a o)) i +foldr f = reduceRight \a x _ -> pure $ f a x -foldl1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t -foldl1 f = foldl1WithIndex (\_ a x -> f a x) +-- | Fold a list from the right, accumulating the result using the +-- | supplied function. The accumulating function receives the index, +-- | the element and the accumulated value. +foldrWithIndex :: forall a b t. TypedArray a t => (Index -> t -> b -> b) -> b -> ArrayView a -> Effect b +foldrWithIndex f = reduceRight \a x o -> pure $ f o a x -foldl1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> Effect t -foldl1WithIndex f = foldl1M (\acc x o -> pure (f o acc x)) - -foldr1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t -foldr1 f = foldr1WithIndex (\_ a x -> f a x) +-- | Folding from the left. Assumes the typed array is non-empty. +foldl1 :: forall a t. Partial => TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t +foldl1 f = reduce1 \a x _ -> pure $ f a x -foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> Effect t -foldr1WithIndex f = foldr1M (\x a o -> pure (f o x a)) +-- | Folding from the right. Assumes the typed array is non-empty. +foldr1 :: forall a t. Partial => TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t +foldr1 f = reduceRight1 \x a _ -> pure $ f a x -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. -copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit +copyWithin :: forall a. ArrayView a -> Index -> Index -> Maybe Index -> Effect Unit copyWithin a t s me = runEffectFn4 copyWithinImpl a t s (toNullable me) -foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit +foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Index Index (Nullable Index) Unit -- | Reverses a typed array in-place. reverse :: forall a. ArrayView a -> Effect Unit @@ -337,24 +339,24 @@ reverse a = runEffectFn1 reverseImpl a foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit -setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Offset -> b -> Effect Boolean +setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Index -> b -> Effect Boolean setInternal lenfn a mo b = let o = fromMaybe 0 mo in if o >= 0 && lenfn b <= length a - o then runEffectFn3 setImpl a o b *> pure true else pure false -foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Index b Unit -- | Stores multiple values in the typed array, reading input values from the second typed array. -setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Boolean +setTyped :: forall a. ArrayView a -> Maybe Index -> ArrayView a -> Effect Boolean setTyped = setInternal length -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. Offset -> Offset -> ArrayView a -> Effect (ArrayView a) +slice :: forall a. Index -> Index -> ArrayView a -> Effect (ArrayView a) slice s e a = runEffectFn3 sliceImpl a s e -foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a) +foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Index Index (ArrayView a) -- | Sorts the values in-place. sort :: forall a. ArrayView a -> Effect Unit @@ -362,9 +364,9 @@ sort a = runEffectFn1 sortImpl a foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. -subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a +subArray :: forall a. Index -> Index -> ArrayView a -> ArrayView a subArray s e a = runFn3 subArrayImpl a s e -foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a) +foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Index Index (ArrayView a) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. toString :: forall a. ArrayView a -> Effect String @@ -377,14 +379,14 @@ join s a = runEffectFn2 joinImpl a s foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String -- | Determine if a certain index is valid. -hasIndex :: forall a. ArrayView a -> Offset -> Boolean +hasIndex :: forall a. ArrayView a -> Index -> Boolean hasIndex a o = runFn2 hasIndexImpl a o -foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean +foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Index Boolean -- | Fetch element at index. -at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Effect (Maybe t) +at :: forall a t. TypedArray a t => ArrayView a -> Index -> Effect (Maybe t) at a n = toMaybe <$> runEffectFn2 unsafeAtImpl a n -foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b +foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Index b infixl 3 at as ! diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index ebc7db0..4470a12 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -58,19 +58,19 @@ genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+3 where div = 4.0 -- | For generating some set of offsets residing inside the generated array -data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) -derive instance genericWithOffset :: Generic (ArrayView a) a' => Generic (WithOffset n a) _ +data WithIndices n a = WithIndices (Vec n TA.Index) (ArrayView a) +derive instance genericWithIndices :: Generic (ArrayView a) a' => Generic (WithIndices n a) _ -genWithOffset :: forall m n a +genWithIndices :: forall m n a . MonadGen m => Nat n => m (ArrayView a) - -> m (WithOffset n a) -genWithOffset gen = do + -> m (WithIndices n a) +genWithIndices gen = do let n = toInt' (Proxy :: Proxy n) xs <- gen let l = TA.length xs mos <- replicateA n (chooseInt 0 (l - 1)) let os = unsafePartial $ case Vec.fromArray mos of Just q -> q - pure (WithOffset os xs) + pure (WithIndices os xs) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index ec4d811..c9599a0 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -7,7 +7,7 @@ import Control.Monad.Gen (suchThat) import Data.Array as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithOffset) +import Data.ArrayBuffer.Typed.Gen (WithIndices(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithIndices) import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint8Array, Uint8ClampedArray, Uint32Array) import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..), fromMaybe, isJust) @@ -50,9 +50,9 @@ typedArrayTests count = do log " - filter p (filter p x) == filter p x" filterIsIdempotentTests count log " - forall os `in` xs. all (\\o -> hasIndex o xs)" - withOffsetHasIndexTests count + withIndicesHasIndexTests count log " - forall os `in` xs. all (\\o -> elem (at o xs) xs)" - withOffsetElemTests count + withIndicesElemTests count log " - any p x => p (find p x)" anyImpliesFindTests count log " - p (at x (findIndex p x))" @@ -118,7 +118,7 @@ type TestableArrayF a b n t q = => TypedArray a t => BytesPerValue a b => Nat b - => WithOffset n a + => WithIndices n a -> Effect q @@ -129,7 +129,7 @@ overAll' mn count f = do let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit chk s _ gen = do log $ " - " <> s - quickCheckGen $ unsafePerformEffect <<< f <$> genWithOffset arr + quickCheckGen $ unsafePerformEffect <<< f <$> genWithIndices arr where arr :: Gen (ArrayView a) arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs @@ -154,7 +154,7 @@ subarrayBehavesLikeArraySliceTests :: Ref Int -> Effect Unit subarrayBehavesLikeArraySliceTests count = overAll count f where f :: forall a b t. TestableArrayF a b D2 t Result - f (WithOffset os xs) = do + f (WithIndices os xs) = do let s = os `Vec.index` d0 e = os `Vec.index` d1 axs <- TA.toArray xs @@ -166,7 +166,7 @@ sliceBehavesLikeArraySliceTests :: Ref Int -> Effect Unit sliceBehavesLikeArraySliceTests count = overAll count f where f :: forall a b t. TestableArrayF a b D2 t Result - f (WithOffset os xs) = do + f (WithIndices os xs) = do let s = os `Vec.index` d0 e = os `Vec.index` d1 axs <- TA.toArray xs @@ -178,7 +178,7 @@ partBehavesLikeTakeDropTests :: Ref Int -> Effect Unit partBehavesLikeTakeDropTests count = overAll count f where f :: forall a b t. TestableArrayF a b D0 t Result - f (WithOffset _ xs) = do + f (WithIndices _ xs) = do let n = 2 axs <- TA.toArray xs pxs <- TA.part (TA.buffer xs) n n :: Effect (ArrayView a) @@ -189,7 +189,7 @@ byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValueEqLength where byteLengthDivBytesPerValueEqLength :: forall a b t. TestableArrayF a b D0 t Result - byteLengthDivBytesPerValueEqLength (WithOffset _ a) = + byteLengthDivBytesPerValueEqLength (WithIndices _ a) = let b = toInt' (Proxy :: Proxy b) in pure $ TA.length a === (TA.byteLength a `div` b) @@ -197,7 +197,7 @@ fromArrayToArrayIsoTests :: Ref Int -> Effect Unit fromArrayToArrayIsoTests count = overAll count fromArrayToArrayIso where fromArrayToArrayIso :: forall a b t. TestableArrayF a b D0 t Result - fromArrayToArrayIso (WithOffset _ xs) = do + fromArrayToArrayIso (WithIndices _ xs) = do axs <- TA.toArray xs xs' <- TA.fromArray axs :: Effect (ArrayView a) axs' <- TA.toArray xs' @@ -208,7 +208,7 @@ allAreFilledTests :: Ref Int -> Effect Unit allAreFilledTests count = overAll count allAreFilled where allAreFilled :: forall a b t. TestableArrayF a b D0 t Result - allAreFilled (WithOffset _ xs) = do + allAreFilled (WithIndices _ xs) = do e <- TA.at xs 0 let x = fromMaybe zero e l = TA.length xs @@ -221,7 +221,7 @@ setSingletonIsEqTests :: Ref Int -> Effect Unit setSingletonIsEqTests count = overAll count setSingletonIsEq where setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result - setSingletonIsEq (WithOffset os xs) = do + setSingletonIsEq (WithIndices os xs) = do e <- TA.at xs 0 case e of Nothing -> pure Success @@ -237,7 +237,7 @@ allImpliesAnyTests :: Ref Int -> Effect Unit allImpliesAnyTests count = overAll count allImpliesAny where allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result - allImpliesAny (WithOffset _ xs) = do + allImpliesAny (WithIndices _ xs) = do let pred x = x /= zero all'' <- TA.all pred xs let all' = all'' "All don't satisfy the predicate" @@ -251,7 +251,7 @@ filterImpliesAllTests :: Ref Int -> Effect Unit filterImpliesAllTests count = overAll count filterImpliesAll where filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result - filterImpliesAll (WithOffset _ xs) = do + filterImpliesAll (WithIndices _ xs) = do let pred x = x /= zero ys <- TA.filter pred xs all' <- TA.all pred ys @@ -263,7 +263,7 @@ filterIsTotalTests :: Ref Int -> Effect Unit filterIsTotalTests count = overAll count filterIsTotal where filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result - filterIsTotal (WithOffset _ xs) = do + filterIsTotal (WithIndices _ xs) = do let pred x = x /= zero ys <- TA.filter pred xs zs <- TA.filter (not pred) ys @@ -276,7 +276,7 @@ filterIsIdempotentTests :: Ref Int -> Effect Unit filterIsIdempotentTests count = overAll count filterIsIdempotent where filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result - filterIsIdempotent (WithOffset _ xs) = do + filterIsIdempotent (WithIndices _ xs) = do let pred x = x /= zero ys <- TA.filter pred xs zs <- TA.filter pred ys @@ -285,19 +285,19 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent pure $ azs === ays -withOffsetHasIndexTests :: Ref Int -> Effect Unit -withOffsetHasIndexTests count = overAll1 count withOffsetHasIndex +withIndicesHasIndexTests :: Ref Int -> Effect Unit +withIndicesHasIndexTests count = overAll1 count withIndicesHasIndex where - withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result - withOffsetHasIndex (WithOffset os xs) = pure $ + withIndicesHasIndex :: forall a b t. TestableArrayF a b D5 t Result + withIndicesHasIndex (WithIndices os xs) = pure $ Array.all (TA.hasIndex xs) os "All doesn't have index of itself" -withOffsetElemTests :: Ref Int -> Effect Unit -withOffsetElemTests count = overAll1 count withOffsetElem +withIndicesElemTests :: Ref Int -> Effect Unit +withIndicesElemTests count = overAll1 count withIndicesElem where - withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result - withOffsetElem (WithOffset os xs) = do + withIndicesElem :: forall a b t. TestableArrayF a b D5 t Result + withIndicesElem (WithIndices os xs) = do let fetch o = TA.at xs o exs <- traverse fetch os pure $ Array.all isJust exs "All doesn't have an elem of itself" @@ -308,7 +308,7 @@ anyImpliesFindTests :: Ref Int -> Effect Unit anyImpliesFindTests count = overAll count anyImpliesFind where anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result - anyImpliesFind (WithOffset _ xs) = do + anyImpliesFind (WithIndices _ xs) = do let pred x = x /= zero a <- TA.any pred xs let p = a "All don't satisfy the predicate" @@ -326,7 +326,7 @@ findIndexImpliesAtTests :: Ref Int -> Effect Unit findIndexImpliesAtTests count = overAll count findIndexImpliesAt where findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - findIndexImpliesAt (WithOffset _ xs) = do + findIndexImpliesAt (WithIndices _ xs) = do let pred x _ = x /= zero mo <- TA.findIndex pred xs case mo of @@ -343,7 +343,7 @@ indexOfImpliesAtTests :: Ref Int -> Effect Unit indexOfImpliesAtTests count = overAll count indexOfImpliesAt where indexOfImpliesAt :: forall a b t. TestableArrayF a b D1 t Result - indexOfImpliesAt (WithOffset _ xs) = do + indexOfImpliesAt (WithIndices _ xs) = do e <- TA.at xs 0 case e of Nothing -> pure Success @@ -360,7 +360,7 @@ lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt where lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - lastIndexOfImpliesAt (WithOffset _ xs) = do + lastIndexOfImpliesAt (WithIndices _ xs) = do e <- TA.at xs 0 case e of Nothing -> pure Success @@ -377,7 +377,7 @@ foldrConsIsToArrayTests :: Ref Int -> Effect Unit foldrConsIsToArrayTests count = overAll count foldrConsIsToArray where foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result - foldrConsIsToArray (WithOffset _ xs) = do + foldrConsIsToArray (WithIndices _ xs) = do axs <- TA.toArray xs rxs <- TA.foldr Array.cons [] xs pure $ rxs === axs @@ -387,7 +387,7 @@ foldlSnocIsToArrayTests :: Ref Int -> Effect Unit foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray where foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result - foldlSnocIsToArray (WithOffset _ xs) = do + foldlSnocIsToArray (WithIndices _ xs) = do axs <- TA.toArray xs rxs <- TA.foldl Array.snoc [] xs pure $ rxs === axs @@ -397,7 +397,7 @@ mapIdentityIsIdentityTests :: Ref Int -> Effect Unit mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity where mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - mapIdentityIsIdentity (WithOffset _ xs) = do + mapIdentityIsIdentity (WithIndices _ xs) = do axs <- TA.toArray xs mxs <- TA.toArray (TA.map identity xs) pure $ axs === mxs @@ -407,7 +407,7 @@ traverseSnocIsToArrayTests :: Ref Int -> Effect Unit traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray where traverseSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result - traverseSnocIsToArray (WithOffset _ xs) = do + traverseSnocIsToArray (WithIndices _ xs) = do ref <- Ref.new [] TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs ys <- Ref.read ref @@ -419,7 +419,7 @@ doubleReverseIsIdentityTests :: Ref Int -> Effect Unit doubleReverseIsIdentityTests count = overAll count doubleReverseIsIdentity where doubleReverseIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - doubleReverseIsIdentity (WithOffset _ xs) = do + doubleReverseIsIdentity (WithIndices _ xs) = do axs <- TA.toArray xs TA.reverse xs TA.reverse xs @@ -431,7 +431,7 @@ reverseIsArrayReverseTests :: Ref Int -> Effect Unit reverseIsArrayReverseTests count = overAll count reverseIsArrayReverse where reverseIsArrayReverse :: forall a b t. TestableArrayF a b D0 t Result - reverseIsArrayReverse (WithOffset _ xs) = do + reverseIsArrayReverse (WithIndices _ xs) = do axs <- TA.toArray xs TA.reverse xs rxs <- TA.toArray xs @@ -442,7 +442,7 @@ sortIsIdempotentTests :: Ref Int -> Effect Unit sortIsIdempotentTests count = overAll count sortIsIdempotent where sortIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result - sortIsIdempotent (WithOffset _ xs) = do + sortIsIdempotent (WithIndices _ xs) = do TA.sort xs ys <- TA.toArray xs TA.sort xs @@ -454,7 +454,7 @@ sortIsArraySortTests :: Ref Int -> Effect Unit sortIsArraySortTests count = overAll count sortIsArraySort where sortIsArraySort :: forall a b t. TestableArrayF a b D0 t Result - sortIsArraySort (WithOffset _ xs) = do + sortIsArraySort (WithIndices _ xs) = do axs <- TA.toArray xs let ys = Array.sort axs TA.sort xs @@ -466,7 +466,7 @@ toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma where toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result - toStringIsJoinWithComma (WithOffset _ xs) = do + toStringIsJoinWithComma (WithIndices _ xs) = do s1 <- TA.join "," xs s2 <- TA.toString xs pure $ s1 === s2 @@ -476,7 +476,7 @@ setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIdentity where setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - setTypedOfSubArrayIsIdentity (WithOffset _ xs) = do + setTypedOfSubArrayIsIdentity (WithIndices _ xs) = do ys <- TA.toArray xs let l = TA.length xs zsSub = TA.subArray 0 l xs @@ -489,7 +489,7 @@ modifyingOriginalMutatesSubArrayTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMutatesSubArray where modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArray (WithOffset _ xs) = do + modifyingOriginalMutatesSubArray (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -506,7 +506,7 @@ modifyingSubArrayMutatesOriginalTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMutatesSubArray where modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArray (WithOffset _ xs) = do + modifyingOriginalMutatesSubArray (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -523,7 +523,7 @@ modifyingOriginalMutatesSubArrayZeroTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOriginalMutatesSubArrayZero where modifyingOriginalMutatesSubArrayZero :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArrayZero (WithOffset _ xs) = do + modifyingOriginalMutatesSubArrayZero (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -540,7 +540,7 @@ modifyingSubArrayMutatesOriginalZeroTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArrayMutatesOriginalZero where modifyingSubArrayMutatesOriginalZero :: forall a b t. TestableArrayF a b D0 t Result - modifyingSubArrayMutatesOriginalZero (WithOffset _ xs) = do + modifyingSubArrayMutatesOriginalZero (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -557,7 +557,7 @@ modifyingOriginalMutatesSubArrayAllTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginalMutatesSubArrayAll where modifyingOriginalMutatesSubArrayAll :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArrayAll (WithOffset _ xs) = do + modifyingOriginalMutatesSubArrayAll (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -574,7 +574,7 @@ modifyingSubArrayMutatesOriginalAllTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArrayMutatesOriginalAll where modifyingSubArrayMutatesOriginalAll :: forall a b t. TestableArrayF a b D0 t Result - modifyingSubArrayMutatesOriginalAll (WithOffset _ xs) = do + modifyingSubArrayMutatesOriginalAll (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -591,7 +591,7 @@ modifyingOriginalMutatesSubArrayPartTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayPartTests count = overAll count modifyingOriginalMutatesSubArrayPart where modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalMutatesSubArrayPart (WithOffset os xs) = do + modifyingOriginalMutatesSubArrayPart (WithIndices os xs) = do let o = Vec.head os l = TA.length xs zsSub = TA.subArray 0 l xs @@ -608,7 +608,7 @@ modifyingOriginalDoesntMutateSliceTests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalDoesntMutateSlice where modifyingOriginalDoesntMutateSlice :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalDoesntMutateSlice (WithOffset _ xs) = do + modifyingOriginalDoesntMutateSlice (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs then pure Success @@ -625,7 +625,7 @@ modifyingOriginalDoesntMutateSlicePartTests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOriginalDoesntMutateSlicePart where modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) = do + modifyingOriginalDoesntMutateSlicePart (WithIndices os xs) = do let l = TA.length xs axs <- TA.toArray =<< TA.slice 0 l xs let o = Vec.head os @@ -644,7 +644,7 @@ modifyingOriginalDoesntMutateSlicePart2Tests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOriginalDoesntMutateSlicePart2 where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) = do + modifyingOriginalDoesntMutateSlicePart2 (WithIndices os xs) = do let o = Vec.head os l = TA.length xs axs <- TA.toArray =<< TA.slice o l xs @@ -663,7 +663,7 @@ copyWithinSelfIsIdentityTests :: Ref Int -> Effect Unit copyWithinSelfIsIdentityTests count = overAll count copyWithinSelfIsIdentity where copyWithinSelfIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - copyWithinSelfIsIdentity (WithOffset _ xs) = do + copyWithinSelfIsIdentity (WithIndices _ xs) = do ys <- TA.toArray xs TA.copyWithin xs 0 0 (Just (TA.length xs)) zs <- TA.toArray xs @@ -674,7 +674,7 @@ copyWithinIsSliceTests :: Ref Int -> Effect Unit copyWithinIsSliceTests count = overAll count copyWithinIsSlice where copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result - copyWithinIsSlice (WithOffset os xs) = do + copyWithinIsSlice (WithIndices os xs) = do let o = Vec.head os l = TA.length xs ys <- TA.toArray =<< TA.slice o l xs @@ -688,7 +688,7 @@ copyWithinViaSetTypedTests :: Ref Int -> Effect Unit copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped where copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result - copyWithinViaSetTyped (WithOffset os xs) = do + copyWithinViaSetTyped (WithIndices os xs) = do let o = Vec.head os txs <- TA.toArray xs xs' <- TA.fromArray txs :: Effect (ArrayView a) From f224bbf120f3c858a65585f8dc1accac23087be6 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 31 Jan 2019 09:16:34 +0100 Subject: [PATCH 190/236] Execute polyFill always --- src/Data/ArrayBuffer/Typed.js | 5 ++++- src/Data/ArrayBuffer/Typed.purs | 6 +----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index d7e2785..cf2d733 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,7 +1,9 @@ "use strict"; -exports.polyFill = function polyFill () { + +// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill +function polyFill () { var typedArrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array @@ -15,6 +17,7 @@ exports.polyFill = function polyFill () { } }; +polyFill(); // module Data.ArrayBuffer.Typed diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 9dd6b15..e28c7c7 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -30,8 +30,7 @@ module Data.ArrayBuffer.Typed - ( polyFill - , Index, Length + ( Index, Length , buffer, byteOffset, byteLength, length , class TypedArray , create, whole, remainder, part, empty, fromArray @@ -65,9 +64,6 @@ import Prelude (Unit, flip, pure, ($), (&&), (*), (*>), (-), (<$>), (<<<), (<=), import Type.Proxy (Proxy(..)) --- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -foreign import polyFill :: Effect Unit - -- | `ArrayBuffer` being mapped by the typed array. foreign import buffer :: forall a. ArrayView a -> ArrayBuffer From 70f7ad4f46e245df3ccd95234a9ac4e287048c05 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 1 Feb 2019 14:33:20 +0100 Subject: [PATCH 191/236] compare/eq, unsafe Show/Ord/Eq/Semigroup/Monoid --- bower.json | 7 +- src/Data/ArrayBuffer/Typed.purs | 12 ++- src/Data/ArrayBuffer/Typed/Gen.purs | 4 +- src/Data/ArrayBuffer/Typed/Unsafe.purs | 32 ++++++++ test/Properties.purs | 2 + test/Properties/DataView.purs | 2 +- test/Properties/Typed/Laws.purs | 104 +++++++++++++++++++++++++ 7 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 src/Data/ArrayBuffer/Typed/Unsafe.purs create mode 100644 test/Properties/Typed/Laws.purs diff --git a/bower.json b/bower.json index 64e11d0..56b7442 100644 --- a/bower.json +++ b/bower.json @@ -20,12 +20,13 @@ "purescript-typelevel": "^4.0.0", "purescript-uint": "^5.1.0", "purescript-sized-vectors": "^3.1.0", - "purescript-float32": "^0.0.1" + "purescript-partial": "^2.0.0", + "purescript-float32": "~0.1.1" }, "devDependencies": { "purescript-debug": "^4.0.0", "purescript-quickcheck": "^5.0.0", - "purescript-partial": "^2.0.0", - "purescript-quickcheck-combinators": "^0.1.0" + "purescript-quickcheck-combinators": "~0.1.0", + "purescript-quickcheck-laws": "^4.0.0" } } diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index e28c7c7..aa14ef6 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -32,6 +32,7 @@ module Data.ArrayBuffer.Typed ( Index, Length , buffer, byteOffset, byteLength, length + , compare, eq , class TypedArray , create, whole, remainder, part, empty, fromArray , fill, set, setTyped, copyWithin @@ -60,7 +61,8 @@ import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) -import Prelude (Unit, flip, pure, ($), (&&), (*), (*>), (-), (<$>), (<<<), (<=), (>=)) +import Prelude (class Eq, class Ord, Ordering, Unit, flip, pure, ($), (&&), (*), (*>), (-), (<$>), (<*>), (<<<), (<=), (>=)) +import Prelude as Prelude import Type.Proxy (Proxy(..)) @@ -390,3 +392,11 @@ infixl 3 at as ! toArray :: forall a t. TypedArray a t => ArrayView a -> Effect (Array t) toArray a = runEffectFn1 toArrayImpl a foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b) + +-- | Compare 2 typed arrays. +compare :: forall a t. TypedArray a t => Ord t => ArrayView a -> ArrayView a -> Effect Ordering +compare a b = Prelude.compare <$> toArray a <*> toArray b + +-- | Equality test for typed arrays. +eq :: forall a t. TypedArray a t => Eq t => ArrayView a -> ArrayView a -> Effect Boolean +eq a b = Prelude.eq <$> toArray a <*> toArray b diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 4470a12..913c880 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -6,7 +6,7 @@ import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) -import Data.Float32 (Float32, fromNumber) as F +import Data.Float32 (Float32, fromNumber') as F import Data.Generic.Rep (class Generic) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, toInt') @@ -51,7 +51,7 @@ genInt32 :: forall m. MonadGen m => m Int genInt32 = chooseInt bottom top genFloat32 :: forall m. MonadGen m => m F.Float32 -genFloat32 = F.fromNumber <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 +genFloat32 = F.fromNumber' <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 genFloat64 :: forall m. MonadGen m => m Number genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+308/div) diff --git a/src/Data/ArrayBuffer/Typed/Unsafe.purs b/src/Data/ArrayBuffer/Typed/Unsafe.purs new file mode 100644 index 0000000..b4714d9 --- /dev/null +++ b/src/Data/ArrayBuffer/Typed/Unsafe.purs @@ -0,0 +1,32 @@ +module Data.ArrayBuffer.Typed.Unsafe where + +import Data.ArrayBuffer.Typed (class TypedArray, toString) +import Data.ArrayBuffer.Typed as TA +import Data.ArrayBuffer.Types (ArrayView) +import Data.Maybe (Maybe(..)) +import Effect.Unsafe (unsafePerformEffect) +import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>)) + +newtype AV a t = AV (ArrayView a) + +instance ordArrayView :: (TypedArray a t, Ord t) => Ord (AV a t) where + compare (AV a) (AV b) = unsafePerformEffect $ TA.compare a b + +instance eqArrayView :: (TypedArray a t, Eq t) => Eq (AV a t) where + eq (AV a) (AV b) = unsafePerformEffect $ TA.eq a b + +instance showArrayView :: (TypedArray a t, Show t) => Show (AV a t) where + show (AV a) = "T[" <> s <> "]" + where s = unsafePerformEffect $ toString a + +instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t) where + append (AV a) (AV b) = unsafePerformEffect do + let la = TA.length a + lb = TA.length b + r <- TA.empty $ la + lb + void $ TA.setTyped r (Just 0) a + void $ TA.setTyped r (Just la) b + pure $ AV r + +instance monoidArrayView :: TypedArray a t => Monoid (AV a t) where + mempty = AV $ unsafePerformEffect $ TA.empty 0 diff --git a/test/Properties.purs b/test/Properties.purs index 77f0d14..6bf5407 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -6,6 +6,7 @@ import Effect.Ref (new, read) as Ref import Prelude (Unit, bind, discard, ($), (<>), (*), show) import Test.Properties.DataView (dataViewTests) import Test.Properties.TypedArray (typedArrayTests) +import Test.Properties.Typed.Laws (typedArrayLaws) propertiesTests :: Effect Unit @@ -14,6 +15,7 @@ propertiesTests = do count <- Ref.new 0 log " - TypedArray Tests:" typedArrayTests count + typedArrayLaws count c <- Ref.read count log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 3a13210..cee9459 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -47,7 +47,7 @@ type TestableViewF a name b n t q = overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a name b t. TestableViewF a name b n t q) -> Effect Unit overAll count f = do - void (Ref.modify (\x -> x + 1) count) + void (Ref.modify (_ + 1) count) log " - Uint32" quickCheckGen $ let f' :: TestableViewF Uint32 "Uint32" D4 n UInt q diff --git a/test/Properties/Typed/Laws.purs b/test/Properties/Typed/Laws.purs new file mode 100644 index 0000000..8a6f02b --- /dev/null +++ b/test/Properties/Typed/Laws.purs @@ -0,0 +1,104 @@ +module Test.Properties.Typed.Laws where + +import Data.ArrayBuffer.Typed (class TypedArray) +import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8) +import Data.ArrayBuffer.Typed.Unsafe (AV(..)) +import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, kind ArrayViewType) +import Data.Float32 as F +import Data.UInt (UInt) +import Effect (Effect) +import Effect.Ref (Ref) +import Effect.Ref as Ref +import Prelude (class Eq, class Monoid, class Ord, class Semigroup, Unit, discard, void, ($), (+), (<$>), (<<<)) +import Test.QuickCheck (class Arbitrary) +import Test.QuickCheck.Gen (Gen) +import Test.QuickCheck.Laws.Data (checkEq, checkMonoid, checkOrd, checkSemigroup) +import Type.Prelude (Proxy(..)) + +newtype A a = A a + +foreign import data ArrayElt :: ArrayViewType -> Type -> Type + +class ArrayEl (a :: ArrayViewType) (t :: Type) where + arb :: Proxy (ArrayView a) -> Gen t + +instance arrayElUint8Clamped :: ArrayEl Uint8Clamped UInt where + arb _ = genUint8 +instance arrayElUint32 :: ArrayEl Uint32 UInt where + arb _ = genUint32 +instance arrayElUint16 :: ArrayEl Uint16 UInt where + arb _ = genUint16 +instance arrayElUint8 :: ArrayEl Uint8 UInt where + arb _ = genUint8 +instance arrayElInt32 :: ArrayEl Int32 Int where + arb _ = genInt32 +instance arrayElInt16 :: ArrayEl Int16 Int where + arb _ = genInt16 +instance arrayElInt8 :: ArrayEl Int8 Int where + arb _ = genInt8 +instance arrayElFloat32 :: ArrayEl Float32 F.Float32 where + arb _ = genFloat32 +instance arrayElFloat64 :: ArrayEl Float64 Number where + arb _ = genFloat64 + +instance arbitraryAAV :: (TypedArray a t, ArrayEl a t) => Arbitrary (A (AV a t)) where + arbitrary = (A <<< AV) <$> genTypedArray (arb (Proxy :: Proxy (ArrayView a))) + +derive newtype instance eqA :: Eq t => Eq (A t) +derive newtype instance ordA :: Ord t => Ord (A t) +derive newtype instance semigroupA :: Semigroup t => Semigroup (A t) +derive newtype instance monoidA :: Monoid t => Monoid (A t) + +typedArrayLaws :: Ref Int -> Effect Unit +typedArrayLaws count = do + do + let f = checkEq + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + + do + let f = checkOrd + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + + do + let f = checkSemigroup + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + + do + let f = checkMonoid + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) From 87e3d4f248f2735fa9c8e722d68736fe677393e7 Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 1 Feb 2019 14:38:32 +0100 Subject: [PATCH 192/236] Add travis script --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1f93807 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +sudo: required +dist: trusty +node_js: 8 +install: + - npm install -g purescript pulp bower +script: + - bower install + - pulp test From ebd2b1045a05ee59ccb7c6ed16521485b3e54886 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 6 Feb 2019 17:59:47 -0700 Subject: [PATCH 193/236] no need to distinguish DataView class and BinaryValue class --- src/Data/ArrayBuffer/DataView.purs | 41 +++++--------------------- src/Data/ArrayBuffer/DataView/Gen.purs | 6 ++-- src/Data/ArrayBuffer/ValueMapping.purs | 13 ++++++++ 3 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 2637003..b88f344 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -7,8 +7,6 @@ module Data.ArrayBuffer.DataView , buffer , byteLength , byteOffset - , class DataView - , class ShowArrayViewType , get , getBE , getFloat32be @@ -48,8 +46,8 @@ module Data.ArrayBuffer.DataView , whole ) where -import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) +import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, kind ArrayViewType) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue, class ShowArrayViewType) import Data.Float32 (Float32) as F import Data.Maybe (Maybe) import Data.Nullable (Nullable, toMaybe) @@ -96,29 +94,6 @@ instance eqEndian :: Eq Endian where eq BE BE = true eq _ _ = false -class BinaryValue a t <= DataView (a :: ArrayViewType) t | a -> t - -instance dataViewUint8Clamped :: DataView Uint8Clamped UInt -instance dataViewUint32 :: DataView Uint32 UInt -instance dataViewUint16 :: DataView Uint16 UInt -instance dataViewUint8 :: DataView Uint8 UInt -instance dataViewInt32 :: DataView Int32 Int -instance dataViewInt16 :: DataView Int16 Int -instance dataViewInt8 :: DataView Int8 Int -instance dataViewFloat32 :: DataView Float32 F.Float32 -instance dataViewFloat64 :: DataView Float64 Number - - -class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name -instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped" -instance showArrayViewTypeViewUint32 :: ShowArrayViewType Uint32 "Uint32" -instance showArrayViewTypeViewUint16 :: ShowArrayViewType Uint16 "Uint16" -instance showArrayViewTypeViewUint8 :: ShowArrayViewType Uint8 "Uint8" -instance showArrayViewTypeViewInt32 :: ShowArrayViewType Int32 "Int32" -instance showArrayViewTypeViewInt16 :: ShowArrayViewType Int16 "Int16" -instance showArrayViewTypeViewInt8 :: ShowArrayViewType Int8 "Int8" -instance showArrayViewTypeViewFloat32 :: ShowArrayViewType Float32 "Float32" -instance showArrayViewTypeViewFloat64 :: ShowArrayViewType Float64 "Float64" getter :: forall t. { functionName :: String @@ -141,7 +116,7 @@ foreign import getterImpl :: forall t get :: forall a name t b - . DataView a t + . BinaryValue a t => BytesPerValue a b => ShowArrayViewType a name => IsSymbol name @@ -157,7 +132,7 @@ get endian prx = } getBE :: forall a name t b - . DataView a t + . BinaryValue a t => BytesPerValue a b => ShowArrayViewType a name => IsSymbol name @@ -166,7 +141,7 @@ getBE :: forall a name t b getBE = get BE getLE :: forall a name t b - . DataView a t + . BinaryValue a t => BytesPerValue a b => ShowArrayViewType a name => IsSymbol name @@ -188,7 +163,7 @@ foreign import setterImpl :: forall t set :: forall a name t b - . DataView a t + . BinaryValue a t => BytesPerValue a b => ShowArrayViewType a name => IsSymbol name @@ -262,7 +237,7 @@ getFloat64le = getLE (AProxy :: AProxy Float64) -- | Store big-endian value at a certain index in a `DataView`. setBE :: forall a name t b - . DataView a t + . BinaryValue a t => BytesPerValue a b => ShowArrayViewType a name => IsSymbol name @@ -272,7 +247,7 @@ setBE = set BE -- | Store little-endian value at a certain index in a `DataView`. setLE :: forall a name t b - . DataView a t + . BinaryValue a t => BytesPerValue a b => ShowArrayViewType a name => IsSymbol name diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 3423d5f..0d61948 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -6,9 +6,9 @@ import Control.Monad.Gen (suchThat) import Control.Monad.Gen.Class (class MonadGen, chooseInt) import Control.Monad.Rec.Class (class MonadRec) import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) -import Data.ArrayBuffer.DataView (whole, byteLength, class DataView) +import Data.ArrayBuffer.DataView (whole, byteLength) import Data.ArrayBuffer.Types (DataView, ByteOffset, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) import Data.Maybe (Maybe(Just)) import Data.Typelevel.Num (class Nat, toInt') import Data.Unfoldable (replicateA) @@ -34,7 +34,7 @@ genWithOffsetAndValue :: forall m n a b t => MonadRec m => Nat n => BytesPerValue a b - => DataView a t + => BinaryValue a t => Nat b => m DataView -- ^ Assumes generated length is at least the minimum length of one value -> m t diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index c17d677..87bc256 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -35,3 +35,16 @@ instance binaryValueInt16 :: BinaryValue Int16 Int instance binaryValueInt8 :: BinaryValue Int8 Int instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number + + + +class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name +instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped" +instance showArrayViewTypeViewUint32 :: ShowArrayViewType Uint32 "Uint32" +instance showArrayViewTypeViewUint16 :: ShowArrayViewType Uint16 "Uint16" +instance showArrayViewTypeViewUint8 :: ShowArrayViewType Uint8 "Uint8" +instance showArrayViewTypeViewInt32 :: ShowArrayViewType Int32 "Int32" +instance showArrayViewTypeViewInt16 :: ShowArrayViewType Int16 "Int16" +instance showArrayViewTypeViewInt8 :: ShowArrayViewType Int8 "Int8" +instance showArrayViewTypeViewFloat32 :: ShowArrayViewType Float32 "Float32" +instance showArrayViewTypeViewFloat64 :: ShowArrayViewType Float64 "Float64" From c99c202215668e0a113a730240d1306a792c3a8b Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Wed, 6 Feb 2019 18:07:54 -0700 Subject: [PATCH 194/236] fixing tests --- test/Properties/DataView.purs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index cee9459..a3e08b4 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -7,7 +7,7 @@ import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genUint16, genUint32, genUint8) import Data.ArrayBuffer.Types (Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) +import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class ShowArrayViewType, class BinaryValue) import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) import Data.UInt (UInt) @@ -37,10 +37,10 @@ type TestableViewF a name b n t q = => Ord t => Semiring t => BytesPerValue a b - => DV.ShowArrayViewType a name + => BinaryValue a t + => ShowArrayViewType a name => IsSymbol name => Nat b - => DV.DataView a t => WithOffsetAndValue n a t -> q From b26e575c438dae5b241f94d2b60dad609443db80 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 14 Sep 2019 21:18:32 -0700 Subject: [PATCH 195/236] upgrading deps - waiting on sized-vectors to get pulled --- bower.json | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bower.json b/bower.json index 56b7442..08731c0 100644 --- a/bower.json +++ b/bower.json @@ -14,19 +14,19 @@ "dependencies": { "purescript-functions": "^4.0.0", "purescript-arraybuffer-types": "^2.0.0", - "purescript-maybe": "^4.0.0", - "purescript-effect": "^2.0.0", - "purescript-nullable": "^4.1.0", - "purescript-typelevel": "^4.0.0", - "purescript-uint": "^5.1.0", - "purescript-sized-vectors": "^3.1.0", - "purescript-partial": "^2.0.0", + "purescript-maybe": "^4.0.1", + "purescript-effect": "^2.0.1", + "purescript-nullable": "^4.1.1", + "purescript-typelevel": "^6.0.0", + "purescript-uint": "^5.1.1", + "purescript-sized-vectors": "git://github.com/athanclark/purescript-sized-vectors.git#4fcf13f5ed949e95242b99bcace1648e29bbcbad", + "purescript-partial": "^2.0.1", "purescript-float32": "~0.1.1" }, "devDependencies": { "purescript-debug": "^4.0.0", - "purescript-quickcheck": "^5.0.0", - "purescript-quickcheck-combinators": "~0.1.0", - "purescript-quickcheck-laws": "^4.0.0" + "purescript-quickcheck": "^6.1.0", + "purescript-quickcheck-combinators": "~0.1.2", + "purescript-quickcheck-laws": "^5.0.1" } } From 9682a2519675296005a906db1da3857b4f93576f Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sun, 15 Sep 2019 17:31:11 -0700 Subject: [PATCH 196/236] generic instance for AV --- src/Data/ArrayBuffer/Typed/Unsafe.purs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Data/ArrayBuffer/Typed/Unsafe.purs b/src/Data/ArrayBuffer/Typed/Unsafe.purs index b4714d9..3075d04 100644 --- a/src/Data/ArrayBuffer/Typed/Unsafe.purs +++ b/src/Data/ArrayBuffer/Typed/Unsafe.purs @@ -4,11 +4,14 @@ import Data.ArrayBuffer.Typed (class TypedArray, toString) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) import Data.Maybe (Maybe(..)) +import Data.Generic.Rep (class Generic) import Effect.Unsafe (unsafePerformEffect) import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>)) newtype AV a t = AV (ArrayView a) +derive instance genericAV :: Generic (AV a t) _ + instance ordArrayView :: (TypedArray a t, Ord t) => Ord (AV a t) where compare (AV a) (AV b) = unsafePerformEffect $ TA.compare a b From 0adc84d8d33aaa1deda2828e68926d2d8b093f81 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 5 Oct 2019 14:51:13 -0700 Subject: [PATCH 197/236] avoiding sized-vector dependency --- bower.json | 1 - src/Data/ArrayBuffer/DataView/Gen.purs | 12 +++--------- src/Data/ArrayBuffer/Typed/Gen.purs | 12 +++--------- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/bower.json b/bower.json index 08731c0..b8bb3f5 100644 --- a/bower.json +++ b/bower.json @@ -19,7 +19,6 @@ "purescript-nullable": "^4.1.1", "purescript-typelevel": "^6.0.0", "purescript-uint": "^5.1.1", - "purescript-sized-vectors": "git://github.com/athanclark/purescript-sized-vectors.git#4fcf13f5ed949e95242b99bcace1648e29bbcbad", "purescript-partial": "^2.0.1", "purescript-float32": "~0.1.1" }, diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 0d61948..441851a 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,6 +1,6 @@ module Data.ArrayBuffer.DataView.Gen where -import Prelude ((<$>), bind, ($), (<=), (-), pure) +import Prelude ((<$>), bind, (<=), (-), pure) import Control.Monad.Gen (suchThat) import Control.Monad.Gen.Class (class MonadGen, chooseInt) @@ -9,12 +9,8 @@ import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole, byteLength) import Data.ArrayBuffer.Types (DataView, ByteOffset, kind ArrayViewType) import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) -import Data.Maybe (Maybe(Just)) import Data.Typelevel.Num (class Nat, toInt') import Data.Unfoldable (replicateA) -import Data.Vec (Vec) -import Data.Vec (fromArray) as Vec -import Partial.Unsafe (unsafePartial) import Type.Proxy (Proxy(..)) @@ -27,7 +23,7 @@ genDataView = whole <$> genArrayBuffer -- | For generating some set of offsets residing inside the generated array, with some computable value data WithOffsetAndValue n (a :: ArrayViewType) t = - WithOffsetAndValue (Vec n ByteOffset) t DataView + WithOffsetAndValue (Array ByteOffset) t DataView genWithOffsetAndValue :: forall m n a b t . MonadGen m @@ -44,8 +40,6 @@ genWithOffsetAndValue gen genT = do b = toInt' (Proxy :: Proxy b) xs <- gen `suchThat` \xs -> b <= byteLength xs let l = byteLength xs - mos <- replicateA n (chooseInt 0 (l - b)) - let os = unsafePartial $ case Vec.fromArray mos of - Just q -> q + os <- replicateA n (chooseInt 0 (l - b)) t <- genT pure (WithOffsetAndValue os t xs) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 913c880..380e28b 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -2,23 +2,19 @@ module Data.ArrayBuffer.Typed.Gen where +import Prelude ((<$>), bind, (/), (-), negate, ($), bottom, pure, top) import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) import Data.Float32 (Float32, fromNumber') as F import Data.Generic.Rep (class Generic) -import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Data.UInt (fromInt) as UInt import Data.UInt.Gen (genUInt) as UInt import Data.Unfoldable (replicateA) -import Data.Vec (Vec) -import Data.Vec (fromArray) as Vec import Effect.Unsafe (unsafePerformEffect) -import Partial.Unsafe (unsafePartial) -import Prelude ((<$>), bind, (/), (-), negate, ($), bottom, pure, top) import Type.Proxy (Proxy(..)) @@ -58,7 +54,7 @@ genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+3 where div = 4.0 -- | For generating some set of offsets residing inside the generated array -data WithIndices n a = WithIndices (Vec n TA.Index) (ArrayView a) +data WithIndices n a = WithIndices (Array TA.Index) (ArrayView a) derive instance genericWithIndices :: Generic (ArrayView a) a' => Generic (WithIndices n a) _ genWithIndices :: forall m n a @@ -70,7 +66,5 @@ genWithIndices gen = do let n = toInt' (Proxy :: Proxy n) xs <- gen let l = TA.length xs - mos <- replicateA n (chooseInt 0 (l - 1)) - let os = unsafePartial $ case Vec.fromArray mos of - Just q -> q + os <- replicateA n (chooseInt 0 (l - 1)) pure (WithIndices os xs) From 525ca98350d8a9822bdede6bf19e429b92ba8281 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 5 Oct 2019 15:13:25 -0700 Subject: [PATCH 198/236] fixed tests --- test/Properties/DataView.purs | 6 ++++-- test/Properties/TypedArray.purs | 38 +++++++++++++++++---------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index a3e08b4..58cde67 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -3,6 +3,7 @@ module Test.Properties.DataView where import Prelude +import Data.Array.Partial (head) as Array import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genUint16, genUint32, genUint8) @@ -12,7 +13,7 @@ import Data.Maybe (Maybe(..)) import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) import Data.UInt (UInt) import Data.Float32 (Float32) as F -import Data.Vec (head) as Vec +-- import Data.Vec (head) as Vec import Data.Symbol (class IsSymbol) import Effect (Effect) import Effect.Console (log) @@ -20,6 +21,7 @@ import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) +import Partial.Unsafe (unsafePartial) @@ -102,7 +104,7 @@ placingAValueIsThereTests endian count = overAll count placingAValueIsThere where placingAValueIsThere :: forall a name b t. TestableViewF a name b D1 t Result placingAValueIsThere (WithOffsetAndValue os t xs) = - let o = Vec.head os + let o = unsafePartial $ Array.head os prx = DV.AProxy :: DV.AProxy a in unsafePerformEffect do _ <- DV.set endian prx xs o t diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index c9599a0..449f1a1 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -4,7 +4,8 @@ module Test.Properties.TypedArray where import Prelude import Control.Monad.Gen (suchThat) -import Data.Array as Array +import Data.Array (all, cons, drop, length, reverse, slice, snoc, sort, take, unsafeIndex) as Array +import Data.Array.Partial (head) as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed.Gen (WithIndices(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithIndices) @@ -12,13 +13,14 @@ import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array import Data.ArrayBuffer.ValueMapping (class BytesPerValue) import Data.Maybe (Maybe(..), fromMaybe, isJust) import Data.Traversable (traverse) -import Data.Typelevel.Num (class Nat, D0, D1, D2, D5, d0, d1, toInt') -import Data.Vec (head, index) as Vec +import Data.Typelevel.Num (class Nat, D0, D1, D2, D5, toInt') +-- import Data.Vec (head, index) as Vec import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) +import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Testable, Result(..), quickCheckGen, (/==), (), (===)) import Test.QuickCheck.Combinators ((==>), (|=|)) import Test.QuickCheck.Gen (Gen) @@ -155,8 +157,8 @@ subarrayBehavesLikeArraySliceTests count = overAll count f where f :: forall a b t. TestableArrayF a b D2 t Result f (WithIndices os xs) = do - let s = os `Vec.index` d0 - e = os `Vec.index` d1 + let s = unsafePartial $ os `Array.unsafeIndex` 0 + e = unsafePartial $ os `Array.unsafeIndex` 1 axs <- TA.toArray xs let sxs = TA.subArray s e xs a <- TA.toArray sxs @@ -167,8 +169,8 @@ sliceBehavesLikeArraySliceTests count = overAll count f where f :: forall a b t. TestableArrayF a b D2 t Result f (WithIndices os xs) = do - let s = os `Vec.index` d0 - e = os `Vec.index` d1 + let s = unsafePartial $ os `Array.unsafeIndex` 0 + e = unsafePartial $ os `Array.unsafeIndex` 1 axs <- TA.toArray xs sxs <- TA.slice s e xs a <- TA.toArray sxs @@ -224,12 +226,12 @@ setSingletonIsEqTests count = overAll count setSingletonIsEq setSingletonIsEq (WithIndices os xs) = do e <- TA.at xs 0 case e of - Nothing -> pure Success - Just x -> do - let o = Vec.head os - _ <- TA.set xs (Just o) [x] - e' <- TA.at xs o - pure $ e' === Just x + Nothing -> pure Success + Just x -> do + let o = unsafePartial $ Array.head os + _ <- TA.set xs (Just o) [x] + e' <- TA.at xs o + pure $ e' === Just x -- | Should work with any arbitrary predicate, but we can't generate them @@ -592,7 +594,7 @@ modifyingOriginalMutatesSubArrayPartTests count = overAll count modifyingOrigina where modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalMutatesSubArrayPart (WithIndices os xs) = do - let o = Vec.head os + let o = unsafePartial $ Array.head os l = TA.length xs zsSub = TA.subArray 0 l xs zs <- TA.toArray zsSub @@ -628,7 +630,7 @@ modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOrigi modifyingOriginalDoesntMutateSlicePart (WithIndices os xs) = do let l = TA.length xs axs <- TA.toArray =<< TA.slice 0 l xs - let o = Vec.head os + let o = unsafePartial $ Array.head os e <- TA.at xs o if Array.all (eq zero) axs || e == Just zero then pure Success @@ -645,7 +647,7 @@ modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOrig where modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result modifyingOriginalDoesntMutateSlicePart2 (WithIndices os xs) = do - let o = Vec.head os + let o = unsafePartial $ Array.head os l = TA.length xs axs <- TA.toArray =<< TA.slice o l xs e <- TA.at xs o @@ -675,7 +677,7 @@ copyWithinIsSliceTests count = overAll count copyWithinIsSlice where copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result copyWithinIsSlice (WithIndices os xs) = do - let o = Vec.head os + let o = unsafePartial $ Array.head os l = TA.length xs ys <- TA.toArray =<< TA.slice o l xs TA.copyWithin xs 0 o Nothing @@ -689,7 +691,7 @@ copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped where copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result copyWithinViaSetTyped (WithIndices os xs) = do - let o = Vec.head os + let o = unsafePartial $ Array.head os txs <- TA.toArray xs xs' <- TA.fromArray txs :: Effect (ArrayView a) let l = TA.length xs' From 7f90bc44247dcbeab70caef5d373833286468266 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Sat, 12 Oct 2019 19:43:28 -0700 Subject: [PATCH 199/236] arbitrary instance for AV --- src/Data/ArrayBuffer/Typed/Unsafe.purs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed/Unsafe.purs b/src/Data/ArrayBuffer/Typed/Unsafe.purs index 3075d04..a536eee 100644 --- a/src/Data/ArrayBuffer/Typed/Unsafe.purs +++ b/src/Data/ArrayBuffer/Typed/Unsafe.purs @@ -6,7 +6,8 @@ import Data.ArrayBuffer.Types (ArrayView) import Data.Maybe (Maybe(..)) import Data.Generic.Rep (class Generic) import Effect.Unsafe (unsafePerformEffect) -import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>)) +import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>), (<$>)) +import Test.QuickCheck (class Arbitrary, arbitrary) newtype AV a t = AV (ArrayView a) @@ -33,3 +34,8 @@ instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t) where instance monoidArrayView :: TypedArray a t => Monoid (AV a t) where mempty = AV $ unsafePerformEffect $ TA.empty 0 + +instance arbitraryArrayView :: (TypedArray a t, Arbitrary t) => Arbitrary (AV a t) where + arbitrary = do + xs <- arbitrary + pure $ unsafePerformEffect $ AV <$> TA.fromArray xs From d073ea8596330b8ad205370668c017a2e9bd2d7a Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Fri, 28 Feb 2020 15:23:11 -0800 Subject: [PATCH 200/236] fixes #27 --- src/Data/ArrayBuffer/Typed.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index aa14ef6..6d17ee1 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -34,7 +34,7 @@ module Data.ArrayBuffer.Typed , buffer, byteOffset, byteLength, length , compare, eq , class TypedArray - , create, whole, remainder, part, empty, fromArray + , create, whole, remainder, part, part', empty, fromArray , fill, set, setTyped, copyWithin , map, traverse, traverse_, filter , mapWithIndex, traverseWithIndex, traverseWithIndex_, filterWithIndex From d09c77a2470788d0a3e2a05a27dc00fac3749a0c Mon Sep 17 00:00:00 2001 From: James Brock Date: Sun, 25 Apr 2021 23:25:08 +0900 Subject: [PATCH 201/236] bower upgrade deps --- bower.json | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/bower.json b/bower.json index b8bb3f5..9f5755a 100644 --- a/bower.json +++ b/bower.json @@ -12,20 +12,21 @@ "output" ], "dependencies": { - "purescript-functions": "^4.0.0", + "purescript-prelude": "^5.0.0", + "purescript-functions": "^5.0.0", "purescript-arraybuffer-types": "^2.0.0", - "purescript-maybe": "^4.0.1", - "purescript-effect": "^2.0.1", - "purescript-nullable": "^4.1.1", + "purescript-maybe": "^5.0.0", + "purescript-effect": "^3.0.0", + "purescript-nullable": "^5.0.0", "purescript-typelevel": "^6.0.0", - "purescript-uint": "^5.1.1", - "purescript-partial": "^2.0.1", - "purescript-float32": "~0.1.1" + "purescript-uint": "^5.1.4", + "purescript-partial": "^3.0.0", + "purescript-float32": "~0.2.0" }, "devDependencies": { - "purescript-debug": "^4.0.0", - "purescript-quickcheck": "^6.1.0", - "purescript-quickcheck-combinators": "~0.1.2", - "purescript-quickcheck-laws": "^5.0.1" + "purescript-debug": "^5.0.0", + "purescript-quickcheck": "^7.0.0", + "purescript-quickcheck-combinators": "~0.1.3", + "purescript-quickcheck-laws": "^6.0.0" } } From 2fd62973b23407b22446ee2361202f8946f63619 Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 26 Apr 2021 00:12:17 +0900 Subject: [PATCH 202/236] Add spago build for purescript v0.14.1 --- .gitignore | 1 + packages.dhall | 34 ++++++++++++++++++++++++++++++++++ spago.dhall | 27 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 packages.dhall create mode 100644 spago.dhall diff --git a/.gitignore b/.gitignore index e4de85d..9cb72c2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ node_modules/ yarn-error.log yarn.lock generated-docs/ +.spago/ diff --git a/packages.dhall b/packages.dhall new file mode 100644 index 0000000..cf30481 --- /dev/null +++ b/packages.dhall @@ -0,0 +1,34 @@ + +let upstream = + https://github.com/purescript/package-sets/releases/download/psc-0.14.1-20210419/packages.dhall sha256:d9a082ffb5c0fabf689574f0680e901ca6f924e01acdbece5eeedd951731375a + +let overrides = {=} + +let additions = + { float32 = + { dependencies = + [ "effect" + , "gen" + , "maybe" + , "prelude" + ] + , repo = + "https://github.com/athanclark/purescript-float32.git" + , version = + "v0.2.0" + } + , uint = + { dependencies = + [ "effect" + , "math" + , "maybe" + , "quickcheck" + , "quickcheck-laws" + ] + , repo = "https://github.com/zaquest/purescript-uint.git" + , version = "v5.1.4" + } + } + +in upstream // overrides // additions + diff --git a/spago.dhall b/spago.dhall new file mode 100644 index 0000000..ef25406 --- /dev/null +++ b/spago.dhall @@ -0,0 +1,27 @@ +{ name = "arraybuffer" +, dependencies = + [ "arraybuffer-types" + , "arrays" + , "console" + , "effect" + , "float32" + , "foldable-traversable" + , "functions" + , "gen" + , "maybe" + , "nullable" + , "partial" + , "prelude" + , "quickcheck" + , "quickcheck-combinators" + , "quickcheck-laws" + , "refs" + , "tailrec" + , "typelevel" + , "typelevel-prelude" + , "uint" + , "unfoldable" + ] +, packages = ./packages.dhall +, sources = [ "src/**/*.purs", "test/**/*.purs" ] +} From 0a80132331463bd32c11443dcb9cc4c5b32d5bf1 Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 21 Jun 2021 12:46:33 +0900 Subject: [PATCH 203/236] Transfer repository to purescript-contrib To transfer the repository, refactor to remove the dependencies on __typelevel__ and __quickcheck-combinators__. Prep for v11. Repair some purs v0.14 warnings. --- .gitignore | 1 + CHANGELOG.md | 22 ++ README.md | 8 +- package.json | 2 +- packages.dhall | 2 +- spago.dhall | 3 +- src/Data/ArrayBuffer/ArrayBuffer.purs | 1 - src/Data/ArrayBuffer/DataView.purs | 126 ++++++------ src/Data/ArrayBuffer/DataView/Gen.purs | 26 +-- src/Data/ArrayBuffer/Typed.purs | 17 +- src/Data/ArrayBuffer/Typed/Gen.purs | 22 +- src/Data/ArrayBuffer/Typed/Unsafe.purs | 5 +- src/Data/ArrayBuffer/ValueMapping.purs | 52 ++--- test/Properties/DataView.purs | 59 +++--- test/Properties/Typed/Laws.purs | 2 +- test/Properties/TypedArray.purs | 266 ++++++++++++++----------- 16 files changed, 328 insertions(+), 286 deletions(-) create mode 100644 CHANGELOG.md diff --git a/.gitignore b/.gitignore index 9cb72c2..f71c254 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ yarn-error.log yarn.lock generated-docs/ .spago/ +.psc-ide-port diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ec49320 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# v11 2021-06-21 + +Jorge Acereda has graciously donated this package to __purescript-contrib__. +For version 11, we have refactored this library so that it depends only on +other packages in __purescript-contrib__. + +https://github.com/purescript-contrib/governance/issues/40 + +We have removed the dependencies on these non-__purescript-contrib__ packages: + +* https://pursuit.purescript.org/packages/purescript-typelevel +* https://pursuit.purescript.org/packages/purescript-quickcheck-combinators + +To upgrade to v11, you might need to do a few simple substitutions +to the type declarations in your own dependent code: + +* Replace the type `AProxy` with `Proxy` from the Prelude. +* Remove most of the `Nat` typeclass constraints. https://github.com/purescript-contrib/purescript-arraybuffer/issues/29 +* Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. + +In v11 of this package, we have also upgraded to PureScript v0.14. + diff --git a/README.md b/README.md index e318957..e9efeb9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,18 @@ # purescript-arraybuffer +[![CI](https://github.com/purescript-contrib/purescript-arraybuffer/workflows/CI/badge.svg?branch=main)](https://github.com/purescript-contrib/purescript-arraybuffer/actions?query=workflow%3ACI+branch%3Amain) +[![Release](https://img.shields.io/github/release/purescript-contrib/purescript-arraybuffer.svg)](https://github.com/purescript-contrib/purescript-arraybuffer/releases) +[![Pursuit](https://pursuit.purescript.org/packages/purescript-arraybuffer/badge)](https://pursuit.purescript.org/packages/purescript-arraybuffer) +[![Maintainer: natefaubion](https://img.shields.io/badge/maintainer-jamesdbrock-teal.svg)](https://github.com/jamesdbrock) ArrayBuffer bindings for PureScript. + ## Installation +Install `arraybuffer` with [Spago](https://github.com/purescript/spago): ``` - bower install purescript-arraybuffer +spago install purescript-arraybuffer ``` ## Documentation diff --git a/package.json b/package.json index 110d9f1..a792c5d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "purescript-arraybuffer", - "version": "9.0.0", + "version": "11.0.0", "main": "index.js", "repository": "git@github.com:jacereda/purescript-arraybuffer.git", "author": "https://github.com/jacereda", diff --git a/packages.dhall b/packages.dhall index cf30481..446440b 100644 --- a/packages.dhall +++ b/packages.dhall @@ -25,7 +25,7 @@ let additions = , "quickcheck" , "quickcheck-laws" ] - , repo = "https://github.com/zaquest/purescript-uint.git" + , repo = "https://github.com/purescript-contrib/purescript-uint.git" , version = "v5.1.4" } } diff --git a/spago.dhall b/spago.dhall index ef25406..41922dc 100644 --- a/spago.dhall +++ b/spago.dhall @@ -13,14 +13,13 @@ , "partial" , "prelude" , "quickcheck" - , "quickcheck-combinators" , "quickcheck-laws" , "refs" , "tailrec" - , "typelevel" , "typelevel-prelude" , "uint" , "unfoldable" + , "tuples" ] , packages = ./packages.dhall , sources = [ "src/**/*.purs", "test/**/*.purs" ] diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 1206040..9bedd45 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,6 +1,5 @@ -- | This module represents the functional bindings to JavaScript's `ArrayBuffer` -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) for details. - module Data.ArrayBuffer.ArrayBuffer ( empty , byteLength diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index b88f344..4daca87 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -1,9 +1,7 @@ -- | This module represents the functional bindings to JavaScript's `DataView` -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. - module Data.ArrayBuffer.DataView - ( AProxy (..) - , Endian (..) + ( Endian(..) , buffer , byteLength , byteOffset @@ -46,20 +44,25 @@ module Data.ArrayBuffer.DataView , whole ) where -import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue, class ShowArrayViewType) +import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, class ShowArrayViewType, byteWidth) import Data.Float32 (Float32) as F import Data.Maybe (Maybe) import Data.Nullable (Nullable, toMaybe) import Data.Symbol (SProxy(..), class IsSymbol, reflectSymbol) -import Data.Typelevel.Num (toInt', class Nat) import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4) import Prelude (class Eq, (<$>), (<>), (==)) import Type.Proxy (Proxy(..)) +-- | Endianness of a multi-byte type. Little-Endian or Big-Endian. +data Endian = LE | BE +instance eqEndian :: Eq Endian where + eq LE LE = true + eq BE BE = true + eq _ _ = false -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView @@ -85,15 +88,6 @@ foreign import byteOffset :: DataView -> ByteOffset foreign import byteLength :: DataView -> ByteLength -data AProxy (a :: ArrayViewType) = AProxy - -data Endian = LE | BE - -instance eqEndian :: Eq Endian where - eq LE LE = true - eq BE BE = true - eq _ _ = false - getter :: forall t. { functionName :: String @@ -115,38 +109,35 @@ foreign import getterImpl :: forall t -get :: forall a name t b +get :: forall a name t . BinaryValue a t - => BytesPerValue a b + => BytesPerType a => ShowArrayViewType a name => IsSymbol name - => Nat b - => Endian -> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) + => Endian -> Proxy a -> DataView -> ByteOffset -> Effect (Maybe t) get endian prx = let le = endian == LE pnm = "get" <> reflectSymbol (SProxy :: SProxy name) - bpv = toInt' (Proxy :: Proxy b) + bpv = byteWidth prx in getter { functionName: pnm , bytesPerValue: bpv , littleEndian: le } -getBE :: forall a name t b +getBE :: forall a name t . BinaryValue a t - => BytesPerValue a b + => BytesPerType a => ShowArrayViewType a name => IsSymbol name - => Nat b - => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) + => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t) getBE = get BE -getLE :: forall a name t b +getLE :: forall a name t . BinaryValue a t - => BytesPerValue a b + => BytesPerType a => ShowArrayViewType a name => IsSymbol name - => Nat b - => AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) + => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t) getLE = get LE setter :: forall t. @@ -162,17 +153,16 @@ foreign import setterImpl :: forall t } DataView ByteOffset t Boolean -set :: forall a name t b +set :: forall a name t . BinaryValue a t - => BytesPerValue a b + => BytesPerType a => ShowArrayViewType a name => IsSymbol name - => Nat b - => Endian -> AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean + => Endian -> Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean set endian prx = let le = endian == LE pnm = "set" <> reflectSymbol (SProxy :: SProxy name) - bpv = toInt' (Proxy :: Proxy b) + bpv = byteWidth prx in setter { functionName: pnm , bytesPerValue: bpv , littleEndian: le @@ -180,134 +170,132 @@ set endian prx = -- | Fetch int8 value at a certain index in a `DataView`. getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int) -getInt8 = getLE (AProxy :: AProxy Int8) +getInt8 = getLE (Proxy :: Proxy Int8) -- | Fetch big-endian int16 value at a certain index in a `DataView`. getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int) -getInt16be = getBE (AProxy :: AProxy Int16) +getInt16be = getBE (Proxy :: Proxy Int16) -- | Fetch little-endian int16 value at a certain index in a `DataView`. getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int) -getInt16le = getLE (AProxy :: AProxy Int16) +getInt16le = getLE (Proxy :: Proxy Int16) -- | Fetch big-endian int32 value at a certain index in a `DataView`. getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int) -getInt32be = getBE (AProxy :: AProxy Int32) +getInt32be = getBE (Proxy :: Proxy Int32) -- | Fetch little-endian int32 value at a certain index in a `DataView`. getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int) -getInt32le = getLE (AProxy :: AProxy Int32) +getInt32le = getLE (Proxy :: Proxy Int32) -- | Fetch uint8 value at a certain index in a `DataView`. getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt) -getUint8 = getLE (AProxy :: AProxy Uint8) +getUint8 = getLE (Proxy :: Proxy Uint8) -- | Fetch big-endian uint16 value at a certain index in a `DataView`. getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt) -getUint16be = getBE (AProxy :: AProxy Uint16) +getUint16be = getBE (Proxy :: Proxy Uint16) -- | Fetch little-endian uint16 value at a certain index in a `DataView`. getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt) -getUint16le = getLE (AProxy :: AProxy Uint16) +getUint16le = getLE (Proxy :: Proxy Uint16) -- | Fetch big-endian uint32 value at a certain index in a `DataView`. getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt) -getUint32be = getBE (AProxy :: AProxy Uint32) +getUint32be = getBE (Proxy :: Proxy Uint32) -- | Fetch little-endian uint32 value at a certain index in a `DataView`. getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt) -getUint32le = getLE (AProxy :: AProxy Uint32) +getUint32le = getLE (Proxy :: Proxy Uint32) -- | Fetch big-endian float32 value at a certain index in a `DataView`. getFloat32be :: DataView -> ByteOffset -> Effect (Maybe F.Float32) -getFloat32be = getBE (AProxy :: AProxy Float32) +getFloat32be = getBE (Proxy :: Proxy Float32) -- | Fetch little-endian float32 value at a certain index in a `DataView`. getFloat32le :: DataView -> ByteOffset -> Effect (Maybe F.Float32) -getFloat32le = getLE (AProxy :: AProxy Float32) +getFloat32le = getLE (Proxy :: Proxy Float32) -- | Fetch big-endian float64 value at a certain index in a `DataView`. getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number) -getFloat64be = getBE (AProxy :: AProxy Float64) +getFloat64be = getBE (Proxy :: Proxy Float64) -- | Fetch little-endian float64 value at a certain index in a `DataView`. getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number) -getFloat64le = getLE (AProxy :: AProxy Float64) +getFloat64le = getLE (Proxy :: Proxy Float64) -- | Store big-endian value at a certain index in a `DataView`. -setBE :: forall a name t b +setBE :: forall a name t . BinaryValue a t - => BytesPerValue a b + => BytesPerType a => ShowArrayViewType a name => IsSymbol name - => Nat b - => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean + => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean setBE = set BE -- | Store little-endian value at a certain index in a `DataView`. -setLE :: forall a name t b +setLE :: forall a name t . BinaryValue a t - => BytesPerValue a b + => BytesPerType a => ShowArrayViewType a name => IsSymbol name - => Nat b - => AProxy a -> DataView -> ByteOffset -> t -> Effect Boolean + => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean setLE = set LE -- | Store int8 value at a certain index in a `DataView`. setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean -setInt8 = setLE (AProxy :: AProxy Int8) +setInt8 = setLE (Proxy :: Proxy Int8) -- | Store big-endian int16 value at a certain index in a `DataView`. setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean -setInt16be = setBE (AProxy :: AProxy Int16) +setInt16be = setBE (Proxy :: Proxy Int16) -- | Store little-endian int16 value at a certain index in a `DataView`. setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean -setInt16le = setLE (AProxy :: AProxy Int16) +setInt16le = setLE (Proxy :: Proxy Int16) -- | Store big-endian int32 value at a certain index in a `DataView`. setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean -setInt32be = setBE (AProxy :: AProxy Int32) +setInt32be = setBE (Proxy :: Proxy Int32) -- | Store little-endian int32 value at a certain index in a `DataView`. setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean -setInt32le = setLE (AProxy :: AProxy Int32) +setInt32le = setLE (Proxy :: Proxy Int32) -- | Store uint8 value at a certain index in a `DataView`. setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean -setUint8 = setLE (AProxy :: AProxy Uint8) +setUint8 = setLE (Proxy :: Proxy Uint8) -- | Store big-endian uint16 value at a certain index in a `DataView`. setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean -setUint16be = setBE (AProxy :: AProxy Uint16) +setUint16be = setBE (Proxy :: Proxy Uint16) -- | Store little-endian uint16 value at a certain index in a `DataView`. setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean -setUint16le = setLE (AProxy :: AProxy Uint16) +setUint16le = setLE (Proxy :: Proxy Uint16) -- | Store big-endian uint32 value at a certain index in a `DataView`. setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean -setUint32be = setBE (AProxy :: AProxy Uint32) +setUint32be = setBE (Proxy :: Proxy Uint32) -- | Store little-endian uint32 value at a certain index in a `DataView`. setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean -setUint32le = setLE (AProxy :: AProxy Uint32) +setUint32le = setLE (Proxy :: Proxy Uint32) -- | Store big-endian float32 value at a certain index in a `DataView`. setFloat32be :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean -setFloat32be = setBE (AProxy :: AProxy Float32) +setFloat32be = setBE (Proxy :: Proxy Float32) -- | Store little-endian float32 value at a certain index in a `DataView`. setFloat32le :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean -setFloat32le = setLE (AProxy :: AProxy Float32) +setFloat32le = setLE (Proxy :: Proxy Float32) -- | Store big-endian float64 value at a certain index in a `DataView`. setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean -setFloat64be = setBE (AProxy :: AProxy Float64) +setFloat64be = setBE (Proxy :: Proxy Float64) -- | Store little-endian float64 value at a certain index in a `DataView`. setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean -setFloat64le = setLE (AProxy :: AProxy Float64) +setFloat64le = setLE (Proxy :: Proxy Float64) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 441851a..f7e55ca 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,16 +1,14 @@ module Data.ArrayBuffer.DataView.Gen where -import Prelude ((<$>), bind, (<=), (-), pure) - import Control.Monad.Gen (suchThat) import Control.Monad.Gen.Class (class MonadGen, chooseInt) import Control.Monad.Rec.Class (class MonadRec) import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) import Data.ArrayBuffer.DataView (whole, byteLength) -import Data.ArrayBuffer.Types (DataView, ByteOffset, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class BinaryValue) -import Data.Typelevel.Num (class Nat, toInt') +import Data.ArrayBuffer.Types (DataView, ByteOffset, ArrayViewType) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, byteWidth) import Data.Unfoldable (replicateA) +import Prelude ((<$>), bind, (<=), (-), pure) import Type.Proxy (Proxy(..)) @@ -22,22 +20,20 @@ genDataView = whole <$> genArrayBuffer -- | For generating some set of offsets residing inside the generated array, with some computable value -data WithOffsetAndValue n (a :: ArrayViewType) t = +data WithOffsetAndValue (a :: ArrayViewType) t = WithOffsetAndValue (Array ByteOffset) t DataView -genWithOffsetAndValue :: forall m n a b t +genWithOffsetAndValue :: forall m a t . MonadGen m => MonadRec m - => Nat n - => BytesPerValue a b + => BytesPerType a => BinaryValue a t - => Nat b - => m DataView -- ^ Assumes generated length is at least the minimum length of one value + => Int -- generated length + -> m DataView -- ^ Assumes generated length is at least the minimum length of one value -> m t - -> m (WithOffsetAndValue n a t) -genWithOffsetAndValue gen genT = do - let n = toInt' (Proxy :: Proxy n) - b = toInt' (Proxy :: Proxy b) + -> m (WithOffsetAndValue a t) +genWithOffsetAndValue n gen genT = do + let b = byteWidth (Proxy :: Proxy a) xs <- gen `suchThat` \xs -> b <= byteLength xs let l = byteLength xs os <- replicateA n (chooseInt 0 (l - b)) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 6d17ee1..472a959 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -43,20 +43,19 @@ module Data.ArrayBuffer.Typed , all, any , allWithIndex, anyWithIndex , unsafeAt, hasIndex, at, (!) - , reduce, reduce1, foldl, foldl1, reduceRight, reduceRight1, foldr, foldr1 - , find, findIndex, indexOf, lastIndexOf + , reduce, reduce1, foldl, foldl1, reduceRight, reduceRight1, foldr, foldr1, foldlWithIndex, foldrWithIndex + , find, findIndex, findWithIndex, indexOf, lastIndexOf , slice, subArray , toString, join, toArray ) where import Data.Array (length) as A -import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) +import Data.ArrayBuffer.Types (ArrayView, ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, byteWidth) import Data.Float32 (Float32) as F import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) import Data.Maybe (Maybe, fromMaybe) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) -import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) @@ -126,17 +125,17 @@ whole :: forall a t. TypedArray a t => ArrayBuffer -> Effect (ArrayView a) whole a = runEffectFn3 create a null null -- | View mapping the rest of an `ArrayBuffer` after an index. -remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Index -> Effect (ArrayView a) +remainder :: forall a b t. TypedArray a t => BytesPerType b => ArrayBuffer -> Index -> Effect (ArrayView a) remainder a x = remainder' a o - where o = x * toInt' (Proxy :: Proxy b) + where o = x * byteWidth (Proxy :: Proxy b) remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) remainder' a x = runEffectFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. -part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Index -> Length -> Effect (ArrayView a) +part :: forall a t. TypedArray a t => BytesPerType a => ArrayBuffer -> Index -> Length -> Effect (ArrayView a) part a x y = part' a o y - where o = x * toInt' (Proxy :: Proxy b) + where o = x * byteWidth (Proxy :: Proxy a) part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) part' a x y = runEffectFn3 create a (notNull x) (notNull y) diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 380e28b..678c14d 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -1,21 +1,18 @@ -- | Functions for generating typed arrays and values. - module Data.ArrayBuffer.Typed.Gen where -import Prelude ((<$>), bind, (/), (-), negate, ($), bottom, pure, top) import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) +import Data.ArrayBuffer.ValueMapping (class BytesPerType) import Data.Float32 (Float32, fromNumber') as F -import Data.Generic.Rep (class Generic) -import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Data.UInt (fromInt) as UInt import Data.UInt.Gen (genUInt) as UInt import Data.Unfoldable (replicateA) import Effect.Unsafe (unsafePerformEffect) -import Type.Proxy (Proxy(..)) +import Prelude (bind, bottom, negate, pure, top, ($), (-), (/), (<$>)) genTypedArray :: forall m a t @@ -54,16 +51,15 @@ genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+3 where div = 4.0 -- | For generating some set of offsets residing inside the generated array -data WithIndices n a = WithIndices (Array TA.Index) (ArrayView a) -derive instance genericWithIndices :: Generic (ArrayView a) a' => Generic (WithIndices n a) _ +data WithIndices a = WithIndices (Array TA.Index) (ArrayView a) -genWithIndices :: forall m n a +genWithIndices :: forall m a . MonadGen m - => Nat n - => m (ArrayView a) - -> m (WithIndices n a) -genWithIndices gen = do - let n = toInt' (Proxy :: Proxy n) + => BytesPerType a + => Int -- Number of offsets residing inside the generated array + -> m (ArrayView a) + -> m (WithIndices a) +genWithIndices n gen = do xs <- gen let l = TA.length xs os <- replicateA n (chooseInt 0 (l - 1)) diff --git a/src/Data/ArrayBuffer/Typed/Unsafe.purs b/src/Data/ArrayBuffer/Typed/Unsafe.purs index a536eee..facd419 100644 --- a/src/Data/ArrayBuffer/Typed/Unsafe.purs +++ b/src/Data/ArrayBuffer/Typed/Unsafe.purs @@ -2,13 +2,14 @@ module Data.ArrayBuffer.Typed.Unsafe where import Data.ArrayBuffer.Typed (class TypedArray, toString) import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Types (ArrayView) -import Data.Maybe (Maybe(..)) +import Data.ArrayBuffer.Types (ArrayView, ArrayViewType) import Data.Generic.Rep (class Generic) +import Data.Maybe (Maybe(..)) import Effect.Unsafe (unsafePerformEffect) import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>), (<$>)) import Test.QuickCheck (class Arbitrary, arbitrary) +newtype AV :: forall k. ArrayViewType -> k -> Type newtype AV a t = AV (ArrayView a) derive instance genericAV :: Generic (AV a t) _ diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 87bc256..e262b2c 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -1,29 +1,33 @@ -- | This module represents type-level mappings between `ArrayViewType`s -- | and meaningful data. - -module Data.ArrayBuffer.ValueMapping where - -import Data.ArrayBuffer.Types (kind ArrayViewType, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.Typelevel.Num (D1, D2, D4, D8) -import Data.UInt (UInt) +module Data.ArrayBuffer.ValueMapping + ( class BytesPerType + , byteWidth + , class BinaryValue + , class ShowArrayViewType + ) +where + +import Data.ArrayBuffer.Types (ArrayViewType, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped) import Data.Float32 (Float32) as F - - --- | Maps a `TypedArray`'s binary casted value, to the space occupied by that value, in bytes. -class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b - -instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped D1 -instance bytesPerValueUint32 :: BytesPerValue Uint32 D4 -instance bytesPerValueUint16 :: BytesPerValue Uint16 D2 -instance bytesPerValueUint8 :: BytesPerValue Uint8 D1 -instance bytesPerValueInt32 :: BytesPerValue Int32 D4 -instance bytesPerValueInt16 :: BytesPerValue Int16 D2 -instance bytesPerValueInt8 :: BytesPerValue Int8 D1 -instance bytesPerValueFloat32 :: BytesPerValue Float32 D4 -instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 - - --- | Maps a `TypedArray`'s binary casted value, to its computable representation in JavaScript. +import Data.UInt (UInt) +import Type.Proxy (Proxy) + +-- | Map of each `ArrayViewType` to the number of bytes of storage it requires. +class BytesPerType (a :: ArrayViewType) where + byteWidth :: (Proxy a) -> Int + +instance bytesPerTypeInt8 :: BytesPerType Int8 where byteWidth _ = 1 +instance bytesPerTypeInt16 :: BytesPerType Int16 where byteWidth _ = 2 +instance bytesPerTypeInt32 :: BytesPerType Int32 where byteWidth _ = 4 +instance bytesPerTypeUint8 :: BytesPerType Uint8 where byteWidth _ = 1 +instance bytesPerTypeUint16 :: BytesPerType Uint16 where byteWidth _ = 2 +instance bytesPerTypeUint32 :: BytesPerType Uint32 where byteWidth _ = 4 +instance bytesPerTypeUint8Clamped :: BytesPerType Uint8Clamped where byteWidth _ = 1 +instance bytesPerTypeFloat32 :: BytesPerType Float32 where byteWidth _ = 4 +instance bytesPerTypeFloat64 :: BytesPerType Float64 where byteWidth _ = 8 + +-- | Maps a `TypedArray`’s binary casted value to its computable representation in JavaScript. class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt @@ -36,8 +40,6 @@ instance binaryValueInt8 :: BinaryValue Int8 Int instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number - - class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped" instance showArrayViewTypeViewUint32 :: ShowArrayViewType Uint32 "Uint32" diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index 58cde67..a981bf7 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -1,6 +1,5 @@ module Test.Properties.DataView where - import Prelude import Data.Array.Partial (head) as Array @@ -8,21 +7,19 @@ import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genUint16, genUint32, genUint8) import Data.ArrayBuffer.Types (Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue, class ShowArrayViewType, class BinaryValue) -import Data.Maybe (Maybe(..)) -import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) -import Data.UInt (UInt) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, class ShowArrayViewType) import Data.Float32 (Float32) as F --- import Data.Vec (head) as Vec +import Data.Maybe (Maybe(..)) import Data.Symbol (class IsSymbol) +import Data.UInt (UInt) import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) -import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) import Partial.Unsafe (unsafePartial) - +import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) +import Type.Proxy (Proxy(..)) dataViewTests :: Ref Int -> Effect Unit @@ -33,79 +30,79 @@ dataViewTests count = do placingAValueIsThereTests DV.LE count -type TestableViewF a name b n t q = +type TestableViewF a name t q = Show t => Eq t => Ord t => Semiring t - => BytesPerValue a b + => BytesPerType a => BinaryValue a t => ShowArrayViewType a name => IsSymbol name - => Nat b - => WithOffsetAndValue n a t + => WithOffsetAndValue a t -> q -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a name b t. TestableViewF a name b n t q) -> Effect Unit +overAll :: forall q . Testable q + => Ref Int -> (forall a name t. TestableViewF a name t q) -> Effect Unit overAll count f = do void (Ref.modify (_ + 1) count) log " - Uint32" quickCheckGen $ - let f' :: TestableViewF Uint32 "Uint32" D4 n UInt q + let f' :: TestableViewF Uint32 "Uint32" UInt q f' = f - in f' <$> genWithOffsetAndValue genDataView genUint32 + in f' <$> genWithOffsetAndValue 4 genDataView genUint32 log " - Uint16" quickCheckGen $ - let f' :: TestableViewF Uint16 "Uint16" D2 n UInt q + let f' :: TestableViewF Uint16 "Uint16" UInt q f' = f - in f' <$> genWithOffsetAndValue genDataView genUint16 + in f' <$> genWithOffsetAndValue 2 genDataView genUint16 log " - Uint8" quickCheckGen $ - let f' :: TestableViewF Uint8 "Uint8" D1 n UInt q + let f' :: TestableViewF Uint8 "Uint8" UInt q f' = f - in f' <$> genWithOffsetAndValue genDataView genUint8 + in f' <$> genWithOffsetAndValue 1 genDataView genUint8 log " - Int32" quickCheckGen $ - let f' :: TestableViewF Int32 "Int32" D4 n Int q + let f' :: TestableViewF Int32 "Int32" Int q f' = f - in f' <$> genWithOffsetAndValue genDataView genInt32 + in f' <$> genWithOffsetAndValue 4 genDataView genInt32 log " - Int16" quickCheckGen $ - let f' :: TestableViewF Int16 "Int16" D2 n Int q + let f' :: TestableViewF Int16 "Int16" Int q f' = f - in f' <$> genWithOffsetAndValue genDataView genInt16 + in f' <$> genWithOffsetAndValue 2 genDataView genInt16 log " - Int8" quickCheckGen $ - let f' :: TestableViewF Int8 "Int8" D1 n Int q + let f' :: TestableViewF Int8 "Int8" Int q f' = f - in f' <$> genWithOffsetAndValue genDataView genInt8 + in f' <$> genWithOffsetAndValue 1 genDataView genInt8 log " - Float32" quickCheckGen $ - let f' :: TestableViewF Float32 "Float32" D4 n F.Float32 q + let f' :: TestableViewF Float32 "Float32" F.Float32 q f' = f - in f' <$> genWithOffsetAndValue genDataView genFloat32 + in f' <$> genWithOffsetAndValue 4 genDataView genFloat32 log " - Float64" quickCheckGen $ - let f' :: TestableViewF Float64 "Float64" D8 n Number q + let f' :: TestableViewF Float64 "Float64" Number q f' = f - in f' <$> genWithOffsetAndValue genDataView genFloat64 + in f' <$> genWithOffsetAndValue 8 genDataView genFloat64 placingAValueIsThereTests :: DV.Endian -> Ref Int -> Effect Unit placingAValueIsThereTests endian count = overAll count placingAValueIsThere where - placingAValueIsThere :: forall a name b t. TestableViewF a name b D1 t Result + placingAValueIsThere :: forall a name t. TestableViewF a name t Result placingAValueIsThere (WithOffsetAndValue os t xs) = let o = unsafePartial $ Array.head os - prx = DV.AProxy :: DV.AProxy a + prx = Proxy :: Proxy a in unsafePerformEffect do _ <- DV.set endian prx xs o t my <- DV.get endian prx xs o diff --git a/test/Properties/Typed/Laws.purs b/test/Properties/Typed/Laws.purs index 8a6f02b..5f7d5c5 100644 --- a/test/Properties/Typed/Laws.purs +++ b/test/Properties/Typed/Laws.purs @@ -3,7 +3,7 @@ module Test.Properties.Typed.Laws where import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8) import Data.ArrayBuffer.Typed.Unsafe (AV(..)) -import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, kind ArrayViewType) +import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, ArrayViewType) import Data.Float32 as F import Data.UInt (UInt) import Effect (Effect) diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 449f1a1..9b054c0 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -9,12 +9,11 @@ import Data.Array.Partial (head) as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Typed.Gen (WithIndices(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithIndices) -import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint8Array, Uint8ClampedArray, Uint32Array) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) +import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray) +import Data.ArrayBuffer.ValueMapping (class BytesPerType, byteWidth) import Data.Maybe (Maybe(..), fromMaybe, isJust) import Data.Traversable (traverse) -import Data.Typelevel.Num (class Nat, D0, D1, D2, D5, toInt') --- import Data.Vec (head, index) as Vec +import Data.Tuple (Tuple(..)) import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) @@ -22,11 +21,9 @@ import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Testable, Result(..), quickCheckGen, (/==), (), (===)) -import Test.QuickCheck.Combinators ((==>), (|=|)) import Test.QuickCheck.Gen (Gen) import Type.Proxy (Proxy(..)) - typedArrayTests :: Ref Int -> Effect Unit typedArrayTests count = do log " - subarrayBehavesLikeArraySlice" @@ -111,51 +108,66 @@ typedArrayTests count = do copyWithinViaSetTypedTests count - -type TestableArrayF a b n t q = +type TestableArrayF a t q = Show t => Eq t => Ord t => Semiring t => TypedArray a t - => BytesPerValue a b - => Nat b - => WithIndices n a + => BytesPerType a + => WithIndices a -> Effect q - -overAll' :: forall q n. Testable q => Nat n => Int -> Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll' mn count f = do +overAll' + :: forall q. Testable q + => Int -- n + -> Int -- “minimum n”? + -> Ref Int -> (forall a t. TestableArrayF a t q) -> Effect Unit +overAll' n mn count f = do void (Ref.modify (_ + 1) count) - let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit - chk s _ gen = do + let chk + :: forall a t + . Show t + => Eq t + => Ord t + => Semiring t + => BytesPerType a + => TypedArray a t + => String + -> Int + -> Proxy (ArrayView a) + -> Gen t + -> Effect Unit + chk s n' _ gen = do log $ " - " <> s - quickCheckGen $ unsafePerformEffect <<< f <$> genWithIndices arr + quickCheckGen $ unsafePerformEffect <<< f <$> genWithIndices n' arr where arr :: Gen (ArrayView a) arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs - chk "Uint8ClampedArray" (Proxy :: Proxy Uint8ClampedArray) genUint8 - chk "Uint32Array" (Proxy :: Proxy Uint32Array) genUint32 - chk "Uint16Array" (Proxy :: Proxy Uint16Array) genUint16 - chk "Uint8Array" (Proxy :: Proxy Uint8Array) genUint8 - chk "Int32Array" (Proxy :: Proxy Int32Array) genInt32 - chk "Int16Array" (Proxy :: Proxy Int16Array) genInt16 - chk "Int8Array" (Proxy :: Proxy Int8Array) genInt8 - chk "Float32Array" (Proxy :: Proxy Float32Array) genFloat32 - chk "Float64Array" (Proxy :: Proxy Float64Array) genFloat64 + chk "Uint8ClampedArray" n (Proxy :: Proxy Uint8ClampedArray) genUint8 + chk "Uint32Array" n (Proxy :: Proxy Uint32Array) genUint32 + chk "Uint16Array" n (Proxy :: Proxy Uint16Array) genUint16 + chk "Uint8Array" n (Proxy :: Proxy Uint8Array) genUint8 + chk "Int32Array" n (Proxy :: Proxy Int32Array) genInt32 + chk "Int16Array" n (Proxy :: Proxy Int16Array) genInt16 + chk "Int8Array" n (Proxy :: Proxy Int8Array) genInt8 + chk "Float32Array" n (Proxy :: Proxy Float32Array) genFloat32 + chk "Float64Array" n (Proxy :: Proxy Float64Array) genFloat64 -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll count f = overAll' 0 count f +overAll :: forall q. Testable q + => Int -> Ref Int -> (forall a t. TestableArrayF a t q) -> Effect Unit +overAll n count f = overAll' n 0 count f -overAll1 :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll1 count f = overAll' 1 count f +overAll1 :: forall q. Testable q + => Int -> Ref Int -> (forall a t. TestableArrayF a t q) -> Effect Unit +overAll1 n count f = overAll' n 1 count f subarrayBehavesLikeArraySliceTests :: Ref Int -> Effect Unit -subarrayBehavesLikeArraySliceTests count = overAll count f +subarrayBehavesLikeArraySliceTests count = overAll 2 count f where - f :: forall a b t. TestableArrayF a b D2 t Result + f :: forall a t. TestableArrayF a t Result f (WithIndices os xs) = do let s = unsafePartial $ os `Array.unsafeIndex` 0 e = unsafePartial $ os `Array.unsafeIndex` 1 @@ -165,9 +177,9 @@ subarrayBehavesLikeArraySliceTests count = overAll count f pure $ Array.slice s e axs === a sliceBehavesLikeArraySliceTests :: Ref Int -> Effect Unit -sliceBehavesLikeArraySliceTests count = overAll count f +sliceBehavesLikeArraySliceTests count = overAll 2 count f where - f :: forall a b t. TestableArrayF a b D2 t Result + f :: forall a t. TestableArrayF a t Result f (WithIndices os xs) = do let s = unsafePartial $ os `Array.unsafeIndex` 0 e = unsafePartial $ os `Array.unsafeIndex` 1 @@ -177,9 +189,9 @@ sliceBehavesLikeArraySliceTests count = overAll count f pure $ Array.slice s e axs === a partBehavesLikeTakeDropTests :: Ref Int -> Effect Unit -partBehavesLikeTakeDropTests count = overAll count f +partBehavesLikeTakeDropTests count = overAll 0 count f where - f :: forall a b t. TestableArrayF a b D0 t Result + f :: forall a t. TestableArrayF a t Result f (WithIndices _ xs) = do let n = 2 axs <- TA.toArray xs @@ -188,17 +200,17 @@ partBehavesLikeTakeDropTests count = overAll count f pure $ Array.take n (Array.drop n axs) === aps byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit -byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValueEqLength +byteLengthDivBytesPerValueTests count = overAll 0 count byteLengthDivBytesPerValueEqLength where - byteLengthDivBytesPerValueEqLength :: forall a b t. TestableArrayF a b D0 t Result - byteLengthDivBytesPerValueEqLength (WithIndices _ a) = - let b = toInt' (Proxy :: Proxy b) - in pure $ TA.length a === (TA.byteLength a `div` b) + byteLengthDivBytesPerValueEqLength :: forall a t. TestableArrayF a t Result + byteLengthDivBytesPerValueEqLength (WithIndices _ xs) = + let b = byteWidth (Proxy :: Proxy a) + in pure $ TA.length xs === (TA.byteLength xs `div` b) fromArrayToArrayIsoTests :: Ref Int -> Effect Unit -fromArrayToArrayIsoTests count = overAll count fromArrayToArrayIso +fromArrayToArrayIsoTests count = overAll 0 count fromArrayToArrayIso where - fromArrayToArrayIso :: forall a b t. TestableArrayF a b D0 t Result + fromArrayToArrayIso :: forall a t. TestableArrayF a t Result fromArrayToArrayIso (WithIndices _ xs) = do axs <- TA.toArray xs xs' <- TA.fromArray axs :: Effect (ArrayView a) @@ -207,9 +219,9 @@ fromArrayToArrayIsoTests count = overAll count fromArrayToArrayIso allAreFilledTests :: Ref Int -> Effect Unit -allAreFilledTests count = overAll count allAreFilled +allAreFilledTests count = overAll 0 count allAreFilled where - allAreFilled :: forall a b t. TestableArrayF a b D0 t Result + allAreFilled :: forall a t. TestableArrayF a t Result allAreFilled (WithIndices _ xs) = do e <- TA.at xs 0 let x = fromMaybe zero e @@ -220,9 +232,9 @@ allAreFilledTests count = overAll count allAreFilled setSingletonIsEqTests :: Ref Int -> Effect Unit -setSingletonIsEqTests count = overAll count setSingletonIsEq +setSingletonIsEqTests count = overAll 1 count setSingletonIsEq where - setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result + setSingletonIsEq :: forall a t. TestableArrayF a t Result setSingletonIsEq (WithIndices os xs) = do e <- TA.at xs 0 case e of @@ -236,23 +248,23 @@ setSingletonIsEqTests count = overAll count setSingletonIsEq -- | Should work with any arbitrary predicate, but we can't generate them allImpliesAnyTests :: Ref Int -> Effect Unit -allImpliesAnyTests count = overAll count allImpliesAny +allImpliesAnyTests count = overAll 0 count allImpliesAny where - allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result + allImpliesAny :: forall a t. TestableArrayF a t Result allImpliesAny (WithIndices _ xs) = do let pred x = x /= zero all'' <- TA.all pred xs let all' = all'' "All don't satisfy the predicate" any'' <- TA.any pred xs let any' = any'' "None satisfy the predicate" - pure $ (TA.length xs === zero) |=| all' ==> any' + pure $ (TA.length xs === zero) `xor` all' `implies` any' -- | Should work with any arbitrary predicate, but we can't generate them filterImpliesAllTests :: Ref Int -> Effect Unit -filterImpliesAllTests count = overAll count filterImpliesAll +filterImpliesAllTests count = overAll 0 count filterImpliesAll where - filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result + filterImpliesAll :: forall a t. TestableArrayF a t Result filterImpliesAll (WithIndices _ xs) = do let pred x = x /= zero ys <- TA.filter pred xs @@ -262,9 +274,9 @@ filterImpliesAllTests count = overAll count filterImpliesAll -- | Should work with any arbitrary predicate, but we can't generate them filterIsTotalTests :: Ref Int -> Effect Unit -filterIsTotalTests count = overAll count filterIsTotal +filterIsTotalTests count = overAll 0 count filterIsTotal where - filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result + filterIsTotal :: forall a t. TestableArrayF a t Result filterIsTotal (WithIndices _ xs) = do let pred x = x /= zero ys <- TA.filter pred xs @@ -275,9 +287,9 @@ filterIsTotalTests count = overAll count filterIsTotal -- | Should work with any arbitrary predicate, but we can't generate them filterIsIdempotentTests :: Ref Int -> Effect Unit -filterIsIdempotentTests count = overAll count filterIsIdempotent +filterIsIdempotentTests count = overAll 0 count filterIsIdempotent where - filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result + filterIsIdempotent :: forall a t. TestableArrayF a t Result filterIsIdempotent (WithIndices _ xs) = do let pred x = x /= zero ys <- TA.filter pred xs @@ -288,17 +300,17 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent withIndicesHasIndexTests :: Ref Int -> Effect Unit -withIndicesHasIndexTests count = overAll1 count withIndicesHasIndex +withIndicesHasIndexTests count = overAll1 5 count withIndicesHasIndex where - withIndicesHasIndex :: forall a b t. TestableArrayF a b D5 t Result + withIndicesHasIndex :: forall a t. TestableArrayF a t Result withIndicesHasIndex (WithIndices os xs) = pure $ Array.all (TA.hasIndex xs) os "All doesn't have index of itself" withIndicesElemTests :: Ref Int -> Effect Unit -withIndicesElemTests count = overAll1 count withIndicesElem +withIndicesElemTests count = overAll1 5 count withIndicesElem where - withIndicesElem :: forall a b t. TestableArrayF a b D5 t Result + withIndicesElem :: forall a t. TestableArrayF a t Result withIndicesElem (WithIndices os xs) = do let fetch o = TA.at xs o exs <- traverse fetch os @@ -307,9 +319,9 @@ withIndicesElemTests count = overAll1 count withIndicesElem -- | Should work with any arbitrary predicate, but we can't generate them anyImpliesFindTests :: Ref Int -> Effect Unit -anyImpliesFindTests count = overAll count anyImpliesFind +anyImpliesFindTests count = overAll 0 count anyImpliesFind where - anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result + anyImpliesFind :: forall a t. TestableArrayF a t Result anyImpliesFind (WithIndices _ xs) = do let pred x = x /= zero a <- TA.any pred xs @@ -320,14 +332,14 @@ anyImpliesFindTests count = overAll count anyImpliesFind Just z -> if pred z then Success else Failed "Found value doesn't satisfy the predicate" - pure $ p ==> q + pure $ p `implies` q -- | Should work with any arbitrary predicate, but we can't generate them findIndexImpliesAtTests :: Ref Int -> Effect Unit -findIndexImpliesAtTests count = overAll count findIndexImpliesAt +findIndexImpliesAtTests count = overAll 0 count findIndexImpliesAt where - findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result + findIndexImpliesAt :: forall a t. TestableArrayF a t Result findIndexImpliesAt (WithIndices _ xs) = do let pred x _ = x /= zero mo <- TA.findIndex pred xs @@ -342,9 +354,9 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt indexOfImpliesAtTests :: Ref Int -> Effect Unit -indexOfImpliesAtTests count = overAll count indexOfImpliesAt +indexOfImpliesAtTests count = overAll 1 count indexOfImpliesAt where - indexOfImpliesAt :: forall a b t. TestableArrayF a b D1 t Result + indexOfImpliesAt :: forall a t. TestableArrayF a t Result indexOfImpliesAt (WithIndices _ xs) = do e <- TA.at xs 0 case e of @@ -359,9 +371,9 @@ indexOfImpliesAtTests count = overAll count indexOfImpliesAt lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit -lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt +lastIndexOfImpliesAtTests count = overAll 0 count lastIndexOfImpliesAt where - lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result + lastIndexOfImpliesAt :: forall a t. TestableArrayF a t Result lastIndexOfImpliesAt (WithIndices _ xs) = do e <- TA.at xs 0 case e of @@ -376,9 +388,9 @@ lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt foldrConsIsToArrayTests :: Ref Int -> Effect Unit -foldrConsIsToArrayTests count = overAll count foldrConsIsToArray +foldrConsIsToArrayTests count = overAll 0 count foldrConsIsToArray where - foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result + foldrConsIsToArray :: forall a t. TestableArrayF a t Result foldrConsIsToArray (WithIndices _ xs) = do axs <- TA.toArray xs rxs <- TA.foldr Array.cons [] xs @@ -386,9 +398,9 @@ foldrConsIsToArrayTests count = overAll count foldrConsIsToArray foldlSnocIsToArrayTests :: Ref Int -> Effect Unit -foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray +foldlSnocIsToArrayTests count = overAll 0 count foldlSnocIsToArray where - foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result + foldlSnocIsToArray :: forall a t. TestableArrayF a t Result foldlSnocIsToArray (WithIndices _ xs) = do axs <- TA.toArray xs rxs <- TA.foldl Array.snoc [] xs @@ -396,9 +408,9 @@ foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray mapIdentityIsIdentityTests :: Ref Int -> Effect Unit -mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity +mapIdentityIsIdentityTests count = overAll 0 count mapIdentityIsIdentity where - mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + mapIdentityIsIdentity :: forall a t. TestableArrayF a t Result mapIdentityIsIdentity (WithIndices _ xs) = do axs <- TA.toArray xs mxs <- TA.toArray (TA.map identity xs) @@ -406,9 +418,9 @@ mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity traverseSnocIsToArrayTests :: Ref Int -> Effect Unit -traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray +traverseSnocIsToArrayTests count = overAll 0 count traverseSnocIsToArray where - traverseSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result + traverseSnocIsToArray :: forall a t. TestableArrayF a t Result traverseSnocIsToArray (WithIndices _ xs) = do ref <- Ref.new [] TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs @@ -418,9 +430,9 @@ traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray doubleReverseIsIdentityTests :: Ref Int -> Effect Unit -doubleReverseIsIdentityTests count = overAll count doubleReverseIsIdentity +doubleReverseIsIdentityTests count = overAll 0 count doubleReverseIsIdentity where - doubleReverseIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + doubleReverseIsIdentity :: forall a t. TestableArrayF a t Result doubleReverseIsIdentity (WithIndices _ xs) = do axs <- TA.toArray xs TA.reverse xs @@ -430,9 +442,9 @@ doubleReverseIsIdentityTests count = overAll count doubleReverseIsIdentity reverseIsArrayReverseTests :: Ref Int -> Effect Unit -reverseIsArrayReverseTests count = overAll count reverseIsArrayReverse +reverseIsArrayReverseTests count = overAll 0 count reverseIsArrayReverse where - reverseIsArrayReverse :: forall a b t. TestableArrayF a b D0 t Result + reverseIsArrayReverse :: forall a t. TestableArrayF a t Result reverseIsArrayReverse (WithIndices _ xs) = do axs <- TA.toArray xs TA.reverse xs @@ -441,9 +453,9 @@ reverseIsArrayReverseTests count = overAll count reverseIsArrayReverse sortIsIdempotentTests :: Ref Int -> Effect Unit -sortIsIdempotentTests count = overAll count sortIsIdempotent +sortIsIdempotentTests count = overAll 0 count sortIsIdempotent where - sortIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result + sortIsIdempotent :: forall a t. TestableArrayF a t Result sortIsIdempotent (WithIndices _ xs) = do TA.sort xs ys <- TA.toArray xs @@ -453,9 +465,9 @@ sortIsIdempotentTests count = overAll count sortIsIdempotent sortIsArraySortTests :: Ref Int -> Effect Unit -sortIsArraySortTests count = overAll count sortIsArraySort +sortIsArraySortTests count = overAll 0 count sortIsArraySort where - sortIsArraySort :: forall a b t. TestableArrayF a b D0 t Result + sortIsArraySort :: forall a t. TestableArrayF a t Result sortIsArraySort (WithIndices _ xs) = do axs <- TA.toArray xs let ys = Array.sort axs @@ -465,9 +477,9 @@ sortIsArraySortTests count = overAll count sortIsArraySort toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit -toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma +toStringIsJoinWithCommaTests count = overAll 0 count toStringIsJoinWithComma where - toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result + toStringIsJoinWithComma :: forall a t. TestableArrayF a t Result toStringIsJoinWithComma (WithIndices _ xs) = do s1 <- TA.join "," xs s2 <- TA.toString xs @@ -475,9 +487,9 @@ toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit -setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIdentity +setTypedOfSubArrayIsIdentityTests count = overAll 0 count setTypedOfSubArrayIsIdentity where - setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + setTypedOfSubArrayIsIdentity :: forall a t. TestableArrayF a t Result setTypedOfSubArrayIsIdentity (WithIndices _ xs) = do ys <- TA.toArray xs let l = TA.length xs @@ -488,9 +500,9 @@ setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIden modifyingOriginalMutatesSubArrayTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMutatesSubArray +modifyingOriginalMutatesSubArrayTests count = overAll 0 count modifyingOriginalMutatesSubArray where - modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result modifyingOriginalMutatesSubArray (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -505,9 +517,9 @@ modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMut modifyingSubArrayMutatesOriginalTests :: Ref Int -> Effect Unit -modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMutatesSubArray +modifyingSubArrayMutatesOriginalTests count = overAll 0 count modifyingOriginalMutatesSubArray where - modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result modifyingOriginalMutatesSubArray (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -522,9 +534,9 @@ modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMut modifyingOriginalMutatesSubArrayZeroTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOriginalMutatesSubArrayZero +modifyingOriginalMutatesSubArrayZeroTests count = overAll 0 count modifyingOriginalMutatesSubArrayZero where - modifyingOriginalMutatesSubArrayZero :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArrayZero :: forall a t. TestableArrayF a t Result modifyingOriginalMutatesSubArrayZero (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -539,9 +551,9 @@ modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOrigina modifyingSubArrayMutatesOriginalZeroTests :: Ref Int -> Effect Unit -modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArrayMutatesOriginalZero +modifyingSubArrayMutatesOriginalZeroTests count = overAll 0 count modifyingSubArrayMutatesOriginalZero where - modifyingSubArrayMutatesOriginalZero :: forall a b t. TestableArrayF a b D0 t Result + modifyingSubArrayMutatesOriginalZero :: forall a t. TestableArrayF a t Result modifyingSubArrayMutatesOriginalZero (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -556,9 +568,9 @@ modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArra modifyingOriginalMutatesSubArrayAllTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginalMutatesSubArrayAll +modifyingOriginalMutatesSubArrayAllTests count = overAll 0 count modifyingOriginalMutatesSubArrayAll where - modifyingOriginalMutatesSubArrayAll :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalMutatesSubArrayAll :: forall a t. TestableArrayF a t Result modifyingOriginalMutatesSubArrayAll (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -573,9 +585,9 @@ modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginal modifyingSubArrayMutatesOriginalAllTests :: Ref Int -> Effect Unit -modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArrayMutatesOriginalAll +modifyingSubArrayMutatesOriginalAllTests count = overAll 0 count modifyingSubArrayMutatesOriginalAll where - modifyingSubArrayMutatesOriginalAll :: forall a b t. TestableArrayF a b D0 t Result + modifyingSubArrayMutatesOriginalAll :: forall a t. TestableArrayF a t Result modifyingSubArrayMutatesOriginalAll (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -590,9 +602,9 @@ modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArray modifyingOriginalMutatesSubArrayPartTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayPartTests count = overAll count modifyingOriginalMutatesSubArrayPart +modifyingOriginalMutatesSubArrayPartTests count = overAll 1 count modifyingOriginalMutatesSubArrayPart where - modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalMutatesSubArrayPart :: forall a t. TestableArrayF a t Result modifyingOriginalMutatesSubArrayPart (WithIndices os xs) = do let o = unsafePartial $ Array.head os l = TA.length xs @@ -607,9 +619,9 @@ modifyingOriginalMutatesSubArrayPartTests count = overAll count modifyingOrigina modifyingOriginalDoesntMutateSliceTests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalDoesntMutateSlice +modifyingOriginalDoesntMutateSliceTests count = overAll 0 count modifyingOriginalDoesntMutateSlice where - modifyingOriginalDoesntMutateSlice :: forall a b t. TestableArrayF a b D0 t Result + modifyingOriginalDoesntMutateSlice :: forall a t. TestableArrayF a t Result modifyingOriginalDoesntMutateSlice (WithIndices _ xs) = do axs <- TA.toArray xs if Array.all (eq zero) axs @@ -624,9 +636,9 @@ modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalD modifyingOriginalDoesntMutateSlicePartTests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOriginalDoesntMutateSlicePart +modifyingOriginalDoesntMutateSlicePartTests count = overAll 1 count modifyingOriginalDoesntMutateSlicePart where - modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalDoesntMutateSlicePart :: forall a t. TestableArrayF a t Result modifyingOriginalDoesntMutateSlicePart (WithIndices os xs) = do let l = TA.length xs axs <- TA.toArray =<< TA.slice 0 l xs @@ -643,9 +655,9 @@ modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOrigi modifyingOriginalDoesntMutateSlicePart2Tests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOriginalDoesntMutateSlicePart2 +modifyingOriginalDoesntMutateSlicePart2Tests count = overAll 1 count modifyingOriginalDoesntMutateSlicePart2 where - modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result + modifyingOriginalDoesntMutateSlicePart2 :: forall a t. TestableArrayF a t Result modifyingOriginalDoesntMutateSlicePart2 (WithIndices os xs) = do let o = unsafePartial $ Array.head os l = TA.length xs @@ -662,9 +674,9 @@ modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOrig copyWithinSelfIsIdentityTests :: Ref Int -> Effect Unit -copyWithinSelfIsIdentityTests count = overAll count copyWithinSelfIsIdentity +copyWithinSelfIsIdentityTests count = overAll 0 count copyWithinSelfIsIdentity where - copyWithinSelfIsIdentity :: forall a b t. TestableArrayF a b D0 t Result + copyWithinSelfIsIdentity :: forall a t. TestableArrayF a t Result copyWithinSelfIsIdentity (WithIndices _ xs) = do ys <- TA.toArray xs TA.copyWithin xs 0 0 (Just (TA.length xs)) @@ -673,9 +685,9 @@ copyWithinSelfIsIdentityTests count = overAll count copyWithinSelfIsIdentity copyWithinIsSliceTests :: Ref Int -> Effect Unit -copyWithinIsSliceTests count = overAll count copyWithinIsSlice +copyWithinIsSliceTests count = overAll 1 count copyWithinIsSlice where - copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result + copyWithinIsSlice :: forall a t. TestableArrayF a t Result copyWithinIsSlice (WithIndices os xs) = do let o = unsafePartial $ Array.head os l = TA.length xs @@ -687,9 +699,9 @@ copyWithinIsSliceTests count = overAll count copyWithinIsSlice copyWithinViaSetTypedTests :: Ref Int -> Effect Unit -copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped +copyWithinViaSetTypedTests count = overAll 1 count copyWithinViaSetTyped where - copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result + copyWithinViaSetTyped :: forall a t. TestableArrayF a t Result copyWithinViaSetTyped (WithIndices os xs) = do let o = unsafePartial $ Array.head os txs <- TA.toArray xs @@ -701,3 +713,27 @@ copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped axs <- TA.toArray xs axs' <- TA.toArray xs' pure $ axs === axs' + +-- | Uses the second failure message as the result failure message +-- | https://github.com/athanclark/purescript-quickcheck-combinators/blob/293e5af07ae47b61d4eae5defef4c0f472bfa9ca/src/Test/QuickCheck/Combinators.purs#L62 +implies :: Result -> Result -> Result +implies x y = case y of + Failed y' -> case x of + Success -> Failed ("Implied failure: " <> y') + _ -> Success + _ -> Success + +-- | Combine two results with "Exclusive Or" logic, and with `", xor "` as the failure message separator, and "XOR" as the failure message if they are both `Success` +-- | https://github.com/athanclark/purescript-quickcheck-combinators/blob/293e5af07ae47b61d4eae5defef4c0f472bfa9ca/src/Test/QuickCheck/Combinators.purs#L44 +xor :: Result -> Result -> Result +xor = xor' ", xor " "XOR" + where + -- Combine two results with "Exclusive Or" logic, and with a failure message separator and failure message if they are both `Success` + xor' :: String -- ^ Separator + -> String -- ^ Success failure message + -> Result -> Result -> Result + xor' m s x y = case Tuple x y of + Tuple (Failed x') (Failed y') -> Failed (x' <> m <> y') + Tuple Success Success -> Failed s + Tuple Success y' -> y' + Tuple x' Success -> x' From 051d43b6c2476a22f91097f6fb2bb9f7da0417e8 Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 21 Jun 2021 20:12:20 +0900 Subject: [PATCH 204/236] Run contrib-updater --- .editorconfig | 13 +++++ .github/ISSUE_TEMPLATE/bug-report.md | 19 +++++++ .github/ISSUE_TEMPLATE/change-request.md | 21 ++++++++ .github/ISSUE_TEMPLATE/config.yml | 8 +++ .github/PULL_REQUEST_TEMPLATE.md | 11 ++++ .github/workflows/ci.yml | 34 ++++++++++++ .gitignore | 19 +++---- CHANGELOG.md | 15 ++++-- CONTRIBUTING.md | 5 ++ README.md | 69 +++++++++++++++++++++--- docs/README.md | 3 ++ 11 files changed, 194 insertions(+), 23 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/ISSUE_TEMPLATE/bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/change-request.md create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/ci.yml create mode 100644 CONTRIBUTING.md create mode 100644 docs/README.md diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7c68b07 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# https://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md new file mode 100644 index 0000000..b79b995 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -0,0 +1,19 @@ +--- +name: Bug report +about: Report an issue +title: "" +labels: bug +assignees: "" +--- + +**Describe the bug** +A clear and concise description of the bug. + +**To Reproduce** +A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue. + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/change-request.md b/.github/ISSUE_TEMPLATE/change-request.md new file mode 100644 index 0000000..a2ee685 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/change-request.md @@ -0,0 +1,21 @@ +--- +name: Change request +about: Propose an improvement to this library +title: "" +labels: "" +assignees: "" +--- + +**Is your change request related to a problem? Please describe.** +A clear and concise description of the problem. + +Examples: + +- It's frustrating to have to [...] +- I was looking for a function to [...] + +**Describe the solution you'd like** +A clear and concise description of what a good solution to you looks like, including any solutions you've already considered. + +**Additional context** +Add any other context about the change request here. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..c47a263 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: PureScript Discourse + url: https://discourse.purescript.org/ + about: Ask and answer questions here. + - name: Functional Programming Slack + url: https://functionalprogramming.slack.com + about: For casual chat and questions (use https://fpchat-invite.herokuapp.com to join). diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..d8780f7 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,11 @@ +**Description of the change** +Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR. + +--- + +**Checklist:** + +- [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username +- [ ] Linked any existing issues or proposals that this pull request should close +- [ ] Updated or added relevant documentation in the README and/or documentation directory +- [ ] Added a test for the contribution (if applicable) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6b0550f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,34 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Set up a PureScript toolchain + uses: purescript-contrib/setup-purescript@main + + - name: Cache PureScript dependencies + uses: actions/cache@v2 + with: + key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }} + path: | + .spago + output + + - name: Install dependencies + run: spago install + + - name: Build source + run: spago build --no-install --purs-args '--censor-lib --strict' + + - name: Run tests + run: spago test --no-install diff --git a/.gitignore b/.gitignore index f71c254..7bca306 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,8 @@ -*~ -output/ -bower_components/ -node_modules/ -.psci -.psci_modules/ -yarn-error.log -yarn.lock -generated-docs/ -.spago/ -.psc-ide-port +.* +!.gitignore +!.github +!.editorconfig + +output +generated-docs +bower_components diff --git a/CHANGELOG.md b/CHANGELOG.md index ec49320..d7eea4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -# v11 2021-06-21 +# Changelog + +Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## v11.0.0 [Unreleased] Jorge Acereda has graciously donated this package to __purescript-contrib__. For version 11, we have refactored this library so that it depends only on @@ -11,12 +15,13 @@ We have removed the dependencies on these non-__purescript-contrib__ packages: * https://pursuit.purescript.org/packages/purescript-typelevel * https://pursuit.purescript.org/packages/purescript-quickcheck-combinators -To upgrade to v11, you might need to do a few simple substitutions +In v11.0.0 of this package, we have also upgraded to PureScript v0.14. + +### Breaking Changes + +To upgrade to v11.0.0, you might need to do a few substitutions to the type declarations in your own dependent code: * Replace the type `AProxy` with `Proxy` from the Prelude. * Remove most of the `Nat` typeclass constraints. https://github.com/purescript-contrib/purescript-arraybuffer/issues/29 * Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. - -In v11 of this package, we have also upgraded to PureScript v0.14. - diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..bff4e66 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,5 @@ +# Contributing to Arraybuffer + +Thanks for your interest in contributing to `arraybuffer`! We welcome new contributions regardless of your level of experience or familiarity with PureScript. + +Every library in the Contributors organization shares a simple handbook that helps new contributors get started. With that in mind, please [read the short contributing guide on purescript-contrib/governance](https://github.com/purescript-contrib/governance/blob/main/contributing.md) before contributing to this library. diff --git a/README.md b/README.md index e9efeb9..8f2e831 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,77 @@ -# purescript-arraybuffer +# arraybuffer + [![CI](https://github.com/purescript-contrib/purescript-arraybuffer/workflows/CI/badge.svg?branch=main)](https://github.com/purescript-contrib/purescript-arraybuffer/actions?query=workflow%3ACI+branch%3Amain) [![Release](https://img.shields.io/github/release/purescript-contrib/purescript-arraybuffer.svg)](https://github.com/purescript-contrib/purescript-arraybuffer/releases) [![Pursuit](https://pursuit.purescript.org/packages/purescript-arraybuffer/badge)](https://pursuit.purescript.org/packages/purescript-arraybuffer) -[![Maintainer: natefaubion](https://img.shields.io/badge/maintainer-jamesdbrock-teal.svg)](https://github.com/jamesdbrock) +[![Maintainer: jacereda](https://img.shields.io/badge/maintainer-jacereda-teal.svg)](https://github.com/jacereda) +[![Maintainer: jamesdbrock](https://img.shields.io/badge/maintainer-jamesdbrock-teal.svg)](https://github.com/jamesdbrock) + + +Bindings and implementation for mutable JavaScript `ArrayBuffer`s. -ArrayBuffer bindings for PureScript. +An `ArrayBuffer` is a built-in JavaScript object for storage of a flat continuous +region of memory. +The `Typed` module provides a view into an `ArrayBuffer` for array +access of aligned local-machine-endian types, for in-process flat memory operations. + +The `DataView` module provides a view into an `ArrayBuffer` for inter-process +flat memory operations. + +* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) +* [ECMA-262](https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybuffer-objects) ## Installation Install `arraybuffer` with [Spago](https://github.com/purescript/spago): -``` -spago install purescript-arraybuffer + +```sh +spago install arraybuffer ``` ## Documentation -Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). +`arraybuffer` documentation is stored in a few places: + +1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-arraybuffer). +2. Written documentation is kept in the [docs directory](./docs). +3. Usage examples can be found in [the test suite](./test). + +If you get stuck, there are several ways to get help: + +- [Open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues) if you have encountered a bug or problem. +- [Search or start a thread on the PureScript Discourse](https://discourse.purescript.org) if you have general questions. You can also ask questions in the `#purescript` and `#purescript-beginners` channels on the [Functional Programming Slack](https://functionalprogramming.slack.com) ([invite link](https://fpchat-invite.herokuapp.com/)). + +## Contributing + +You can contribute to `arraybuffer` in several ways: + +1. If you encounter a problem or have a question, please [open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues). We'll do our best to work with you to resolve or answer it. + +2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions. + +3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. + +## Other packages + +These are some other packages which provide more `ArrayBuffer` features. + +### Reading and Writing + +* [__arraybuffer-class__](https://pursuit.purescript.org/packages/purescript-arraybuffer-class) +* [__dynamic-buffers__](https://pursuit.purescript.org/packages/purescript-dynamic-buffers) +* [__parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview) +* [__arraybuffer-builder__](https://pursuit.purescript.org/packages/purescript-arraybuffer-builder) + +### Node.js + +* [__node-buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer) + +### UTF + +* [__text-encoding__](https://pursuit.purescript.org/packages/purescript-text-encoding) + +### Base64 -See https://github.com/AlexaDeWit/purescript-arraybuffer-codecs if you need text/base64 encoding/decoding functions. +* [__base64-codec__](https://pursuit.purescript.org/packages/purescript-base64-codec) diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..2e2f3dd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,3 @@ +# Arraybuffer Documentation + +This directory contains documentation for `arraybuffer`. If you are interested in contributing new documentation, please read the [contributor guidelines](../CONTRIBUTING.md) and [What Nobody Tells You About Documentation](https://documentation.divio.com) for help getting started. From ff799d07584e32cde5ce4492080e9119bb828783 Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 5 Jul 2021 09:23:39 +0900 Subject: [PATCH 205/236] Don't export Typed.part' --- CHANGELOG.md | 5 ++++- src/Data/ArrayBuffer/Typed.purs | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7eea4b..1df7523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,10 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## v11.0.0 [Unreleased] +## v11.0.0 Jorge Acereda has graciously donated this package to __purescript-contrib__. + For version 11, we have refactored this library so that it depends only on other packages in __purescript-contrib__. @@ -25,3 +26,5 @@ to the type declarations in your own dependent code: * Replace the type `AProxy` with `Proxy` from the Prelude. * Remove most of the `Nat` typeclass constraints. https://github.com/purescript-contrib/purescript-arraybuffer/issues/29 * Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. + +We have also privatized `Typed.part'`. https://github.com/purescript-contrib/purescript-arraybuffer/issues/32 diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 472a959..b7828a6 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -34,7 +34,7 @@ module Data.ArrayBuffer.Typed , buffer, byteOffset, byteLength, length , compare, eq , class TypedArray - , create, whole, remainder, part, part', empty, fromArray + , create, whole, remainder, part, empty, fromArray , fill, set, setTyped, copyWithin , map, traverse, traverse_, filter , mapWithIndex, traverseWithIndex, traverseWithIndex_, filterWithIndex @@ -137,6 +137,8 @@ part :: forall a t. TypedArray a t => BytesPerType a => ArrayBuffer -> Index -> part a x y = part' a o y where o = x * byteWidth (Proxy :: Proxy a) +-- | The ByteOffset must be aligned. +-- | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#byteoffset_must_be_aligned part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) part' a x y = runEffectFn3 create a (notNull x) (notNull y) From 146469d8b90edddda05d6c488177550686dd54a4 Mon Sep 17 00:00:00 2001 From: James Brock Date: Sun, 11 Jul 2021 21:39:21 +0900 Subject: [PATCH 206/236] Split test dependencies out into spago-test.dhall --- .github/workflows/ci.yml | 8 ++++- README.md | 8 +++++ spago-test.dhall | 14 ++++++++ spago.dhall | 10 +----- src/Data/ArrayBuffer/Typed/Unsafe.purs | 42 ------------------------ test/Properties/Typed/Laws.purs | 45 +++++++++++++++++++++++--- 6 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 spago-test.dhall delete mode 100644 src/Data/ArrayBuffer/Typed/Unsafe.purs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b0550f..805034b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,5 +30,11 @@ jobs: - name: Build source run: spago build --no-install --purs-args '--censor-lib --strict' + - name: Install test dependencies + run: spago -x spago-test.dhall install + + - name: Build tests + run: spago -x spago-test.dhall build --no-install --purs-args '--censor-lib --strict' + - name: Run tests - run: spago test --no-install + run: spago -x spago-test.dhall test --no-install diff --git a/README.md b/README.md index 8f2e831..67ddab0 100644 --- a/README.md +++ b/README.md @@ -75,3 +75,11 @@ These are some other packages which provide more `ArrayBuffer` features. ### Base64 * [__base64-codec__](https://pursuit.purescript.org/packages/purescript-base64-codec) + +## Development + +Run the tests with + +```sh +spago -x spago-test.dhall test +``` diff --git a/spago-test.dhall b/spago-test.dhall new file mode 100644 index 0000000..ab1a012 --- /dev/null +++ b/spago-test.dhall @@ -0,0 +1,14 @@ +let conf = ./spago.dhall +in conf // { +, dependencies = conf.dependencies # + [ "console" + , "foldable-traversable" + , "partial" + , "refs" + , "typelevel-prelude" + , "tuples" + , "quickcheck" + , "quickcheck-laws" + ] +, sources = conf.sources # [ "test/**/*.purs" ] +} diff --git a/spago.dhall b/spago.dhall index 41922dc..c7d8717 100644 --- a/spago.dhall +++ b/spago.dhall @@ -2,25 +2,17 @@ , dependencies = [ "arraybuffer-types" , "arrays" - , "console" , "effect" , "float32" - , "foldable-traversable" , "functions" , "gen" , "maybe" , "nullable" - , "partial" , "prelude" - , "quickcheck" - , "quickcheck-laws" - , "refs" , "tailrec" - , "typelevel-prelude" , "uint" , "unfoldable" - , "tuples" ] , packages = ./packages.dhall -, sources = [ "src/**/*.purs", "test/**/*.purs" ] +, sources = [ "src/**/*.purs" ] } diff --git a/src/Data/ArrayBuffer/Typed/Unsafe.purs b/src/Data/ArrayBuffer/Typed/Unsafe.purs deleted file mode 100644 index facd419..0000000 --- a/src/Data/ArrayBuffer/Typed/Unsafe.purs +++ /dev/null @@ -1,42 +0,0 @@ -module Data.ArrayBuffer.Typed.Unsafe where - -import Data.ArrayBuffer.Typed (class TypedArray, toString) -import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Types (ArrayView, ArrayViewType) -import Data.Generic.Rep (class Generic) -import Data.Maybe (Maybe(..)) -import Effect.Unsafe (unsafePerformEffect) -import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>), (<$>)) -import Test.QuickCheck (class Arbitrary, arbitrary) - -newtype AV :: forall k. ArrayViewType -> k -> Type -newtype AV a t = AV (ArrayView a) - -derive instance genericAV :: Generic (AV a t) _ - -instance ordArrayView :: (TypedArray a t, Ord t) => Ord (AV a t) where - compare (AV a) (AV b) = unsafePerformEffect $ TA.compare a b - -instance eqArrayView :: (TypedArray a t, Eq t) => Eq (AV a t) where - eq (AV a) (AV b) = unsafePerformEffect $ TA.eq a b - -instance showArrayView :: (TypedArray a t, Show t) => Show (AV a t) where - show (AV a) = "T[" <> s <> "]" - where s = unsafePerformEffect $ toString a - -instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t) where - append (AV a) (AV b) = unsafePerformEffect do - let la = TA.length a - lb = TA.length b - r <- TA.empty $ la + lb - void $ TA.setTyped r (Just 0) a - void $ TA.setTyped r (Just la) b - pure $ AV r - -instance monoidArrayView :: TypedArray a t => Monoid (AV a t) where - mempty = AV $ unsafePerformEffect $ TA.empty 0 - -instance arbitraryArrayView :: (TypedArray a t, Arbitrary t) => Arbitrary (AV a t) where - arbitrary = do - xs <- arbitrary - pure $ unsafePerformEffect $ AV <$> TA.fromArray xs diff --git a/test/Properties/Typed/Laws.purs b/test/Properties/Typed/Laws.purs index 5f7d5c5..8cf2f5c 100644 --- a/test/Properties/Typed/Laws.purs +++ b/test/Properties/Typed/Laws.purs @@ -1,19 +1,24 @@ module Test.Properties.Typed.Laws where -import Data.ArrayBuffer.Typed (class TypedArray) +import Prelude +-- import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>), (<$>)) +-- import Prelude (class Eq, class Monoid, class Ord, class Semigroup, Unit, discard, void, ($), (+), (<$>), (<<<)) +import Data.ArrayBuffer.Typed (class TypedArray, toString) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8) -import Data.ArrayBuffer.Typed.Unsafe (AV(..)) import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, ArrayViewType) import Data.Float32 as F import Data.UInt (UInt) import Effect (Effect) import Effect.Ref (Ref) import Effect.Ref as Ref -import Prelude (class Eq, class Monoid, class Ord, class Semigroup, Unit, discard, void, ($), (+), (<$>), (<<<)) -import Test.QuickCheck (class Arbitrary) +import Test.QuickCheck (class Arbitrary, arbitrary) import Test.QuickCheck.Gen (Gen) import Test.QuickCheck.Laws.Data (checkEq, checkMonoid, checkOrd, checkSemigroup) import Type.Prelude (Proxy(..)) +import Data.ArrayBuffer.Typed as TA +import Data.Generic.Rep (class Generic) +import Data.Maybe (Maybe(..)) +import Effect.Unsafe (unsafePerformEffect) newtype A a = A a @@ -102,3 +107,35 @@ typedArrayLaws count = do f (Proxy :: Proxy (A (AV Uint32 UInt))) f (Proxy :: Proxy (A (AV Uint8 UInt))) f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + +newtype AV :: forall k. ArrayViewType -> k -> Type +newtype AV a t = AV (ArrayView a) + +derive instance genericAV :: Generic (AV a t) _ + +instance ordArrayView :: (TypedArray a t, Ord t) => Ord (AV a t) where + compare (AV a) (AV b) = unsafePerformEffect $ TA.compare a b + +instance eqArrayView :: (TypedArray a t, Eq t) => Eq (AV a t) where + eq (AV a) (AV b) = unsafePerformEffect $ TA.eq a b + +instance showArrayView :: (TypedArray a t, Show t) => Show (AV a t) where + show (AV a) = "T[" <> s <> "]" + where s = unsafePerformEffect $ toString a + +instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t) where + append (AV a) (AV b) = unsafePerformEffect do + let la = TA.length a + lb = TA.length b + r <- TA.empty $ la + lb + void $ TA.setTyped r (Just 0) a + void $ TA.setTyped r (Just la) b + pure $ AV r + +instance monoidArrayView :: TypedArray a t => Monoid (AV a t) where + mempty = AV $ unsafePerformEffect $ TA.empty 0 + +instance arbitraryArrayView :: (TypedArray a t, Arbitrary t) => Arbitrary (AV a t) where + arbitrary = do + xs <- arbitrary + pure $ unsafePerformEffect $ AV <$> TA.fromArray xs \ No newline at end of file From 97baec38b93d202ef6feaa8cd6abcebc0a73410c Mon Sep 17 00:00:00 2001 From: James Brock Date: Sun, 11 Jul 2021 21:42:57 +0900 Subject: [PATCH 207/236] 'bower install' resolutions --- bower.json | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/bower.json b/bower.json index 9f5755a..0157b1d 100644 --- a/bower.json +++ b/bower.json @@ -28,5 +28,45 @@ "purescript-quickcheck": "^7.0.0", "purescript-quickcheck-combinators": "~0.1.3", "purescript-quickcheck-laws": "^6.0.0" + }, + "resolutions": { + "purescript-effect": "^3.0.0", + "purescript-functions": "^5.0.0", + "purescript-partial": "^3.0.0", + "purescript-prelude": "^5.0.0", + "purescript-maybe": "^5.0.0", + "purescript-quickcheck": "^7.0.0", + "purescript-control": "^5.0.0", + "purescript-invariant": "^5.0.0", + "purescript-newtype": "^4.0.0", + "purescript-quickcheck-laws": "^6.0.0", + "purescript-arrays": "^6.0.0", + "purescript-enums": "^5.0.0", + "purescript-foldable-traversable": "^5.0.0", + "purescript-console": "^5.0.0", + "purescript-exceptions": "^5.0.0", + "purescript-gen": "^3.0.0", + "purescript-either": "^5.0.0", + "purescript-integers": "^5.0.0", + "purescript-lazy": "^5.0.0", + "purescript-lcg": "^3.0.0", + "purescript-lists": "^6.0.0", + "purescript-identity": "^5.0.0", + "purescript-math": "^3.0.0", + "purescript-nonempty": "^6.0.0", + "purescript-record": "^3.0.0", + "purescript-st": "^5.0.0", + "purescript-strings": "^5.0.0", + "purescript-transformers": "^5.0.0", + "purescript-tuples": "^6.0.0", + "purescript-unfoldable": "^5.0.0", + "purescript-tailrec": "^5.0.0", + "purescript-unsafe-coerce": "^5.0.0", + "purescript-orders": "^5.0.0", + "purescript-bifunctors": "^5.0.0", + "purescript-random": "^5.0.0", + "purescript-distributive": "^5.0.0", + "purescript-refs": "^5.0.0", + "purescript-type-equality": "^4.0.0" } } From 9ec68e53f073e4f9b377c89de9d664bf7511492a Mon Sep 17 00:00:00 2001 From: James Brock Date: Sun, 11 Jul 2021 21:30:05 +0900 Subject: [PATCH 208/236] float32 and uint are back in package-sets --- packages.dhall | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/packages.dhall b/packages.dhall index 446440b..87f92e8 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,34 +1,10 @@ let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.14.1-20210419/packages.dhall sha256:d9a082ffb5c0fabf689574f0680e901ca6f924e01acdbece5eeedd951731375a + https://github.com/purescript/package-sets/releases/download/psc-0.14.2-20210713/packages.dhall sha256:654c3148cb995f642c73b4508d987d9896e2ad3ea1d325a1e826c034c0d3cd7b let overrides = {=} -let additions = - { float32 = - { dependencies = - [ "effect" - , "gen" - , "maybe" - , "prelude" - ] - , repo = - "https://github.com/athanclark/purescript-float32.git" - , version = - "v0.2.0" - } - , uint = - { dependencies = - [ "effect" - , "math" - , "maybe" - , "quickcheck" - , "quickcheck-laws" - ] - , repo = "https://github.com/purescript-contrib/purescript-uint.git" - , version = "v5.1.4" - } - } +let additions = {=} in upstream // overrides // additions From 76b8eb03fd24759044db038c50cf79f471255d03 Mon Sep 17 00:00:00 2001 From: James Brock Date: Fri, 16 Jul 2021 19:05:55 +0900 Subject: [PATCH 209/236] README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 67ddab0..7113484 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ access of aligned local-machine-endian types, for in-process flat memory operati The `DataView` module provides a view into an `ArrayBuffer` for inter-process flat memory operations. -* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) -* [ECMA-262](https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybuffer-objects) +* [MDN `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) +* [ECMA-262 `ArrayBuffer`](https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybuffer-objects) ## Installation @@ -53,7 +53,7 @@ You can contribute to `arraybuffer` in several ways: 3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. -## Other packages +## Related packages These are some other packages which provide more `ArrayBuffer` features. From e9c896d369b02e5096d6d125f8a66d4b0f347405 Mon Sep 17 00:00:00 2001 From: James Brock Date: Fri, 16 Jul 2021 19:13:16 +0900 Subject: [PATCH 210/236] v11.0.0 From 26a3bc5900d38960de3f7232e2ba6dd2fdc7bd2e Mon Sep 17 00:00:00 2001 From: James Brock Date: Fri, 16 Jul 2021 19:19:52 +0900 Subject: [PATCH 211/236] spago bump-version --no-dry-run patch --- LICENSE.txt | 21 ++++++++++++ bower.json | 98 +++++++++++++++-------------------------------------- spago.dhall | 2 ++ 3 files changed, 51 insertions(+), 70 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f6503c4 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 PureScript Contrib + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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. diff --git a/bower.json b/bower.json index 0157b1d..e11d42e 100644 --- a/bower.json +++ b/bower.json @@ -1,72 +1,30 @@ { - "name": "purescript-arraybuffer", - "license": "MIT", - "repository": { - "type": "git", - "url": "git://github.com/jacereda/purescript-arraybuffer.git" - }, - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "output" - ], - "dependencies": { - "purescript-prelude": "^5.0.0", - "purescript-functions": "^5.0.0", - "purescript-arraybuffer-types": "^2.0.0", - "purescript-maybe": "^5.0.0", - "purescript-effect": "^3.0.0", - "purescript-nullable": "^5.0.0", - "purescript-typelevel": "^6.0.0", - "purescript-uint": "^5.1.4", - "purescript-partial": "^3.0.0", - "purescript-float32": "~0.2.0" - }, - "devDependencies": { - "purescript-debug": "^5.0.0", - "purescript-quickcheck": "^7.0.0", - "purescript-quickcheck-combinators": "~0.1.3", - "purescript-quickcheck-laws": "^6.0.0" - }, - "resolutions": { - "purescript-effect": "^3.0.0", - "purescript-functions": "^5.0.0", - "purescript-partial": "^3.0.0", - "purescript-prelude": "^5.0.0", - "purescript-maybe": "^5.0.0", - "purescript-quickcheck": "^7.0.0", - "purescript-control": "^5.0.0", - "purescript-invariant": "^5.0.0", - "purescript-newtype": "^4.0.0", - "purescript-quickcheck-laws": "^6.0.0", - "purescript-arrays": "^6.0.0", - "purescript-enums": "^5.0.0", - "purescript-foldable-traversable": "^5.0.0", - "purescript-console": "^5.0.0", - "purescript-exceptions": "^5.0.0", - "purescript-gen": "^3.0.0", - "purescript-either": "^5.0.0", - "purescript-integers": "^5.0.0", - "purescript-lazy": "^5.0.0", - "purescript-lcg": "^3.0.0", - "purescript-lists": "^6.0.0", - "purescript-identity": "^5.0.0", - "purescript-math": "^3.0.0", - "purescript-nonempty": "^6.0.0", - "purescript-record": "^3.0.0", - "purescript-st": "^5.0.0", - "purescript-strings": "^5.0.0", - "purescript-transformers": "^5.0.0", - "purescript-tuples": "^6.0.0", - "purescript-unfoldable": "^5.0.0", - "purescript-tailrec": "^5.0.0", - "purescript-unsafe-coerce": "^5.0.0", - "purescript-orders": "^5.0.0", - "purescript-bifunctors": "^5.0.0", - "purescript-random": "^5.0.0", - "purescript-distributive": "^5.0.0", - "purescript-refs": "^5.0.0", - "purescript-type-equality": "^4.0.0" - } + "name": "purescript-arraybuffer", + "license": [ + "MIT" + ], + "repository": { + "type": "git", + "url": "https://github.com/purescript-contrib/purescript-arraybuffer" + }, + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "output" + ], + "dependencies": { + "purescript-arraybuffer-types": "^v3.0.0", + "purescript-arrays": "^v6.0.1", + "purescript-effect": "^v3.0.0", + "purescript-float32": "^v1.0.0", + "purescript-functions": "^v5.0.0", + "purescript-gen": "^v3.0.0", + "purescript-maybe": "^v5.0.0", + "purescript-nullable": "^v5.0.0", + "purescript-prelude": "^v5.0.1", + "purescript-tailrec": "^v5.0.1", + "purescript-uint": "^v6.0.3", + "purescript-unfoldable": "^v5.0.0" + } } diff --git a/spago.dhall b/spago.dhall index c7d8717..48fc095 100644 --- a/spago.dhall +++ b/spago.dhall @@ -15,4 +15,6 @@ ] , packages = ./packages.dhall , sources = [ "src/**/*.purs" ] +, license = "MIT" +, repository = "https://github.com/purescript-contrib/purescript-arraybuffer" } From 23eaac80d0f20f6de35b25374daf52b4ec75cc2c Mon Sep 17 00:00:00 2001 From: James Brock Date: Fri, 16 Jul 2021 19:42:13 +0900 Subject: [PATCH 212/236] =?UTF-8?q?v11.0.0=20=E2=86=92=20v11.0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From d1115c754e8d02840c3dc1b7135fc96ea071c745 Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Sun, 25 Jul 2021 12:34:35 -0400 Subject: [PATCH 213/236] Replace Slack links with Discord (#33) --- .github/ISSUE_TEMPLATE/config.yml | 8 ++++---- README.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index c47a263..d63c9d7 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -2,7 +2,7 @@ blank_issues_enabled: false contact_links: - name: PureScript Discourse url: https://discourse.purescript.org/ - about: Ask and answer questions here. - - name: Functional Programming Slack - url: https://functionalprogramming.slack.com - about: For casual chat and questions (use https://fpchat-invite.herokuapp.com to join). + about: Ask and answer questions on the PureScript discussion forum. + - name: PureScript Discord + url: https://discord.com/invite/sMqwYUbvz6/ + about: Ask and answer questions on the PureScript chat. diff --git a/README.md b/README.md index 7113484..fc6d01c 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ spago install arraybuffer If you get stuck, there are several ways to get help: - [Open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues) if you have encountered a bug or problem. -- [Search or start a thread on the PureScript Discourse](https://discourse.purescript.org) if you have general questions. You can also ask questions in the `#purescript` and `#purescript-beginners` channels on the [Functional Programming Slack](https://functionalprogramming.slack.com) ([invite link](https://fpchat-invite.herokuapp.com/)). +- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://discord.com/invite/sMqwYUbvz6) chat. ## Contributing From edccb06d7c5bc4b98b2d73eca60e40f49b03e106 Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Wed, 28 Jul 2021 11:32:08 -0400 Subject: [PATCH 214/236] Update to the latest package set --- .travis.yml | 9 --------- package.json | 9 --------- packages.dhall | 4 +--- 3 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 .travis.yml delete mode 100644 package.json diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1f93807..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: node_js -sudo: required -dist: trusty -node_js: 8 -install: - - npm install -g purescript pulp bower -script: - - bower install - - pulp test diff --git a/package.json b/package.json deleted file mode 100644 index a792c5d..0000000 --- a/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "purescript-arraybuffer", - "version": "11.0.0", - "main": "index.js", - "repository": "git@github.com:jacereda/purescript-arraybuffer.git", - "author": "https://github.com/jacereda", - "license": "MIT", - "devDependencies": {} -} \ No newline at end of file diff --git a/packages.dhall b/packages.dhall index 87f92e8..bea1ce8 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,10 +1,8 @@ - let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.14.2-20210713/packages.dhall sha256:654c3148cb995f642c73b4508d987d9896e2ad3ea1d325a1e826c034c0d3cd7b + https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c let overrides = {=} let additions = {=} in upstream // overrides // additions - From 20f4de11f92ebb7fa78cd4ca67b2d4b707721637 Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Wed, 4 Aug 2021 13:02:21 -0400 Subject: [PATCH 215/236] Replace Discord links with PS chat --- .github/ISSUE_TEMPLATE/config.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index d63c9d7..8d7661e 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -4,5 +4,5 @@ contact_links: url: https://discourse.purescript.org/ about: Ask and answer questions on the PureScript discussion forum. - name: PureScript Discord - url: https://discord.com/invite/sMqwYUbvz6/ + url: https://purescript.org/chat about: Ask and answer questions on the PureScript chat. diff --git a/README.md b/README.md index fc6d01c..72d7c55 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ spago install arraybuffer If you get stuck, there are several ways to get help: - [Open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues) if you have encountered a bug or problem. -- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://discord.com/invite/sMqwYUbvz6) chat. +- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat. ## Contributing From 3e8cb28b8372adf74bca220451fdad6008cabe9a Mon Sep 17 00:00:00 2001 From: James Brock Date: Tue, 21 Sep 2021 14:20:22 +0900 Subject: [PATCH 216/236] Delete the TypedArray polyfill The `polyFill()` for TypedArray was causing problems with `purs bundle` v0.14.4. https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 The reference link which the `polyFill()` function comments referred to is dead. Add a section on polyfills to the README, with new reference links. --- CHANGELOG.md | 6 ++++++ README.md | 17 +++++++++++++++++ src/Data/ArrayBuffer/Typed.js | 19 ------------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df7523..b4a8950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +@jamesdbrock Delete the `TypedArray` polyfill which was preventing this +library from working with `purs bundle` v0.14.4. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + ## v11.0.0 Jorge Acereda has graciously donated this package to __purescript-contrib__. diff --git a/README.md b/README.md index 72d7c55..749faf0 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,23 @@ You can contribute to `arraybuffer` in several ways: 3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. +## Usage + +### Polyfill + +This library relies on runtime implementations of +[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) +and +[`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) +([Structured Data](https://tc39.es/ecma262/multipage/structured-data.html#sec-structured-data)), +and +[`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) +([Indexed Collections](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-indexed-collections)). + +If you want to be sure that those implemtations are available in your target +runtime environment, you might want to consider using a polyfill such as +[__core-js__ Typed Arrays](https://github.com/zloirock/core-js#ecmascript-typed-arrays). + ## Related packages These are some other packages which provide more `ArrayBuffer` features. diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index cf2d733..2ab0cb6 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,24 +1,5 @@ "use strict"; - - -// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -function polyFill () { - var typedArrayTypes = - [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array - , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array - ]; - - for (var k in typedArrayTypes) { - for (var v in Array.prototype) { - if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) - typedArrayTypes[k].prototype[v] = Array.prototype[v]; - } - } -}; - -polyFill(); - // module Data.ArrayBuffer.Typed exports.buffer = function buffer (v) { From 674d443b3cb187b201282a519a2c8a3835ea6f38 Mon Sep 17 00:00:00 2001 From: James Brock Date: Wed, 22 Sep 2021 01:09:54 +0900 Subject: [PATCH 217/236] README fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 749faf0..c89142a 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ and [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) ([Indexed Collections](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-indexed-collections)). -If you want to be sure that those implemtations are available in your target +If you want to be sure that those implementations are available in your target runtime environment, you might want to consider using a polyfill such as [__core-js__ Typed Arrays](https://github.com/zloirock/core-js#ecmascript-typed-arrays). From d29c01f0421c41cfa5e873e40fbac1a8ecdac5c2 Mon Sep 17 00:00:00 2001 From: James Brock Date: Wed, 22 Sep 2021 01:12:02 +0900 Subject: [PATCH 218/236] prep for v11.0.2 --- CHANGELOG.md | 5 +++-- bower.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4a8950..1830534 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,9 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Unreleased +## v11.0.2 -@jamesdbrock Delete the `TypedArray` polyfill which was preventing this +Delete the `TypedArray` polyfill which was preventing this library from working with `purs bundle` v0.14.4. https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 @@ -34,3 +34,4 @@ to the type declarations in your own dependent code: * Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. We have also privatized `Typed.part'`. https://github.com/purescript-contrib/purescript-arraybuffer/issues/32 + diff --git a/bower.json b/bower.json index e11d42e..4d6947c 100644 --- a/bower.json +++ b/bower.json @@ -14,7 +14,7 @@ "output" ], "dependencies": { - "purescript-arraybuffer-types": "^v3.0.0", + "purescript-arraybuffer-types": "^v3.0.1", "purescript-arrays": "^v6.0.1", "purescript-effect": "^v3.0.0", "purescript-float32": "^v1.0.0", From ff9e181c20b2a5f0b0cd6d7448818d366e50c232 Mon Sep 17 00:00:00 2001 From: James Brock Date: Wed, 22 Sep 2021 01:14:15 +0900 Subject: [PATCH 219/236] =?UTF-8?q?v11.0.1=20=E2=86=92=20v11.0.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 341d123426c0be6e41c1992d4419c2495fbd9350 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 23 Sep 2021 20:34:23 +0900 Subject: [PATCH 220/236] Revert to v11.0.1 for issue #37 --- .github/ISSUE_TEMPLATE/config.yml | 8 ++++---- .travis.yml | 9 +++++++++ CHANGELOG.md | 7 ------- README.md | 19 +------------------ bower.json | 2 +- package.json | 9 +++++++++ packages.dhall | 4 +++- src/Data/ArrayBuffer/Typed.js | 19 +++++++++++++++++++ 8 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 .travis.yml create mode 100644 package.json diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 8d7661e..c47a263 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -2,7 +2,7 @@ blank_issues_enabled: false contact_links: - name: PureScript Discourse url: https://discourse.purescript.org/ - about: Ask and answer questions on the PureScript discussion forum. - - name: PureScript Discord - url: https://purescript.org/chat - about: Ask and answer questions on the PureScript chat. + about: Ask and answer questions here. + - name: Functional Programming Slack + url: https://functionalprogramming.slack.com + about: For casual chat and questions (use https://fpchat-invite.herokuapp.com to join). diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1f93807 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +sudo: required +dist: trusty +node_js: 8 +install: + - npm install -g purescript pulp bower +script: + - bower install + - pulp test diff --git a/CHANGELOG.md b/CHANGELOG.md index 1830534..1df7523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,6 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## v11.0.2 - -Delete the `TypedArray` polyfill which was preventing this -library from working with `purs bundle` v0.14.4. -https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 - ## v11.0.0 Jorge Acereda has graciously donated this package to __purescript-contrib__. @@ -34,4 +28,3 @@ to the type declarations in your own dependent code: * Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. We have also privatized `Typed.part'`. https://github.com/purescript-contrib/purescript-arraybuffer/issues/32 - diff --git a/README.md b/README.md index c89142a..7113484 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ spago install arraybuffer If you get stuck, there are several ways to get help: - [Open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues) if you have encountered a bug or problem. -- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat. +- [Search or start a thread on the PureScript Discourse](https://discourse.purescript.org) if you have general questions. You can also ask questions in the `#purescript` and `#purescript-beginners` channels on the [Functional Programming Slack](https://functionalprogramming.slack.com) ([invite link](https://fpchat-invite.herokuapp.com/)). ## Contributing @@ -53,23 +53,6 @@ You can contribute to `arraybuffer` in several ways: 3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. -## Usage - -### Polyfill - -This library relies on runtime implementations of -[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) -and -[`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) -([Structured Data](https://tc39.es/ecma262/multipage/structured-data.html#sec-structured-data)), -and -[`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) -([Indexed Collections](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-indexed-collections)). - -If you want to be sure that those implementations are available in your target -runtime environment, you might want to consider using a polyfill such as -[__core-js__ Typed Arrays](https://github.com/zloirock/core-js#ecmascript-typed-arrays). - ## Related packages These are some other packages which provide more `ArrayBuffer` features. diff --git a/bower.json b/bower.json index 4d6947c..e11d42e 100644 --- a/bower.json +++ b/bower.json @@ -14,7 +14,7 @@ "output" ], "dependencies": { - "purescript-arraybuffer-types": "^v3.0.1", + "purescript-arraybuffer-types": "^v3.0.0", "purescript-arrays": "^v6.0.1", "purescript-effect": "^v3.0.0", "purescript-float32": "^v1.0.0", diff --git a/package.json b/package.json new file mode 100644 index 0000000..a792c5d --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "purescript-arraybuffer", + "version": "11.0.0", + "main": "index.js", + "repository": "git@github.com:jacereda/purescript-arraybuffer.git", + "author": "https://github.com/jacereda", + "license": "MIT", + "devDependencies": {} +} \ No newline at end of file diff --git a/packages.dhall b/packages.dhall index bea1ce8..87f92e8 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,8 +1,10 @@ + let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c + https://github.com/purescript/package-sets/releases/download/psc-0.14.2-20210713/packages.dhall sha256:654c3148cb995f642c73b4508d987d9896e2ad3ea1d325a1e826c034c0d3cd7b let overrides = {=} let additions = {=} in upstream // overrides // additions + diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 2ab0cb6..cf2d733 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,5 +1,24 @@ "use strict"; + + +// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill +function polyFill () { + var typedArrayTypes = + [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array + , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array + ]; + + for (var k in typedArrayTypes) { + for (var v in Array.prototype) { + if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) + typedArrayTypes[k].prototype[v] = Array.prototype[v]; + } + } +}; + +polyFill(); + // module Data.ArrayBuffer.Typed exports.buffer = function buffer (v) { From 5c8fc86f9ae3d548d8a6e998209824175be7615d Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 23 Sep 2021 21:58:36 +0900 Subject: [PATCH 221/236] =?UTF-8?q?v11.0.2=20=E2=86=92=20v11.0.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 36aa9e2f689dc549f753988ee6510219ef99bbc8 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 23 Sep 2021 22:03:53 +0900 Subject: [PATCH 222/236] Revert back to v11.0.2 for issue #37 --- .github/ISSUE_TEMPLATE/config.yml | 8 ++++---- .travis.yml | 9 --------- CHANGELOG.md | 7 +++++++ README.md | 19 ++++++++++++++++++- bower.json | 2 +- package.json | 9 --------- packages.dhall | 4 +--- src/Data/ArrayBuffer/Typed.js | 19 ------------------- 8 files changed, 31 insertions(+), 46 deletions(-) delete mode 100644 .travis.yml delete mode 100644 package.json diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index c47a263..8d7661e 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -2,7 +2,7 @@ blank_issues_enabled: false contact_links: - name: PureScript Discourse url: https://discourse.purescript.org/ - about: Ask and answer questions here. - - name: Functional Programming Slack - url: https://functionalprogramming.slack.com - about: For casual chat and questions (use https://fpchat-invite.herokuapp.com to join). + about: Ask and answer questions on the PureScript discussion forum. + - name: PureScript Discord + url: https://purescript.org/chat + about: Ask and answer questions on the PureScript chat. diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1f93807..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: node_js -sudo: required -dist: trusty -node_js: 8 -install: - - npm install -g purescript pulp bower -script: - - bower install - - pulp test diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df7523..1830534 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v11.0.2 + +Delete the `TypedArray` polyfill which was preventing this +library from working with `purs bundle` v0.14.4. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + ## v11.0.0 Jorge Acereda has graciously donated this package to __purescript-contrib__. @@ -28,3 +34,4 @@ to the type declarations in your own dependent code: * Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. We have also privatized `Typed.part'`. https://github.com/purescript-contrib/purescript-arraybuffer/issues/32 + diff --git a/README.md b/README.md index 7113484..c89142a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ spago install arraybuffer If you get stuck, there are several ways to get help: - [Open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues) if you have encountered a bug or problem. -- [Search or start a thread on the PureScript Discourse](https://discourse.purescript.org) if you have general questions. You can also ask questions in the `#purescript` and `#purescript-beginners` channels on the [Functional Programming Slack](https://functionalprogramming.slack.com) ([invite link](https://fpchat-invite.herokuapp.com/)). +- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat. ## Contributing @@ -53,6 +53,23 @@ You can contribute to `arraybuffer` in several ways: 3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. +## Usage + +### Polyfill + +This library relies on runtime implementations of +[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) +and +[`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) +([Structured Data](https://tc39.es/ecma262/multipage/structured-data.html#sec-structured-data)), +and +[`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) +([Indexed Collections](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-indexed-collections)). + +If you want to be sure that those implementations are available in your target +runtime environment, you might want to consider using a polyfill such as +[__core-js__ Typed Arrays](https://github.com/zloirock/core-js#ecmascript-typed-arrays). + ## Related packages These are some other packages which provide more `ArrayBuffer` features. diff --git a/bower.json b/bower.json index e11d42e..4d6947c 100644 --- a/bower.json +++ b/bower.json @@ -14,7 +14,7 @@ "output" ], "dependencies": { - "purescript-arraybuffer-types": "^v3.0.0", + "purescript-arraybuffer-types": "^v3.0.1", "purescript-arrays": "^v6.0.1", "purescript-effect": "^v3.0.0", "purescript-float32": "^v1.0.0", diff --git a/package.json b/package.json deleted file mode 100644 index a792c5d..0000000 --- a/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "purescript-arraybuffer", - "version": "11.0.0", - "main": "index.js", - "repository": "git@github.com:jacereda/purescript-arraybuffer.git", - "author": "https://github.com/jacereda", - "license": "MIT", - "devDependencies": {} -} \ No newline at end of file diff --git a/packages.dhall b/packages.dhall index 87f92e8..bea1ce8 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,10 +1,8 @@ - let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.14.2-20210713/packages.dhall sha256:654c3148cb995f642c73b4508d987d9896e2ad3ea1d325a1e826c034c0d3cd7b + https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c let overrides = {=} let additions = {=} in upstream // overrides // additions - diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index cf2d733..2ab0cb6 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,24 +1,5 @@ "use strict"; - - -// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -function polyFill () { - var typedArrayTypes = - [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array - , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array - ]; - - for (var k in typedArrayTypes) { - for (var v in Array.prototype) { - if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) - typedArrayTypes[k].prototype[v] = Array.prototype[v]; - } - } -}; - -polyFill(); - // module Data.ArrayBuffer.Typed exports.buffer = function buffer (v) { From c77bd25981653fae35ee82ef4cb44b1149f1862b Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 23 Sep 2021 22:24:26 +0900 Subject: [PATCH 223/236] Prep for v12.0.0 --- CHANGELOG.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1830534..16ed4a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,33 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v12.0.0 + +Delete the `TypedArray` polyfill which was preventing this +library from working with `purs bundle` v0.14.4. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + +### Breaking Changes + +May lose partial polyfill `TypedArray` support for only the methods present +in regular JavaScript Arrays. +https://web.archive.org/web/20171019084331/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill + +## v11.0.3 + +Revert to v11.0.1. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/37 + ## v11.0.2 Delete the `TypedArray` polyfill which was preventing this library from working with `purs bundle` v0.14.4. https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 +## v11.0.1 + +Regenerate `bower.json`. + ## v11.0.0 Jorge Acereda has graciously donated this package to __purescript-contrib__. From 383d746fc355a3c12494c9b1f7686b519183dbc4 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 23 Sep 2021 22:33:13 +0900 Subject: [PATCH 224/236] =?UTF-8?q?v11.0.3=20=E2=86=92=20v12.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 51271ec72102f4dcb7064f4d9220700ebf5bd8f2 Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Fri, 19 Nov 2021 23:10:04 -0500 Subject: [PATCH 225/236] Introduce purs-tidy formatter (#38) * Add purs-tidy formatter * Run purs-tidy * review --- .github/workflows/ci.yml | 5 + .gitignore | 1 + .tidyrc.json | 10 + src/Data/ArrayBuffer/ArrayBuffer.purs | 2 + src/Data/ArrayBuffer/ArrayBuffer/Gen.purs | 5 +- src/Data/ArrayBuffer/DataView.purs | 312 +++++---- src/Data/ArrayBuffer/DataView/Gen.purs | 26 +- src/Data/ArrayBuffer/Typed.purs | 129 +++- src/Data/ArrayBuffer/Typed/Gen.purs | 30 +- src/Data/ArrayBuffer/ValueMapping.purs | 47 +- test/Main.purs | 1 - test/Properties.purs | 1 - test/Properties/ArrayBuffer.purs | 1 - test/Properties/DataView.purs | 110 +-- test/Properties/Typed/Laws.purs | 20 +- test/Properties/TypedArray.purs | 813 +++++++++++----------- 16 files changed, 836 insertions(+), 677 deletions(-) create mode 100644 .tidyrc.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 805034b..4aaad3f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,8 @@ jobs: - name: Set up a PureScript toolchain uses: purescript-contrib/setup-purescript@main + with: + purs-tidy: "latest" - name: Cache PureScript dependencies uses: actions/cache@v2 @@ -38,3 +40,6 @@ jobs: - name: Run tests run: spago -x spago-test.dhall test --no-install + + - name: Check formatting + run: purs-tidy check src test diff --git a/.gitignore b/.gitignore index 7bca306..7e82b68 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ !.gitignore !.github !.editorconfig +!.tidyrc.json output generated-docs diff --git a/.tidyrc.json b/.tidyrc.json new file mode 100644 index 0000000..4f013c1 --- /dev/null +++ b/.tidyrc.json @@ -0,0 +1,10 @@ +{ + "importSort": "source", + "importWrap": "source", + "indent": 2, + "operatorsFile": null, + "ribbon": 1, + "typeArrowPlacement": "first", + "unicode": "never", + "width": null +} diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index 9bedd45..fecb62b 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -14,6 +14,7 @@ import Effect.Uncurried (EffectFn1, runEffectFn1) -- | Create an `ArrayBuffer` with the given capacity. empty :: ByteLength -> Effect ArrayBuffer empty l = runEffectFn1 emptyImpl l + foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. @@ -22,4 +23,5 @@ foreign import byteLength :: ArrayBuffer -> ByteLength -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer slice s e a = runFn3 sliceImpl a s e + foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index dbe909a..381931c 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -6,8 +6,5 @@ import Data.ArrayBuffer.Typed.Gen (genTypedArray, genUint8) import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array) import Prelude ((<$>)) - -genArrayBuffer :: forall m - . MonadGen m - => m ArrayBuffer +genArrayBuffer :: forall m. MonadGen m => m ArrayBuffer genArrayBuffer = buffer <$> (genTypedArray genUint8 :: m Uint8Array) diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 4daca87..610090e 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -1,48 +1,48 @@ -- | This module represents the functional bindings to JavaScript's `DataView` -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. module Data.ArrayBuffer.DataView - ( Endian(..) - , buffer - , byteLength - , byteOffset - , get - , getBE - , getFloat32be - , getFloat32le - , getFloat64be - , getFloat64le - , getInt16be - , getInt16le - , getInt32be - , getInt32le - , getInt8 - , getLE - , getUint16be - , getUint16le - , getUint32be - , getUint32le - , getUint8 - , part - , remainder - , set - , setBE - , setFloat32be - , setFloat32le - , setFloat64be - , setFloat64le - , setInt16be - , setInt16le - , setInt32be - , setInt32le - , setInt8 - , setLE - , setUint16be - , setUint16le - , setUint32be - , setUint32le - , setUint8 - , whole - ) where + ( Endian(..) + , buffer + , byteLength + , byteOffset + , get + , getBE + , getFloat32be + , getFloat32le + , getFloat64be + , getFloat64le + , getInt16be + , getInt16le + , getInt32be + , getInt32le + , getInt8 + , getLE + , getUint16be + , getUint16le + , getUint32be + , getUint32le + , getUint8 + , part + , remainder + , set + , setBE + , setFloat32be + , setFloat32le + , setFloat64be + , setFloat64le + , setInt16be + , setInt16le + , setInt32be + , setInt32le + , setInt8 + , setLE + , setUint16be + , setUint16le + , setUint32be + , setUint32le + , setUint8 + , whole + ) where import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, class ShowArrayViewType, byteWidth) @@ -67,15 +67,16 @@ instance eqEndian :: Eq Endian where -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView - -- | View mapping the rest of an `ArrayBuffer` after an index. remainder :: ArrayBuffer -> ByteOffset -> Effect DataView remainder a o = runEffectFn2 remainderImpl a o + foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView -- | View mapping a region of the `ArrayBuffer`. part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView part a o l = runEffectFn3 partImpl a o l + foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView -- | `ArrayBuffer` being mapped by the view. @@ -87,86 +88,129 @@ foreign import byteOffset :: DataView -> ByteOffset -- | Represents the length of this view. foreign import byteLength :: DataView -> ByteLength - - -getter :: forall t. - { functionName :: String - , bytesPerValue :: ByteLength - , littleEndian :: Boolean - } - -> DataView -> ByteOffset -> Effect (Maybe t) -getter data' d o = toMaybe <$> - runEffectFn3 getterImpl +getter + :: forall t + . { functionName :: String + , bytesPerValue :: ByteLength + , littleEndian :: Boolean + } + -> DataView + -> ByteOffset + -> Effect (Maybe t) +getter data' dataView offset = + toMaybe <$> runEffectFn3 getterImpl { functionName: data'.functionName , littleEndian: data'.littleEndian , bytesPerValue: data'.bytesPerValue - } d o -foreign import getterImpl :: forall t - . EffectFn3 { functionName :: String - , littleEndian :: Boolean - , bytesPerValue :: ByteLength - } DataView ByteOffset (Nullable t) - - - -get :: forall a name t - . BinaryValue a t - => BytesPerType a - => ShowArrayViewType a name - => IsSymbol name - => Endian -> Proxy a -> DataView -> ByteOffset -> Effect (Maybe t) -get endian prx = - let le = endian == LE - pnm = "get" <> reflectSymbol (SProxy :: SProxy name) - bpv = byteWidth prx - in getter { functionName: pnm - , bytesPerValue: bpv - , littleEndian: le - } - -getBE :: forall a name t - . BinaryValue a t - => BytesPerType a - => ShowArrayViewType a name - => IsSymbol name - => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t) + } + dataView + offset + +foreign import getterImpl + :: forall t + . EffectFn3 + { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } + DataView + ByteOffset + (Nullable t) + +get + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Endian + -> Proxy a + -> DataView + -> ByteOffset + -> Effect (Maybe t) +get endian prx = do + let + le = endian == LE + pnm = "get" <> reflectSymbol (SProxy :: SProxy name) + bpv = byteWidth prx + + getter + { functionName: pnm + , bytesPerValue: bpv + , littleEndian: le + } + +getBE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> Effect (Maybe t) getBE = get BE -getLE :: forall a name t - . BinaryValue a t - => BytesPerType a - => ShowArrayViewType a name - => IsSymbol name - => Proxy a -> DataView -> ByteOffset -> Effect (Maybe t) +getLE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> Effect (Maybe t) getLE = get LE -setter :: forall t. - { functionName :: String - , bytesPerValue :: ByteLength - , littleEndian :: Boolean - } -> DataView -> ByteOffset -> t -> Effect Boolean -setter d o t = runEffectFn4 setterImpl d o t -foreign import setterImpl :: forall t - . EffectFn4 { functionName :: String - , littleEndian :: Boolean - , bytesPerValue :: ByteLength - } DataView ByteOffset t Boolean - - -set :: forall a name t - . BinaryValue a t - => BytesPerType a - => ShowArrayViewType a name - => IsSymbol name - => Endian -> Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean -set endian prx = - let le = endian == LE - pnm = "set" <> reflectSymbol (SProxy :: SProxy name) - bpv = byteWidth prx - in setter { functionName: pnm - , bytesPerValue: bpv - , littleEndian: le - } +setter + :: forall t + . { functionName :: String + , bytesPerValue :: ByteLength + , littleEndian :: Boolean + } + -> DataView + -> ByteOffset + -> t + -> Effect Boolean +setter dataView offset t = runEffectFn4 setterImpl dataView offset t + +foreign import setterImpl + :: forall t + . EffectFn4 + { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } + DataView + ByteOffset + t + Boolean + +set + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Endian + -> Proxy a + -> DataView + -> ByteOffset + -> t + -> Effect Boolean +set endian prx = do + let + le = endian == LE + pnm = "set" <> reflectSymbol (SProxy :: SProxy name) + bpv = byteWidth prx + + setter + { functionName: pnm + , bytesPerValue: bpv + , littleEndian: le + } -- | Fetch int8 value at a certain index in a `DataView`. getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int) @@ -224,23 +268,32 @@ getFloat64be = getBE (Proxy :: Proxy Float64) getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number) getFloat64le = getLE (Proxy :: Proxy Float64) - -- | Store big-endian value at a certain index in a `DataView`. -setBE :: forall a name t - . BinaryValue a t - => BytesPerType a - => ShowArrayViewType a name - => IsSymbol name - => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean +setBE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> t + -> Effect Boolean setBE = set BE -- | Store little-endian value at a certain index in a `DataView`. -setLE :: forall a name t - . BinaryValue a t - => BytesPerType a - => ShowArrayViewType a name - => IsSymbol name - => Proxy a -> DataView -> ByteOffset -> t -> Effect Boolean +setLE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> t + -> Effect Boolean setLE = set LE -- | Store int8 value at a certain index in a `DataView`. @@ -267,7 +320,6 @@ setInt32le = setLE (Proxy :: Proxy Int32) setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean setUint8 = setLE (Proxy :: Proxy Uint8) - -- | Store big-endian uint16 value at a certain index in a `DataView`. setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean setUint16be = setBE (Proxy :: Proxy Uint16) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index f7e55ca..cd1c974 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -11,27 +11,23 @@ import Data.Unfoldable (replicateA) import Prelude ((<$>), bind, (<=), (-), pure) import Type.Proxy (Proxy(..)) - -genDataView :: forall m - . MonadGen m - => m DataView +genDataView :: forall m. MonadGen m => m DataView genDataView = whole <$> genArrayBuffer - - -- | For generating some set of offsets residing inside the generated array, with some computable value data WithOffsetAndValue (a :: ArrayViewType) t = WithOffsetAndValue (Array ByteOffset) t DataView -genWithOffsetAndValue :: forall m a t - . MonadGen m - => MonadRec m - => BytesPerType a - => BinaryValue a t - => Int -- generated length - -> m DataView -- ^ Assumes generated length is at least the minimum length of one value - -> m t - -> m (WithOffsetAndValue a t) +genWithOffsetAndValue + :: forall m a t + . MonadGen m + => MonadRec m + => BytesPerType a + => BinaryValue a t + => Int -- generated length + -> m DataView -- ^ Assumes generated length is at least the minimum length of one value + -> m t + -> m (WithOffsetAndValue a t) genWithOffsetAndValue n gen genT = do let b = byteWidth (Proxy :: Proxy a) xs <- gen `suchThat` \xs -> b <= byteLength xs diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b7828a6..b8d593b 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -28,25 +28,65 @@ -- | - `toString` prints to a CSV, `join` allows you to supply the delimiter -- | - `toArray` returns an array of numeric values - module Data.ArrayBuffer.Typed - ( Index, Length - , buffer, byteOffset, byteLength, length - , compare, eq + ( Index + , Length + , buffer + , byteOffset + , byteLength + , length + , compare + , eq , class TypedArray - , create, whole, remainder, part, empty, fromArray - , fill, set, setTyped, copyWithin - , map, traverse, traverse_, filter - , mapWithIndex, traverseWithIndex, traverseWithIndex_, filterWithIndex - , sort, reverse + , create + , whole + , remainder + , part + , empty + , fromArray + , fill + , set + , setTyped + , copyWithin + , map + , traverse + , traverse_ + , filter + , mapWithIndex + , traverseWithIndex + , traverseWithIndex_ + , filterWithIndex + , sort + , reverse , elem - , all, any - , allWithIndex, anyWithIndex - , unsafeAt, hasIndex, at, (!) - , reduce, reduce1, foldl, foldl1, reduceRight, reduceRight1, foldr, foldr1, foldlWithIndex, foldrWithIndex - , find, findIndex, findWithIndex, indexOf, lastIndexOf - , slice, subArray - , toString, join, toArray + , all + , any + , allWithIndex + , anyWithIndex + , unsafeAt + , hasIndex + , at + , (!) + , reduce + , reduce1 + , foldl + , foldl1 + , reduceRight + , reduceRight1 + , foldr + , foldr1 + , foldlWithIndex + , foldrWithIndex + , find + , findIndex + , findWithIndex + , indexOf + , lastIndexOf + , slice + , subArray + , toString + , join + , toArray ) where import Data.Array (length) as A @@ -64,7 +104,6 @@ import Prelude (class Eq, class Ord, Ordering, Unit, flip, pure, ($), (&&), (*), import Prelude as Prelude import Type.Proxy (Proxy(..)) - -- | `ArrayBuffer` being mapped by the typed array. foreign import buffer :: forall a. ArrayView a -> ArrayBuffer @@ -77,6 +116,7 @@ foreign import byteLength :: forall a. ArrayView a -> ByteLength -- | Represents the number of elements in this typed array. length :: forall a. ArrayView a -> Length length = lengthImpl + foreign import lengthImpl :: forall a. ArrayView a -> Length -- object creator implementations for each typed array @@ -91,32 +131,38 @@ foreign import newInt8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Null foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array - -- | Value-oriented array index. type Index = Int -- | Value-oriented array length. type Length = Int - class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where create = newUint8ClampedArray + instance typedArrayUint32 :: TypedArray Uint32 UInt where create = newUint32Array + instance typedArrayUint16 :: TypedArray Uint16 UInt where create = newUint16Array + instance typedArrayUint8 :: TypedArray Uint8 UInt where create = newUint8Array + instance typedArrayInt32 :: TypedArray Int32 Int where create = newInt32Array + instance typedArrayInt16 :: TypedArray Int16 Int where create = newInt16Array + instance typedArrayInt8 :: TypedArray Int8 Int where create = newInt8Array + instance typedArrayFloat32 :: TypedArray Float32 F.Float32 where create = newFloat32Array + instance typedArrayFloat64 :: TypedArray Float64 Number where create = newFloat64Array @@ -127,7 +173,8 @@ whole a = runEffectFn3 create a null null -- | View mapping the rest of an `ArrayBuffer` after an index. remainder :: forall a b t. TypedArray a t => BytesPerType b => ArrayBuffer -> Index -> Effect (ArrayView a) remainder a x = remainder' a o - where o = x * byteWidth (Proxy :: Proxy b) + where + o = x * byteWidth (Proxy :: Proxy b) remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) remainder' a x = runEffectFn3 create a (notNull x) null @@ -135,7 +182,8 @@ remainder' a x = runEffectFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. part :: forall a t. TypedArray a t => BytesPerType a => ArrayBuffer -> Index -> Length -> Effect (ArrayView a) part a x y = part' a o y - where o = x * byteWidth (Proxy :: Proxy a) + where + o = x * byteWidth (Proxy :: Proxy a) -- | The ByteOffset must be aligned. -- | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#byteoffset_must_be_aligned @@ -153,6 +201,7 @@ fromArray a = runEffectFn3 create a null null -- | Fill the array with a value. fill :: forall a t. TypedArray a t => t -> Index -> Index -> ArrayView a -> Effect Unit fill x s e a = runEffectFn4 fillImpl x s e a + foreign import fillImpl :: forall a b. EffectFn4 b Index Index (ArrayView a) Unit -- | Stores multiple values into the typed array. @@ -162,7 +211,6 @@ set = setInternal A.length ap1 :: forall a b c. (a -> c) -> (a -> b -> c) ap1 f = \x _ -> f x - -- | Maps a new value over the typed array, creating a new buffer and -- | typed array as well. map :: forall a t. TypedArray a t => (t -> t) -> ArrayView a -> ArrayView a @@ -176,6 +224,7 @@ mapWithIndex = mapWithIndex' <<< flip mapWithIndex' :: forall a t. TypedArray a t => (t -> Index -> t) -> ArrayView a -> ArrayView a mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 \x o -> pure (f x o))) + foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Index b) (ArrayView a) -- | Traverses over each value, returning a new one. @@ -199,6 +248,7 @@ traverseWithIndex_ = traverseWithIndex_' <<< flip traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Index -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) + foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Index Unit) Unit -- | Test a predicate to pass on all values. @@ -212,6 +262,7 @@ allWithIndex = every <<< flip every :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect Boolean every p a = runEffectFn2 everyImpl a (mkFn2 p) + foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) Boolean -- | Test a predicate to pass on any value. @@ -224,6 +275,7 @@ anyWithIndex = some <<< flip some :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect Boolean some p a = runEffectFn2 someImpl a (mkFn2 p) + foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) Boolean -- | Returns a new typed array with all values that pass the predicate. @@ -238,11 +290,13 @@ filterWithIndex = filterWithIndex' <<< flip filterWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p) + foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (ArrayView a) -- | Tests if a value is an element of the typed array. elem :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect Boolean elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) + foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) Boolean -- | Fetch element at index. @@ -252,21 +306,25 @@ unsafeAt a o = runEffectFn2 unsafeAtImpl a o -- | Folding from the left. reduce :: forall a t b. TypedArray a t => (b -> t -> Index -> Effect b) -> b -> ArrayView a -> Effect b reduce f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Index c) c c -- | Folding from the left. Assumes the typed array is non-empty. reduce1 :: forall a t. Partial => TypedArray a t => (t -> t -> Index -> Effect t) -> ArrayView a -> Effect t reduce1 f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Index b) b -- | Folding from the right. reduceRight :: forall a t b. TypedArray a t => (t -> b -> Index -> Effect b) -> b -> ArrayView a -> Effect b reduceRight f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 \acc x o -> f x acc o) i + foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Index c) c c -- | Folding from the right. Assumes the typed array is non-empty. reduceRight1 :: forall a t. Partial => TypedArray a t => (t -> t -> Index -> Effect t) -> ArrayView a -> Effect t reduceRight1 f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 \acc x o -> f x acc o) + foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Index b) b -- | Returns the first value satisfying the predicate. @@ -280,21 +338,25 @@ findWithIndex = findWithIndex' <<< flip findWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f) + foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (Nullable b) -- | Returns the first index of the value satisfying the predicate. findIndex :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (Maybe Index) findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f) + foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (Nullable Index) -- | Returns the first index of the element, if it exists, from the left. indexOf :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect (Maybe Index) indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) + foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) (Nullable Index) -- | Returns the first index of the element, if it exists, from the right. lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect (Maybe Index) lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) + foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) (Nullable Index) -- | Fold a list from the left, accumulating the result using the @@ -330,23 +392,24 @@ foldr1 f = reduceRight1 \x a _ -> pure $ f a x -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. copyWithin :: forall a. ArrayView a -> Index -> Index -> Maybe Index -> Effect Unit copyWithin a t s me = runEffectFn4 copyWithinImpl a t s (toNullable me) + foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Index Index (Nullable Index) Unit -- | Reverses a typed array in-place. reverse :: forall a. ArrayView a -> Effect Unit reverse a = runEffectFn1 reverseImpl a -foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit +foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Index -> b -> Effect Boolean -setInternal lenfn a mo b = +setInternal lenfn a mo b = do let o = fromMaybe 0 mo - in if o >= 0 && lenfn b <= length a - o - then runEffectFn3 setImpl a o b *> pure true - else pure false -foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Index b Unit - + if o >= 0 && lenfn b <= length a - o then + runEffectFn3 setImpl a o b *> pure true + else + pure false +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Index b Unit -- | Stores multiple values in the typed array, reading input values from the second typed array. setTyped :: forall a. ArrayView a -> Maybe Index -> ArrayView a -> Effect Boolean @@ -355,36 +418,43 @@ setTyped = setInternal length -- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. slice :: forall a. Index -> Index -> ArrayView a -> Effect (ArrayView a) slice s e a = runEffectFn3 sliceImpl a s e + foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Index Index (ArrayView a) -- | Sorts the values in-place. sort :: forall a. ArrayView a -> Effect Unit sort a = runEffectFn1 sortImpl a + foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit -- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. subArray :: forall a. Index -> Index -> ArrayView a -> ArrayView a subArray s e a = runFn3 subArrayImpl a s e + foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Index Index (ArrayView a) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. toString :: forall a. ArrayView a -> Effect String toString a = runEffectFn1 toStringImpl a + foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String -- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. join :: forall a. String -> ArrayView a -> Effect String join s a = runEffectFn2 joinImpl a s + foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String -- | Determine if a certain index is valid. hasIndex :: forall a. ArrayView a -> Index -> Boolean hasIndex a o = runFn2 hasIndexImpl a o + foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Index Boolean -- | Fetch element at index. at :: forall a t. TypedArray a t => ArrayView a -> Index -> Effect (Maybe t) at a n = toMaybe <$> runEffectFn2 unsafeAtImpl a n + foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Index b infixl 3 at as ! @@ -392,6 +462,7 @@ infixl 3 at as ! -- | Turn typed array into an array. toArray :: forall a t. TypedArray a t => ArrayView a -> Effect (Array t) toArray a = runEffectFn1 toArrayImpl a + foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b) -- | Compare 2 typed arrays. diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index 678c14d..b2091ff 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -14,12 +14,12 @@ import Data.Unfoldable (replicateA) import Effect.Unsafe (unsafePerformEffect) import Prelude (bind, bottom, negate, pure, top, ($), (-), (/), (<$>)) - -genTypedArray :: forall m a t - . MonadGen m - => TypedArray a t - => m t - -> m (ArrayView a) +genTypedArray + :: forall m a t + . MonadGen m + => TypedArray a t + => m t + -> m (ArrayView a) genTypedArray gen = sized \s -> do n <- chooseInt 0 s a <- replicateA n gen @@ -47,18 +47,20 @@ genFloat32 :: forall m. MonadGen m => m F.Float32 genFloat32 = F.fromNumber' <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 genFloat64 :: forall m. MonadGen m => m Number -genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+308/div) - where div = 4.0 +genFloat64 = chooseFloat ((-1.7976931348623157e+308) / div) (1.7976931348623157e+308 / div) + where + div = 4.0 -- | For generating some set of offsets residing inside the generated array data WithIndices a = WithIndices (Array TA.Index) (ArrayView a) -genWithIndices :: forall m a - . MonadGen m - => BytesPerType a - => Int -- Number of offsets residing inside the generated array - -> m (ArrayView a) - -> m (WithIndices a) +genWithIndices + :: forall m a + . MonadGen m + => BytesPerType a + => Int -- Number of offsets residing inside the generated array + -> m (ArrayView a) + -> m (WithIndices a) genWithIndices n gen = do xs <- gen let l = TA.length xs diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index e262b2c..70ac4f7 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -1,12 +1,11 @@ -- | This module represents type-level mappings between `ArrayViewType`s -- | and meaningful data. module Data.ArrayBuffer.ValueMapping - ( class BytesPerType - , byteWidth - , class BinaryValue - , class ShowArrayViewType - ) -where + ( class BytesPerType + , byteWidth + , class BinaryValue + , class ShowArrayViewType + ) where import Data.ArrayBuffer.Types (ArrayViewType, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped) import Data.Float32 (Float32) as F @@ -17,15 +16,32 @@ import Type.Proxy (Proxy) class BytesPerType (a :: ArrayViewType) where byteWidth :: (Proxy a) -> Int -instance bytesPerTypeInt8 :: BytesPerType Int8 where byteWidth _ = 1 -instance bytesPerTypeInt16 :: BytesPerType Int16 where byteWidth _ = 2 -instance bytesPerTypeInt32 :: BytesPerType Int32 where byteWidth _ = 4 -instance bytesPerTypeUint8 :: BytesPerType Uint8 where byteWidth _ = 1 -instance bytesPerTypeUint16 :: BytesPerType Uint16 where byteWidth _ = 2 -instance bytesPerTypeUint32 :: BytesPerType Uint32 where byteWidth _ = 4 -instance bytesPerTypeUint8Clamped :: BytesPerType Uint8Clamped where byteWidth _ = 1 -instance bytesPerTypeFloat32 :: BytesPerType Float32 where byteWidth _ = 4 -instance bytesPerTypeFloat64 :: BytesPerType Float64 where byteWidth _ = 8 +instance bytesPerTypeInt8 :: BytesPerType Int8 where + byteWidth _ = 1 + +instance bytesPerTypeInt16 :: BytesPerType Int16 where + byteWidth _ = 2 + +instance bytesPerTypeInt32 :: BytesPerType Int32 where + byteWidth _ = 4 + +instance bytesPerTypeUint8 :: BytesPerType Uint8 where + byteWidth _ = 1 + +instance bytesPerTypeUint16 :: BytesPerType Uint16 where + byteWidth _ = 2 + +instance bytesPerTypeUint32 :: BytesPerType Uint32 where + byteWidth _ = 4 + +instance bytesPerTypeUint8Clamped :: BytesPerType Uint8Clamped where + byteWidth _ = 1 + +instance bytesPerTypeFloat32 :: BytesPerType Float32 where + byteWidth _ = 4 + +instance bytesPerTypeFloat64 :: BytesPerType Float64 where + byteWidth _ = 8 -- | Maps a `TypedArray`’s binary casted value to its computable representation in JavaScript. class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t @@ -41,6 +57,7 @@ instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name + instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped" instance showArrayViewTypeViewUint32 :: ShowArrayViewType Uint32 "Uint32" instance showArrayViewTypeViewUint16 :: ShowArrayViewType Uint16 "Uint16" diff --git a/test/Main.purs b/test/Main.purs index 40066b3..4ad0759 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -8,6 +8,5 @@ import Test.Properties (propertiesTests) main :: Effect Unit main = do - log "Starting tests..." propertiesTests diff --git a/test/Properties.purs b/test/Properties.purs index 6bf5407..bb529fc 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -8,7 +8,6 @@ import Test.Properties.DataView (dataViewTests) import Test.Properties.TypedArray (typedArrayTests) import Test.Properties.Typed.Laws (typedArrayLaws) - propertiesTests :: Effect Unit propertiesTests = do do diff --git a/test/Properties/ArrayBuffer.purs b/test/Properties/ArrayBuffer.purs index 3be4160..8ab6fe4 100644 --- a/test/Properties/ArrayBuffer.purs +++ b/test/Properties/ArrayBuffer.purs @@ -1,2 +1 @@ module Test.Properties.ArrayBuffer where - diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index a981bf7..e7199bf 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -21,7 +21,6 @@ import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) import Type.Proxy (Proxy(..)) - dataViewTests :: Ref Int -> Effect Unit dataViewTests count = do log " - setBE x o => getBE o === Just x" @@ -29,9 +28,8 @@ dataViewTests count = do log " - setLE x o => getLE o === Just x" placingAValueIsThereTests DV.LE count - type TestableViewF a name t q = - Show t + Show t => Eq t => Ord t => Semiring t @@ -42,68 +40,88 @@ type TestableViewF a name t q = => WithOffsetAndValue a t -> q - -overAll :: forall q . Testable q - => Ref Int -> (forall a name t. TestableViewF a name t q) -> Effect Unit +overAll + :: forall q + . Testable q + => Ref Int + -> (forall a name t. TestableViewF a name t q) + -> Effect Unit overAll count f = do void (Ref.modify (_ + 1) count) log " - Uint32" - quickCheckGen $ - let f' :: TestableViewF Uint32 "Uint32" UInt q - f' = f - in f' <$> genWithOffsetAndValue 4 genDataView genUint32 + quickCheckGen do + let + f' :: TestableViewF Uint32 "Uint32" UInt q + f' = f + + f' <$> genWithOffsetAndValue 4 genDataView genUint32 log " - Uint16" - quickCheckGen $ - let f' :: TestableViewF Uint16 "Uint16" UInt q - f' = f - in f' <$> genWithOffsetAndValue 2 genDataView genUint16 + quickCheckGen do + let + f' :: TestableViewF Uint16 "Uint16" UInt q + f' = f + + f' <$> genWithOffsetAndValue 2 genDataView genUint16 log " - Uint8" - quickCheckGen $ - let f' :: TestableViewF Uint8 "Uint8" UInt q - f' = f - in f' <$> genWithOffsetAndValue 1 genDataView genUint8 + quickCheckGen do + let + f' :: TestableViewF Uint8 "Uint8" UInt q + f' = f + + f' <$> genWithOffsetAndValue 1 genDataView genUint8 log " - Int32" - quickCheckGen $ - let f' :: TestableViewF Int32 "Int32" Int q - f' = f - in f' <$> genWithOffsetAndValue 4 genDataView genInt32 + quickCheckGen do + let + f' :: TestableViewF Int32 "Int32" Int q + f' = f + + f' <$> genWithOffsetAndValue 4 genDataView genInt32 log " - Int16" - quickCheckGen $ - let f' :: TestableViewF Int16 "Int16" Int q - f' = f - in f' <$> genWithOffsetAndValue 2 genDataView genInt16 + quickCheckGen do + let + f' :: TestableViewF Int16 "Int16" Int q + f' = f + + f' <$> genWithOffsetAndValue 2 genDataView genInt16 log " - Int8" - quickCheckGen $ - let f' :: TestableViewF Int8 "Int8" Int q - f' = f - in f' <$> genWithOffsetAndValue 1 genDataView genInt8 + quickCheckGen do + let + f' :: TestableViewF Int8 "Int8" Int q + f' = f + + f' <$> genWithOffsetAndValue 1 genDataView genInt8 log " - Float32" - quickCheckGen $ - let f' :: TestableViewF Float32 "Float32" F.Float32 q - f' = f - in f' <$> genWithOffsetAndValue 4 genDataView genFloat32 + quickCheckGen do + let + f' :: TestableViewF Float32 "Float32" F.Float32 q + f' = f + + f' <$> genWithOffsetAndValue 4 genDataView genFloat32 log " - Float64" - quickCheckGen $ - let f' :: TestableViewF Float64 "Float64" Number q - f' = f - in f' <$> genWithOffsetAndValue 8 genDataView genFloat64 + quickCheckGen do + let + f' :: TestableViewF Float64 "Float64" Number q + f' = f + f' <$> genWithOffsetAndValue 8 genDataView genFloat64 placingAValueIsThereTests :: DV.Endian -> Ref Int -> Effect Unit placingAValueIsThereTests endian count = overAll count placingAValueIsThere where - placingAValueIsThere :: forall a name t. TestableViewF a name t Result - placingAValueIsThere (WithOffsetAndValue os t xs) = - let o = unsafePartial $ Array.head os - prx = Proxy :: Proxy a - in unsafePerformEffect do - _ <- DV.set endian prx xs o t - my <- DV.get endian prx xs o - pure (my === Just t) + placingAValueIsThere :: forall a name t. TestableViewF a name t Result + placingAValueIsThere (WithOffsetAndValue os t xs) = do + let + o = unsafePartial $ Array.head os + prx = Proxy :: Proxy a + + unsafePerformEffect do + _ <- DV.set endian prx xs o t + my <- DV.get endian prx xs o + pure (my === Just t) diff --git a/test/Properties/Typed/Laws.purs b/test/Properties/Typed/Laws.purs index 8cf2f5c..b58f08c 100644 --- a/test/Properties/Typed/Laws.purs +++ b/test/Properties/Typed/Laws.purs @@ -1,8 +1,6 @@ module Test.Properties.Typed.Laws where import Prelude --- import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>), (<$>)) --- import Prelude (class Eq, class Monoid, class Ord, class Semigroup, Unit, discard, void, ($), (+), (<$>), (<<<)) import Data.ArrayBuffer.Typed (class TypedArray, toString) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8) import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, ArrayViewType) @@ -29,20 +27,28 @@ class ArrayEl (a :: ArrayViewType) (t :: Type) where instance arrayElUint8Clamped :: ArrayEl Uint8Clamped UInt where arb _ = genUint8 + instance arrayElUint32 :: ArrayEl Uint32 UInt where arb _ = genUint32 + instance arrayElUint16 :: ArrayEl Uint16 UInt where arb _ = genUint16 + instance arrayElUint8 :: ArrayEl Uint8 UInt where arb _ = genUint8 + instance arrayElInt32 :: ArrayEl Int32 Int where arb _ = genInt32 + instance arrayElInt16 :: ArrayEl Int16 Int where arb _ = genInt16 + instance arrayElInt8 :: ArrayEl Int8 Int where arb _ = genInt8 + instance arrayElFloat32 :: ArrayEl Float32 F.Float32 where arb _ = genFloat32 + instance arrayElFloat64 :: ArrayEl Float64 Number where arb _ = genFloat64 @@ -121,12 +127,14 @@ instance eqArrayView :: (TypedArray a t, Eq t) => Eq (AV a t) where instance showArrayView :: (TypedArray a t, Show t) => Show (AV a t) where show (AV a) = "T[" <> s <> "]" - where s = unsafePerformEffect $ toString a + where + s = unsafePerformEffect $ toString a instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t) where append (AV a) (AV b) = unsafePerformEffect do - let la = TA.length a - lb = TA.length b + let + la = TA.length a + lb = TA.length b r <- TA.empty $ la + lb void $ TA.setTyped r (Just 0) a void $ TA.setTyped r (Just la) b @@ -138,4 +146,4 @@ instance monoidArrayView :: TypedArray a t => Monoid (AV a t) where instance arbitraryArrayView :: (TypedArray a t, Arbitrary t) => Arbitrary (AV a t) where arbitrary = do xs <- arbitrary - pure $ unsafePerformEffect $ AV <$> TA.fromArray xs \ No newline at end of file + pure $ unsafePerformEffect $ AV <$> TA.fromArray xs diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index 9b054c0..36dfc8c 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -1,6 +1,5 @@ module Test.Properties.TypedArray where - import Prelude import Control.Monad.Gen (suchThat) @@ -107,9 +106,8 @@ typedArrayTests count = do log " - copyWithin o x == setTyped x (slice o x)" copyWithinViaSetTypedTests count - type TestableArrayF a t q = - Show t + Show t => Eq t => Ord t => Semiring t @@ -119,31 +117,36 @@ type TestableArrayF a t q = -> Effect q overAll' - :: forall q. Testable q + :: forall q + . Testable q => Int -- n -> Int -- “minimum n”? - -> Ref Int -> (forall a t. TestableArrayF a t q) -> Effect Unit + -> Ref Int + -> (forall a t. TestableArrayF a t q) + -> Effect Unit overAll' n mn count f = do void (Ref.modify (_ + 1) count) - let chk - :: forall a t - . Show t - => Eq t - => Ord t - => Semiring t - => BytesPerType a - => TypedArray a t - => String - -> Int - -> Proxy (ArrayView a) - -> Gen t - -> Effect Unit - chk s n' _ gen = do - log $ " - " <> s - quickCheckGen $ unsafePerformEffect <<< f <$> genWithIndices n' arr - where arr :: Gen (ArrayView a) - arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs + let + chk + :: forall a t + . Show t + => Eq t + => Ord t + => Semiring t + => BytesPerType a + => TypedArray a t + => String + -> Int + -> Proxy (ArrayView a) + -> Gen t + -> Effect Unit + chk s n' _ gen = do + log $ " - " <> s + quickCheckGen $ unsafePerformEffect <<< f <$> genWithIndices n' arr + where + arr :: Gen (ArrayView a) + arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs chk "Uint8ClampedArray" n (Proxy :: Proxy Uint8ClampedArray) genUint8 chk "Uint32Array" n (Proxy :: Proxy Uint32Array) genUint32 @@ -155,564 +158,541 @@ overAll' n mn count f = do chk "Float32Array" n (Proxy :: Proxy Float32Array) genFloat32 chk "Float64Array" n (Proxy :: Proxy Float64Array) genFloat64 - -overAll :: forall q. Testable q - => Int -> Ref Int -> (forall a t. TestableArrayF a t q) -> Effect Unit +overAll + :: forall q + . Testable q + => Int + -> Ref Int + -> (forall a t. TestableArrayF a t q) + -> Effect Unit overAll n count f = overAll' n 0 count f -overAll1 :: forall q. Testable q - => Int -> Ref Int -> (forall a t. TestableArrayF a t q) -> Effect Unit +overAll1 + :: forall q + . Testable q + => Int + -> Ref Int + -> (forall a t. TestableArrayF a t q) + -> Effect Unit overAll1 n count f = overAll' n 1 count f subarrayBehavesLikeArraySliceTests :: Ref Int -> Effect Unit subarrayBehavesLikeArraySliceTests count = overAll 2 count f where - f :: forall a t. TestableArrayF a t Result - f (WithIndices os xs) = do - let s = unsafePartial $ os `Array.unsafeIndex` 0 - e = unsafePartial $ os `Array.unsafeIndex` 1 - axs <- TA.toArray xs - let sxs = TA.subArray s e xs - a <- TA.toArray sxs - pure $ Array.slice s e axs === a + f :: forall a t. TestableArrayF a t Result + f (WithIndices os xs) = do + let + s = unsafePartial $ os `Array.unsafeIndex` 0 + e = unsafePartial $ os `Array.unsafeIndex` 1 + axs <- TA.toArray xs + let sxs = TA.subArray s e xs + a <- TA.toArray sxs + pure $ Array.slice s e axs === a sliceBehavesLikeArraySliceTests :: Ref Int -> Effect Unit sliceBehavesLikeArraySliceTests count = overAll 2 count f where - f :: forall a t. TestableArrayF a t Result - f (WithIndices os xs) = do - let s = unsafePartial $ os `Array.unsafeIndex` 0 - e = unsafePartial $ os `Array.unsafeIndex` 1 - axs <- TA.toArray xs - sxs <- TA.slice s e xs - a <- TA.toArray sxs - pure $ Array.slice s e axs === a + f :: forall a t. TestableArrayF a t Result + f (WithIndices os xs) = do + let + s = unsafePartial $ os `Array.unsafeIndex` 0 + e = unsafePartial $ os `Array.unsafeIndex` 1 + axs <- TA.toArray xs + sxs <- TA.slice s e xs + a <- TA.toArray sxs + pure $ Array.slice s e axs === a partBehavesLikeTakeDropTests :: Ref Int -> Effect Unit partBehavesLikeTakeDropTests count = overAll 0 count f where - f :: forall a t. TestableArrayF a t Result - f (WithIndices _ xs) = do - let n = 2 - axs <- TA.toArray xs - pxs <- TA.part (TA.buffer xs) n n :: Effect (ArrayView a) - aps <- TA.toArray pxs - pure $ Array.take n (Array.drop n axs) === aps + f :: forall a t. TestableArrayF a t Result + f (WithIndices _ xs) = do + let n = 2 + axs <- TA.toArray xs + pxs <- TA.part (TA.buffer xs) n n :: Effect (ArrayView a) + aps <- TA.toArray pxs + pure $ Array.take n (Array.drop n axs) === aps byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit byteLengthDivBytesPerValueTests count = overAll 0 count byteLengthDivBytesPerValueEqLength where - byteLengthDivBytesPerValueEqLength :: forall a t. TestableArrayF a t Result - byteLengthDivBytesPerValueEqLength (WithIndices _ xs) = - let b = byteWidth (Proxy :: Proxy a) - in pure $ TA.length xs === (TA.byteLength xs `div` b) + byteLengthDivBytesPerValueEqLength :: forall a t. TestableArrayF a t Result + byteLengthDivBytesPerValueEqLength (WithIndices _ xs) = do + let b = byteWidth (Proxy :: Proxy a) + pure $ TA.length xs === (TA.byteLength xs `div` b) fromArrayToArrayIsoTests :: Ref Int -> Effect Unit fromArrayToArrayIsoTests count = overAll 0 count fromArrayToArrayIso where - fromArrayToArrayIso :: forall a t. TestableArrayF a t Result - fromArrayToArrayIso (WithIndices _ xs) = do - axs <- TA.toArray xs - xs' <- TA.fromArray axs :: Effect (ArrayView a) - axs' <- TA.toArray xs' - pure $ axs' === axs - + fromArrayToArrayIso :: forall a t. TestableArrayF a t Result + fromArrayToArrayIso (WithIndices _ xs) = do + axs <- TA.toArray xs + xs' <- TA.fromArray axs :: Effect (ArrayView a) + axs' <- TA.toArray xs' + pure $ axs' === axs allAreFilledTests :: Ref Int -> Effect Unit allAreFilledTests count = overAll 0 count allAreFilled where - allAreFilled :: forall a t. TestableArrayF a t Result - allAreFilled (WithIndices _ xs) = do - e <- TA.at xs 0 - let x = fromMaybe zero e - l = TA.length xs - TA.fill x 0 l xs - b <- TA.all (_ == x) xs - pure (b "All aren't the filled value") - + allAreFilled :: forall a t. TestableArrayF a t Result + allAreFilled (WithIndices _ xs) = do + e <- TA.at xs 0 + let + x = fromMaybe zero e + l = TA.length xs + TA.fill x 0 l xs + b <- TA.all (_ == x) xs + pure (b "All aren't the filled value") setSingletonIsEqTests :: Ref Int -> Effect Unit setSingletonIsEqTests count = overAll 1 count setSingletonIsEq where - setSingletonIsEq :: forall a t. TestableArrayF a t Result - setSingletonIsEq (WithIndices os xs) = do - e <- TA.at xs 0 - case e of - Nothing -> pure Success - Just x -> do - let o = unsafePartial $ Array.head os - _ <- TA.set xs (Just o) [x] - e' <- TA.at xs o - pure $ e' === Just x - + setSingletonIsEq :: forall a t. TestableArrayF a t Result + setSingletonIsEq (WithIndices os xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success + Just x -> do + let o = unsafePartial $ Array.head os + _ <- TA.set xs (Just o) [ x ] + e' <- TA.at xs o + pure $ e' === Just x -- | Should work with any arbitrary predicate, but we can't generate them allImpliesAnyTests :: Ref Int -> Effect Unit allImpliesAnyTests count = overAll 0 count allImpliesAny where - allImpliesAny :: forall a t. TestableArrayF a t Result - allImpliesAny (WithIndices _ xs) = do - let pred x = x /= zero - all'' <- TA.all pred xs - let all' = all'' "All don't satisfy the predicate" - any'' <- TA.any pred xs - let any' = any'' "None satisfy the predicate" - pure $ (TA.length xs === zero) `xor` all' `implies` any' - + allImpliesAny :: forall a t. TestableArrayF a t Result + allImpliesAny (WithIndices _ xs) = do + let pred x = x /= zero + all'' <- TA.all pred xs + let all' = all'' "All don't satisfy the predicate" + any'' <- TA.any pred xs + let any' = any'' "None satisfy the predicate" + pure $ (TA.length xs === zero) `xor` all' `implies` any' -- | Should work with any arbitrary predicate, but we can't generate them filterImpliesAllTests :: Ref Int -> Effect Unit filterImpliesAllTests count = overAll 0 count filterImpliesAll where - filterImpliesAll :: forall a t. TestableArrayF a t Result - filterImpliesAll (WithIndices _ xs) = do - let pred x = x /= zero - ys <- TA.filter pred xs - all' <- TA.all pred ys - pure $ all' "Filter doesn't imply all" - + filterImpliesAll :: forall a t. TestableArrayF a t Result + filterImpliesAll (WithIndices _ xs) = do + let pred x = x /= zero + ys <- TA.filter pred xs + all' <- TA.all pred ys + pure $ all' "Filter doesn't imply all" -- | Should work with any arbitrary predicate, but we can't generate them filterIsTotalTests :: Ref Int -> Effect Unit filterIsTotalTests count = overAll 0 count filterIsTotal where - filterIsTotal :: forall a t. TestableArrayF a t Result - filterIsTotal (WithIndices _ xs) = do - let pred x = x /= zero - ys <- TA.filter pred xs - zs <- TA.filter (not pred) ys - azs <- TA.toArray zs - pure $ azs === [] - + filterIsTotal :: forall a t. TestableArrayF a t Result + filterIsTotal (WithIndices _ xs) = do + let pred x = x /= zero + ys <- TA.filter pred xs + zs <- TA.filter (not pred) ys + azs <- TA.toArray zs + pure $ azs === [] -- | Should work with any arbitrary predicate, but we can't generate them filterIsIdempotentTests :: Ref Int -> Effect Unit filterIsIdempotentTests count = overAll 0 count filterIsIdempotent where - filterIsIdempotent :: forall a t. TestableArrayF a t Result - filterIsIdempotent (WithIndices _ xs) = do - let pred x = x /= zero - ys <- TA.filter pred xs - zs <- TA.filter pred ys - azs <- TA.toArray zs - ays <- TA.toArray ys - pure $ azs === ays - + filterIsIdempotent :: forall a t. TestableArrayF a t Result + filterIsIdempotent (WithIndices _ xs) = do + let pred x = x /= zero + ys <- TA.filter pred xs + zs <- TA.filter pred ys + azs <- TA.toArray zs + ays <- TA.toArray ys + pure $ azs === ays withIndicesHasIndexTests :: Ref Int -> Effect Unit withIndicesHasIndexTests count = overAll1 5 count withIndicesHasIndex where - withIndicesHasIndex :: forall a t. TestableArrayF a t Result - withIndicesHasIndex (WithIndices os xs) = pure $ - Array.all (TA.hasIndex xs) os "All doesn't have index of itself" - + withIndicesHasIndex :: forall a t. TestableArrayF a t Result + withIndicesHasIndex (WithIndices os xs) = pure $ + Array.all (TA.hasIndex xs) os "All doesn't have index of itself" withIndicesElemTests :: Ref Int -> Effect Unit withIndicesElemTests count = overAll1 5 count withIndicesElem where - withIndicesElem :: forall a t. TestableArrayF a t Result - withIndicesElem (WithIndices os xs) = do - let fetch o = TA.at xs o - exs <- traverse fetch os - pure $ Array.all isJust exs "All doesn't have an elem of itself" - + withIndicesElem :: forall a t. TestableArrayF a t Result + withIndicesElem (WithIndices os xs) = do + let fetch o = TA.at xs o + exs <- traverse fetch os + pure $ Array.all isJust exs "All doesn't have an elem of itself" -- | Should work with any arbitrary predicate, but we can't generate them anyImpliesFindTests :: Ref Int -> Effect Unit anyImpliesFindTests count = overAll 0 count anyImpliesFind where - anyImpliesFind :: forall a t. TestableArrayF a t Result - anyImpliesFind (WithIndices _ xs) = do - let pred x = x /= zero - a <- TA.any pred xs - let p = a "All don't satisfy the predicate" - idx <- TA.find pred xs - let q = case idx of - Nothing -> Failed "Doesn't have a value satisfying the predicate" - Just z -> if pred z - then Success - else Failed "Found value doesn't satisfy the predicate" - pure $ p `implies` q - + anyImpliesFind :: forall a t. TestableArrayF a t Result + anyImpliesFind (WithIndices _ xs) = do + let pred x = x /= zero + a <- TA.any pred xs + let p = a "All don't satisfy the predicate" + idx <- TA.find pred xs + let + q = case idx of + Nothing -> Failed "Doesn't have a value satisfying the predicate" + Just z -> + if pred z then Success + else Failed "Found value doesn't satisfy the predicate" + pure $ p `implies` q -- | Should work with any arbitrary predicate, but we can't generate them findIndexImpliesAtTests :: Ref Int -> Effect Unit findIndexImpliesAtTests count = overAll 0 count findIndexImpliesAt where - findIndexImpliesAt :: forall a t. TestableArrayF a t Result - findIndexImpliesAt (WithIndices _ xs) = do - let pred x _ = x /= zero - mo <- TA.findIndex pred xs - case mo of - Nothing -> pure Success - Just o -> do - e <- TA.at xs o - case e of - Nothing -> pure $ Failed "No value at found index" - Just x -> pure $ pred x o "Find index implies at" - - + findIndexImpliesAt :: forall a t. TestableArrayF a t Result + findIndexImpliesAt (WithIndices _ xs) = do + let pred x _ = x /= zero + mo <- TA.findIndex pred xs + case mo of + Nothing -> pure Success + Just o -> do + e <- TA.at xs o + case e of + Nothing -> pure $ Failed "No value at found index" + Just x -> pure $ pred x o "Find index implies at" indexOfImpliesAtTests :: Ref Int -> Effect Unit indexOfImpliesAtTests count = overAll 1 count indexOfImpliesAt where - indexOfImpliesAt :: forall a t. TestableArrayF a t Result - indexOfImpliesAt (WithIndices _ xs) = do - e <- TA.at xs 0 - case e of - Nothing -> pure Success - Just y -> do - idx <- TA.indexOf y Nothing xs - case idx of - Nothing -> pure $ Failed "no index of" - Just o -> do - e' <- TA.at xs o - pure $ e' === Just y - + indexOfImpliesAt :: forall a t. TestableArrayF a t Result + indexOfImpliesAt (WithIndices _ xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success + Just y -> do + idx <- TA.indexOf y Nothing xs + case idx of + Nothing -> pure $ Failed "no index of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit lastIndexOfImpliesAtTests count = overAll 0 count lastIndexOfImpliesAt where - lastIndexOfImpliesAt :: forall a t. TestableArrayF a t Result - lastIndexOfImpliesAt (WithIndices _ xs) = do - e <- TA.at xs 0 - case e of - Nothing -> pure Success - Just y -> do - idx <- TA.lastIndexOf y Nothing xs - case idx of - Nothing -> pure $ Failed "no lastIndex of" - Just o -> do - e' <- TA.at xs o - pure $ e' === Just y - + lastIndexOfImpliesAt :: forall a t. TestableArrayF a t Result + lastIndexOfImpliesAt (WithIndices _ xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success + Just y -> do + idx <- TA.lastIndexOf y Nothing xs + case idx of + Nothing -> pure $ Failed "no lastIndex of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y foldrConsIsToArrayTests :: Ref Int -> Effect Unit foldrConsIsToArrayTests count = overAll 0 count foldrConsIsToArray where - foldrConsIsToArray :: forall a t. TestableArrayF a t Result - foldrConsIsToArray (WithIndices _ xs) = do - axs <- TA.toArray xs - rxs <- TA.foldr Array.cons [] xs - pure $ rxs === axs - + foldrConsIsToArray :: forall a t. TestableArrayF a t Result + foldrConsIsToArray (WithIndices _ xs) = do + axs <- TA.toArray xs + rxs <- TA.foldr Array.cons [] xs + pure $ rxs === axs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit foldlSnocIsToArrayTests count = overAll 0 count foldlSnocIsToArray where - foldlSnocIsToArray :: forall a t. TestableArrayF a t Result - foldlSnocIsToArray (WithIndices _ xs) = do - axs <- TA.toArray xs - rxs <- TA.foldl Array.snoc [] xs - pure $ rxs === axs - + foldlSnocIsToArray :: forall a t. TestableArrayF a t Result + foldlSnocIsToArray (WithIndices _ xs) = do + axs <- TA.toArray xs + rxs <- TA.foldl Array.snoc [] xs + pure $ rxs === axs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit mapIdentityIsIdentityTests count = overAll 0 count mapIdentityIsIdentity where - mapIdentityIsIdentity :: forall a t. TestableArrayF a t Result - mapIdentityIsIdentity (WithIndices _ xs) = do - axs <- TA.toArray xs - mxs <- TA.toArray (TA.map identity xs) - pure $ axs === mxs - + mapIdentityIsIdentity :: forall a t. TestableArrayF a t Result + mapIdentityIsIdentity (WithIndices _ xs) = do + axs <- TA.toArray xs + mxs <- TA.toArray (TA.map identity xs) + pure $ axs === mxs traverseSnocIsToArrayTests :: Ref Int -> Effect Unit traverseSnocIsToArrayTests count = overAll 0 count traverseSnocIsToArray where - traverseSnocIsToArray :: forall a t. TestableArrayF a t Result - traverseSnocIsToArray (WithIndices _ xs) = do - ref <- Ref.new [] - TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs - ys <- Ref.read ref - axs <- TA.toArray xs - pure $ axs === ys - + traverseSnocIsToArray :: forall a t. TestableArrayF a t Result + traverseSnocIsToArray (WithIndices _ xs) = do + ref <- Ref.new [] + TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs + ys <- Ref.read ref + axs <- TA.toArray xs + pure $ axs === ys doubleReverseIsIdentityTests :: Ref Int -> Effect Unit doubleReverseIsIdentityTests count = overAll 0 count doubleReverseIsIdentity where - doubleReverseIsIdentity :: forall a t. TestableArrayF a t Result - doubleReverseIsIdentity (WithIndices _ xs) = do - axs <- TA.toArray xs - TA.reverse xs - TA.reverse xs - axs' <- TA.toArray xs - pure $ axs === axs' - + doubleReverseIsIdentity :: forall a t. TestableArrayF a t Result + doubleReverseIsIdentity (WithIndices _ xs) = do + axs <- TA.toArray xs + TA.reverse xs + TA.reverse xs + axs' <- TA.toArray xs + pure $ axs === axs' reverseIsArrayReverseTests :: Ref Int -> Effect Unit reverseIsArrayReverseTests count = overAll 0 count reverseIsArrayReverse where - reverseIsArrayReverse :: forall a t. TestableArrayF a t Result - reverseIsArrayReverse (WithIndices _ xs) = do - axs <- TA.toArray xs - TA.reverse xs - rxs <- TA.toArray xs - pure $ Array.reverse axs === rxs - + reverseIsArrayReverse :: forall a t. TestableArrayF a t Result + reverseIsArrayReverse (WithIndices _ xs) = do + axs <- TA.toArray xs + TA.reverse xs + rxs <- TA.toArray xs + pure $ Array.reverse axs === rxs sortIsIdempotentTests :: Ref Int -> Effect Unit sortIsIdempotentTests count = overAll 0 count sortIsIdempotent where - sortIsIdempotent :: forall a t. TestableArrayF a t Result - sortIsIdempotent (WithIndices _ xs) = do - TA.sort xs - ys <- TA.toArray xs - TA.sort xs - zs <- TA.toArray xs - pure $ zs === ys - + sortIsIdempotent :: forall a t. TestableArrayF a t Result + sortIsIdempotent (WithIndices _ xs) = do + TA.sort xs + ys <- TA.toArray xs + TA.sort xs + zs <- TA.toArray xs + pure $ zs === ys sortIsArraySortTests :: Ref Int -> Effect Unit sortIsArraySortTests count = overAll 0 count sortIsArraySort where - sortIsArraySort :: forall a t. TestableArrayF a t Result - sortIsArraySort (WithIndices _ xs) = do - axs <- TA.toArray xs - let ys = Array.sort axs - TA.sort xs - sxs <- TA.toArray xs - pure $ sxs === ys - + sortIsArraySort :: forall a t. TestableArrayF a t Result + sortIsArraySort (WithIndices _ xs) = do + axs <- TA.toArray xs + let ys = Array.sort axs + TA.sort xs + sxs <- TA.toArray xs + pure $ sxs === ys toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit toStringIsJoinWithCommaTests count = overAll 0 count toStringIsJoinWithComma where - toStringIsJoinWithComma :: forall a t. TestableArrayF a t Result - toStringIsJoinWithComma (WithIndices _ xs) = do - s1 <- TA.join "," xs - s2 <- TA.toString xs - pure $ s1 === s2 - + toStringIsJoinWithComma :: forall a t. TestableArrayF a t Result + toStringIsJoinWithComma (WithIndices _ xs) = do + s1 <- TA.join "," xs + s2 <- TA.toString xs + pure $ s1 === s2 setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit setTypedOfSubArrayIsIdentityTests count = overAll 0 count setTypedOfSubArrayIsIdentity where - setTypedOfSubArrayIsIdentity :: forall a t. TestableArrayF a t Result - setTypedOfSubArrayIsIdentity (WithIndices _ xs) = do - ys <- TA.toArray xs - let l = TA.length xs - zsSub = TA.subArray 0 l xs - _ <- TA.setTyped xs Nothing zsSub - zs <- TA.toArray xs - pure $ zs === ys - + setTypedOfSubArrayIsIdentity :: forall a t. TestableArrayF a t Result + setTypedOfSubArrayIsIdentity (WithIndices _ xs) = do + ys <- TA.toArray xs + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + _ <- TA.setTyped xs Nothing zsSub + zs <- TA.toArray xs + pure $ zs === ys modifyingOriginalMutatesSubArrayTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayTests count = overAll 0 count modifyingOriginalMutatesSubArray where - modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result - modifyingOriginalMutatesSubArray (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray zsSub - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs /== ys - + modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArray (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalTests count = overAll 0 count modifyingOriginalMutatesSubArray where - modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result - modifyingOriginalMutatesSubArray (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray xs - TA.fill zero 0 l zsSub - ys <- TA.toArray xs - pure $ zs /== ys - + modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArray (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray xs + TA.fill zero 0 l zsSub + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayZeroTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayZeroTests count = overAll 0 count modifyingOriginalMutatesSubArrayZero where - modifyingOriginalMutatesSubArrayZero :: forall a t. TestableArrayF a t Result - modifyingOriginalMutatesSubArrayZero (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray zsSub - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs /== ys - + modifyingOriginalMutatesSubArrayZero :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArrayZero (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalZeroTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalZeroTests count = overAll 0 count modifyingSubArrayMutatesOriginalZero where - modifyingSubArrayMutatesOriginalZero :: forall a t. TestableArrayF a t Result - modifyingSubArrayMutatesOriginalZero (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray xs - TA.fill zero 0 l zsSub - ys <- TA.toArray xs - pure $ zs /== ys - + modifyingSubArrayMutatesOriginalZero :: forall a t. TestableArrayF a t Result + modifyingSubArrayMutatesOriginalZero (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray xs + TA.fill zero 0 l zsSub + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayAllTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayAllTests count = overAll 0 count modifyingOriginalMutatesSubArrayAll where - modifyingOriginalMutatesSubArrayAll :: forall a t. TestableArrayF a t Result - modifyingOriginalMutatesSubArrayAll (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray zsSub - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs /== ys - + modifyingOriginalMutatesSubArrayAll :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArrayAll (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalAllTests :: Ref Int -> Effect Unit modifyingSubArrayMutatesOriginalAllTests count = overAll 0 count modifyingSubArrayMutatesOriginalAll where - modifyingSubArrayMutatesOriginalAll :: forall a t. TestableArrayF a t Result - modifyingSubArrayMutatesOriginalAll (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray xs - TA.fill zero 0 l zsSub - ys <- TA.toArray xs - pure $ zs /== ys - + modifyingSubArrayMutatesOriginalAll :: forall a t. TestableArrayF a t Result + modifyingSubArrayMutatesOriginalAll (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray xs + TA.fill zero 0 l zsSub + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayPartTests :: Ref Int -> Effect Unit modifyingOriginalMutatesSubArrayPartTests count = overAll 1 count modifyingOriginalMutatesSubArrayPart where - modifyingOriginalMutatesSubArrayPart :: forall a t. TestableArrayF a t Result - modifyingOriginalMutatesSubArrayPart (WithIndices os xs) = do - let o = unsafePartial $ Array.head os - l = TA.length xs - zsSub = TA.subArray 0 l xs - zs <- TA.toArray zsSub - if o == 0 || Array.all (eq zero) zs - then pure Success - else do - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs /== ys - + modifyingOriginalMutatesSubArrayPart :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArrayPart (WithIndices os xs) = do + let + o = unsafePartial $ Array.head os + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + if o == 0 || Array.all (eq zero) zs then pure Success + else do + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingOriginalDoesntMutateSliceTests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSliceTests count = overAll 0 count modifyingOriginalDoesntMutateSlice where - modifyingOriginalDoesntMutateSlice :: forall a t. TestableArrayF a t Result - modifyingOriginalDoesntMutateSlice (WithIndices _ xs) = do - axs <- TA.toArray xs - if Array.all (eq zero) axs - then pure Success - else do - let l = TA.length xs - zsSub <- TA.slice 0 l xs - zs <- TA.toArray zsSub - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs === ys - + modifyingOriginalDoesntMutateSlice :: forall a t. TestableArrayF a t Result + modifyingOriginalDoesntMutateSlice (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let l = TA.length xs + zsSub <- TA.slice 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs === ys modifyingOriginalDoesntMutateSlicePartTests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSlicePartTests count = overAll 1 count modifyingOriginalDoesntMutateSlicePart where - modifyingOriginalDoesntMutateSlicePart :: forall a t. TestableArrayF a t Result - modifyingOriginalDoesntMutateSlicePart (WithIndices os xs) = do - let l = TA.length xs - axs <- TA.toArray =<< TA.slice 0 l xs - let o = unsafePartial $ Array.head os - e <- TA.at xs o - if Array.all (eq zero) axs || e == Just zero - then pure Success - else do - zsSub <- TA.slice o l xs - zs <- TA.toArray zsSub - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs === ys - + modifyingOriginalDoesntMutateSlicePart :: forall a t. TestableArrayF a t Result + modifyingOriginalDoesntMutateSlicePart (WithIndices os xs) = do + let l = TA.length xs + axs <- TA.toArray =<< TA.slice 0 l xs + let o = unsafePartial $ Array.head os + e <- TA.at xs o + if Array.all (eq zero) axs || e == Just zero then pure Success + else do + zsSub <- TA.slice o l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs === ys modifyingOriginalDoesntMutateSlicePart2Tests :: Ref Int -> Effect Unit modifyingOriginalDoesntMutateSlicePart2Tests count = overAll 1 count modifyingOriginalDoesntMutateSlicePart2 where - modifyingOriginalDoesntMutateSlicePart2 :: forall a t. TestableArrayF a t Result - modifyingOriginalDoesntMutateSlicePart2 (WithIndices os xs) = do - let o = unsafePartial $ Array.head os - l = TA.length xs - axs <- TA.toArray =<< TA.slice o l xs - e <- TA.at xs o - if Array.all (eq zero) axs || e == Just zero - then pure Success - else do - zsSub <- TA.slice o l xs - zs <- TA.toArray zsSub - TA.fill zero 0 l xs - ys <- TA.toArray zsSub - pure $ zs === ys - + modifyingOriginalDoesntMutateSlicePart2 :: forall a t. TestableArrayF a t Result + modifyingOriginalDoesntMutateSlicePart2 (WithIndices os xs) = do + let + o = unsafePartial $ Array.head os + l = TA.length xs + axs <- TA.toArray =<< TA.slice o l xs + e <- TA.at xs o + if Array.all (eq zero) axs || e == Just zero then pure Success + else do + zsSub <- TA.slice o l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs === ys copyWithinSelfIsIdentityTests :: Ref Int -> Effect Unit copyWithinSelfIsIdentityTests count = overAll 0 count copyWithinSelfIsIdentity where - copyWithinSelfIsIdentity :: forall a t. TestableArrayF a t Result - copyWithinSelfIsIdentity (WithIndices _ xs) = do - ys <- TA.toArray xs - TA.copyWithin xs 0 0 (Just (TA.length xs)) - zs <- TA.toArray xs - pure $ zs === ys - + copyWithinSelfIsIdentity :: forall a t. TestableArrayF a t Result + copyWithinSelfIsIdentity (WithIndices _ xs) = do + ys <- TA.toArray xs + TA.copyWithin xs 0 0 (Just (TA.length xs)) + zs <- TA.toArray xs + pure $ zs === ys copyWithinIsSliceTests :: Ref Int -> Effect Unit copyWithinIsSliceTests count = overAll 1 count copyWithinIsSlice where - copyWithinIsSlice :: forall a t. TestableArrayF a t Result - copyWithinIsSlice (WithIndices os xs) = do - let o = unsafePartial $ Array.head os - l = TA.length xs - ys <- TA.toArray =<< TA.slice o l xs - TA.copyWithin xs 0 o Nothing - axs <- TA.toArray xs - zs <- pure $ Array.drop (Array.length ys) axs - pure $ axs === ys <> zs - + copyWithinIsSlice :: forall a t. TestableArrayF a t Result + copyWithinIsSlice (WithIndices os xs) = do + let + o = unsafePartial $ Array.head os + l = TA.length xs + ys <- TA.toArray =<< TA.slice o l xs + TA.copyWithin xs 0 o Nothing + axs <- TA.toArray xs + zs <- pure $ Array.drop (Array.length ys) axs + pure $ axs === ys <> zs copyWithinViaSetTypedTests :: Ref Int -> Effect Unit copyWithinViaSetTypedTests count = overAll 1 count copyWithinViaSetTyped where - copyWithinViaSetTyped :: forall a t. TestableArrayF a t Result - copyWithinViaSetTyped (WithIndices os xs) = do - let o = unsafePartial $ Array.head os - txs <- TA.toArray xs - xs' <- TA.fromArray txs :: Effect (ArrayView a) - let l = TA.length xs' - ys <- TA.slice o l xs' - _ <- TA.setTyped xs' Nothing ys - TA.copyWithin xs 0 o Nothing - axs <- TA.toArray xs - axs' <- TA.toArray xs' - pure $ axs === axs' + copyWithinViaSetTyped :: forall a t. TestableArrayF a t Result + copyWithinViaSetTyped (WithIndices os xs) = do + let o = unsafePartial $ Array.head os + txs <- TA.toArray xs + xs' <- TA.fromArray txs :: Effect (ArrayView a) + let l = TA.length xs' + ys <- TA.slice o l xs' + _ <- TA.setTyped xs' Nothing ys + TA.copyWithin xs 0 o Nothing + axs <- TA.toArray xs + axs' <- TA.toArray xs' + pure $ axs === axs' -- | Uses the second failure message as the result failure message -- | https://github.com/athanclark/purescript-quickcheck-combinators/blob/293e5af07ae47b61d4eae5defef4c0f472bfa9ca/src/Test/QuickCheck/Combinators.purs#L62 @@ -729,9 +709,12 @@ xor :: Result -> Result -> Result xor = xor' ", xor " "XOR" where -- Combine two results with "Exclusive Or" logic, and with a failure message separator and failure message if they are both `Success` - xor' :: String -- ^ Separator - -> String -- ^ Success failure message - -> Result -> Result -> Result + xor' + :: String -- ^ Separator + -> String -- ^ Success failure message + -> Result + -> Result + -> Result xor' m s x y = case Tuple x y of Tuple (Failed x') (Failed y') -> Failed (x' <> m <> y') Tuple Success Success -> Failed s From a5033499ee92a6cff0b7307f62ae14b067cc6de2 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Tue, 22 Mar 2022 14:40:25 -0500 Subject: [PATCH 226/236] Update to PureScript v0.15.0 (#41) * Migrated FFI to ES modules via 'lebab' * Replaced 'export var' with 'export const' * Removed '"use strict";' in FFI files * Update to CI to use 'unstable' purescript * Add CI test: verify 'bower.json' file works via pulp * Ignore spago-based tests (temporarily) * Update Bower dependencies to master or main * Update packages.dhall to 'prepare-0.15' package set * Fix FFI exports * Update SProxy to use Proxy * Installed bower dev dependency: purescript-quickcheck-laws@main * Fix Proxy import * Add changelog entry * Update .github/workflows/ci.yml Co-authored-by: Thomas Honeyman Co-authored-by: Thomas Honeyman --- .github/workflows/ci.yml | 14 ++- CHANGELOG.md | 6 ++ bower.json | 59 +++++------ packages.dhall | 8 +- src/Data/ArrayBuffer/ArrayBuffer.js | 12 +-- src/Data/ArrayBuffer/DataView.js | 34 +++---- src/Data/ArrayBuffer/DataView.purs | 6 +- src/Data/ArrayBuffer/Typed.js | 148 +++++++++++++--------------- test/Properties/Typed/Laws.purs | 2 +- 9 files changed, 147 insertions(+), 142 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4aaad3f..f5d37bc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,6 +16,7 @@ jobs: - name: Set up a PureScript toolchain uses: purescript-contrib/setup-purescript@main with: + purescript: "unstable" purs-tidy: "latest" - name: Cache PureScript dependencies @@ -38,8 +39,17 @@ jobs: - name: Build tests run: spago -x spago-test.dhall build --no-install --purs-args '--censor-lib --strict' - - name: Run tests - run: spago -x spago-test.dhall test --no-install +# - name: Run tests +# run: spago -x spago-test.dhall test --no-install - name: Check formatting run: purs-tidy check src test + + - name: Verify Bower & Pulp + run: | + npm install bower pulp@16.0.0-0 + npx bower install + npx pulp build -- --censor-lib --strict + if [ -d "test" ]; then + npx pulp test + fi diff --git a/CHANGELOG.md b/CHANGELOG.md index 16ed4a7..982d560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +Breaking Changes: +- Migrate FFI to ES modules (#41 by @JordanMartinez) +- Replaced polymorphic proxies with monomorphic `Proxy` (#41 by @JordanMartinez) + ## v12.0.0 Delete the `TypedArray` polyfill which was preventing this diff --git a/bower.json b/bower.json index 4d6947c..85fd87c 100644 --- a/bower.json +++ b/bower.json @@ -1,30 +1,33 @@ { - "name": "purescript-arraybuffer", - "license": [ - "MIT" - ], - "repository": { - "type": "git", - "url": "https://github.com/purescript-contrib/purescript-arraybuffer" - }, - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "output" - ], - "dependencies": { - "purescript-arraybuffer-types": "^v3.0.1", - "purescript-arrays": "^v6.0.1", - "purescript-effect": "^v3.0.0", - "purescript-float32": "^v1.0.0", - "purescript-functions": "^v5.0.0", - "purescript-gen": "^v3.0.0", - "purescript-maybe": "^v5.0.0", - "purescript-nullable": "^v5.0.0", - "purescript-prelude": "^v5.0.1", - "purescript-tailrec": "^v5.0.1", - "purescript-uint": "^v6.0.3", - "purescript-unfoldable": "^v5.0.0" - } + "name": "purescript-arraybuffer", + "license": [ + "MIT" + ], + "repository": { + "type": "git", + "url": "https://github.com/purescript-contrib/purescript-arraybuffer" + }, + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "output" + ], + "dependencies": { + "purescript-arraybuffer-types": "main", + "purescript-arrays": "master", + "purescript-effect": "master", + "purescript-float32": "main", + "purescript-functions": "master", + "purescript-gen": "master", + "purescript-maybe": "master", + "purescript-nullable": "main", + "purescript-prelude": "master", + "purescript-tailrec": "master", + "purescript-uint": "main", + "purescript-unfoldable": "master" + }, + "devDependencies": { + "purescript-quickcheck-laws": "main" + } } diff --git a/packages.dhall b/packages.dhall index bea1ce8..582d6d3 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,8 +1,4 @@ let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c + https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall -let overrides = {=} - -let additions = {=} - -in upstream // overrides // additions +in upstream diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index b341921..9bb2fc1 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -1,15 +1,13 @@ -"use strict"; - // module Data.ArrayBuffer.ArrayBuffer -exports.emptyImpl = function empty (s) { +export function emptyImpl(s) { return new ArrayBuffer(s); }; -exports.byteLength = function byteLength (a) { +export function byteLength(a) { return a.byteLength; -}; +} -exports.sliceImpl = function sliceImpl (a, s, e) { +export function sliceImpl(a, s, e) { return a.slice(s, e); -}; +} diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index 6bfee81..1eeb060 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -1,43 +1,41 @@ -"use strict"; - // module Data.ArrayBuffer.DataView -exports.whole = function whole (b) { +export function whole(b) { return new DataView(b); -}; +} -exports.remainderImpl = function remainderImpl (b,i) { +export function remainderImpl(b, i) { return new DataView(b,i); -}; +} -exports.partImpl = function partImpl (b,i,j) { +export function partImpl(b, i, j) { return new DataView(b,i,j); -}; +} -exports.buffer = function buffer (v) { +export function buffer(v) { return v.buffer; -}; +} -exports.byteOffset = function byteOffset (v) { +export function byteOffset(v) { return v.byteOffset; -}; +} -exports.byteLength = function byteLength (v) { +export function byteLength(v) { return v.byteLength; -}; +} -exports.getterImpl = function getterImpl (data, v, o) { +export function getterImpl(data, v, o) { return ((o + data.bytesPerValue) >>> 0) <= v.byteLength ? v[data.functionName].call(v,o,data.littleEndian) : null; -}; +} -exports.setterImpl = function setterImpl (data, v, o, n) { +export function setterImpl(data, v, o, n) { if (((o + data.bytesPerValue) >>> 0) <= v.byteLength) { v[data.functionName].call(v,o,n,data.littleEndian); return true; } else { return false; } -}; +} diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 610090e..badab60 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -49,7 +49,7 @@ import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, cla import Data.Float32 (Float32) as F import Data.Maybe (Maybe) import Data.Nullable (Nullable, toMaybe) -import Data.Symbol (SProxy(..), class IsSymbol, reflectSymbol) +import Data.Symbol (class IsSymbol, reflectSymbol) import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4) @@ -131,7 +131,7 @@ get get endian prx = do let le = endian == LE - pnm = "get" <> reflectSymbol (SProxy :: SProxy name) + pnm = "get" <> reflectSymbol (Proxy :: Proxy name) bpv = byteWidth prx getter @@ -203,7 +203,7 @@ set set endian prx = do let le = endian == LE - pnm = "set" <> reflectSymbol (SProxy :: SProxy name) + pnm = "set" <> reflectSymbol (Proxy :: Proxy name) bpv = byteWidth prx setter diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index 2ab0cb6..fc19771 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,20 +1,18 @@ -"use strict"; - // module Data.ArrayBuffer.Typed -exports.buffer = function buffer (v) { +export function buffer(v) { return v.buffer; -}; +} -exports.byteOffset = function byteOffset (v) { +export function byteOffset(v) { return v.byteOffset; -}; +} -exports.byteLength = function byteLength (v) { +export function byteLength(v) { return v.byteLength; -}; +} -exports.lengthImpl = function lemgthImpl (v) { +export function lengthImpl(v) { return v.length; }; @@ -36,130 +34,126 @@ function newArray (f) { }; } -exports.newUint8ClampedArray = newArray(Uint8ClampedArray); -exports.newUint32Array = newArray(Uint32Array); -exports.newUint16Array = newArray(Uint16Array); -exports.newUint8Array = newArray(Uint8Array); -exports.newInt32Array = newArray(Int32Array); -exports.newInt16Array = newArray(Int16Array); -exports.newInt8Array = newArray(Int8Array); -exports.newFloat32Array = newArray(Float32Array); -exports.newFloat64Array = newArray(Float64Array); - +export const newUint8ClampedArray = newArray(Uint8ClampedArray); +export const newUint32Array = newArray(Uint32Array); +export const newUint16Array = newArray(Uint16Array); +export const newUint8Array = newArray(Uint8Array); +export const newInt32Array = newArray(Int32Array); +export const newInt16Array = newArray(Int16Array); +export const newInt8Array = newArray(Int8Array); +export const newFloat32Array = newArray(Float32Array); +export const newFloat64Array = newArray(Float64Array); // ------ -exports.everyImpl = function everyImpl (a,p) { +export function everyImpl(a, p) { return a.every(p); -}; -exports.someImpl = function someImpl (a,p) { - return a.some(p); -}; +} +export function someImpl(a, p) { + return a.some(p); +} -exports.fillImpl = function fillImpl (x, s, e, a) { +export function fillImpl(x, s, e, a) { return a.fill(x,s,e); -}; - +} -exports.mapImpl = function mapImpl (a,f) { +export function mapImpl(a, f) { return a.map(f); -}; +} -exports.forEachImpl = function forEachImpl (a,f) { +export function forEachImpl(a, f) { a.forEach(f); -}; +} -exports.filterImpl = function filterImpl (a,p) { +export function filterImpl(a, p) { return a.filter(p); -}; +} -exports.includesImpl = function includesImpl (a,x,mo) { +export function includesImpl(a, x, mo) { return mo === null ? a.includes(x) : a.includes(x,mo); -}; +} -exports.reduceImpl = function reduceImpl (a,f,i) { +export function reduceImpl(a, f, i) { return a.reduce(f,i); -}; -exports.reduce1Impl = function reduce1Impl (a,f) { +} + +export function reduce1Impl(a, f) { return a.reduce(f); -}; -exports.reduceRightImpl = function reduceRightImpl (a,f,i) { +} + +export function reduceRightImpl(a, f, i) { return a.reduceRight(f,i); -}; -exports.reduceRight1Impl = function reduceRight1Impl (a,f) { +} + +export function reduceRight1Impl(a, f) { return a.reduceRight(f); -}; +} -exports.findImpl = function findImpl (a,f) { +export function findImpl(a, f) { return a.find(f); -}; +} -exports.findIndexImpl = function findIndexImpl (a,f) { +export function findIndexImpl(a, f) { var r = a.findIndex(f); return r === -1 ? null : r; -}; -exports.indexOfImpl = function indexOfImpl (a,x,mo) { +} + +export function indexOfImpl(a, x, mo) { var r = mo === null ? a.indexOf(x) : a.indexOf(x,mo); return r === -1 ? null : r; -}; -exports.lastIndexOfImpl = function lastIndexOfImpl (a,x,mo) { +} + +export function lastIndexOfImpl(a, x, mo) { var r = mo === null ? a.lastIndexOf(x) : a.lastIndexOf(x,mo); return r === -1 ? null : r; -}; - - +} -exports.copyWithinImpl = function copyWithinImpl (a,t,s,me) { +export function copyWithinImpl(a, t, s, me) { if (me === null) { a.copyWithin(t,s); } else { a.copyWithin(t,s,me); } -}; - +} -exports.reverseImpl = function reverseImpl (a) { +export function reverseImpl(a) { a.reverse(); -}; - +} -exports.setImpl = function setImpl (a, off, b) { +export function setImpl(a, off, b) { a.set(b,off); -}; - +} -exports.sliceImpl = function sliceImpl (a, s, e) { +export function sliceImpl(a, s, e) { return a.slice(s,e); -}; +} -exports.sortImpl = function sortImpl (a) { +export function sortImpl(a) { a.sort(); -}; - +} -exports.subArrayImpl = function subArrayImpl (a, s, e) { +export function subArrayImpl(a, s, e) { return a.subarray(s, e); -}; - +} -exports.toStringImpl = function toStringImpl (a) { +export function toStringImpl(a) { return a.toString(); -}; +} -exports.joinImpl = function joinImpl (a,s) { +export function joinImpl(a, s) { return a.join(s); -}; +} -exports.unsafeAtImpl = function(a, i) { +export function unsafeAtImpl(a, i) { return a[i]; } -exports.hasIndexImpl = function(a, i) { +export function hasIndexImpl(a, i) { return i in a; } -exports.toArrayImpl = function(a) { +export function toArrayImpl(a) { var l = a.length; var ret = new Array(l); for (var i = 0; i < l; i++) diff --git a/test/Properties/Typed/Laws.purs b/test/Properties/Typed/Laws.purs index b58f08c..21ac0dd 100644 --- a/test/Properties/Typed/Laws.purs +++ b/test/Properties/Typed/Laws.purs @@ -12,7 +12,7 @@ import Effect.Ref as Ref import Test.QuickCheck (class Arbitrary, arbitrary) import Test.QuickCheck.Gen (Gen) import Test.QuickCheck.Laws.Data (checkEq, checkMonoid, checkOrd, checkSemigroup) -import Type.Prelude (Proxy(..)) +import Type.Proxy (Proxy(..)) import Data.ArrayBuffer.Typed as TA import Data.Generic.Rep (class Generic) import Data.Maybe (Maybe(..)) From 3b5ba3040f060057e864805546a50f319853517d Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Wed, 27 Apr 2022 18:08:50 -0500 Subject: [PATCH 227/236] Prepare v13.0.0 release (1st PS 0.15.0-compatible release) (#44) * Update the bower dependencies * Uncomment spago tests * Update the changelog --- .github/workflows/ci.yml | 4 ++-- CHANGELOG.md | 10 ++++++++++ bower.json | 26 +++++++++++++------------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5d37bc..a9d110d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,8 +39,8 @@ jobs: - name: Build tests run: spago -x spago-test.dhall build --no-install --purs-args '--censor-lib --strict' -# - name: Run tests -# run: spago -x spago-test.dhall test --no-install + - name: Run tests + run: spago -x spago-test.dhall test --no-install - name: Check formatting run: purs-tidy check src test diff --git a/CHANGELOG.md b/CHANGELOG.md index 982d560..ad44db1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ Notable changes to this project are documented in this file. The format is based ## Unreleased +Breaking changes: + +New features: + +Bugfixes: + +Other improvements: + +## [v13.0.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.0.0) - 2022-04-27 + Breaking Changes: - Migrate FFI to ES modules (#41 by @JordanMartinez) - Replaced polymorphic proxies with monomorphic `Proxy` (#41 by @JordanMartinez) diff --git a/bower.json b/bower.json index 85fd87c..74106e6 100644 --- a/bower.json +++ b/bower.json @@ -14,20 +14,20 @@ "output" ], "dependencies": { - "purescript-arraybuffer-types": "main", - "purescript-arrays": "master", - "purescript-effect": "master", - "purescript-float32": "main", - "purescript-functions": "master", - "purescript-gen": "master", - "purescript-maybe": "master", - "purescript-nullable": "main", - "purescript-prelude": "master", - "purescript-tailrec": "master", - "purescript-uint": "main", - "purescript-unfoldable": "master" + "purescript-arraybuffer-types": "^3.0.2", + "purescript-arrays": "^7.0.0", + "purescript-effect": "^4.0.0", + "purescript-float32": "^2.0.0", + "purescript-functions": "^6.0.0", + "purescript-gen": "^4.0.0", + "purescript-maybe": "^6.0.0", + "purescript-nullable": "^6.0.0", + "purescript-prelude": "^6.0.0", + "purescript-tailrec": "^6.0.0", + "purescript-uint": "^7.0.0", + "purescript-unfoldable": "^6.0.0" }, "devDependencies": { - "purescript-quickcheck-laws": "main" + "purescript-quickcheck-laws": "^7.0.0" } } From bae93c3368828195fe8bd44add0e1a665e793eb6 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 1 Dec 2022 17:34:51 +0900 Subject: [PATCH 228/236] New module: Data.ArrayBuffer.Cast (#46) * Upgrade packages.dhall * New module: Data.ArrayBuffer.Cast --- CHANGELOG.md | 2 ++ packages.dhall | 3 +- spago-test.dhall | 1 - src/Data/ArrayBuffer/Cast.purs | 46 ++++++++++++++++++++++++++ src/Data/ArrayBuffer/ValueMapping.purs | 7 ++-- 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/Data/ArrayBuffer/Cast.purs diff --git a/CHANGELOG.md b/CHANGELOG.md index ad44db1..7d83901 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Breaking changes: New features: +- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock) + Bugfixes: Other improvements: diff --git a/packages.dhall b/packages.dhall index 582d6d3..2ffa9a7 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,4 +1,5 @@ let upstream = - https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall + https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221102/packages.dhall + sha256:8628e413718876ce26983db1d0ce9d9e1588129117fa3bb8ed9f618db6914127 in upstream diff --git a/spago-test.dhall b/spago-test.dhall index ab1a012..7037d90 100644 --- a/spago-test.dhall +++ b/spago-test.dhall @@ -5,7 +5,6 @@ in conf // { , "foldable-traversable" , "partial" , "refs" - , "typelevel-prelude" , "tuples" , "quickcheck" , "quickcheck-laws" diff --git a/src/Data/ArrayBuffer/Cast.purs b/src/Data/ArrayBuffer/Cast.purs new file mode 100644 index 0000000..10e5b0a --- /dev/null +++ b/src/Data/ArrayBuffer/Cast.purs @@ -0,0 +1,46 @@ +-- | `DataView` represents unaligned memory of unknown endianness. +-- | +-- | `ArrayView` represents arrays of aligned elements of +-- | local-machine endianness. +-- | For the cases of `Int8Array`, `Uint8Array`, `Uint8ClampedArray`, +-- | the elements +-- | are single bytes, so they are always aligned and they have no +-- | endianness. Therefore in those cases we can freely cast back and forth +-- | to `DataView`. +module Data.ArrayBuffer.Cast + ( fromInt8Array + , fromUint8Array + , fromUint8ClampedArray + , toInt8Array + , toUint8Array + , toUint8ClampedArray + ) where + +import Data.ArrayBuffer.DataView as DV +import Data.ArrayBuffer.Typed as AT +import Data.ArrayBuffer.Types (DataView, Uint8Array, Uint8ClampedArray, Int8Array) +import Effect (Effect) + +-- | Cast an `Int8Array` to a `DataView`. +fromInt8Array :: Int8Array -> Effect DataView +fromInt8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to an `Int8Array`. +toInt8Array :: DataView -> Effect Int8Array +toInt8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) + +-- | Cast a `UInt8Array` to a `DataView`. +fromUint8Array :: Uint8Array -> Effect DataView +fromUint8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to a `Uint8Array`. +toUint8Array :: DataView -> Effect Uint8Array +toUint8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) + +-- | Cast a `UInt8ClampedArray` to a `DataView`. +fromUint8ClampedArray :: Uint8ClampedArray -> Effect DataView +fromUint8ClampedArray x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to a `Uint8ClampedArray`. +toUint8ClampedArray :: DataView -> Effect Uint8ClampedArray +toUint8ClampedArray x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 70ac4f7..8694a19 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -12,7 +12,8 @@ import Data.Float32 (Float32) as F import Data.UInt (UInt) import Type.Proxy (Proxy) --- | Map of each `ArrayViewType` to the number of bytes of storage it requires. +-- | Type-level map of each `ArrayViewType` to the number of bytes of storage +-- | it requires. class BytesPerType (a :: ArrayViewType) where byteWidth :: (Proxy a) -> Int @@ -43,7 +44,8 @@ instance bytesPerTypeFloat32 :: BytesPerType Float32 where instance bytesPerTypeFloat64 :: BytesPerType Float64 where byteWidth _ = 8 --- | Maps a `TypedArray`’s binary casted value to its computable representation in JavaScript. +-- | Type-level map of `TypedArray`’s binary casted value to its +-- | representation in JavaScript. class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt @@ -56,6 +58,7 @@ instance binaryValueInt8 :: BinaryValue Int8 Int instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number +-- | Type-level map of `TypedArray` to its element type name. class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped" From 42073e2b36742a8f0f10ccfe8237853746501cba Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 1 Dec 2022 17:38:15 +0900 Subject: [PATCH 229/236] Prep for v13.1.0 --- CHANGELOG.md | 8 +++++-- bower.json | 59 +++++++++++++++++++++++++--------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d83901..7e1187c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,12 +8,16 @@ Breaking changes: New features: -- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock) - Bugfixes: Other improvements: +## [v13.1.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.1.0) - 2022-12-01 + +New features: + +- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock) + ## [v13.0.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.0.0) - 2022-04-27 Breaking Changes: diff --git a/bower.json b/bower.json index 74106e6..d7d9ae7 100644 --- a/bower.json +++ b/bower.json @@ -1,33 +1,30 @@ { - "name": "purescript-arraybuffer", - "license": [ - "MIT" - ], - "repository": { - "type": "git", - "url": "https://github.com/purescript-contrib/purescript-arraybuffer" - }, - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "output" - ], - "dependencies": { - "purescript-arraybuffer-types": "^3.0.2", - "purescript-arrays": "^7.0.0", - "purescript-effect": "^4.0.0", - "purescript-float32": "^2.0.0", - "purescript-functions": "^6.0.0", - "purescript-gen": "^4.0.0", - "purescript-maybe": "^6.0.0", - "purescript-nullable": "^6.0.0", - "purescript-prelude": "^6.0.0", - "purescript-tailrec": "^6.0.0", - "purescript-uint": "^7.0.0", - "purescript-unfoldable": "^6.0.0" - }, - "devDependencies": { - "purescript-quickcheck-laws": "^7.0.0" - } + "name": "purescript-arraybuffer", + "license": [ + "MIT" + ], + "repository": { + "type": "git", + "url": "https://github.com/purescript-contrib/purescript-arraybuffer" + }, + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "output" + ], + "dependencies": { + "purescript-arraybuffer-types": "^v3.0.2", + "purescript-arrays": "^v7.1.0", + "purescript-effect": "^v4.0.0", + "purescript-float32": "^v2.0.0", + "purescript-functions": "^v6.0.0", + "purescript-gen": "^v4.0.0", + "purescript-maybe": "^v6.0.0", + "purescript-nullable": "^v6.0.0", + "purescript-prelude": "^v6.0.1", + "purescript-tailrec": "^v6.1.0", + "purescript-uint": "^v7.0.0", + "purescript-unfoldable": "^v6.0.0" + } } From 374df82368b9b43cae6b71515c0a21de0e0c3592 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 1 Dec 2022 17:48:03 +0900 Subject: [PATCH 230/236] =?UTF-8?q?v13.0.0=20=E2=86=92=20v13.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 1b5d3fb046e97547c54e9ffe1d7081c7e7457620 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 1 Dec 2022 17:57:45 +0900 Subject: [PATCH 231/236] Remove npx pulp test from CI (#47) --- .github/workflows/ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9d110d..28243e2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,6 +50,3 @@ jobs: npm install bower pulp@16.0.0-0 npx bower install npx pulp build -- --censor-lib --strict - if [ -d "test" ]; then - npx pulp test - fi From 6cc31540ad2bd3ba717f084717e5210938308498 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 1 Dec 2022 18:00:13 +0900 Subject: [PATCH 232/236] =?UTF-8?q?v13.1.0=20=E2=86=92=20v13.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From fe2b1d5c7c7be3995dd7e912ddd051e4b1867916 Mon Sep 17 00:00:00 2001 From: James Brock Date: Thu, 1 Dec 2022 18:24:39 +0900 Subject: [PATCH 233/236] README web-encoding instead of text-encoding --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c89142a..ee4c15d 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ These are some other packages which provide more `ArrayBuffer` features. ### UTF -* [__text-encoding__](https://pursuit.purescript.org/packages/purescript-text-encoding) +* [__web-encoding__](https://pursuit.purescript.org/packages/purescript-web-encoding) ### Base64 From 464ddc8917252db0290da8d5851d9179346bd37b Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 13 Feb 2023 15:36:58 +0900 Subject: [PATCH 234/236] Correct `Typed` `slice` and `subArray` docs (#51) --- CHANGELOG.md | 2 ++ src/Data/ArrayBuffer/Typed.purs | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e1187c..6c00e6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ Bugfixes: Other improvements: +- Correct `Typed` `slice` and `subArray` docs (#51 by @jamesdbrock) + ## [v13.1.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.1.0) - 2022-12-01 New features: diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b8d593b..e46132d 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -23,8 +23,8 @@ -- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array -- | - `find` and `findIndex` are searching functions via a predicate -- | - `indexOf` and `lastIndexOf` are searching functions via equality --- | - `slice` returns a new typed array on the same array buffer content as the input --- | - `subArray` returns a new typed array with a separate array buffer +-- | - `slice` returns a new typed array with a new copied underlying `ArrayBuffer` +-- | - `subArray` returns a new typed array view of the same `ArrayBuffer` -- | - `toString` prints to a CSV, `join` allows you to supply the delimiter -- | - `toArray` returns an array of numeric values @@ -211,7 +211,7 @@ set = setInternal A.length ap1 :: forall a b c. (a -> c) -> (a -> b -> c) ap1 f = \x _ -> f x --- | Maps a new value over the typed array, creating a new buffer and +-- | Maps a new value over the typed array, creating a new `ArrayBuffer` and -- | typed array as well. map :: forall a t. TypedArray a t => (t -> t) -> ArrayView a -> ArrayView a map = mapWithIndex' <<< ap1 @@ -415,7 +415,8 @@ foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Index b Unit setTyped :: forall a. ArrayView a -> Maybe Index -> ArrayView a -> Effect Boolean setTyped = setInternal length --- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. +-- | Copy part of the contents of a typed array into a new `ArrayBuffer`, +-- | between the start and end indices. slice :: forall a. Index -> Index -> ArrayView a -> Effect (ArrayView a) slice s e a = runEffectFn3 sliceImpl a s e @@ -427,7 +428,8 @@ sort a = runEffectFn1 sortImpl a foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit --- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. +-- | Returns a new typed array view of the same `ArrayBuffer`, beginning at +-- | the index and ending at the second. subArray :: forall a. Index -> Index -> ArrayView a -> ArrayView a subArray s e a = runFn3 subArrayImpl a s e From e70974bffa51d622441d8ea47cec2b9fc715f3ee Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 13 Feb 2023 15:39:29 +0900 Subject: [PATCH 235/236] Prep for v13.2.0 purs 15.7 --- CHANGELOG.md | 4 ++++ bower.json | 2 +- packages.dhall | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c00e6a..fb92693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ Bugfixes: Other improvements: +## [v13.2.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.2.0) - 2023-02-13 + +Other improvements: + - Correct `Typed` `slice` and `subArray` docs (#51 by @jamesdbrock) ## [v13.1.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.1.0) - 2022-12-01 diff --git a/bower.json b/bower.json index d7d9ae7..e0cb70c 100644 --- a/bower.json +++ b/bower.json @@ -15,7 +15,7 @@ ], "dependencies": { "purescript-arraybuffer-types": "^v3.0.2", - "purescript-arrays": "^v7.1.0", + "purescript-arrays": "^v7.2.0", "purescript-effect": "^v4.0.0", "purescript-float32": "^v2.0.0", "purescript-functions": "^v6.0.0", diff --git a/packages.dhall b/packages.dhall index 2ffa9a7..8af7890 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,5 +1,5 @@ let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221102/packages.dhall - sha256:8628e413718876ce26983db1d0ce9d9e1588129117fa3bb8ed9f618db6914127 + https://github.com/purescript/package-sets/releases/download/psc-0.15.7-20230211/packages.dhall + sha256:c44fcd5b1b7a1adf85bbd4ed2eeb08865c44996bd0c8b1e1fdcd3dea8cfab914 in upstream From a1e62aa93360fcc363766024a3c737bb6b75d562 Mon Sep 17 00:00:00 2001 From: James Brock Date: Mon, 13 Feb 2023 15:50:39 +0900 Subject: [PATCH 236/236] =?UTF-8?q?v13.1.1=20=E2=86=92=20v13.2.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit