Skip to content

Commit f68f7cd

Browse files
committed
Moved *Impl functions near their call site
1 parent bf9c682 commit f68f7cd

File tree

3 files changed

+44
-70
lines changed

3 files changed

+44
-70
lines changed

src/Data/ArrayBuffer/ArrayBuffer.purs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@ import Data.Function.Uncurried (Fn3, runFn3)
1212
import Effect (Effect)
1313
import Effect.Uncurried (EffectFn1, runEffectFn1)
1414

15-
16-
foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer
17-
18-
1915
-- | Create an `ArrayBuffer` with the given capacity.
2016
empty :: ByteLength -> Effect ArrayBuffer
2117
empty l = runEffectFn1 emptyImpl l
18+
foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer
2219

2320
-- | Represents the length of an `ArrayBuffer` in bytes.
2421
foreign import byteLength :: ArrayBuffer -> ByteLength
2522

26-
foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer
27-
2823
-- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive.
2924
slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer
3025
slice s e a = runFn3 sliceImpl a s e
26+
foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer

src/Data/ArrayBuffer/DataView.purs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ import Type.Proxy (Proxy(..))
6666
-- | View mapping the whole `ArrayBuffer`.
6767
foreign import whole :: ArrayBuffer -> DataView
6868

69-
foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView
7069

7170
-- | View mapping the rest of an `ArrayBuffer` after an index.
7271
remainder :: ArrayBuffer -> ByteOffset -> Effect DataView
7372
remainder a o = runEffectFn2 remainderImpl a o
74-
75-
foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView
73+
foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView
7674

7775
-- | View mapping a region of the `ArrayBuffer`.
7876
part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView
7977
part a o l = runEffectFn3 partImpl a o l
78+
foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView
8079

8180
-- | `ArrayBuffer` being mapped by the view.
8281
foreign import buffer :: DataView -> ArrayBuffer
@@ -121,12 +120,6 @@ instance showArrayViewTypeViewInt8 :: ShowArrayViewType Int8 "Int8"
121120
instance showArrayViewTypeViewFloat32 :: ShowArrayViewType Float32 "Float32"
122121
instance showArrayViewTypeViewFloat64 :: ShowArrayViewType Float64 "Float64"
123122

124-
foreign import getterImpl :: forall t
125-
. EffectFn3 { functionName :: String
126-
, littleEndian :: Boolean
127-
, bytesPerValue :: ByteLength
128-
} DataView ByteOffset (Nullable t)
129-
130123
getter :: forall t.
131124
{ functionName :: String
132125
, bytesPerValue :: ByteLength
@@ -139,6 +132,12 @@ getter data' d o = toMaybe <$>
139132
, littleEndian: data'.littleEndian
140133
, bytesPerValue: data'.bytesPerValue
141134
} d o
135+
foreign import getterImpl :: forall t
136+
. EffectFn3 { functionName :: String
137+
, littleEndian :: Boolean
138+
, bytesPerValue :: ByteLength
139+
} DataView ByteOffset (Nullable t)
140+
142141

143142

144143
get :: forall a name t b
@@ -175,18 +174,18 @@ getLE :: forall a name t b
175174
=> AProxy a -> DataView -> ByteOffset -> Effect (Maybe t)
176175
getLE = get LE
177176

178-
foreign import setterImpl :: forall t
179-
. EffectFn4 { functionName :: String
180-
, littleEndian :: Boolean
181-
, bytesPerValue :: ByteLength
182-
} DataView ByteOffset t Boolean
183-
184177
setter :: forall t.
185178
{ functionName :: String
186179
, bytesPerValue :: ByteLength
187180
, littleEndian :: Boolean
188181
} -> DataView -> ByteOffset -> t -> Effect Boolean
189182
setter d o t = runEffectFn4 setterImpl d o t
183+
foreign import setterImpl :: forall t
184+
. EffectFn4 { functionName :: String
185+
, littleEndian :: Boolean
186+
, bytesPerValue :: ByteLength
187+
} DataView ByteOffset t Boolean
188+
190189

191190
set :: forall a name t b
192191
. DataView a t

src/Data/ArrayBuffer/Typed.purs

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ foreign import byteOffset :: forall a. ArrayView a -> ByteOffset
7777
-- | Represents the length of this typed array, in bytes.
7878
foreign import byteLength :: forall a. ArrayView a -> ByteLength
7979

80-
foreign import lengthImpl :: forall a. ArrayView a -> Length
81-
8280
-- | Represents the number of elements in this typed array.
8381
length :: forall a. ArrayView a -> Length
8482
length = lengthImpl
85-
83+
foreign import lengthImpl :: forall a. ArrayView a -> Length
8684

8785
-- object creator implementations for each typed array
8886

@@ -99,24 +97,6 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N
9997

10098
-- ----
10199

102-
foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
103-
foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
104-
105-
foreign import fillImpl :: forall a b. EffectFn4 b Offset Offset (ArrayView a) Unit
106-
107-
foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a)
108-
foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit
109-
foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a)
110-
foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean
111-
foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c
112-
foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b
113-
foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c
114-
foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b
115-
foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b)
116-
foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset)
117-
foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
118-
foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
119-
120100

121101
-- | Value-oriented array offset.
122102
type Offset = Int
@@ -177,6 +157,7 @@ fromArray a = runEffectFn3 create a null null
177157
-- | Fill the array with a value.
178158
fill :: forall a t. TypedArray a t => t -> Offset -> Offset -> ArrayView a -> Effect Unit
179159
fill x s e a = runEffectFn4 fillImpl x s e a
160+
foreign import fillImpl :: forall a b. EffectFn4 b Offset Offset (ArrayView a) Unit
180161

181162
-- | Stores multiple values into the typed array.
182163
set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean
@@ -199,6 +180,7 @@ mapWithIndex = mapWithIndex' <<< flip
199180

200181
mapWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a
201182
mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o))))
183+
foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a)
202184

203185
-- | Traverses over each value, returning a new one.
204186
traverse :: forall a t. TypedArray a t => (t -> Effect t) -> ArrayView a -> Effect (ArrayView a)
@@ -221,6 +203,7 @@ traverseWithIndex_ = traverseWithIndex_' <<< flip
221203

222204
traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit
223205
traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f)
206+
foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit
224207

225208
-- | Test a predicate to pass on all values.
226209
all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean
@@ -232,6 +215,7 @@ allWithIndex = every <<< flip
232215

233216
every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean
234217
every p a = runEffectFn2 everyImpl a (mkFn2 p)
218+
foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
235219

236220
-- | Test a predicate to pass on any value.
237221
any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean
@@ -243,6 +227,7 @@ anyWithIndex = some <<< flip
243227

244228
some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean
245229
some p a = runEffectFn2 someImpl a (mkFn2 p)
230+
foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
246231

247232
-- | Returns a new typed array with all values that pass the predicate.
248233
filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a)
@@ -253,10 +238,12 @@ filterWithIndex = filterWithIndex' <<< flip
253238

254239
filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (ArrayView a)
255240
filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p)
241+
foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a)
256242

257243
-- | Tests if a value is an element of the typed array.
258244
elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect Boolean
259245
elem x mo a = runEffectFn3 includesImpl a x (toNullable mo)
246+
foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean
260247

261248
-- | Fetch element at index.
262249
unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t
@@ -265,18 +252,22 @@ unsafeAt a o = runEffectFn2 unsafeAtImpl a o
265252
-- | Folding from the left.
266253
foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b
267254
foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i
255+
foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c
268256

269257
-- | Folding from the left. Assumes the typed array is non-empty.
270258
foldl1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t
271259
foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f)
260+
foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b
272261

273262
-- | Folding from the right.
274263
foldrM :: forall a t b. TypedArray a t => (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b
275264
foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i
265+
foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c
276266

277267
-- | Folding from the right. Assumes the typed array is non-empty.
278268
foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t
279269
foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o))
270+
foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b
280271

281272
-- | Returns the first value satisfying the predicate.
282273
find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t)
@@ -287,19 +278,22 @@ findWithIndex = findWithIndex' <<< flip
287278

288279
findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe t)
289280
findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f)
281+
foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b)
290282

291283
-- | Returns the first index of the value satisfying the predicate.
292284
findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe Offset)
293285
findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f)
286+
foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset)
294287

295288
-- | Returns the first index of the element, if it exists, from the left.
296289
indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset)
297290
indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo)
291+
foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
298292

299293
-- | Returns the first index of the element, if it exists, from the right.
300294
lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset)
301295
lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo)
302-
296+
foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
303297

304298
foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> Effect b
305299
foldl f = foldlWithIndex' (\a x _ -> f a x)
@@ -331,84 +325,69 @@ foldr1 f = foldr1WithIndex (\_ a x -> f a x)
331325
foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> Effect t
332326
foldr1WithIndex f = foldr1M (\x a o -> pure (f o x a))
333327

334-
foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit
335-
336328
-- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details.
337329
copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit
338330
copyWithin a t s me = runEffectFn4 copyWithinImpl a t s (toNullable me)
339-
340-
foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit
331+
foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit
341332

342333
-- | Reverses a typed array in-place.
343334
reverse :: forall a. ArrayView a -> Effect Unit
344335
reverse a = runEffectFn1 reverseImpl a
336+
foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit
345337

346-
foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit
347338

348339
setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Offset -> b -> Effect Boolean
349340
setInternal lenfn a mo b =
350341
let o = fromMaybe 0 mo
351342
in if o >= 0 && lenfn b <= length a - o
352343
then runEffectFn3 setImpl a o b *> pure true
353344
else pure false
345+
foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit
346+
354347

355348

356349
-- | Stores multiple values in the typed array, reading input values from the second typed array.
357350
setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Boolean
358351
setTyped = setInternal length
359352

360-
361-
-- | Copy the entire contents of the typed array into a new buffer.
362-
foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a)
363-
364353
-- | Copy part of the contents of a typed array into a new buffer, between some start and end indices.
365354
slice :: forall a. Offset -> Offset -> ArrayView a -> Effect (ArrayView a)
366355
slice s e a = runEffectFn3 sliceImpl a s e
367-
368-
foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit
356+
foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Offset Offset (ArrayView a)
369357

370358
-- | Sorts the values in-place.
371359
sort :: forall a. ArrayView a -> Effect Unit
372360
sort a = runEffectFn1 sortImpl a
373-
374-
375-
foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a)
361+
foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit
376362

377363
-- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second.
378364
subArray :: forall a. Offset -> Offset -> ArrayView a -> ArrayView a
379365
subArray s e a = runFn3 subArrayImpl a s e
380-
381-
foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String
366+
foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Offset Offset (ArrayView a)
382367

383368
-- | 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.
384369
toString :: forall a. ArrayView a -> Effect String
385370
toString a = runEffectFn1 toStringImpl a
386-
387-
foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String
371+
foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String
388372

389373
-- | 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.
390374
join :: forall a. String -> ArrayView a -> Effect String
391375
join s a = runEffectFn2 joinImpl a s
392-
393-
394-
foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean
376+
foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String
395377

396378
-- | Determine if a certain index is valid.
397379
hasIndex :: forall a. ArrayView a -> Offset -> Boolean
398380
hasIndex a o = runFn2 hasIndexImpl a o
399-
400-
401-
foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b
381+
foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean
402382

403383
-- | Fetch element at index.
404384
at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Effect (Maybe t)
405385
at a n = toMaybe <$> runEffectFn2 unsafeAtImpl a n
386+
foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Offset b
406387

407388
infixl 3 at as !
408389

409-
410-
foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b)
411-
412390
-- | Turn typed array into an array.
413391
toArray :: forall a t. TypedArray a t => ArrayView a -> Effect (Array t)
414392
toArray a = runEffectFn1 toArrayImpl a
393+
foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b)

0 commit comments

Comments
 (0)