1
1
package scala
2
2
import reflect .ClassTag
3
3
4
- import scala .collection .immutable
4
+ import scala .collection ._
5
+ import scala .collection .mutable .Buffer
5
6
6
7
opaque type IArray [+ T ] = Array [_ <: T ]
7
8
@@ -39,9 +40,6 @@ object IArray:
39
40
extension (arr : IArray [Object ]) def length : Int = arr.asInstanceOf [Array [Object ]].length
40
41
extension [T ](arr : IArray [T ]) def length : Int = arr.asInstanceOf [Array [T ]].length
41
42
42
- /** Returns this array concatenated with the given array. */
43
- extension [T ](arr : IArray [T ]) def ++ [U >: T : ClassTag ](that : IArray [U ]): IArray [U ] =
44
- genericArrayOps(arr) ++ that
45
43
46
44
/** Tests whether this array contains a given value as an element. */
47
45
extension [T ](arr : IArray [T ]) def contains (elem : T ): Boolean =
@@ -104,17 +102,17 @@ object IArray:
104
102
genericArrayOps(arr).flatten
105
103
106
104
/** Folds the elements of this array using the specified associative binary operator. */
107
- extension [T ](arr : IArray [T ]) def fold [U >: T : ClassTag ](z : U )(op : (U , U ) => U ): U =
105
+ extension [T ](arr : IArray [T ]) def fold [U >: T ](z : U )(op : (U , U ) => U ): U =
108
106
genericArrayOps(arr).fold(z)(op)
109
107
110
108
/** Applies a binary operator to a start value and all elements of this array,
111
109
* going left to right. */
112
- extension [T ](arr : IArray [T ]) def foldLeft [U : ClassTag ](z : U )(op : (U , T ) => U ): U =
110
+ extension [T ](arr : IArray [T ]) def foldLeft [U ](z : U )(op : (U , T ) => U ): U =
113
111
genericArrayOps(arr).foldLeft(z)(op)
114
112
115
113
/** Applies a binary operator to all elements of this array and a start value,
116
114
* going right to left. */
117
- extension [T ](arr : IArray [T ]) def foldRight [U : ClassTag ](z : U )(op : (T , U ) => U ): U =
115
+ extension [T ](arr : IArray [T ]) def foldRight [U ](z : U )(op : (T , U ) => U ): U =
118
116
genericArrayOps(arr).foldRight(z)(op)
119
117
120
118
/** Tests whether a predicate holds for all elements of this array. */
@@ -217,7 +215,7 @@ object IArray:
217
215
218
216
/** Sorts this array according to the Ordering which results from transforming
219
217
* an implicitly given Ordering with a transformation function. */
220
- extension [T ](arr : IArray [T ]) def sortBy [U : ClassTag ](f : T => U )(using math.Ordering [U ]): IArray [T ] =
218
+ extension [T ](arr : IArray [T ]) def sortBy [U ](f : T => U )(using math.Ordering [U ]): IArray [T ] =
221
219
genericArrayOps(arr).sortBy(f)
222
220
223
221
/** Sorts this array according to a comparison function. */
@@ -236,10 +234,6 @@ object IArray:
236
234
extension [T ](arr : IArray [T ]) def splitAt (n : Int ): (IArray [T ], IArray [T ]) =
237
235
genericArrayOps(arr).splitAt(n)
238
236
239
- /** Tests whether this array starts with the given array. */
240
- extension [T ](arr : IArray [T ]) def startsWith [U >: T : ClassTag ](that : IArray [U ], offset : Int = 0 ): Boolean =
241
- genericArrayOps(arr).startsWith(that)
242
-
243
237
/** The rest of the array without its first element. */
244
238
extension [T ](arr : IArray [T ]) def tail : IArray [T ] =
245
239
genericArrayOps(arr).tail
@@ -264,11 +258,137 @@ object IArray:
264
258
extension [U : ClassTag , V : ClassTag ](arr : IArray [(U , V )]) def unzip : (IArray [U ], IArray [V ]) =
265
259
genericArrayOps(arr).unzip
266
260
267
- /** Returns an array formed from this array and another iterable collection
268
- * by combining corresponding elements in pairs.
269
- * If one of the two collections is longer than the other, its remaining elements are ignored. */
270
- extension [T ](arr : IArray [T ]) def zip [U : ClassTag ](that : IArray [U ]): IArray [(T , U )] =
271
- genericArrayOps(arr).zip(that)
261
+ extension [A ](arr : IArray [A ])
262
+ // def ++[B >: A: ClassTag](suffix: IArray[B]): IArray[B] = genericArrayOps(arr) ++ suffix.toSeq
263
+ def ++ [B >: A : ClassTag ](suffix : IterableOnce [B ]): IArray [B ] = genericArrayOps(arr) ++ suffix
264
+ def :+ [B >: A : ClassTag ](x : B ): IArray [B ] = genericArrayOps(arr) :+ x
265
+ // def :++ [B >: A: ClassTag](suffix: IArray[B]): IArray[B] = genericArrayOps(arr) :++ suffix
266
+ def :++ [B >: A : ClassTag ](suffix : IterableOnce [B ]): IArray [B ] = genericArrayOps(arr) :++ suffix
267
+ def addString (b : mutable.StringBuilder ): mutable.StringBuilder = arr.toSeq.addString(b)
268
+ def addString (b : mutable.StringBuilder , sep : String ): mutable.StringBuilder = arr.toSeq.addString(b, sep)
269
+ def addString (b : mutable.StringBuilder , start : String , sep : String , end : String ): mutable.StringBuilder = arr.toSeq.addString(b, start, sep, end)
270
+ def appended [B >: A : ClassTag ](x : B ): IArray [B ] = genericArrayOps(arr).appended(x)
271
+ // def appendedAll[B >: A: ClassTag](suffix: IArray[B]): IArray[B] = genericArrayOps(arr).appendedAll(suffix)
272
+ def appendedAll [B >: A : ClassTag ](suffix : IterableOnce [B ]): IArray [B ] = genericArrayOps(arr).appendedAll(suffix)
273
+ def collect [B : ClassTag ](pf : PartialFunction [A , B ]): IArray [B ] = genericArrayOps(arr).collect(pf)
274
+ def collectFirst [B ](f : PartialFunction [A , B ]): Option [B ] = genericArrayOps(arr).collectFirst(f)
275
+ def combinations (n : Int ): Iterator [IArray [A ]] = genericArrayOps(arr).combinations(n)
276
+ // def concat[B >: A: ClassTag](suffix: IArray[B]): IArray[B] = genericArrayOps(arr).concat(suffix)
277
+ def concat [B >: A : ClassTag ](suffix : IterableOnce [B ]): IArray [B ] = genericArrayOps(arr).concat(suffix)
278
+ // def containsSlice[B](that: IArray[B]): Boolean = arr.toSeq.containsSlice(that.toSeq)
279
+ def containsSlice [B ](that : Seq [B ]): Boolean = arr.toSeq.containsSlice(that)
280
+ // def corresponds[B](that: IArray[B])(p: (A, B) => Boolean): Boolean = arr.toSeq.corresponds(that.toSeq)(p)
281
+ def corresponds [B ](that : IterableOnce [B ])(p : (A , B ) => Boolean ): Boolean = arr.toSeq.corresponds(that)(p)
282
+ // def diff[B >: A](that: IArray[B]): IArray[A] = genericArrayOps(arr).diff(that.toSeq)
283
+ def diff [B >: A ](that : Seq [B ]): IArray [A ] = genericArrayOps(arr).diff(that)
284
+ def distinct : IArray [A ] = genericArrayOps(arr).distinct
285
+ def distinctBy [B ](f : A => B ): IArray [A ] = genericArrayOps(arr).distinctBy(f)
286
+ def empty : immutable.ArraySeq [A ] = arr.toSeq.empty
287
+ // def startsWith[B >: A](that: IArray[B], offset: Int = 0): Boolean = genericArrayOps(arr).startsWith(that)
288
+ def startsWith [B >: A ](that : IterableOnce [B ], offset : Int = 0 ): Boolean = genericArrayOps(arr).startsWith(that, offset)
289
+ // def endsWith[B >: A](that: IArray[B]): Boolean = genericArrayOps(arr).endsWith(that)
290
+ def endsWith [B >: A ](that : Iterable [B ]): Boolean = genericArrayOps(arr).endsWith(that)
291
+ def findLast (p : A => Boolean ): Option [A ] = arr.toSeq.findLast(p)
292
+ def groupBy [K ](f : A => K ): immutable.Map [K , IArray [A ]] = genericArrayOps(arr).groupBy(f)
293
+ def groupMap [K , B : ClassTag ](key : A => K )(f : A => B ): immutable.Map [K , IArray [B ]] = genericArrayOps(arr).groupMap(key)(f)
294
+ def groupMapReduce [K , B ](key : (A ) => K )(f : (A ) => B )(reduce : (B , B ) => B ): immutable.Map [K , B ] = arr.toSeq.groupMapReduce(key)(f)(reduce)
295
+ def grouped (size : Int ): Iterator [IArray [A ]] = genericArrayOps(arr).grouped(size)
296
+ // def indexOfSlice[B >: A](that: IArray[B]): Int = arr.toSeq.indexOfSlice(that)
297
+ def indexOfSlice [B >: A ](that : Seq [B ]): Int = arr.toSeq.indexOfSlice(that)
298
+ // def indexOfSlice[B >: A](that: IArray[B], from: Int): Int = arr.toSeq.indexOfSlice(that, from)
299
+ def indexOfSlice [B >: A ](that : Seq [B ], from : Int ): Int = arr.toSeq.indexOfSlice(that, from)
300
+ def inits : Iterator [IArray [A ]] = genericArrayOps(arr).inits
301
+ // def intersect[B >: A](that: IArray[B]): IArray[A] = genericArrayOps(arr).intersect(that)
302
+ def intersect [B >: A ](that : Seq [B ]): IArray [A ] = genericArrayOps(arr).intersect(that)
303
+ def isTraversableAgain : Boolean = arr.toSeq.isTraversableAgain
304
+ def knownSize : Int = arr.length
305
+ // def lastIndexOfSlice[B >: A](that: IArray[B]): Int = arr.toSeq.lastIndexOfSlice(that)
306
+ def lastIndexOfSlice [B >: A ](that : Seq [B ]): Int = arr.toSeq.lastIndexOfSlice(that)
307
+ // def lastIndexOfSlice[B >: A](that: IArray[B], end: Int): Int = arr.toSeq.lastIndexOfSlice(that, end)
308
+ def lastIndexOfSlice [B >: A ](that : Seq [B ], end : Int ): Int = arr.toSeq.lastIndexOfSlice(that, end)
309
+ // def lazyZip[B](that: IArray[B]): LazyZip2[A, B, IArray[A]] = genericArrayOps(arr).lazyZip[B](that).asInstanceOf[LazyZip2[A, B, IArray[A]]]
310
+ def lazyZip [B ](that : Iterable [B ]): LazyZip2 [A , B , IArray [A ]] = genericArrayOps(arr).lazyZip[B ](that).asInstanceOf [LazyZip2 [A , B , IArray [A ]]]
311
+ def lengthCompare (len : Int ): Int = genericArrayOps(arr).lengthCompare(len)
312
+ def lengthIs : IterableOps .SizeCompareOps = arr.toSeq.lengthIs
313
+ def max [B >: A ](using math.Ordering [B ]): A = arr.toSeq.max[B ]
314
+ def maxBy [B ](f : A => B )(using math.Ordering [B ]): A = arr.toSeq.maxBy(f)
315
+ def maxByOption [B ](f : A => B )(using math.Ordering [B ]): Option [A ] = arr.toSeq.maxByOption(f)
316
+ def maxOption [B >: A ](using math.Ordering [B ]): Option [B ] = arr.toSeq.maxOption[B ]
317
+ def min [B >: A ](using math.Ordering [B ]): A = arr.toSeq.min[B ]
318
+ def minBy [B ](f : A => B )(using math.Ordering [B ]): A = arr.toSeq.minBy(f)
319
+ def minByOption [B ](f : A => B )(using math.Ordering [B ]): Option [A ] = arr.toSeq.minByOption(f)
320
+ def minOption [B >: A ](using math.Ordering [B ]): Option [B ] = arr.toSeq.minOption[B ]
321
+ def mkString : String = arr.toSeq.mkString
322
+ def mkString (sep : String ): String = arr.toSeq.mkString(sep)
323
+ def mkString (start : String , sep : String , end : String ): String = arr.toSeq.mkString(start, sep, end)
324
+ def padTo [B >: A : ClassTag ](len : Int , elem : B ): IArray [B ] = genericArrayOps(arr).padTo(len, elem)
325
+ def partitionMap [A1 : ClassTag , A2 : ClassTag ](f : A => Either [A1 , A2 ]): (IArray [A1 ], IArray [A2 ]) = genericArrayOps(arr).partitionMap(f)
326
+ def patch [B >: A : ClassTag ](from : Int , other : IterableOnce [B ], replaced : Int ): IArray [B ] = genericArrayOps(arr).patch(from, other, replaced)
327
+ def permutations : Iterator [IArray [A ]] = genericArrayOps(arr).permutations
328
+ def prepended [B >: A : ClassTag ](x : B ): IArray [B ] = genericArrayOps(arr).prepended(x)
329
+ def prependedAll [B >: A : ClassTag ](prefix : IterableOnce [B ]): IArray [B ] = genericArrayOps(arr).prependedAll(prefix)
330
+ def product [B >: A ](using math.Numeric [B ]): B = arr.toSeq.product[B ]
331
+ def reduce [B >: A ](op : (B , B ) => B ): B = arr.toSeq.reduce(op)
332
+ def reduceLeft [B >: A ](op : (B , A ) => B ): B = arr.toSeq.reduceLeft(op)
333
+ def reduceRight [B >: A ](op : (A , B ) => B ): B = arr.toSeq.reduceRight(op)
334
+ def reverseIterator : Iterator [A ] = genericArrayOps(arr).reverseIterator
335
+ // def sameElements[B >: A](that: IArray[B]): Boolean = arr.toSeq.sameElements(that)
336
+ def sameElements [B >: A ](that : IterableOnce [B ]): Boolean = arr.toSeq.sameElements(that)
337
+ def search [B >: A ](elem : B )(using Ordering [B ]): Searching .SearchResult = arr.toSeq.search(elem)
338
+ def search [B >: A ](elem : B , from : Int , to : Int )(using Ordering [B ]): Searching .SearchResult = arr.toSeq.search(elem, from, to)
339
+ def segmentLength (p : (A ) => Boolean , from : Int ): Int = arr.toSeq.segmentLength(p, from)
340
+ def segmentLength (p : (A ) => Boolean ): Int = arr.toSeq.segmentLength(p)
341
+ // def sizeCompare(that: IArray[_]): Int = arr.toSeq.sizeCompare(that)
342
+ def sizeCompare (that : Iterable [_]): Int = arr.toSeq.sizeCompare(that)
343
+ def sizeCompare (otherSize : Int ): Int = genericArrayOps(arr).sizeCompare(otherSize)
344
+ def sizeIs : IterableOps .SizeCompareOps = arr.toSeq.sizeIs
345
+ def sliding (size : Int , step : Int = 1 ): Iterator [IArray [A ]] = genericArrayOps(arr).sliding(size, step)
346
+ def stepper [S <: Stepper [_]](using StepperShape [A , S ]): S = genericArrayOps(arr).stepper[S ]
347
+ def sum [B >: A ](using math.Numeric [B ]): B = arr.toSeq.sum[B ]
348
+ def tails : Iterator [IArray [A ]] = genericArrayOps(arr).tails
349
+ def tapEach [U ](f : (A ) => U ): IArray [A ] =
350
+ arr.toSeq.foreach(f)
351
+ arr
352
+ def to [C1 ](factory : Factory [A , C1 ]): C1 = arr.toSeq.to(factory)
353
+ def toBuffer [B >: A ]: Buffer [B ] = arr.toSeq.toBuffer[B ]
354
+ def toIndexedSeq : immutable.IndexedSeq [A ] = arr.toSeq.toIndexedSeq
355
+ def toIterable : Iterable [A ] = arr.toSeq.toIterable
356
+ def toList : List [A ] = arr.toSeq.toList
357
+ def toSet : Set [A ] = arr.toSeq.toSet
358
+ def toVector : Vector [A ] = arr.toSeq.toVector
359
+ def updated [B >: A : ClassTag ](index : Int , elem : B ): IArray [B ] = genericArrayOps(arr).updated(index, elem)
360
+ def view : SeqView [A ] = genericArrayOps(arr).view
361
+ // TODO: def withFilter(p: A => Boolean): ArrayOps.WithFilter[A] = genericArrayOps(arr).withFilter(p)
362
+ // def zip[B](that: IArray[B]): IArray[(A, B)] = genericArrayOps(arr).zip(that)
363
+ def zip [B ](that : IterableOnce [B ]): IArray [(A , B )] = genericArrayOps(arr).zip(that)
364
+ // def zipAll[A1 >: A, B](that: IArray[B], thisElem: A1, thatElem: B): IArray[(A1, B)] = genericArrayOps(arr).zipAll(that, thisElem, thatElem)
365
+ def zipAll [A1 >: A , B ](that : Iterable [B ], thisElem : A1 , thatElem : B ): IArray [(A1 , B )] = genericArrayOps(arr).zipAll(that, thisElem, thatElem)
366
+ def zipWithIndex : IArray [(A , Int )] = genericArrayOps(arr).zipWithIndex
367
+ end extension
368
+
369
+ extension [A : ClassTag ](arr : IArray [IterableOnce [A ]])
370
+ def flatten : IArray [A ] = genericArrayOps(arr).flatten
371
+
372
+ extension [A , B >: A : ClassTag ](prefix : IterableOnce [A ])
373
+ def ++: (arr : IArray [B ]): IArray [B ] = genericArrayOps(arr).prependedAll(prefix)
374
+
375
+ extension [A , B >: A : ClassTag ](prefix : IArray [A ])
376
+ def ++: (arr : IArray [B ]): IArray [B ] = genericArrayOps(arr).prependedAll(prefix)
377
+
378
+ extension [A , B >: A : ClassTag ](x : A )
379
+ def +: (arr : IArray [B ]): IArray [B ] = genericArrayOps(arr).prepended(x)
380
+
381
+
382
+ extension [A1 , A2 ](arr : IArray [(A1 , A2 )])
383
+ def toMap : Map [A1 , A2 ] = arr.toSeq.toMap
384
+
385
+ extension [A1 : ClassTag , A2 : ClassTag , A3 : ClassTag ](arr : IArray [(A1 , A2 , A3 )])
386
+ def unzip3 : (IArray [A1 ], IArray [A2 ], IArray [A3 ]) = genericArrayOps(arr).unzip3
387
+
388
+ // TODO
389
+ // extension [A](arr: IArray[IArray[A]])
390
+ // def transpose[B](implicit asArray: A => Array[B]): IArray[IArray[B]] =
391
+ // genericArrayOps(arr).transpose(using asArray.asInstanceOf)
272
392
273
393
/** Conversion from IArray to immutable.ArraySeq */
274
394
extension [T ](arr : IArray [T ]) def toSeq : immutable.ArraySeq [T ] =
@@ -372,7 +492,7 @@ object IArray:
372
492
// `Array.concat` should arguably take in a `Seq[Array[_ <: T]]`,
373
493
// but since it currently takes a `Seq[Array[T]]` we have to perform a cast,
374
494
// knowing tacitly that `concat` is not going to do the wrong thing.
375
- Array .concat[T ](xss.asInstanceOf [Seq [Array [T ]]]: _* )
495
+ Array .concat[T ](xss.asInstanceOf [immutable. Seq [Array [T ]]]: _* )
376
496
377
497
/** Returns an immutable array that contains the results of some element computation a number
378
498
* of times. Each element is determined by a separate computation.
@@ -521,4 +641,4 @@ object IArray:
521
641
def unapplySeq [T ](x : IArray [T ]): Array .UnapplySeqWrapper [_ <: T ] =
522
642
Array .unapplySeq(x)
523
643
524
- end IArray
644
+ end IArray
0 commit comments