Skip to content

Commit ad0d7c8

Browse files
Lampeselijunchen
authored andcommitted
style: format
1 parent f7b6500 commit ad0d7c8

File tree

5 files changed

+51
-58
lines changed

5 files changed

+51
-58
lines changed

array/array.mbt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ test "iter" {
3939
let mut i = 0
4040
let mut failed = false
4141
[1, 2, 3, 4, 5].iter(
42-
fn(elem) {
43-
if elem != i + 1 { failed = true }
44-
i = i + 1
45-
}
42+
fn(elem) { if elem != i + 1 { failed = true }; i = i + 1 },
4643
)
47-
if failed { return Err("iter test failed") }
44+
if failed {
45+
return Err("iter test failed")
46+
}
4847
}
4948
5049
/// Iterates over the array with index.
@@ -74,11 +73,15 @@ test "iteri" {
7473
let mut failed = false
7574
[1, 2, 3, 4, 5].iteri(
7675
fn(index, elem) {
77-
if index != i || elem != i + 1 { failed = true }
76+
if index != i || elem != i + 1 {
77+
failed = true
78+
}
7879
i = i + 1
7980
},
8081
)
81-
if failed { return Err("iteri test failed") }
82+
if failed {
83+
return Err("iteri test failed")
84+
}
8285
}
8386
8487
/// Applies a function to each element of the array and returns a new array with the results.
@@ -109,10 +112,7 @@ test "map" {
109112
pub fn map_with_index[T, U](self : Array[T], f : (T, Int) -> U) -> Array[U] {
110113
let res = Array::make(self.length(), f(self[0], 0))
111114
loop 1 {
112-
i => if i < self.length() {
113-
res[i] = f(self[i], i)
114-
continue i + 1
115-
}
115+
i => if i < self.length() { res[i] = f(self[i], i); continue i + 1 }
116116
}
117117
res
118118
}
@@ -144,10 +144,7 @@ pub fn new[T](length : Int, value : () -> T) -> Array[T] {
144144
} else {
145145
let array = Array::make(length, value())
146146
loop 1 {
147-
i => if i < length {
148-
array[i] = value()
149-
continue i + 1
150-
}
147+
i => if i < length { array[i] = value(); continue i + 1 }
151148
}
152149
array
153150
}
@@ -165,10 +162,7 @@ pub fn new_with_index[T](length : Int, value : (Int) -> T) -> Array[T] {
165162
} else {
166163
let array = Array::make(length, value(0))
167164
loop 1 {
168-
i => if i < length {
169-
array[i] = value(i)
170-
continue i + 1
171-
}
165+
i => if i < length { array[i] = value(i); continue i + 1 }
172166
}
173167
array
174168
}
@@ -181,7 +175,7 @@ test "new index" {
181175
}
182176
183177
/// Create a new array with given values.
184-
pub fn Array::from_array[T](array: Array[T]) -> Array[T] {
178+
pub fn Array::from_array[T](array : Array[T]) -> Array[T] {
185179
array
186180
}
187181

assertion/assertion.mbt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn debug_string[T : Debug](t : T) -> String {
2121
buf.to_string()
2222
}
2323

24-
pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
24+
pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] {
2525
if a == b {
2626
Ok(())
2727
} else {
@@ -31,7 +31,7 @@ pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
3131
}
3232
}
3333

34-
pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
34+
pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] {
3535
if a != b {
3636
Ok(())
3737
} else {
@@ -41,15 +41,15 @@ pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
4141
}
4242
}
4343

44-
pub fn assert_false(x : Bool) -> Result[Unit,String] {
44+
pub fn assert_false(x : Bool) -> Result[Unit, String] {
4545
if x == false {
4646
Ok(())
4747
} else {
4848
Err("assert_false failed")
4949
}
5050
}
5151

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

6868
test "assert_eq.eq" {
6969
assert_eq(1, 1)?
70-
assert_eq("123","123")?
70+
assert_eq("123", "123")?
7171
}

list/list.mbt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ test "iter" {
7070
let ls = from_array([1, 2, 3, 4, 5])
7171
let mut i = 0
7272
let mut failed = false
73-
ls.iter(fn(x) { i = i + 1; if x != i { failed = true }})
74-
if failed { return Err("iter test failed") }
73+
ls.iter(fn(x) { i = i + 1; if x != i { failed = true } })
74+
if failed {
75+
return Err("iter test failed")
76+
}
7577
}
7678
7779
/// Iterates over the list with index.
@@ -91,13 +93,10 @@ test "iteri" {
9193
let mut v = 0
9294
let mut failed = false
9395
let ls = from_array([1, 2, 3, 4, 5])
94-
ls.iteri(
95-
fn(i, x) {
96-
if (x != v + 1 || i != v) { failed = true }
97-
v = v + 1
98-
}
99-
)
100-
if failed { return Err("iteri test failed") }
96+
ls.iteri(fn(i, x) { if x != v + 1 || i != v { failed = true }; v = v + 1 })
97+
if failed {
98+
return Err("iteri test failed")
99+
}
101100
}
102101
103102
/// Maps the list.
@@ -425,7 +424,10 @@ pub fn separate_by[T](self : List[T], separator : T) -> List[T] {
425424
426425
test "separate_by" {
427426
let ls = from_array(["1", "|", "2", "|", "3", "|", "4", "|", "5"])
428-
@assertion.assert_eq(from_array(["1", "2", "3", "4", "5"]).separate_by("|"), ls)?
427+
@assertion.assert_eq(
428+
from_array(["1", "2", "3", "4", "5"]).separate_by("|"),
429+
ls,
430+
)?
429431
}
430432
431433
/// Check if the list is empty.
@@ -590,8 +592,10 @@ test "contain" {
590592
/// // from_array([0, 1, 2])
591593
/// unfold(0, fn { i => if i == 3 { None } else { Some(i, i + 1) } }) |> debug
592594
/// ```
593-
pub fn unfold[T, State](init : State, f : (State) -> Option[(T, State)]) ->
594-
List[T] {
595+
pub fn unfold[T, State](
596+
init : State,
597+
f : (State) -> Option[(T, State)]
598+
) -> List[T] {
595599
match f(init) {
596600
Some(element, new_state) => Cons(element, unfold(new_state, f))
597601
None => Nil

option/option.mbt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub fn unless[T](condition : Bool, value : () -> T) -> Option[T] {
5757
when(condition.not(), value)
5858
}
5959
60-
6160
/// Creates an empty `Option` of type `T`.
6261
pub fn empty[T]() -> Option[T] {
6362
None
@@ -103,8 +102,8 @@ pub fn bind[T, U](self : Option[T], f : (T) -> Option[U]) -> Option[U] {
103102
test "bind" {
104103
let a = Option::Some(5)
105104
let b : Option[Int] = None
106-
@assertion.assert_eq(a.bind(fn(x){ Some(x * 2) }), Some(10))?
107-
@assertion.assert_eq(b.bind(fn(x){ Some(x * 2) }), None)?
105+
@assertion.assert_eq(a.bind(fn(x) { Some(x * 2) }), Some(10))?
106+
@assertion.assert_eq(b.bind(fn(x) { Some(x * 2) }), None)?
108107
}
109108
110109
/// Flattens an `Option` of `Option` into a single `Option`.
@@ -124,10 +123,10 @@ pub fn flatten[T](self : Option[Option[T]]) -> Option[T] {
124123
}
125124
126125
test "flatten" {
127-
let a : Option[Option[Int]] = Some(Some(42));
128-
@assertion.assert_eq(flatten(a),Some(42))?
126+
let a : Option[Option[Int]] = Some(Some(42))
127+
@assertion.assert_eq(flatten(a), Some(42))?
129128
let b : Option[Option[Int]] = Some(None)
130-
@assertion.assert_eq(flatten(b),None)?
129+
@assertion.assert_eq(flatten(b), None)?
131130
}
132131
133132
/// Checks if the option is empty.
@@ -140,7 +139,7 @@ pub fn is_empty[T](self : Option[T]) -> Bool {
140139
141140
test "is_empty" {
142141
let x = Option::Some(3)
143-
let y: Option[Int] = None
142+
let y : Option[Int] = None
144143
@assertion.assert_false(x.is_empty())?
145144
@assertion.assert_true(y.is_empty())?
146145
}
@@ -166,8 +165,8 @@ pub fn filter[T](self : Option[T], f : (T) -> Bool) -> Option[T] {
166165
167166
test "filter" {
168167
let x = Option::Some(3)
169-
@assertion.assert_eq(x.filter(fn(x){ x > 5 }),None)?
170-
@assertion.assert_eq(x.filter(fn(x){ x < 5 }),Some(3))?
168+
@assertion.assert_eq(x.filter(fn(x) { x > 5 }), None)?
169+
@assertion.assert_eq(x.filter(fn(x) { x < 5 }), Some(3))?
171170
}
172171
173172
/// Return the contained `Some` value or the provided default.
@@ -196,6 +195,6 @@ pub fn or_else[T](self : Option[T], default : () -> T) -> T {
196195
197196
test "or else" {
198197
let x = Option::Some(3)
199-
@assertion.assert_eq(x.or_else(fn () { 5 }), 3)?
200-
@assertion.assert_eq((None : Option[Int]).or_else(fn () { 5 }), 5)?
201-
}
198+
@assertion.assert_eq(x.or_else(fn() { 5 }), 3)?
199+
@assertion.assert_eq((None : Option[Int]).or_else(fn() { 5 }), 5)?
200+
}

ref/ref.mbt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,23 @@ pub fn ref[T](x : T) -> Ref[T] {
2020
{ val: x }
2121
}
2222

23-
2423
/// Maps the value of a `Ref` using a given function.
2524
///
2625
/// # Example
2726
///
2827
/// ```
2928
/// debug(ref(1).map(fn(a){ a + 1 })) //output: ref(2)
3029
/// ```
31-
pub fn map[T,R](self : Ref[T], f : (T) -> R) -> Ref[R] {
30+
pub fn map[T, R](self : Ref[T], f : (T) -> R) -> Ref[R] {
3231
{ val: f(self.val) }
3332
}
3433
3534
test "map" {
3635
let x = ref(1)
37-
let y = map(x, fn(a){ a + 1 })
36+
let y = map(x, fn(a) { a + 1 })
3837
@assertion.assert_eq(y.val, 2)?
3938
}
4039
41-
4240
/// This function allows you to temporarily replace the value of a reference with a new value,
4341
/// execute a given function, and then restore the original value of the reference.
4442
///
@@ -63,7 +61,7 @@ test "map" {
6361
/// })
6462
/// debug(x) //output: ref(1)
6563
/// ```
66-
pub fn protect[T,R](self : Ref[T], a : T, f : () -> R) -> R {
64+
pub fn protect[T, R](self : Ref[T], a : T, f : () -> R) -> R {
6765
let old = self.val
6866
self.val = a
6967
let r = f()
@@ -74,9 +72,7 @@ pub fn protect[T,R](self : Ref[T], a : T, f : () -> R) -> R {
7472
test "protect" {
7573
let x = ref(1)
7674
@assertion.assert_eq(x.val, 1)?
77-
protect(x, 2, fn(){
78-
@assertion.assert_eq(x.val, 2)
79-
})?
75+
protect(x, 2, fn() { @assertion.assert_eq(x.val, 2) })?
8076
@assertion.assert_eq(x.val, 1)?
8177
}
8278
@@ -107,4 +103,4 @@ test "swap" {
107103
108104
pub fn update[T](self : Ref[T], f : (T) -> T) {
109105
self.val = f(self.val)
110-
}
106+
}

0 commit comments

Comments
 (0)