Skip to content

Commit

Permalink
rename forall/exists to all/any in sorted_set (moonbitlang#580)
Browse files Browse the repository at this point in the history
* rename forall/exists to all/any in sorted_set

* update interface file

* format code
  • Loading branch information
Yoorkin authored Jun 21, 2024
1 parent a5ee3a9 commit 8b397d4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
8 changes: 4 additions & 4 deletions immut/sorted_set/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ ImmutableSet::[1, 2, 3, 4, 5].fold(0, fn(acc, x) { acc + x }) // 15
ImmutableSet::[1, 2, 3].map(fn(x){ x * 2}) // ImmutableSet::[2, 4, 6]
```

### Forall & Exists
### All & Any

`forall` and `exists` can detect whether all elements in the set match or if there are elements that match.
`all` and `any` can detect whether all elements in the set match or if there are elements that match.

```moonbit
ImmutableSet::[2, 4, 6].forall(fn(v) { v % 2 == 0}) // true
ImmutableSet::[1, 4, 3].exists(fn(v) { v % 2 == 0}) // true
ImmutableSet::[2, 4, 6].all(fn(v) { v % 2 == 0}) // true
ImmutableSet::[1, 4, 3].any(fn(v) { v % 2 == 0}) // true
```

### Stringify
Expand Down
14 changes: 6 additions & 8 deletions immut/sorted_set/immutable_set.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,13 @@ pub fn map[T : Compare, U : Compare](
/// # Example
///
/// ```
/// println(ImmutableSet::[2, 4, 6].forall(fn(v) { v % 2 == 0}))
/// println(ImmutableSet::[2, 4, 6].all(fn(v) { v % 2 == 0}))
/// // output: true
/// ```
pub fn forall[T : Compare](self : ImmutableSet[T], f : (T) -> Bool) -> Bool {
pub fn all[T : Compare](self : ImmutableSet[T], f : (T) -> Bool) -> Bool {
match self {
Empty => true
Node(~left, ~value, ~right, ..) =>
f(value) && left.forall(f) && right.forall(f)
Node(~left, ~value, ~right, ..) => f(value) && left.all(f) && right.all(f)
}
}

Expand All @@ -485,14 +484,13 @@ pub fn forall[T : Compare](self : ImmutableSet[T], f : (T) -> Bool) -> Bool {
/// # Example
///
/// ```
/// println(ImmutableSet::[1, 4, 3].exists(fn(v) { v % 2 == 0}))
/// println(ImmutableSet::[1, 4, 3].any(fn(v) { v % 2 == 0}))
/// // output: true
/// ```
pub fn exists[T : Compare](self : ImmutableSet[T], f : (T) -> Bool) -> Bool {
pub fn any[T : Compare](self : ImmutableSet[T], f : (T) -> Bool) -> Bool {
match self {
Empty => false
Node(~left, ~value, ~right, ..) =>
f(value) || left.exists(f) || right.exists(f)
Node(~left, ~value, ~right, ..) => f(value) || left.any(f) || right.any(f)
}
}

Expand Down
12 changes: 6 additions & 6 deletions immut/sorted_set/immutable_set_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ test "map" {
)?
}

test "forall" {
inspect(ImmutableSet::[2, 4, 6].forall(fn(v) { v % 2 == 0 }), content="true")?
inspect(ImmutableSet::[1, 3, 5].forall(fn(v) { v % 2 == 0 }), content="false")?
test "all" {
inspect(ImmutableSet::[2, 4, 6].all(fn(v) { v % 2 == 0 }), content="true")?
inspect(ImmutableSet::[1, 3, 5].all(fn(v) { v % 2 == 0 }), content="false")?
}

test "exists" {
inspect(ImmutableSet::[1, 4, 3].exists(fn(v) { v % 2 == 0 }), content="true")?
inspect(ImmutableSet::[1, 5, 3].exists(fn(v) { v % 2 == 0 }), content="false")?
test "any" {
inspect(ImmutableSet::[1, 4, 3].any(fn(v) { v % 2 == 0 }), content="true")?
inspect(ImmutableSet::[1, 5, 3].any(fn(v) { v % 2 == 0 }), content="false")?
}

test "fold" {
Expand Down
4 changes: 2 additions & 2 deletions immut/sorted_set/sorted_set.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ package moonbitlang/core/immut/sorted_set
type ImmutableSet
impl ImmutableSet {
add[T : Compare + Eq](Self[T], T) -> Self[T]
all[T : Compare + Eq](Self[T], (T) -> Bool) -> Bool
any[T : Compare + Eq](Self[T], (T) -> Bool) -> Bool
as_iter[T](Self[T]) -> Iter[T]
contains[T : Compare + Eq](Self[T], T) -> Bool
debug_write[T : Debug](Self[T], Buffer) -> Unit
default[T : Default]() -> Self[T]
diff[T : Compare + Eq](Self[T], Self[T]) -> Self[T]
disjoint[T : Compare + Eq](Self[T], Self[T]) -> Bool
exists[T : Compare + Eq](Self[T], (T) -> Bool) -> Bool
filter[T : Compare + Eq](Self[T], (T) -> Bool) -> Self[T]
fold[T : Compare + Eq, U](Self[T], (U, T) -> U, ~init : U) -> U
forall[T : Compare + Eq](Self[T], (T) -> Bool) -> Bool
from_array[T : Compare + Eq](Array[T]) -> Self[T]
inter[T : Compare + Eq](Self[T], Self[T]) -> Self[T]
is_empty[T : Compare + Eq](Self[T]) -> Bool
Expand Down

0 comments on commit 8b397d4

Please sign in to comment.