1///|
2/// A `Slice` is a slice of an `Array`.
3///
4/// A separate Slice type was needed because of this issue:
5/// https://github.com/moonbitlang/core/issues/1063
6///
7/// This struct is based on MoonBit's "ArrayView" implementation here:
8/// https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt
9/// which has the following copyright:
10///
11/// Copyright 2024 International Digital Economy Academy
12///
13/// Licensed under the Apache License, Version 2.0 (the "License");
14/// you may not use this file except in compliance with the License.
15/// You may obtain a copy of the License at
16///
17///     http://www.apache.org/licenses/LICENSE-2.0
18///
19/// Unless required by applicable law or agreed to in writing, software
20/// distributed under the License is distributed on an "AS IS" BASIS,
21/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22/// See the License for the specific language governing permissions and
23/// limitations under the License.
24struct Slice[T] {
25  
Array[T]
buf
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
]
26
Int
start
:
Int
Int
27
Int
len
:
Int
Int
28} 29 30///| 31/// length returns the length of the slice. 32pub fn[T]
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::
(self : Slice[T]) -> Int

length returns the length of the slice.

length
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
]) ->
Int
Int
{
33
Slice[T]
self
.
Int
len
34} 35 36///| 37pub fn[T]
(self : Slice[T], index : Int) -> T
op_get
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
Int
index
:
Int
Int
) ->

type parameter T

T
{
38 guard
Int
index
(self_ : Int, other : Int) -> Bool
>=
0
(Bool, Bool) -> Bool
&&
Int
index
(self_ : Int, other : Int) -> Bool
<
Slice[T]
self
.
Int
len
else {
39
(msg : String) -> T

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
(
40 "index out of bounds: the len is from 0 to \{
Slice[T]
self
.
Int
len
} but the index is \{
Int
index
}",
41 ) 42 } 43
Slice[T]
self
.
Array[T]
buf
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
index
]
44} 45 46///| 47pub fn[T]
(self : Slice[T], index : Int, value : T) -> Unit
op_set
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
Int
index
:
Int
Int
,
T
value
:

type parameter T

T
) ->
Unit
Unit
{
48 guard
Int
index
(self_ : Int, other : Int) -> Bool
>=
0
(Bool, Bool) -> Bool
&&
Int
index
(self_ : Int, other : Int) -> Bool
<
Slice[T]
self
.
Int
len
else {
49
(msg : String) -> Unit

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
(
50 "index out of bounds: the len is from 0 to \{
Slice[T]
self
.
Int
len
} but the index is \{
Int
index
}",
51 ) 52 } 53
Slice[T]
self
.
Array[T]
buf
(Array[T], Int, T) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic Array::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
index
] =
T
value
54} 55 56///| 57pub fn[T]
(self : Slice[T], i : Int, j : Int) -> Unit
swap
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
Int
i
:
Int
Int
,
Int
j
:
Int
Int
) ->
Unit
Unit
{
58 guard
Int
i
(self_ : Int, other : Int) -> Bool
>=
0
(Bool, Bool) -> Bool
&&
Int
i
(self_ : Int, other : Int) -> Bool
<
Slice[T]
self
.
Int
len
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
>=
0
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Slice[T]
self
.
Int
len
else {
59
(msg : String) -> Unit

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
(
60 "index out of bounds: the len is from 0 to \{
Slice[T]
self
.
Int
len
} but the index is (\{
Int
i
}, \{
Int
j
})",
61 ) 62 } 63 let
T
temp
=
Slice[T]
self
.
Array[T]
buf
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
i
]
64
Slice[T]
self
.
Array[T]
buf
(Array[T], Int, T) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic Array::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
i
] =
Slice[T]
self
.
Array[T]
buf
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
j
]
65
Slice[T]
self
.
Array[T]
buf
(Array[T], Int, T) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic Array::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
j
] =
T
temp
66} 67 68///| 69pub fn[T]
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::
(buf : Array[T], start~ : Int = .., end? : Int) -> Slice[T]
new
(
Array[T]
buf
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
],
Int
start
~ :
Int
Int
= 0,
Int?
end
? :
Int
Int
) ->
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
] {
70 let
Int
len
=
Array[T]
buf
.
(self : Array[T]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

test "Array::length" {
  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
}
length
()
71 let
Int
end
= match
Int?
end
{
72
Int?
None
=>
Int
len
73
(Int) -> Int?
Some
(
Int
end
) =>
Int
end
74 } 75 guard
Int
start
(self_ : Int, other : Int) -> Bool
>=
0
(Bool, Bool) -> Bool
&&
Int
start
(self_ : Int, other : Int) -> Bool
<=
Int
end
(Bool, Bool) -> Bool
&&
Int
end
(self_ : Int, other : Int) -> Bool
<=
Int
len
else {
76
(msg : String) -> Slice[T]

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("View start index out of bounds")
77 } 78
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::{
Array[T]
buf
,
Int
start
,
Int
len
:
Int
end
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Int
start
}
79} 80 81///| 82pub fn[T]
(self : Slice[T]) -> ArrayView[T]
as_array_view
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
]) ->
type ArrayView[T]

An ArrayView represents a view into a section of an array without copying the data.

Example

let arr = [1, 2, 3, 4, 5]
let view = arr[1:4]  // Creates a view of elements at indices 1,2,3
assert_eq(view[0], 2)
assert_eq(view.length(), 3)
ArrayView
[

type parameter T

T
] {
83 let
Int
end
=
Slice[T]
self
.
Int
len
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
self
.
Int
start
84
Slice[T]
self
.
Array[T]
buf
[
Slice[T]
self
.
Int
start
:
Int
end
]
85} 86 87///| 88pub fn[T]
(self : Slice[T], start~ : Int = .., end? : Int) -> Slice[T]
op_as_view
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
Int
start
~ :
Int
Int
= 0,
Int?
end
? :
Int
Int
) ->
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
] {
89 let
Int
len
=
Slice[T]
self
.
Array[T]
buf
.
(self : Array[T]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

test "Array::length" {
  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
}
length
()
90 let
Int
end
= match
Int?
end
{
91
Int?
None
=>
Slice[T]
self
.
Int
len
92
(Int) -> Int?
Some
(
Int
end
) =>
Int
end
93 } 94 guard
Slice[T]
self
.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
start
(self_ : Int, other : Int) -> Bool
>=
0
(Bool, Bool) -> Bool
&&
Int
start
(self_ : Int, other : Int) -> Bool
<=
Int
end
(Bool, Bool) -> Bool
&&
Int
end
(self_ : Int, other : Int) -> Bool
<=
Int
len
else {
95
(msg : String) -> Slice[T]

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("View start index out of bounds")
96 } 97
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::{
Array[T]
buf
:
Slice[T]
self
.
Array[T]
buf
,
Int
start
:
Slice[T]
self
.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
start
,
Int
len
:
Int
end
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Int
start
}
98} 99 100///| 101/// cap returns the total capacity of the slice regardless of the current length. 102pub fn[T]
(self : Slice[T]) -> Int

cap returns the total capacity of the slice regardless of the current length.

cap
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
]) ->
Int
Int
{
103
Slice[T]
self
.
Array[T]
buf
.
(self : Array[T]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

test "Array::length" {
  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
}
length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Slice[T]
self
.
Int
start
104} 105 106///| 107/// append appends `s` to the end of the slice, reallocating the underlying 108/// array if necessary, then returns the new slice. 109pub fn[T]
(self : Slice[T], s : Slice[T]) -> Slice[T]

append appends s to the end of the slice, reallocating the underlying array if necessary, then returns the new slice.

append
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
Slice[T]
s
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
]) ->
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
] {
110 let
Int
remaining
=
Slice[T]
self
.
(self : Slice[T]) -> Int

cap returns the total capacity of the slice regardless of the current length.

cap
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
111 if
Int
remaining
(self_ : Int, other : Int) -> Bool
>=
Slice[T]
s
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() {
112 // plenty of room, reuse current internal Array. 113 let
Int
idx
=
Slice[T]
self
.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
self
.
Int
len
114 for
Int
i
in
Int
0
..<
Slice[T]
s
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() {
115
Slice[T]
self
.
Array[T]
buf
(Array[T], Int, T) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic Array::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
idx
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
i
] =
Slice[T]
s
(Slice[T], Int) -> T
[
i]
116 } 117 return {
Array[T]
buf
:
Slice[T]
self
.
Array[T]
buf
,
Int
start
:
Slice[T]
self
.
Int
start
,
Int
len
:
Slice[T]
self
.
Int
len
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
s
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() }
118 } 119 // reallocate array, copy the data, then return the new slice 120 let
Array[T]
arr
=
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(capacity~ : Int) -> Array[T]

Creates a new empty array with an optional initial capacity.

Parameters:

  • capacity : The initial capacity of the array. If 0 (default), creates an array with minimum capacity. Must be non-negative.

Returns a new empty array of type Array[T] with the specified initial capacity.

Example:

test "Array::new" {
  let arr : Array[Int] = Array::new(capacity=10)
  inspect(arr.length(), content="0")
  inspect(arr.capacity(), content="10")
}

test "Array::new/default" {
  let arr : Array[Int] = Array::new()
  inspect(arr.length(), content="0")
}
new
(
Int
capacity
=
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
s
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
())
121 let
Int
idx
=
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
122 for
Int
i
in
Int
0
..<
Int
idx
{
123
Array[T]
arr
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
Slice[T]
self
.
Array[T]
buf
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
self
.
Int
start
])
124 } 125 for
Int
i
in
Int
0
..<
Slice[T]
s
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() {
126
Array[T]
arr
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
Slice[T]
s
.
Array[T]
buf
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
s
.
Int
start
])
127 } 128 {
Array[T]
buf
:
Array[T]
arr
,
Int
start
: 0,
Int
len
:
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Slice[T]
s
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() }
129} 130 131///| 132/// push appends `e` to the end of the slice, reallocating the underlying 133/// array if necessary, then returns the new slice. 134pub fn[T]
(self : Slice[T], e : T) -> Slice[T]

push appends e to the end of the slice, reallocating the underlying array if necessary, then returns the new slice.

push
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
T
e
:

type parameter T

T
) ->
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
] {
135
Slice[T]
self
.
(self : Slice[T], s : Slice[T]) -> Slice[T]

append appends s to the end of the slice, reallocating the underlying array if necessary, then returns the new slice.

append
(
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::
(buf : Array[T], start~ : Int = .., end? : Int) -> Slice[T]
new
([
T
e
]))
136} 137 138///| 139pub fn[A]
(self : Slice[A]) -> Iter[A]
iter
(
Slice[A]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter A

A
]) ->
type Iter[T]
Iter
[

type parameter A

A
] {
140
type Iter[T]
Iter
::
(((A) -> IterResult) -> IterResult) -> Iter[A]

Do not use this method, it is for internal use only.

new
(fn(
(A) -> IterResult
yield_fn
) {
141 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Slice[A]
self
.
Int
len
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
142 let
A
v
=
Slice[A]
self
.
Array[A]
buf
(Array[A], Int) -> A

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
i
]
143 guard
(A) -> IterResult
yield_fn
(
A
v
) is
IterResult
IterContinue
else { break
IterResult
IterEnd
}
144 } else { 145
IterResult
IterContinue
146 } 147 }) 148} 149 150///| 151pub fn[A]
(self : Slice[A]) -> Iter2[Int, A]
iter2
(
Slice[A]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter A

A
]) ->
type Iter2[A, B]
Iter2
[
Int
Int
,

type parameter A

A
] {
152
type Iter2[A, B]
Iter2
::
(((Int, A) -> IterResult) -> IterResult) -> Iter2[Int, A]
new
(fn(
(Int, A) -> IterResult
yield_fn
) {
153 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Slice[A]
self
.
Int
len
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
154 let
A
v
=
Slice[A]
self
.
Array[A]
buf
(Array[A], Int) -> A

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
self.
Int
start
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
i
]
155 guard
(Int, A) -> IterResult
yield_fn
(
Int
i
,
A
v
) is
IterResult
IterContinue
else { break
IterResult
IterEnd
}
156 } else { 157
IterResult
IterContinue
158 } 159 }) 160} 161 162///| 163/// Fold out values from an Slice according to certain rules. 164/// 165/// # Example 166/// ``` 167/// let sum = [1, 2, 3, 4, 5][:].fold(init=0, fn { sum, elem => sum + elem }) 168/// sum // 15 169/// ``` 170pub fn[A, B]
(self : Slice[A], init~ : B, f : (B, A) -> B) -> B

Fold out values from an Slice according to certain rules.

Example

let sum = [1, 2, 3, 4, 5][:].fold(init=0, fn { sum, elem => sum + elem })
sum // 15
fold
(
Slice[A]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter A

A
],
B
init
~ :

type parameter B

B
,
(B, A) -> B
f
: (

type parameter B

B
,

type parameter A

A
) ->

type parameter B

B
) ->

type parameter B

B
{
171 for
Int
i
= 0,
B
acc
=
B
init
;
Int
i
(self_ : Int, other : Int) -> Bool
<
Slice[A]
self
.
(self : Slice[A]) -> Int

length returns the length of the slice.

length
(); {
172 continue
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1,
(B, A) -> B
f
(
B
acc
,
Slice[A]
self
(Slice[A], Int) -> A
[
i])
173 } else { 174
B
acc
175 } 176} 177 178///| 179/// Fold out values from an Slice according to certain rules in reversed turn. 180/// 181/// # Example 182/// ``` 183/// let sum = [1, 2, 3, 4, 5][:].rev_fold(init=0, fn { sum, elem => sum + elem }) 184/// sum // 15 185/// ``` 186pub fn[A, B]
(self : Slice[A], init~ : B, f : (B, A) -> B) -> B

Fold out values from an Slice according to certain rules in reversed turn.

Example

let sum = [1, 2, 3, 4, 5][:].rev_fold(init=0, fn { sum, elem => sum + elem })
sum // 15
rev_fold
(
Slice[A]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter A

A
],
B
init
~ :

type parameter B

B
,
(B, A) -> B
f
: (

type parameter B

B
,

type parameter A

A
) ->

type parameter B

B
) ->

type parameter B

B
{
187 for
Int
i
=
Slice[A]
self
.
(self : Slice[A]) -> Int

length returns the length of the slice.

length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
1,
B
acc
=
B
init
;
Int
i
(self_ : Int, other : Int) -> Bool
>=
0; {
188 continue
Int
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
1,
(B, A) -> B
f
(
B
acc
,
Slice[A]
self
(Slice[A], Int) -> A
[
i])
189 } else { 190
B
acc
191 } 192} 193 194///| 195/// Fold out values from an Slice according to certain rules with index. 196/// 197/// # Example 198/// ``` 199/// let sum = [1, 2, 3, 4, 5][:].foldi(init=0, fn { index, sum, elem => sum + index }) 200/// sum // 10 201/// ``` 202pub fn[A, B]
(self : Slice[A], init~ : B, f : (Int, B, A) -> B) -> B

Fold out values from an Slice according to certain rules with index.

Example

let sum = [1, 2, 3, 4, 5][:].foldi(init=0, fn { index, sum, elem => sum + index })
sum // 10
foldi
(
Slice[A]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter A

A
],
B
init
~ :

type parameter B

B
,
(Int, B, A) -> B
f
: (
Int
Int
,

type parameter B

B
,

type parameter A

A
) ->

type parameter B

B
) ->

type parameter B

B
{
203 for
Int
i
= 0,
B
acc
=
B
init
;
Int
i
(self_ : Int, other : Int) -> Bool
<
Slice[A]
self
.
(self : Slice[A]) -> Int

length returns the length of the slice.

length
(); {
204 continue
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1,
(Int, B, A) -> B
f
(
Int
i
,
B
acc
,
Slice[A]
self
(Slice[A], Int) -> A
[
i])
205 } else { 206
B
acc
207 } 208} 209 210///| 211/// Fold out values from an Slice according to certain rules in reversed turn with index. 212/// 213/// # Example 214/// ``` 215/// let sum = [1, 2, 3, 4, 5][:].rev_foldi(~init=0, fn { index, sum, elem => sum + index }) 216/// sum // 10 217/// ``` 218pub fn[A, B]
(self : Slice[A], init~ : B, f : (Int, B, A) -> B) -> B

Fold out values from an Slice according to certain rules in reversed turn with index.

Example

let sum = [1, 2, 3, 4, 5][:].rev_foldi(~init=0, fn { index, sum, elem => sum + index })
sum // 10
rev_foldi
(
Slice[A]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter A

A
],
B
init
~ :

type parameter B

B
,
(Int, B, A) -> B
f
: (
Int
Int
,

type parameter B

B
,

type parameter A

A
) ->

type parameter B

B
) ->

type parameter B

B
{
219 let
Int
len
=
Slice[A]
self
.
(self : Slice[A]) -> Int

length returns the length of the slice.

length
()
220 for
Int
i
=
Int
len
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
1,
B
acc
=
B
init
;
Int
i
(self_ : Int, other : Int) -> Bool
>=
0; {
221 continue
Int
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
1,
(Int, B, A) -> B
f
(
Int
len
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Int
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
1,
B
acc
,
Slice[A]
self
(Slice[A], Int) -> A
[
i])
222 } else { 223
B
acc
224 } 225} 226 227///| 228pub impl[X :
trait Show {
  output(Self, &Logger) -> Unit
  to_string(Self) -> String
}

Trait for types that can be converted to String

Show
]
trait Show {
  output(Self, &Logger) -> Unit
  to_string(Self) -> String
}

Trait for types that can be converted to String

Show
for
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter X

X
] with
(self : Slice[X], logger : &Logger) -> Unit
output
(
Slice[X]
self
,
&Logger
logger
) {
229
&Logger
logger
.
(self : &Logger, iter : Iter[X], prefix~ : String = .., suffix~ : String = .., sep~ : String = .., trailing~ : Bool = ..) -> Unit
write_iter
(
Slice[X]
self
.
(self : Slice[X]) -> Iter[X]
iter
())
230} 231 232// pub fn Slice::to_string[X : Show](self : Slice[X]) -> String { 233// Show::to_string(self) 234// } 235 236///| 237pub fn[T]
(self : Slice[T]) -> Unit
rev_inplace
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
]) ->
Unit
Unit
{
238 let
Int
mid_len
=
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

test "Int::op_div" {
  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
}

test "panic Int::op_div/division_by_zero" {
  ignore(42 / 0) // Panics with division by zero
}
/
2
239 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Int
mid_len
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
240 let
Int
j
=
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Int
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
1
241
Slice[T]
self
.
(self : Slice[T], i : Int, j : Int) -> Unit
swap
(
Int
i
,
Int
j
)
242 } 243} 244 245///| 246pub fn[T]
(self : Slice[T], f : (T) -> Unit) -> Unit
each
(
Slice[T]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
],
(T) -> Unit
f
: (

type parameter T

T
) ->
Unit
Unit
) ->
Unit
Unit
{
247 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
248
(T) -> Unit
f
(
Slice[T]
self
(Slice[T], Int) -> T
[
i])
249 } 250} 251 252///| 253/// Compares two slices for equality. 254pub impl[T :
trait Eq {
  op_equal(Self, Self) -> Bool
}

Trait for types whose elements can test for equality

Eq
]
trait Eq {
  op_equal(Self, Self) -> Bool
}

Trait for types whose elements can test for equality

Eq
for
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[

type parameter T

T
] with
(self : Slice[T], other : Slice[T]) -> Bool

Compares two slices for equality.

op_equal
(
Slice[T]
self
,
Slice[T]
other
) {
255 guard
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "Int::op_equal" {
  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
}
==
Slice[T]
other
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() else { return false }
256 for
Int
i
= 0 {
257 // CR: format issue 258 if
Int
i
(self_ : Int, other : Int) -> Bool
>=
Slice[T]
self
.
(self : Slice[T]) -> Int

length returns the length of the slice.

length
() {
259 break true 260 } 261 if
Slice[T]
self
(Slice[T], Int) -> T
[
i]
(x : T, y : T) -> Bool
!=
Slice[T]
other
(Slice[T], Int) -> T
[
i] {
262 break false 263 } 264 continue
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1
265 } 266} 267 268///| 269/// to_bytes returns slice as Bytes. 270pub fn
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::
(self : Slice[Byte]) -> Bytes

to_bytes returns slice as Bytes.

to_bytes
(
Slice[Byte]
self
:
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
[
Byte
Byte
]) ->
Bytes
Bytes
{
271
(Iter[Byte]) -> Bytes
Bytes::
(iter : Iter[Byte]) -> Bytes

Creates a new bytes sequence from an iterator of bytes.

Parameters:

  • iterator : An iterator that yields bytes.

Returns a new bytes sequence containing all the bytes from the iterator.

Example:

test "from_iter" {
  let iter = Iter::singleton(b'h')
  let bytes = @bytes.from_iter(iter)
  inspect(bytes, content=
    #|b"\x68"
  )
}
from_iter
(
Slice[Byte]
self
.
(self : Slice[Byte]) -> Iter[Byte]
iter
())
272} 273