Skip to content

Commit

Permalink
Merge branch 'main' into myfreess/add-array-zip
Browse files Browse the repository at this point in the history
  • Loading branch information
myfreess authored Jan 27, 2025
2 parents 01aafb4 + 4a6e310 commit fee014c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bytes/view_pattern_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025 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.

test "View::pattern" {
let view = b"\x01\x02\x03"[:]
match view {
[0x01, 0x02, .. rest] => inspect!(rest, content="b\"\\x03\"")
_ => abort("should not happen")
}
}
59 changes: 59 additions & 0 deletions immut/list/dps_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2025 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.

///|
pub enum L {
Nil
Cons(Int, mut tail~ : L)
} derive(Eq, Show)

///|
/// tail recursive map using dps
pub fn map(self : L, f : (Int) -> Int) -> L {
match self {
Nil => Nil
Cons(head, tail~) => {
let cell = Cons(f(head), tail=Nil)
map_dps(f, tail, cell)
cell
}
}
}

///|
fn map_dps(f : (Int) -> Int, lst : L, dest : L) -> Unit {
loop lst, dest {
Nil, Cons(_, ..) as c => c.tail = Nil
// c.tail = lst
// this is a common error
Cons(head, tail~), Cons(_, ..) as c => {
let cell = Cons(f(head), tail=Nil)
c.tail = cell
continue tail, cell
}
_, Nil => abort("map_dps: dest is Nil")
}
}

test "map" {
let lstL : L = Cons(
1,
tail=Cons(2, tail=Cons(3, tail=Cons(4, tail=Cons(5, tail=Nil)))),
)
let lst : L = map(lstL, fn { x => x * 2 })
inspect!(
lst,
content="Cons(2, tail=Cons(4, tail=Cons(6, tail=Cons(8, tail=Cons(10, tail=Nil)))))",
)
}
6 changes: 6 additions & 0 deletions int16/int16.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub let max_value : Int16 = 32767

///|
pub let min_value : Int16 = -32768

///|
pub fn op_add(self : Int16, that : Int16) -> Int16 {
(self.to_int() + that.to_int()).to_int16()
Expand Down
3 changes: 3 additions & 0 deletions int16/int16.mbti
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package moonbitlang/core/int16

// Values
let max_value : Int16

let min_value : Int16

// Types and methods
impl Int16 {
Expand Down
30 changes: 30 additions & 0 deletions string/view_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ test "index_at with surrogate pairs" {
inspect!(offset, content="StringIndex(6)")
let offset = str.index_at(4)
inspect!(offset, content="None")
inspect!(
str[1:2],
content=
#|"🤣"
,
)
inspect!(
str[1:3],
content=
#|"🤣🤣"
,
)
inspect!(
str[1:-1],
content=
#|"🤣"
,
)
inspect!(
str[1:-2],
content=
#|""
,
)
inspect!(
str[:-1],
content=
#|"🤣🤣"
,
)
}

test "stringview basic" {
Expand Down
6 changes: 6 additions & 0 deletions uint16/uint16.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub let max_value : UInt16 = 65535

///|
pub let min_value : UInt16 = 0

///|
pub fn op_add(self : UInt16, that : UInt16) -> UInt16 {
(self.to_int() + that.to_int()).to_uint16()
Expand Down
3 changes: 3 additions & 0 deletions uint16/uint16.mbti
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package moonbitlang/core/uint16

// Values
let max_value : UInt16

let min_value : UInt16

// Types and methods
impl UInt16 {
Expand Down

0 comments on commit fee014c

Please sign in to comment.