Skip to content

Commit a90366c

Browse files
authored
Revert "migrate view"
This reverts commit 8421d62.
1 parent 8421d62 commit a90366c

27 files changed

+564
-584
lines changed

array/array.mbti

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package moonbitlang/core/array
22

3-
alias @moonbitlang/core/bytes as @bytes
43
alias @moonbitlang/core/quickcheck as @quickcheck
54

65
// Values
@@ -9,7 +8,6 @@ alias @moonbitlang/core/quickcheck as @quickcheck
98
impl FixedArray {
109
all[T](Self[T], (T) -> Bool) -> Bool
1110
any[T](Self[T], (T) -> Bool) -> Bool
12-
blit_from_bytesview(Self[Byte], Int, @bytes.BytesView) -> Unit
1311
contains[T : Eq](Self[T], T) -> Bool
1412
copy[T](Self[T]) -> Self[T]
1513
each[T](Self[T], (T) -> Unit) -> Unit
@@ -70,24 +68,10 @@ impl ArrayView {
7068
contains[T : Eq](Self[T], T) -> Bool
7169
each[T](Self[T], (T) -> Unit) -> Unit
7270
eachi[T](Self[T], (Int, T) -> Unit) -> Unit
73-
filter[T](Self[T], (T) -> Bool) -> Array[T]
74-
fold[A, B](Self[A], init~ : B, (B, A) -> B) -> B
75-
foldi[A, B](Self[A], init~ : B, (Int, B, A) -> B) -> B
76-
iter[A](Self[A]) -> Iter[A]
77-
map[T, U](Self[T], (T) -> U) -> Array[U]
78-
map_inplace[T](Self[T], (T) -> T) -> Unit
79-
mapi[T, U](Self[T], (Int, T) -> U) -> Array[U]
80-
mapi_inplace[T](Self[T], (Int, T) -> T) -> Unit
81-
rev_fold[A, B](Self[A], init~ : B, (B, A) -> B) -> B
82-
rev_foldi[A, B](Self[A], init~ : B, (Int, B, A) -> B) -> B
8371
rev_inplace[T](Self[T]) -> Unit
8472
}
85-
impl[T : Compare] Compare for ArrayView[T]
86-
impl[T : Eq] Eq for ArrayView[T]
87-
impl[X : Show] Show for ArrayView[X]
8873

8974
// Type aliases
90-
pub typealias View[T] = ArrayView[T]
9175

9276
// Traits
9377

array/blit.mbt

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,3 @@ test "copy" {
5656
a.blit_to(a, len=2, src_offset=1)
5757
inspect!(a, content="[2, 3, 3, 4]")
5858
}
59-
60-
///|
61-
/// Copy bytes from a @bytes.View into a fixed array of bytes.
62-
///
63-
/// Parameters:
64-
///
65-
/// * `self` : The destination fixed array of bytes.
66-
/// * `bytes_offset` : The starting position in the destination array where bytes will be copied.
67-
/// * `src` : The source View to copy from.
68-
///
69-
/// Throws a panic if:
70-
/// * `bytes_offset` is negative
71-
/// * The destination array is too small to hold all bytes from the source View
72-
///
73-
/// Example:
74-
///
75-
/// ```moonbit
76-
/// let arr = FixedArray::make(4, b'\x00')
77-
/// let view = b"\x01\x02\x03"[1:]
78-
/// arr.blit_from_bytesview(1, view)
79-
/// inspect!(arr, content="[b'\\x00', b'\\x02', b'\\x03', b'\\x00']")
80-
/// ```
81-
pub fn FixedArray::blit_from_bytesview(
82-
self : FixedArray[Byte],
83-
bytes_offset : Int,
84-
src : @bytes.View
85-
) -> Unit {
86-
let src_len = src.length()
87-
guard bytes_offset >= 0 && bytes_offset + src_len - 1 < self.length()
88-
for i = 0, j = bytes_offset; i < src_len; i = i + 1, j = j + 1 {
89-
self[j] = src[i]
90-
}
91-
}

array/moon.pkg.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"import": [
33
"moonbitlang/core/builtin",
4-
"moonbitlang/core/bytes",
4+
55
"moonbitlang/core/test",
66
"moonbitlang/core/quickcheck",
77
"moonbitlang/core/quickcheck/splitmix"
88
],
9-
"test-import": ["moonbitlang/core/random", "moonbitlang/core/json"],
9+
"test-import": ["moonbitlang/core/random"],
1010
"targets": {
1111
"array_js.mbt": ["js"],
1212
"array_nonjs.mbt": ["not", "js"],

array/view.mbt

Lines changed: 6 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
///|
16-
///
17-
/// A `@array.View` represents a view into a section of an array without copying the data.
18-
///
19-
/// # Example
20-
///
21-
/// ```moonbit
22-
/// let arr = [1, 2, 3, 4, 5]
23-
/// let view = arr[1:4] // Creates a view of elements at indices 1,2,3
24-
/// assert_eq!(view[0], 2)
25-
/// assert_eq!(view.length(), 3)
26-
/// ```
27-
pub typealias View[T] = ArrayView[T]
28-
2915
///|
3016
/// Reverses the elements in the array view in place.
3117
///
@@ -42,7 +28,7 @@ pub typealias View[T] = ArrayView[T]
4228
/// inspect!(arr, content="[5, 4, 3, 2, 1]")
4329
/// }
4430
/// ```
45-
pub fn View::rev_inplace[T](self : View[T]) -> Unit {
31+
pub fn ArrayView::rev_inplace[T](self : ArrayView[T]) -> Unit {
4632
let mid_len = self.length() / 2
4733
for i = 0; i < mid_len; i = i + 1 {
4834
let j = self.length() - i - 1
@@ -69,7 +55,7 @@ pub fn View::rev_inplace[T](self : View[T]) -> Unit {
6955
/// inspect!(sum, content="6")
7056
/// }
7157
/// ```
72-
pub fn View::each[T](self : View[T], f : (T) -> Unit) -> Unit {
58+
pub fn ArrayView::each[T](self : ArrayView[T], f : (T) -> Unit) -> Unit {
7359
for v in self {
7460
f(v)
7561
}
@@ -86,7 +72,7 @@ pub fn View::each[T](self : View[T], f : (T) -> Unit) -> Unit {
8672
/// v.eachi(fn (i, x) { sum = sum + x + i })
8773
/// assert_eq!(sum, 15)
8874
/// ```
89-
pub fn View::eachi[T](self : View[T], f : (Int, T) -> Unit) -> Unit {
75+
pub fn ArrayView::eachi[T](self : ArrayView[T], f : (Int, T) -> Unit) -> Unit {
9076
for i, v in self {
9177
f(i, v)
9278
}
@@ -102,7 +88,7 @@ pub fn View::eachi[T](self : View[T], f : (Int, T) -> Unit) -> Unit {
10288
/// assert_false!(v[:].all(fn(elem) { elem % 2 == 0 }))
10389
/// assert_true!(v[1:4].all(fn(elem) { elem % 2 == 0 }))
10490
/// ```
105-
pub fn View::all[T](self : View[T], f : (T) -> Bool) -> Bool {
91+
pub fn ArrayView::all[T](self : ArrayView[T], f : (T) -> Bool) -> Bool {
10692
for v in self {
10793
if not(f(v)) {
10894
return false
@@ -121,7 +107,7 @@ pub fn View::all[T](self : View[T], f : (T) -> Bool) -> Bool {
121107
/// assert_true!(v.any(fn(ele) { ele < 6 }))
122108
/// assert_false!(v.any(fn(ele) { ele < 1 }))
123109
/// ```
124-
pub fn View::any[T](self : View[T], f : (T) -> Bool) -> Bool {
110+
pub fn ArrayView::any[T](self : ArrayView[T], f : (T) -> Bool) -> Bool {
125111
for v in self {
126112
if f(v) {
127113
return true
@@ -151,7 +137,7 @@ pub fn View::any[T](self : View[T], f : (T) -> Bool) -> Bool {
151137
/// inspect!(arr.contains(6), content="false")
152138
/// }
153139
/// ```
154-
pub fn View::contains[T : Eq](self : View[T], value : T) -> Bool {
140+
pub fn ArrayView::contains[T : Eq](self : ArrayView[T], value : T) -> Bool {
155141
for v in self {
156142
if v == value {
157143
break true
@@ -160,224 +146,3 @@ pub fn View::contains[T : Eq](self : View[T], value : T) -> Bool {
160146
false
161147
}
162148
}
163-
164-
///|
165-
/// Returns an iterator that yields each element of the array view in sequence
166-
/// from start to end.
167-
///
168-
/// Parameters:
169-
///
170-
/// * `array_view` : The array view to iterate over.
171-
///
172-
/// Returns an iterator that yields elements of type `A` from the array view.
173-
///
174-
/// Example:
175-
///
176-
/// ```moonbit
177-
/// test "View::iter" {
178-
/// let arr = [1, 2, 3]
179-
/// let view = arr[1:]
180-
/// let mut sum = 0
181-
/// view.iter().each(fn(x) { sum = sum + x })
182-
/// inspect!(sum, content="5")
183-
/// }
184-
/// ```
185-
pub fn View::iter[A](self : View[A]) -> Iter[A] {
186-
Iter::new(fn(yield_) {
187-
for v in self {
188-
guard let IterContinue = yield_(v) else { x => break x }
189-
190-
} else {
191-
IterContinue
192-
}
193-
})
194-
}
195-
196-
///|
197-
/// Fold out values from an View according to certain rules.
198-
///
199-
/// # Example
200-
/// ```
201-
/// let sum = [1, 2, 3, 4, 5][:].fold(init=0, fn { sum, elem => sum + elem })
202-
/// assert_eq!(sum, 15)
203-
/// ```
204-
pub fn View::fold[A, B](self : View[A], init~ : B, f : (B, A) -> B) -> B {
205-
for i = 0, acc = init; i < self.length(); {
206-
continue i + 1, f(acc, self[i])
207-
} else {
208-
acc
209-
}
210-
}
211-
212-
///|
213-
/// Fold out values from an View according to certain rules in reversed turn.
214-
///
215-
/// # Example
216-
/// ```
217-
/// let sum = [1, 2, 3, 4, 5][:].rev_fold(init=0, fn { sum, elem => sum + elem })
218-
/// assert_eq!(sum, 15)
219-
/// ```
220-
pub fn View::rev_fold[A, B](self : View[A], init~ : B, f : (B, A) -> B) -> B {
221-
for i = self.length() - 1, acc = init; i >= 0; {
222-
continue i - 1, f(acc, self[i])
223-
} else {
224-
acc
225-
}
226-
}
227-
228-
///|
229-
/// Fold out values from an View according to certain rules with index.
230-
///
231-
/// # Example
232-
/// ```
233-
/// let sum = [1, 2, 3, 4, 5][:].foldi(init=0, fn { index, sum, _elem => sum + index })
234-
/// assert_eq!(sum, 10)
235-
/// ```
236-
pub fn View::foldi[A, B](self : View[A], init~ : B, f : (Int, B, A) -> B) -> B {
237-
for i = 0, acc = init; i < self.length(); {
238-
continue i + 1, f(i, acc, self[i])
239-
} else {
240-
acc
241-
}
242-
}
243-
244-
///|
245-
/// Fold out values from an View according to certain rules in reversed turn with index.
246-
///
247-
/// # Example
248-
/// ```
249-
/// let sum = [1, 2, 3, 4, 5][:].rev_foldi(init=0, fn { index, sum, _elem => sum + index })
250-
/// assert_eq!(sum, 10)
251-
/// ```
252-
pub fn View::rev_foldi[A, B](
253-
self : View[A],
254-
init~ : B,
255-
f : (Int, B, A) -> B
256-
) -> B {
257-
let len = self.length()
258-
for i = len - 1, acc = init; i >= 0; {
259-
continue i - 1, f(len - i - 1, acc, self[i])
260-
} else {
261-
acc
262-
}
263-
}
264-
265-
///|
266-
/// Maps a function over the elements of the array view.
267-
///
268-
/// # Example
269-
/// ```
270-
/// let v = [3, 4, 5]
271-
/// let v2 = v[1:].map(fn (x) {x + 1})
272-
/// assert_eq!(v2, [5, 6])
273-
/// ```
274-
pub fn View::map[T, U](self : View[T], f : (T) -> U) -> Array[U] {
275-
if self.length() == 0 {
276-
return []
277-
}
278-
Array::makei(self.length(), fn(i) { f(self[i]) })
279-
}
280-
281-
///|
282-
/// Maps a function over the elements of the array view in place.
283-
///
284-
/// # Example
285-
/// ```
286-
/// let v = [3, 4, 5]
287-
/// v[1:].map_inplace(fn (x) {x + 1})
288-
/// assert_eq!(v, [3, 5, 6])
289-
/// ```
290-
pub fn View::map_inplace[T](self : View[T], f : (T) -> T) -> Unit {
291-
for i, v in self {
292-
self[i] = f(v)
293-
}
294-
}
295-
296-
///|
297-
/// Maps a function over the elements of the array view with index.
298-
///
299-
/// # Example
300-
/// ```
301-
/// let v = [3, 4, 5]
302-
/// let v2 = v[1:].mapi(fn (i, x) {x + i})
303-
/// assert_eq!(v2, [4, 6])
304-
/// ```
305-
pub fn View::mapi[T, U](self : View[T], f : (Int, T) -> U) -> Array[U] {
306-
if self.length() == 0 {
307-
return []
308-
}
309-
Array::makei(self.length(), fn(i) { f(i, self[i]) })
310-
}
311-
312-
///|
313-
/// Maps a function over the elements of the array view with index in place.
314-
///
315-
/// # Example
316-
/// ```
317-
/// let v = [3, 4, 5]
318-
/// v[1:].mapi_inplace(fn (i, x) {x + i})
319-
/// assert_eq!(v, [3, 4, 6])
320-
/// ```
321-
pub fn View::mapi_inplace[T](self : View[T], f : (Int, T) -> T) -> Unit {
322-
for i, v in self {
323-
self[i] = f(i, v)
324-
}
325-
}
326-
327-
///|
328-
/// Filters the array view with a predicate function.
329-
///
330-
/// # Example
331-
/// ```
332-
/// let arr = [1, 2, 3, 4, 5, 6]
333-
/// let v = arr[2:].filter(fn (x) { x % 2 == 0 })
334-
/// assert_eq!(v, [4, 6])
335-
/// ```
336-
pub fn View::filter[T](self : View[T], f : (T) -> Bool) -> Array[T] {
337-
let arr = []
338-
for v in self {
339-
if f(v) {
340-
arr.push(v)
341-
}
342-
}
343-
arr
344-
}
345-
346-
///|
347-
pub impl[X : Show] Show for View[X] with output(self, logger) {
348-
logger.write_iter(self.iter())
349-
}
350-
351-
///|
352-
pub impl[T : Eq] Eq for View[T] with op_equal(self, other) -> Bool {
353-
if self.length() != other.length() {
354-
return false
355-
}
356-
for i in 0..<self.length() {
357-
if not(self[i] == other[i]) {
358-
return false
359-
}
360-
} else {
361-
true
362-
}
363-
}
364-
365-
///|
366-
pub impl[T : Compare] Compare for View[T] with compare(self, other) -> Int {
367-
let len_self = self.length()
368-
let len_other = other.length()
369-
if len_self < len_other {
370-
-1
371-
} else if len_self > len_other {
372-
1
373-
} else {
374-
for i in 0..<len_self {
375-
let cmp = self[i].compare(other[i])
376-
if cmp != 0 {
377-
break cmp
378-
}
379-
} else {
380-
0
381-
}
382-
}
383-
}

0 commit comments

Comments
 (0)