Skip to content

Commit

Permalink
rename forall/exists to all/any in sorted_set
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin committed Jun 20, 2024
1 parent eede6a6 commit 23bdd97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 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
12 changes: 6 additions & 6 deletions immut/sorted_set/immutable_set.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,14 @@ 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)
f(value) && left.all(f) && right.all(f)
}
}

Expand All @@ -485,14 +485,14 @@ 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)
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

0 comments on commit 23bdd97

Please sign in to comment.