Skip to content

Commit

Permalink
return Unit explicitly (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: Hongbo Zhang <bob.hongbo.zhang@foxmail.com>
  • Loading branch information
lijunchen and bobzhang committed Mar 15, 2024
1 parent 3a04a26 commit fd9b6bf
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// ```
/// [1, 2, 3, 4, 5].iter(fn(x){ print("\(x) ") }) //output: 1 2 3 4 5
/// ```
pub fn iter[T](self : Array[T], f : (T) -> Unit) {
pub fn iter[T](self : Array[T], f : (T) -> Unit) -> Unit {
for i = 0; i < self.length(); i = i + 1 {
f(self[i])
}
Expand Down Expand Up @@ -53,7 +53,7 @@ test "iter" {
/// print("(\(index),\(elem)) ")
/// }) //output: (0,1) (1,2) (2,3) (3,4) (4,5)
/// ```
pub fn iteri[T](self : Array[T], f : (Int, T) -> Unit) {
pub fn iteri[T](self : Array[T], f : (Int, T) -> Unit) -> Unit {
for i = 0; i < self.length(); i = i + 1 {
f(i, self[i])
}
Expand All @@ -73,7 +73,7 @@ test "iteri" {
@assertion.assert_false(failed)?
}

pub fn iter_rev[T](self : Array[T], f : (T) -> Unit) {
pub fn iter_rev[T](self : Array[T], f : (T) -> Unit) -> Unit {
for i = self.length() - 1; i >= 0; i = i - 1 {
f(self[i])
}
Expand All @@ -88,7 +88,7 @@ test "iter rev" {
@assertion.assert_false(failed)?
}

pub fn iteri_rev[T](self : Array[T], f : (Int, T) -> Unit) {
pub fn iteri_rev[T](self : Array[T], f : (Int, T) -> Unit) -> Unit {
for i = self.length() - 1; i >= 0; i = i - 1 {
f(i, self[i])
}
Expand Down
4 changes: 2 additions & 2 deletions list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test "length" {
/// ```
/// from_array([1, 2, 3, 4, 5]).iter(print) // output: 12345
/// ```
pub fn iter[T](self : List[T], f : (T) -> Unit) {
pub fn iter[T](self : List[T], f : (T) -> Unit) -> Unit {
match self {
Nil => ()
Cons(head, tail) => {
Expand All @@ -81,7 +81,7 @@ test "iter" {
/// from_array([1, 2, 3, 4, 5]).iteri(fn(i, x) { print("(\(i),\(x)) ") })
/// // output: (0,1) (1,2) (2,3) (3,4) (4,5)
/// ```
pub fn iteri[T](self : List[T], f : (Int, T) -> Unit) {
pub fn iteri[T](self : List[T], f : (Int, T) -> Unit) -> Unit {
loop self, 0 {
Nil, _ => ()
Cons(x, xs), i => {
Expand Down
2 changes: 1 addition & 1 deletion map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn lookup[K : Compare, V](self : Map[K, V], key : K) -> Option[V] {
}

/// Iterate over the key-value pairs in the map.
pub fn iter[K : Compare, V](self : Map[K, V], f : (K, V) -> Unit) {
pub fn iter[K : Compare, V](self : Map[K, V], f : (K, V) -> Unit) -> Unit {
match self {
Empty => ()
Tree(k, v, _, l, r) => {
Expand Down
4 changes: 2 additions & 2 deletions ref/ref.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ test "protect" {
/// debug(x) //output: ref(2)
/// debug(y) //output: ref(1)
/// ```
pub fn swap[T](self : Ref[T], that : Ref[T]) {
pub fn swap[T](self : Ref[T], that : Ref[T]) -> Unit {
let tmp = self.val
self.val = that.val
that.val = tmp
Expand All @@ -98,7 +98,7 @@ test "swap" {
@assertion.assert_eq(y.val, 1)?
}

pub fn update[T](self : Ref[T], f : (T) -> T) {
pub fn update[T](self : Ref[T], f : (T) -> T) -> Unit {
self.val = f(self.val)
}

Expand Down
2 changes: 1 addition & 1 deletion stack/stack.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test "from_stack" {
/// s.clear()
/// println(s) //output: {elements: Nil, len: 0}
/// ```
pub fn clear[T](self : Stack[T]) {
pub fn clear[T](self : Stack[T]) -> Unit {
self.elements = Nil
self.len = 0
}
Expand Down
34 changes: 17 additions & 17 deletions vec/vec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn capacity[T](self : Vec[T]) -> Int {
}

/// Reallocate the vector with a new capacity.
fn realloc[T](self : Vec[T]) {
fn realloc[T](self : Vec[T]) -> Unit {
let old_cap = self.len
let new_cap = if old_cap == 0 { 8 } else { old_cap * 2 }
let new_buf = UninitializedArray::make(new_cap)
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn op_get[T](self : Vec[T], index : Int) -> T {
/// v.push(3)
/// println(v[0]) // 3
/// ```
pub fn op_set[T](self : Vec[T], index : Int, value : T) {
pub fn op_set[T](self : Vec[T], index : Int, value : T) -> Unit {
if index >= self.len {
let len = self.len
abort("index out of bounds: the len is \(len) but the index is \(index)")
Expand Down Expand Up @@ -175,7 +175,7 @@ test "pop" {
/// let v = Vec::new()
/// v.push(3)
/// ```
pub fn push[T](self : Vec[T], value : T) {
pub fn push[T](self : Vec[T], value : T) -> Unit {
if self.len == self.buf.length() {
self.realloc()
}
Expand All @@ -200,7 +200,7 @@ test "push" {
/// let v2 = Vec::[6, 7, 8]
/// v1.append(v2)
/// ```
pub fn append[T](self : Vec[T], other : Vec[T]) {
pub fn append[T](self : Vec[T], other : Vec[T]) -> Unit {
for i = 0; i < other.len; i = i + 1 {
self.push(other[i])
}
Expand Down Expand Up @@ -230,7 +230,7 @@ test "append" {
/// let mut sum = 0
/// v.iter(fn (x) {sum = sum + x})
/// ```
pub fn iter[T](self : Vec[T], f : (T) -> Unit) {
pub fn iter[T](self : Vec[T], f : (T) -> Unit) -> Unit {
for i = 0; i < self.len; i = i + 1 {
f(self[i])
}
Expand All @@ -257,7 +257,7 @@ test "iter" {
/// let mut sum = 0
/// v.iteri(fn (i, x) {sum = sum + x + i})
/// ```
pub fn iteri[T](self : Vec[T], f : (Int, T) -> Unit) {
pub fn iteri[T](self : Vec[T], f : (Int, T) -> Unit) -> Unit {
for i = 0; i < self.len; i = i + 1 {
f(i, self[i])
}
Expand All @@ -279,7 +279,7 @@ test "iteri" {
/// let v = Vec::from_array([3, 4, 5])
/// v.clear()
/// ```
pub fn clear[T](self : Vec[T]) {
pub fn clear[T](self : Vec[T]) -> Unit {
self.len = 0
}

Expand All @@ -297,7 +297,7 @@ test "clear" {
/// let v = Vec::from_array([3, 4, 5])
/// v.map(fn (x) {x + 1})
/// ```
pub fn map[T](self : Vec[T], f : (T) -> T) {
pub fn map[T](self : Vec[T], f : (T) -> T) -> Unit {
for i = 0; i < self.len; i = i + 1 {
self.buf[i] = f(self.buf[i])
}
Expand All @@ -318,7 +318,7 @@ test "map" {
/// let v = Vec::from_array([3, 4, 5])
/// v.mapi(fn (i, x) {x + i})
/// ```
pub fn mapi[T](self : Vec[T], f : (Int, T) -> T) {
pub fn mapi[T](self : Vec[T], f : (Int, T) -> T) -> Unit {
for i = 0; i < self.len; i = i + 1 {
self.buf[i] = f(i, self.buf[i])
}
Expand Down Expand Up @@ -357,7 +357,7 @@ test "is_empty" {
/// let v = Vec::[3, 4, 5]
/// v.reverse()
/// ```
pub fn reverse[T](self : Vec[T]) {
pub fn reverse[T](self : Vec[T]) -> Unit {
for i = 0; i < self.len / 2; i = i + 1 {
let temp = self.buf[i]
self.buf[i] = self.buf[self.len - i - 1]
Expand Down Expand Up @@ -518,7 +518,7 @@ test "search" {
/// let v = Vec::[3, 4, 5]
/// v.swap(1, 2)
/// ```
pub fn swap[T](self : Vec[T], i : Int, j : Int) {
pub fn swap[T](self : Vec[T], i : Int, j : Int) -> Unit {
if i >= self.len || j >= self.len {
abort("index out of bounds")
}
Expand Down Expand Up @@ -554,7 +554,7 @@ fn partition[T : Compare](self : Vec[T], left : Int, right : Int) -> Int {
}

/// Quicksort helper
fn quicksort[T : Compare](self : Vec[T], left : Int, right : Int) {
fn quicksort[T : Compare](self : Vec[T], left : Int, right : Int) -> Unit {
if left <= right {
let idx = self.partition(0, right)
self.quicksort(left, idx - 1)
Expand All @@ -571,7 +571,7 @@ fn quicksort[T : Compare](self : Vec[T], left : Int, right : Int) {
/// let v = Vec::[3, 4, 5, 1, 2]
/// v.sort()
/// ```
pub fn sort[T : Compare](self : Vec[T]) {
pub fn sort[T : Compare](self : Vec[T]) -> Unit {
self.quicksort(0, self.len - 1)
}

Expand Down Expand Up @@ -621,7 +621,7 @@ test "remove" {
/// let v = Vec::[3, 4, 5]
/// v.retain(fn(x) { x > 3 })
/// ```
pub fn retain[T](self : Vec[T], f : (T) -> Bool) {
pub fn retain[T](self : Vec[T], f : (T) -> Bool) -> Unit {
let mut i = 0
while i < self.len {
if f(self.buf[i]).not() {
Expand All @@ -646,7 +646,7 @@ test "retain" {
/// ```
/// Vec::[3, 4, 5].resize(1)
/// ```
pub fn resize[T](self : Vec[T], new_len : Int, f : T) {
pub fn resize[T](self : Vec[T], new_len : Int, f : T) -> Unit {
if new_len < 0 {
abort("negative new length")
}
Expand Down Expand Up @@ -683,7 +683,7 @@ test "resize" {
/// ```
/// Vec::[3, 4, 5].insert(1, 6)
/// ```
pub fn insert[T](self : Vec[T], index : Int, value : T) {
pub fn insert[T](self : Vec[T], index : Int, value : T) -> Unit {
if index > self.len {
abort("index out of bounds")
}
Expand Down Expand Up @@ -714,7 +714,7 @@ test "insert" {
/// ```
/// Vec::with_capacity(3).fill(3)
/// ```
pub fn fill[T](self : Vec[T], value : T) {
pub fn fill[T](self : Vec[T], value : T) -> Unit {
for i = 0; i < self.capacity(); i = i + 1 {
self.buf[i] = value
}
Expand Down

0 comments on commit fd9b6bf

Please sign in to comment.