Skip to content

Commit

Permalink
add ImmutableVec::to_array method moonbitlang#50
Browse files Browse the repository at this point in the history
  • Loading branch information
ZnPdCo committed May 24, 2024
1 parent f1bb1c7 commit d533deb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
4 changes: 4 additions & 0 deletions immut/array/array.mbti
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package moonbitlang/core/immut/array

alias @moonbitlang/core/iter as @iter

// Values
fn immutable_push[T](Array[T], T) -> Array[T]

Expand All @@ -12,6 +14,7 @@ fn length[T](ImmutableVec[T]) -> Int
// Types and methods
type ImmutableVec
impl ImmutableVec {
as_iter[T](Self[T]) -> @iter.Iter[T]
copy[T](Self[T]) -> Self[T]
debug_write[T : Debug](Self[T], Buffer) -> Unit
empty[T]() -> Self[T]
Expand All @@ -27,6 +30,7 @@ impl ImmutableVec {
op_get[T](Self[T], Int) -> T
push[T](Self[T], T) -> Self[T]
set[T](Self[T], Int, T) -> Self[T]
to_array[T](Self[T]) -> Array[T]
to_string[T : Show](Self[T]) -> String
}

Expand Down
45 changes: 19 additions & 26 deletions immut/array/immutable_vec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub fn ImmutableVec::empty[T]() -> ImmutableVec[T] {
{ tree: Tree::empty(), size: 0, shift: 0 }
}

test "is_empty" {
let v = ImmutableVec::[1, 2, 3, 4, 5]
let ve : ImmutableVec[Int] = ImmutableVec::empty()
inspect(is_empty(v), content="false")?
inspect(is_empty(ve), content="true")?
}

pub fn to_string[T : Show](self : ImmutableVec[T]) -> String {
let mut s = "ImmutableVec::[".to_string()
for i = 0; i < self.size; i = i + 1 {
Expand All @@ -39,16 +46,18 @@ pub fn is_empty[T](v : ImmutableVec[T]) -> Bool {
v.size == 0
}

pub fn to_array[T : Show](self : ImmutableVec[T]) -> String {
let mut s = "ImmutableVec::[".to_string()
pub fn to_array[T](self : ImmutableVec[T]) -> Array[T] {
let arr = Array::new()
for i = 0; i < self.size; i = i + 1 {
s = s + self[i].to_string()
if i < self.size - 1 {
s = s + ", "
}
arr.push(self[i])
}
s = s + "]"
s
arr
}

test "to_array" {
let v = ImmutableVec::[1, 2, 3, 4]
inspect(v.to_array().to_string(), content="[1, 2, 3, 4]")?
inspect(v.push(5).to_array().to_string(), content="[1, 2, 3, 4, 5]")?
}

pub fn as_iter[T](self : ImmutableVec[T]) -> @iter.Iter[T] {
Expand All @@ -68,29 +77,13 @@ pub fn as_iter[T](self : ImmutableVec[T]) -> @iter.Iter[T] {
test "as_iter" {
let buf = Buffer::make(20)
let v = ImmutableVec::[1, 2, 3]
v.as_iter().iter(
fn(e) {
buf.write_string("[\(e)]")
},
)
v.as_iter().iter(fn(e) { buf.write_string("[\(e)]") })
inspect(buf, content="[1][2][3]")?
buf.reset()
v.as_iter().take(2).iter(
fn(e) {
buf.write_string("[\(e)]")
},
)
v.as_iter().take(2).iter(fn(e) { buf.write_string("[\(e)]") })
inspect(buf, content="[1][2]")?
}


test "is_empty" {
let v = ImmutableVec::[1, 2, 3, 4, 5]
let ve : ImmutableVec[Int] = ImmutableVec::empty()
inspect(is_empty(v), content="false")?
inspect(is_empty(ve), content="true")?
}

pub fn length[T](v : ImmutableVec[T]) -> Int {
v.size
}
Expand Down

0 comments on commit d533deb

Please sign in to comment.