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

Conversation

fantix
Copy link
Contributor

@fantix fantix commented Mar 9, 2024

Ported from muts.

/// print(@math.maximum(1, 2)) // output: 2
/// print(@math.maximum(2, 2)) // output: 2
/// ```
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?

Comment on lines 83 to 87
let v1 = 1
let v2 = 2
let x1 = "v\(v1)"
let x2 = "v\(v2)"

// We need another value that `== x2` but not `=== x2`
let x2t = "v\(v2)"
@assertion.assert_is_not(x2, x2t).unwrap()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we have a way to reuse code for testing, without introducing footprints in release builds?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, we are working on that.

Comment on lines +18 to +24
/// 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
/// ```
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"

@bobzhang bobzhang merged commit 2303438 into moonbitlang:main Mar 19, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants