Skip to content

Commit 8c59b1d

Browse files
committed
Changed fill/slice/subArray to be consistent with Array/Node.Buffer
1 parent f6caddc commit 8c59b1d

File tree

5 files changed

+94
-74
lines changed

5 files changed

+94
-74
lines changed

src/Data/ArrayBuffer/ArrayBuffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ exports.byteLength = function byteLength (a) {
1010
return a.byteLength;
1111
};
1212

13-
exports.sliceImpl = function sliceImpl (a, ms, me) {
14-
return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me);
13+
exports.sliceImpl = function sliceImpl (a, s, e) {
14+
return a.slice(s, e);
1515
};

src/Data/ArrayBuffer/ArrayBuffer.purs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ module Data.ArrayBuffer.ArrayBuffer
99

1010
import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength)
1111
import Data.Function.Uncurried (Fn3, runFn3)
12-
import Data.Maybe (Maybe(..))
13-
import Data.Nullable (Nullable, notNull, null)
14-
import Data.Tuple (Tuple(..))
1512
import Effect (Effect)
1613
import Effect.Uncurried (EffectFn1, runEffectFn1)
1714

@@ -26,12 +23,8 @@ empty l = runEffectFn1 emptyImpl l
2623
-- | Represents the length of an `ArrayBuffer` in bytes.
2724
foreign import byteLength :: ArrayBuffer -> ByteLength
2825

29-
foreign import sliceImpl :: Fn3 ArrayBuffer (Nullable ByteOffset) (Nullable ByteOffset) ArrayBuffer
26+
foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer
3027

3128
-- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive.
32-
slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer
33-
slice a mz = case mz of
34-
Nothing -> runFn3 sliceImpl a null null
35-
Just (Tuple s me) -> case me of
36-
Nothing -> runFn3 sliceImpl a (notNull s) null
37-
Just e -> runFn3 sliceImpl a (notNull s) (notNull e)
29+
slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer
30+
slice s e a = runFn3 sliceImpl a s e

src/Data/ArrayBuffer/Typed.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,17 @@ exports.setImpl = function setImpl (a, off, b) {
145145
};
146146

147147

148-
exports.sliceImpl = function sliceImpl (a,ms,me) {
149-
return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me);
148+
exports.sliceImpl = function sliceImpl (a, s, e) {
149+
return a.slice(s,e);
150150
};
151151

152-
153152
exports.sortImpl = function sortImpl (a) {
154153
a.sort();
155154
};
156155

157156

158-
exports.subArrayImpl = function subArrayImpl (a,ms,me) {
159-
return me === null ? (ms === null ? a.subarray() : a.subarray(ms)) : a.subarray(ms,me);
157+
exports.subArrayImpl = function subArrayImpl (a, s, e) {
158+
return a.subarray(s, e);
160159
};
161160

162161

src/Data/ArrayBuffer/Typed.purs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Data.ArrayBuffer.Typed
55
( polyFill
6-
, Offset, Length, Range
6+
, Offset, Length
77
, buffer, byteOffset, byteLength, length
88
, class TypedArray
99
, create, whole, remainder, part, empty, fromArray
@@ -28,7 +28,6 @@ import Data.Float32 (Float32) as F
2828
import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3)
2929
import Data.Maybe (Maybe(..), fromMaybe)
3030
import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable)
31-
import Data.Tuple (Tuple(..))
3231
import Data.Typelevel.Num (class Nat, toInt')
3332
import Data.UInt (UInt)
3433
import Effect (Effect)
@@ -75,7 +74,7 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N
7574
foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
7675
foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
7776

78-
foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b (Nullable Offset) (Nullable Offset) Unit
77+
foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit
7978

8079
foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a)
8180
foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit
@@ -96,10 +95,6 @@ type Offset = Int
9695
-- | Value-oriented array length
9796
type Length = Int
9897

99-
-- | Represents a range of indices, where if omitted, it represents the whole span.
100-
-- | If only the second argument is omitted, then it represents the remainder of the span after the first index.
101-
type Range = Maybe (Tuple Offset (Maybe Offset))
102-
10398

10499
-- TODO use purescript-quotient
105100
-- | Typeclass that associates a measured user-level type with a typed array.
@@ -184,10 +179,8 @@ fromArray :: forall a t. TypedArray a t => Array t -> Effect (ArrayView a)
184179
fromArray a = runEffectFn3 create a null null
185180

186181
-- | Fill the array with a value
187-
fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit
188-
fill a x mz = case mz of
189-
Nothing -> runEffectFn4 fillImpl a x null null
190-
Just (Tuple s me) -> runEffectFn4 fillImpl a x (notNull s) (toNullable me)
182+
fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit
183+
fill x s e a = runEffectFn4 fillImpl a x s e
191184

192185
-- | Stores multiple values into the typed array
193186
set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean
@@ -367,13 +360,11 @@ setTyped = setInternal length
367360

368361

369362
-- | Copy the entire contents of the typed array into a new buffer.
370-
foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a)
363+
foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a)
371364

372365
-- | Copy part of the contents of a typed array into a new buffer, between some start and end indices.
373-
slice :: forall a. ArrayView a -> Range -> Effect (ArrayView a)
374-
slice a mz = case mz of
375-
Nothing -> runEffectFn3 sliceImpl a null null
376-
Just (Tuple s me) -> runEffectFn3 sliceImpl a (notNull s) (toNullable me)
366+
slice :: forall a. Offset -> Offset -> ArrayView a -> Effect (ArrayView a)
367+
slice s e a = runEffectFn3 sliceImpl a s e
377368

378369
foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit
379370

@@ -382,7 +373,7 @@ sort :: forall a. ArrayView a -> Effect Unit
382373
sort a = runEffectFn1 sortImpl a
383374

384375

385-
foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a)
376+
foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a)
386377

387378
-- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second.
388379
-- |
@@ -391,10 +382,8 @@ foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nu
391382
-- | mutable replica of the original array - the sub-array reference reflects mutations to the original array.
392383
-- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves
393384
-- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`.
394-
subArray :: forall a. ArrayView a -> Range -> ArrayView a
395-
subArray a mz = case mz of
396-
Nothing -> runFn3 subArrayImpl a null null
397-
Just (Tuple s me) -> runFn3 subArrayImpl a (notNull s) (toNullable me)
385+
subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a
386+
subArray s e a = runFn3 subArrayImpl a s e
398387

399388
-- | 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.
400389
foreign import toString :: forall a. ArrayView a -> String

0 commit comments

Comments
 (0)