From 53c7b2369b842e82db52dfb7b44a836073b5f921 Mon Sep 17 00:00:00 2001 From: Athan Clark Date: Tue, 4 Dec 2018 10:29:21 -0700 Subject: [PATCH 001/126] 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 002/126] 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 003/126] 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 004/126] 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 005/126] 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 006/126] 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 007/126] 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 008/126] 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 009/126] 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 010/126] 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 011/126] 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 012/126] 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 013/126] 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 014/126] 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 015/126] 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 016/126] 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 017/126] 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 018/126] 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 019/126] 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 020/126] 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 021/126] 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 022/126] 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 023/126] 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 024/126] 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 025/126] 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 026/126] 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 027/126] 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 028/126] 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 029/126] 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 030/126] 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 031/126] 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 032/126] 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 033/126] 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 034/126] 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 035/126] 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 036/126] 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 037/126] 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 038/126] 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 039/126] 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 040/126] 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 041/126] 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 042/126] 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 043/126] 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 044/126] 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 045/126] 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 046/126] 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 047/126] 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 048/126] 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 049/126] 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 050/126] 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 051/126] 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 052/126] 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 053/126] 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 054/126] 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 055/126] 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 056/126] 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 057/126] 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 058/126] 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 059/126] 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 060/126] 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 061/126] 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 062/126] 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 063/126] 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 064/126] 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 065/126] 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 066/126] 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 067/126] 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 068/126] 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 069/126] 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 070/126] 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 071/126] 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 072/126] 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 073/126] 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 074/126] 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 075/126] 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 076/126] 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 077/126] 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 078/126] 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 079/126] 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 080/126] 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 081/126] 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 082/126] 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 083/126] 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 084/126] 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 085/126] 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 086/126] 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 087/126] 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 088/126] 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 089/126] 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 090/126] 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 091/126] 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 092/126] 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 093/126] 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 094/126] 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 095/126] 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 096/126] 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 097/126] 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 098/126] 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 099/126] 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 100/126] 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 101/126] ... 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 102/126] 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 103/126] 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 104/126] 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 105/126] 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 106/126] 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 107/126] 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 108/126] 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 109/126] 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 110/126] 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 111/126] 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 112/126] 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 113/126] 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 114/126] 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 115/126] 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 116/126] 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 117/126] 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 118/126] 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 119/126] 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 07a501f7e133ee64217cb8e9336fa06e53547b7a Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Thu, 24 Jan 2019 18:57:59 +0100 Subject: [PATCH 120/126] 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 121/126] 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 122/126] 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 123/126] 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 124/126] 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 125/126] 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 126/126] 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