Skip to content
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

style: format #27

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ test "iter" {
let mut i = 0
let mut failed = false
[1, 2, 3, 4, 5].iter(
fn(elem) {
if elem != i + 1 { failed = true }
i = i + 1
}
fn(elem) { if elem != i + 1 { failed = true }; i = i + 1 },
)
if failed { return Err("iter test failed") }
if failed {
return Err("iter test failed")
}
}

/// Iterates over the array with index.
Expand Down Expand Up @@ -74,11 +73,15 @@ test "iteri" {
let mut failed = false
[1, 2, 3, 4, 5].iteri(
fn(index, elem) {
if index != i || elem != i + 1 { failed = true }
if index != i || elem != i + 1 {
failed = true
}
i = i + 1
},
)
if failed { return Err("iteri test failed") }
if failed {
return Err("iteri test failed")
}
}

/// Applies a function to each element of the array and returns a new array with the results.
Expand Down Expand Up @@ -109,10 +112,7 @@ test "map" {
pub fn map_with_index[T, U](self : Array[T], f : (T, Int) -> U) -> Array[U] {
let res = Array::make(self.length(), f(self[0], 0))
loop 1 {
i => if i < self.length() {
res[i] = f(self[i], i)
continue i + 1
}
i => if i < self.length() { res[i] = f(self[i], i); continue i + 1 }
}
res
}
Expand Down Expand Up @@ -144,10 +144,7 @@ pub fn new[T](length : Int, value : () -> T) -> Array[T] {
} else {
let array = Array::make(length, value())
loop 1 {
i => if i < length {
array[i] = value()
continue i + 1
}
i => if i < length { array[i] = value(); continue i + 1 }
}
array
}
Expand All @@ -165,10 +162,7 @@ pub fn new_with_index[T](length : Int, value : (Int) -> T) -> Array[T] {
} else {
let array = Array::make(length, value(0))
loop 1 {
i => if i < length {
array[i] = value(i)
continue i + 1
}
i => if i < length { array[i] = value(i); continue i + 1 }
}
array
}
Expand All @@ -181,7 +175,7 @@ test "new index" {
}

/// Create a new array with given values.
pub fn Array::from_array[T](array: Array[T]) -> Array[T] {
pub fn Array::from_array[T](array : Array[T]) -> Array[T] {
array
}

Expand Down
10 changes: 5 additions & 5 deletions assertion/assertion.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn debug_string[T : Debug](t : T) -> String {
buf.to_string()
}

pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] {
if a == b {
Ok(())
} else {
Expand All @@ -31,7 +31,7 @@ pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
}
}

pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] {
if a != b {
Ok(())
} else {
Expand All @@ -41,15 +41,15 @@ pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
}
}

pub fn assert_false(x : Bool) -> Result[Unit,String] {
pub fn assert_false(x : Bool) -> Result[Unit, String] {
if x == false {
Ok(())
} else {
Err("assert_false failed")
}
}

pub fn assert_true(x : Bool) -> Result[Unit,String] {
pub fn assert_true(x : Bool) -> Result[Unit, String] {
if x {
Ok(())
} else {
Expand All @@ -67,5 +67,5 @@ test "assert_false.false" {

test "assert_eq.eq" {
assert_eq(1, 1)?
assert_eq("123","123")?
assert_eq("123", "123")?
}
28 changes: 16 additions & 12 deletions list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ test "iter" {
let ls = from_array([1, 2, 3, 4, 5])
let mut i = 0
let mut failed = false
ls.iter(fn(x) { i = i + 1; if x != i { failed = true }})
if failed { return Err("iter test failed") }
ls.iter(fn(x) { i = i + 1; if x != i { failed = true } })
if failed {
return Err("iter test failed")
}
}

/// Iterates over the list with index.
Expand All @@ -91,13 +93,10 @@ test "iteri" {
let mut v = 0
let mut failed = false
let ls = from_array([1, 2, 3, 4, 5])
ls.iteri(
fn(i, x) {
if (x != v + 1 || i != v) { failed = true }
v = v + 1
}
)
if failed { return Err("iteri test failed") }
ls.iteri(fn(i, x) { if x != v + 1 || i != v { failed = true }; v = v + 1 })
if failed {
return Err("iteri test failed")
}
}

/// Maps the list.
Expand Down Expand Up @@ -425,7 +424,10 @@ pub fn separate_by[T](self : List[T], separator : T) -> List[T] {

test "separate_by" {
let ls = from_array(["1", "|", "2", "|", "3", "|", "4", "|", "5"])
@assertion.assert_eq(from_array(["1", "2", "3", "4", "5"]).separate_by("|"), ls)?
@assertion.assert_eq(
from_array(["1", "2", "3", "4", "5"]).separate_by("|"),
ls,
)?
}

/// Check if the list is empty.
Expand Down Expand Up @@ -590,8 +592,10 @@ test "contain" {
/// // from_array([0, 1, 2])
/// unfold(0, fn { i => if i == 3 { None } else { Some(i, i + 1) } }) |> debug
/// ```
pub fn unfold[T, State](init : State, f : (State) -> Option[(T, State)]) ->
List[T] {
pub fn unfold[T, State](
init : State,
f : (State) -> Option[(T, State)]
) -> List[T] {
match f(init) {
Some(element, new_state) => Cons(element, unfold(new_state, f))
None => Nil
Expand Down
23 changes: 11 additions & 12 deletions option/option.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub fn unless[T](condition : Bool, value : () -> T) -> Option[T] {
when(condition.not(), value)
}


/// Creates an empty `Option` of type `T`.
pub fn empty[T]() -> Option[T] {
None
Expand Down Expand Up @@ -103,8 +102,8 @@ pub fn bind[T, U](self : Option[T], f : (T) -> Option[U]) -> Option[U] {
test "bind" {
let a = Option::Some(5)
let b : Option[Int] = None
@assertion.assert_eq(a.bind(fn(x){ Some(x * 2) }), Some(10))?
@assertion.assert_eq(b.bind(fn(x){ Some(x * 2) }), None)?
@assertion.assert_eq(a.bind(fn(x) { Some(x * 2) }), Some(10))?
@assertion.assert_eq(b.bind(fn(x) { Some(x * 2) }), None)?
}

/// Flattens an `Option` of `Option` into a single `Option`.
Expand All @@ -124,10 +123,10 @@ pub fn flatten[T](self : Option[Option[T]]) -> Option[T] {
}

test "flatten" {
let a : Option[Option[Int]] = Some(Some(42));
@assertion.assert_eq(flatten(a),Some(42))?
let a : Option[Option[Int]] = Some(Some(42))
@assertion.assert_eq(flatten(a), Some(42))?
let b : Option[Option[Int]] = Some(None)
@assertion.assert_eq(flatten(b),None)?
@assertion.assert_eq(flatten(b), None)?
}

/// Checks if the option is empty.
Expand All @@ -140,7 +139,7 @@ pub fn is_empty[T](self : Option[T]) -> Bool {

test "is_empty" {
let x = Option::Some(3)
let y: Option[Int] = None
let y : Option[Int] = None
@assertion.assert_false(x.is_empty())?
@assertion.assert_true(y.is_empty())?
}
Expand All @@ -166,8 +165,8 @@ pub fn filter[T](self : Option[T], f : (T) -> Bool) -> Option[T] {

test "filter" {
let x = Option::Some(3)
@assertion.assert_eq(x.filter(fn(x){ x > 5 }),None)?
@assertion.assert_eq(x.filter(fn(x){ x < 5 }),Some(3))?
@assertion.assert_eq(x.filter(fn(x) { x > 5 }), None)?
@assertion.assert_eq(x.filter(fn(x) { x < 5 }), Some(3))?
}

/// Return the contained `Some` value or the provided default.
Expand Down Expand Up @@ -196,6 +195,6 @@ pub fn or_else[T](self : Option[T], default : () -> T) -> T {

test "or else" {
let x = Option::Some(3)
@assertion.assert_eq(x.or_else(fn () { 5 }), 3)?
@assertion.assert_eq((None : Option[Int]).or_else(fn () { 5 }), 5)?
}
@assertion.assert_eq(x.or_else(fn() { 5 }), 3)?
@assertion.assert_eq((None : Option[Int]).or_else(fn() { 5 }), 5)?
}
14 changes: 5 additions & 9 deletions ref/ref.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@ pub fn ref[T](x : T) -> Ref[T] {
{ val: x }
}


/// Maps the value of a `Ref` using a given function.
///
/// # Example
///
/// ```
/// debug(ref(1).map(fn(a){ a + 1 })) //output: ref(2)
/// ```
pub fn map[T,R](self : Ref[T], f : (T) -> R) -> Ref[R] {
pub fn map[T, R](self : Ref[T], f : (T) -> R) -> Ref[R] {
{ val: f(self.val) }
}

test "map" {
let x = ref(1)
let y = map(x, fn(a){ a + 1 })
let y = map(x, fn(a) { a + 1 })
@assertion.assert_eq(y.val, 2)?
}


/// This function allows you to temporarily replace the value of a reference with a new value,
/// execute a given function, and then restore the original value of the reference.
///
Expand All @@ -63,7 +61,7 @@ test "map" {
/// })
/// debug(x) //output: ref(1)
/// ```
pub fn protect[T,R](self : Ref[T], a : T, f : () -> R) -> R {
pub fn protect[T, R](self : Ref[T], a : T, f : () -> R) -> R {
let old = self.val
self.val = a
let r = f()
Expand All @@ -74,9 +72,7 @@ pub fn protect[T,R](self : Ref[T], a : T, f : () -> R) -> R {
test "protect" {
let x = ref(1)
@assertion.assert_eq(x.val, 1)?
protect(x, 2, fn(){
@assertion.assert_eq(x.val, 2)
})?
protect(x, 2, fn() { @assertion.assert_eq(x.val, 2) })?
@assertion.assert_eq(x.val, 1)?
}

Expand Down Expand Up @@ -107,4 +103,4 @@ test "swap" {

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