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

Add @math.maximum() and @math.minimum() #35

Merged
merged 1 commit into from
Mar 19, 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
93 changes: 93 additions & 0 deletions math/algebraic.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Compares and returns the maximum of two values.
///
/// Returns the second argument if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// print(@math.maximum(1, 2)) // output: 2
/// print(@math.maximum(2, 2)) // output: 2
/// ```
Comment on lines +15 to +24
Copy link
Contributor Author

@fantix fantix Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About conventions of doc strings:

  1. I used "Compares" instead of "Compare", while both forms can be found in the code base. I believe it implies "(this function) compares and returns ...".
  2. I used "Examples" instead of "Example" because I do have multiple examples. I think we should stick with "Examples" (like Rust) across the repo even if it's just a single example.
  3. I used @math.maximum instead of just maximum so that users could directly copy it from the documentation and run without errors.
  4. I'm not sure we like the print(...) // output: ... style in doc strings, it's incompatible with potential future doc tests. Though, @assertion.assert_eq(@math.maximum(1, 2), 2) is just too long to read, so I ended up following what the rest of this repo has been doing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions, about the last one, we will consider generate example based docs directly from test "example"

pub fn maximum[T : Compare](x : T, y : T) -> T {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full name maximum follows the convention in list/list.mbt:

pub fn maximum[T : Compare](self : List[T]) -> T {

However, I personally prefer max and min, thoughts?

if x > y {
x
} else {
y
}
}

test "maximum.value" {
@assertion.assert_eq(maximum(1, 2), 2)?
@assertion.assert_eq(maximum(2, 1), 2)?
@assertion.assert_eq(maximum(2, 2), 2)?
}

test "maximum.ref" {
let v1 = 1
let v2 = 2
let x1 = "v\(v1)"
let x2 = "v\(v2)"

// We need another value that equals to x2 by value but not reference
let x2t = "v\(v2)"
@assertion.assert_is_not(x2, x2t).unwrap()

@assertion.assert_is(maximum(x1, x2), x2)?
@assertion.assert_is(maximum(x2, x1), x2)?
@assertion.assert_is(maximum(x2, x2t), x2t)?
@assertion.assert_is(maximum(x2t, x2), x2)?
}

/// Compares and returns the minimum of two values.
///
/// Returns the first argument if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// print(@math.minimum(1, 2)) // output: 1
/// print(@math.minimum(2, 2)) // output: 2
/// ```
pub fn minimum[T : Compare](x : T, y : T) -> T {
if x > y {
y
} else {
x
}
}

test "minimum.value" {
@assertion.assert_eq(minimum(1, 2), 1)?
@assertion.assert_eq(minimum(2, 1), 1)?
@assertion.assert_eq(minimum(2, 2), 2)?
}

test "minimum.ref" {
let v1 = 1
let v2 = 2
let x1 = "v\(v1)"
let x2 = "v\(v2)"

// We need another value that equals to x2 by value but not reference
let x2t = "v\(v2)"
@assertion.assert_is_not(x2, x2t).unwrap()

@assertion.assert_is(minimum(x1, x2), x1)?
@assertion.assert_is(minimum(x2, x1), x1)?
@assertion.assert_is(minimum(x2, x2t), x2)?
@assertion.assert_is(minimum(x2t, x2), x2t)?
}
5 changes: 5 additions & 0 deletions math/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"import": [
{ "path": "moonbitlang/core/assertion", "alias": "assertion" }
]
}