Skip to content

Commit 08f70cb

Browse files
Merge pull request #7795 from brunnerant/iarray-extmtds
IArray extension methods
2 parents 2e3fbfd + 8216307 commit 08f70cb

File tree

3 files changed

+407
-0
lines changed

3 files changed

+407
-0
lines changed

library/src-bootstrapped/scala/IArray.scala

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,223 @@ object opaques
3838
def (arr: IArray[Double]) length: Int = arr.asInstanceOf[Array[Double]].length
3939
def (arr: IArray[Object]) length: Int = arr.asInstanceOf[Array[Object]].length
4040
def [T](arr: IArray[T]) length: Int = arr.asInstanceOf[Array[T]].length
41+
42+
/** Returns this array concatenated with the given array. */
43+
def [T, U >: T: ClassTag](arr: IArray[T]) ++(that: IArray[U]): IArray[U] =
44+
(genericArrayOps(arr) ++ that.asInstanceOf[Array[U]]).asInstanceOf[IArray[U]]
45+
46+
/** Tests whether this array contains a given value as an element. */
47+
def [T](arr: IArray[T]) contains(elem: T): Boolean =
48+
genericArrayOps(arr.asInstanceOf[Array[T]]).contains(elem)
49+
50+
/** Counts the number of elements in this array which satisfy a predicate */
51+
def [T](arr: IArray[T]) count(p: T => Boolean): Int =
52+
genericArrayOps(arr).count(p)
53+
54+
/** The rest of the array without its `n` first elements. */
55+
def [T](arr: IArray[T]) drop(n: Int): IArray[T] =
56+
genericArrayOps(arr).drop(n).asInstanceOf[IArray[T]]
57+
58+
/** The rest of the array without its `n` last elements. */
59+
def [T](arr: IArray[T]) dropRight(n: Int): IArray[T] =
60+
genericArrayOps(arr).dropRight(n).asInstanceOf[IArray[T]]
61+
62+
/** Drops longest prefix of elements that satisfy a predicate. */
63+
def [T](arr: IArray[T]) dropWhile(p: T => Boolean): IArray[T] =
64+
genericArrayOps(arr).dropWhile(p).asInstanceOf[IArray[T]]
65+
66+
/** Tests whether a predicate holds for at least one element of this array. */
67+
def [T](arr: IArray[T]) exists(p: T => Boolean): IArray[T] =
68+
genericArrayOps(arr).exists(p).asInstanceOf[IArray[T]]
69+
70+
/** Selects all elements of this array which satisfy a predicate. */
71+
def [T](arr: IArray[T]) filter(p: T => Boolean): IArray[T] =
72+
genericArrayOps(arr).filter(p).asInstanceOf[IArray[T]]
73+
74+
/** Selects all elements of this array which do not satisfy a predicate. */
75+
def [T](arr: IArray[T]) filterNot(p: T => Boolean): IArray[T] =
76+
genericArrayOps(arr).filterNot(p).asInstanceOf[IArray[T]]
77+
78+
/** Finds the first element of the array satisfying a predicate, if any. */
79+
def [T](arr: IArray[T]) find(p: T => Boolean): Option[T] =
80+
genericArrayOps(arr).find(p)
81+
82+
/** Builds a new array by applying a function to all elements of this array
83+
* and using the elements of the resulting collections. */
84+
def [T, U: ClassTag](arr: IArray[T]) flatMap(f: T => IterableOnce[U]): IArray[U] =
85+
genericArrayOps(arr).flatMap(f).asInstanceOf[IArray[U]]
86+
87+
/** Flattens a two-dimensional array by concatenating all its rows
88+
* into a single array. */
89+
def [T, U: ClassTag](arr: IArray[T]) flatten(given T => Iterable[U]): IArray[U] =
90+
genericArrayOps(arr).flatten.asInstanceOf[IArray[U]]
91+
92+
/** Folds the elements of this array using the specified associative binary operator. */
93+
def [T, U >: T: ClassTag](arr: IArray[T]) fold(z: U)(op: (U, U) => U): U =
94+
genericArrayOps(arr).fold(z)(op)
95+
96+
/** Applies a binary operator to a start value and all elements of this array,
97+
* going left to right. */
98+
def [T, U: ClassTag](arr: IArray[T]) foldLeft(z: U)(op: (U, T) => U): U =
99+
genericArrayOps(arr).foldLeft(z)(op)
100+
101+
/** Applies a binary operator to all elements of this array and a start value,
102+
* going right to left. */
103+
def [T, U: ClassTag](arr: IArray[T]) foldRight(z: U)(op: (T, U) => U): U =
104+
genericArrayOps(arr).foldRight(z)(op)
105+
106+
/** Tests whether a predicate holds for all elements of this array. */
107+
def [T](arr: IArray[T]) forall(p: T => Boolean): Boolean =
108+
genericArrayOps(arr).forall(p)
109+
110+
/** Apply `f` to each element for its side effects. */
111+
def [T, U](arr: IArray[T]) foreach(f: T => U): Unit =
112+
genericArrayOps(arr).foreach(f)
113+
114+
/** Selects the first element of this array. */
115+
def [T](arr: IArray[T]) head: T =
116+
genericArrayOps(arr).head
117+
118+
/** Optionally selects the first element. */
119+
def [T](arr: IArray[T]) headOption: Option[T] =
120+
genericArrayOps(arr).headOption
121+
122+
/** Finds index of first occurrence of some value in this array after or at some start index. */
123+
def [T](arr: IArray[T]) indexOf(elem: T, from: Int = 0): Int =
124+
genericArrayOps(arr.asInstanceOf[Array[T]]).indexOf(elem, from)
125+
126+
/** Finds index of the first element satisfying some predicate after or at some start index. */
127+
def [T](arr: IArray[T]) indexWhere(p: T => Boolean, from: Int = 0): Int =
128+
genericArrayOps(arr).indexWhere(p, from)
129+
130+
/** Produces the range of all indices of this sequence. */
131+
def [T](arr: IArray[T]) indices: Range =
132+
genericArrayOps(arr).indices
133+
134+
/** The initial part of the array without its last element. */
135+
def [T](arr: IArray[T]) init: IArray[T] =
136+
genericArrayOps(arr).init.asInstanceOf[IArray[T]]
137+
138+
/** Tests whether the array is empty. */
139+
def [T](arr: IArray[T]) isEmpty: Boolean =
140+
genericArrayOps(arr).isEmpty
141+
142+
/** An iterator yielding the elemenst of this array. */
143+
def [T](arr: IArray[T]) iterator: Iterator[T] =
144+
genericArrayOps(arr).iterator
145+
146+
/** Selects the last element. */
147+
def [T](arr: IArray[T]) last: T =
148+
genericArrayOps(arr).last
149+
150+
/** Optionally selects the last element. */
151+
def [T](arr: IArray[T]) lastOption: Option[T] =
152+
genericArrayOps(arr).lastOption
153+
154+
/** Finds index of last occurrence of some value in this array before or at a given end index. */
155+
def [T](arr: IArray[T]) lastIndexOf(elem: T, end: Int = arr.length - 1): Int =
156+
genericArrayOps(arr.asInstanceOf[Array[T]]).lastIndexOf(elem, end)
157+
158+
/** Finds index of last element satisfying some predicate before or at given end index. */
159+
def [T](arr: IArray[T]) lastIndexWhere(p: T => Boolean, end: Int = arr.length - 1): Int =
160+
genericArrayOps(arr).lastIndexWhere(p, end)
161+
162+
/** Builds a new array by applying a function to all elements of this array. */
163+
def [T, U: ClassTag](arr: IArray[T]) map(f: T => U): IArray[U] =
164+
genericArrayOps(arr).map(f).asInstanceOf[IArray[U]]
165+
166+
/** Tests whether the array is not empty. */
167+
def [T](arr: IArray[T]) nonEmpty: Boolean =
168+
genericArrayOps(arr).nonEmpty
169+
170+
/** A pair of, first, all elements that satisfy predicate `p` and, second, all elements that do not. */
171+
def [T](arr: IArray[T]) partition(p: T => Boolean): (IArray[T], IArray[T]) =
172+
genericArrayOps(arr).partition(p) match {
173+
case (x, y) => (x.asInstanceOf[IArray[T]], y.asInstanceOf[IArray[T]])
174+
}
175+
176+
/** Returns a new array with the elements in reversed order. */
177+
def [T](arr: IArray[T]) reverse: IArray[T] =
178+
genericArrayOps(arr).reverse.asInstanceOf[IArray[T]]
179+
180+
/** Computes a prefix scan of the elements of the array. */
181+
def [T, U >: T: ClassTag](arr: IArray[T]) scan(z: U)(op: (U, U) => U): IArray[U] =
182+
genericArrayOps(arr).scan(z)(op).asInstanceOf[IArray[U]]
183+
184+
/** Produces an array containing cumulative results of applying the binary
185+
* operator going left to right. */
186+
def [T, U: ClassTag](arr: IArray[T]) scanLeft(z: U)(op: (U, T) => U): IArray[U] =
187+
genericArrayOps(arr).scanLeft(z)(op).asInstanceOf[IArray[U]]
188+
189+
/** Produces an array containing cumulative results of applying the binary
190+
* operator going right to left. */
191+
def [T, U: ClassTag](arr: IArray[T]) scanRight(z: U)(op: (T, U) => U): IArray[U] =
192+
genericArrayOps(arr).scanRight(z)(op).asInstanceOf[IArray[U]]
193+
194+
/** The size of this array. */
195+
def [T](arr: IArray[T]) size: Int =
196+
arr.length
197+
198+
/** Selects the interval of elements between the given indices. */
199+
def [T](arr: IArray[T]) slice(from: Int, until: Int): IArray[T] =
200+
genericArrayOps(arr).slice(from, until).asInstanceOf[IArray[T]]
201+
202+
/** Sorts this array according to the Ordering which results from transforming
203+
* an implicitly given Ordering with a transformation function. */
204+
def [T, U: ClassTag](arr: IArray[T]) sortBy(f: T => U)(given math.Ordering[U]): IArray[T] =
205+
genericArrayOps(arr).sortBy(f).asInstanceOf[IArray[T]]
206+
207+
/** Sorts this array according to a comparison function. */
208+
def [T](arr: IArray[T]) sortWith(f: (T, T) => Boolean): IArray[T] =
209+
genericArrayOps(arr).sortWith(f).asInstanceOf[IArray[T]]
210+
211+
/** Sorts this array according to an Ordering. */
212+
def [T](arr: IArray[T]) sorted(given math.Ordering[T]): IArray[T] =
213+
genericArrayOps(arr).sorted.asInstanceOf[IArray[T]]
214+
215+
/** Splits this array into a prefix/suffix pair according to a predicate. */
216+
def [T](arr: IArray[T]) span(p: T => Boolean): (IArray[T], IArray[T]) =
217+
genericArrayOps(arr).span(p) match {
218+
case (x, y) => (x.asInstanceOf[IArray[T]], y.asInstanceOf[IArray[T]])
219+
}
220+
221+
/** Splits this array into two at a given position. */
222+
def [T](arr: IArray[T]) splitAt(n: Int): (IArray[T], IArray[T]) =
223+
genericArrayOps(arr).splitAt(n) match {
224+
case (x, y) => (x.asInstanceOf[IArray[T]], y.asInstanceOf[IArray[T]])
225+
}
226+
227+
/** Tests whether this array starts with the given array. */
228+
def [T, U >: T: ClassTag](arr: IArray[T]) startsWith(that: IArray[U], offset: Int = 0): Boolean =
229+
genericArrayOps(arr).startsWith(that.asInstanceOf[Array[U]])
230+
231+
/** The rest of the array without its first element. */
232+
def [T](arr: IArray[T]) tail: IArray[T] =
233+
genericArrayOps(arr).tail.asInstanceOf[IArray[T]]
234+
235+
/** An array containing the first `n` elements of this array. */
236+
def [T](arr: IArray[T]) take(n: Int): IArray[T] =
237+
genericArrayOps(arr).take(n).asInstanceOf[IArray[T]]
238+
239+
/** An array containing the last `n` elements of this array. */
240+
def [T](arr: IArray[T]) takeRight(n: Int): IArray[T] =
241+
genericArrayOps(arr).takeRight(n).asInstanceOf[IArray[T]]
242+
243+
/** Takes longest prefix of elements that satisfy a predicate. */
244+
def [T](arr: IArray[T]) takeWhile(p: T => Boolean): IArray[T] =
245+
genericArrayOps(arr).takeWhile(p).asInstanceOf[IArray[T]]
246+
247+
/** Converts an array of pairs into an array of first elements and an array of second elements. */
248+
def [U: ClassTag, V: ClassTag](arr: IArray[(U, V)]) unzip: (IArray[U], IArray[V]) =
249+
genericArrayOps(arr).unzip match {
250+
case (x, y) => (x.asInstanceOf[IArray[U]], y.asInstanceOf[IArray[V]])
251+
}
252+
253+
/** Returns an array formed from this array and another iterable collection
254+
* by combining corresponding elements in pairs.
255+
* If one of the two collections is longer than the other, its remaining elements are ignored. */
256+
def [T, U: ClassTag](arr: IArray[T]) zip(that: IArray[U]): IArray[(T, U)] =
257+
genericArrayOps(arr).zip(that).asInstanceOf[IArray[(T, U)]]
41258
end opaques
42259

43260
type IArray[+T] = opaques.IArray[T]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
IArray(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
2+
true
3+
5
4+
IArray(9,10)
5+
IArray(1,2)
6+
IArray(8,9,10)
7+
true
8+
IArray(2,4,6,8,10)
9+
IArray(1,3,5,7,9)
10+
Some(5)
11+
IArray(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10)
12+
IArray(1,2,3,4)
13+
55
14+
12345678910
15+
10987654321
16+
false
17+
1
18+
2
19+
3
20+
4
21+
5
22+
6
23+
7
24+
8
25+
9
26+
10
27+
11
28+
Some(1)
29+
-1
30+
3
31+
0,1,2,3,4,5,6,7,8,9
32+
IArray(1,2,3,4,5,6,7,8,9)
33+
false
34+
1,2,3
35+
10
36+
Some(20)
37+
6
38+
3
39+
IArray(11,12,13,14,15,16,17,18,19,20)
40+
true
41+
IArray(2,4,6,8,10)
42+
IArray(1,3,5,7,9)
43+
IArray(10,9,8,7,6,5,4,3,2,1)
44+
IArray(0,1,3,6,10,15,21,28,36,45,55)
45+
IArray(,1,12,123,1234,12345,123456,1234567,12345678,123456789,12345678910)
46+
IArray(10987654321,1098765432,109876543,10987654,1098765,109876,10987,1098,109,10,)
47+
10
48+
IArray(6,7)
49+
IArray(10,9,8,7,6,5,4,3,2,1)
50+
IArray(10,1,2,3,4,5,6,7,8,9)
51+
IArray(1,2,3,4,5,6,7,8,9,10)
52+
IArray(1,2,3,4)
53+
IArray(5,6,7,8,9,10)
54+
IArray(1,2,3,4,5,6,7)
55+
IArray(8,9,10)
56+
false
57+
IArray(2,3,4,5,6,7,8,9,10)
58+
IArray(1,2,3)
59+
IArray(7,8,9,10)
60+
IArray(1,2)
61+
IArray(1,2,3)
62+
IArray(1,2,3)
63+
IArray((1,11),(2,12),(3,13),(4,14),(5,15),(6,16),(7,17),(8,18),(9,19),(10,20))

0 commit comments

Comments
 (0)