Skip to content

ArrayView::join and FixedArray::join #2125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions array/array.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl FixedArray {
from_array[T](Array[T]) -> Self[T]
from_iter[T](Iter[T]) -> Self[T]
is_sorted[T : Compare](Self[T]) -> Bool
join(Self[String], String) -> String
last[A](Self[A]) -> A?
makei[T](Int, (Int) -> T) -> Self[T]
map[T, U](Self[T], (T) -> U) -> Self[U]
Expand Down Expand Up @@ -99,6 +100,7 @@ impl ArrayView {
fold[A, B](Self[A], init~ : B, (B, A) -> B) -> B
foldi[A, B](Self[A], init~ : B, (Int, B, A) -> B) -> B
iter[A](Self[A]) -> Iter[A]
join(Self[String], String) -> String
map[T, U](Self[T], (T) -> U) -> Array[U]
map_inplace[T](Self[T], (T) -> T) -> Unit
mapi[T, U](Self[T], (Int, T) -> U) -> Array[U]
Expand Down
39 changes: 39 additions & 0 deletions array/fixedarray.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,45 @@ pub fn FixedArray::last[A](self : FixedArray[A]) -> A? {
}
}

///|
/// Concatenate strings within an array into a single complete string.
///
/// Example:
///
/// ```moonbit
/// test "FixedArray::join" {
/// let fixed_array:FixedArray[String]=["1","2","3"]
/// inspect!(fixed_array.join(","), content="1,2,3")
/// }
/// ```
pub fn FixedArray::join(
self : FixedArray[String],
separator : @string.View
) -> String {
let len = self.length()
if len == 0 {
return ""
}
let first = self[0]
let mut size_hint = first.length()
for i = 1; i < len; i = i + 1 {
size_hint += separator.length() + self[i].length()
}
let string = StringBuilder::new(size_hint~)
if separator.is_empty() {
for i = 0; i < len; i = i + 1 {
string.write_string(self[i])
}
} else {
string.write_string(self[0])
for i = 1; i < len; i = i + 1 {
string.write_string(separator.to_string())
string.write_string(self[i])
}
}
string.to_string()
}

///|
test "FixedArray::last/empty" {
let empty : FixedArray[Int] = []
Expand Down
17 changes: 17 additions & 0 deletions array/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,23 @@ pub fn View::to_array[T](self : View[T]) -> Array[T] {
}
}

///|
/// Concatenate strings within an array into a single complete string.
///
/// Example:
///
/// ```moonbit
/// test "View::join" {
/// let a:Array[String]=["1","2","3"]
/// let array_view = a[:]
/// inspect!(array_view.join(","), content="1,2,3")
/// }
/// ```
pub fn View::join(self : ArrayView[String], separator : String) -> String {
let array = self.to_array()
@string.concat(array, separator~)
}

///|
pub impl[X : Show] Show for View[X] with output(self, logger) {
logger.write_iter(self.iter())
Expand Down