modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.17k stars 2.59k forks source link

[Feature Request] polymorphic string functions, like `isdigit()` #2975

Open dimitrilw opened 4 months ago

dimitrilw commented 4 months ago

Review Mojo's priorities

What is your request?

Right now, isdigit() (found here) expects to receive a numeric value. I.e., the two noted lines fail:

image

But if I pass ord(<value>), then it works:

image

In Python, the str.isdigit() method is called from the string itself. More, it does not require advance alteration of the input.

image

Perhaps Mojo's isdigit() function should be polymorphic to take different inputs. For example, in Python, it can assess a full string of characters to confirm all are digits:

image

I'm thinking that the Mojo function should perform similarly. Not necessarily as a String.method(), but rather just something that can take different inputs, to include something like isdigit("1x3"), which would evaluate as False.

If Modular peeps agree, then I will gladly work to make this real and make a PR.

Note, the same could be said about other functions: islower, isupper, etc.

Thoughts?

What is your motivation for this change?

Pythonic style to simplify onboarding Py-Peeps. Also, easier-to-read code; I believe isdigit("x") is more intuitive than isdigit(ord("x")).

Any other details?

No response

dimitrilw commented 4 months ago

I have a fork that adds:

It follows the same pattern as bgreni's isdigit additions.

I also added tests for each of those additions, but want to ensure I pass all tests locally before submitting the PR. I could use a little hand-holding on "here's how you recompile Mojo locally from your local fork and ensure your system uses that mojo binary to run the test suite."

dimitrilw commented 4 months ago

PR to close this ticket when merged: https://github.com/modularml/mojo/pull/3095

soraros commented 4 months ago

isdigit(Int8) is actually isdigit(Char). Mojo used to use signed 8bit integer for char.

dimitrilw commented 4 months ago

Not sure I follow.

The function signature at the time I originally wrote this was only one: fn isdigit(c: UInt8) -> Bool:. Then bgreni added fn isdigit(c: String) -> Bool:, which is a pass-through to the original function.

soraros commented 4 months ago

I was merely pointing out that one should think of the "numeric" overload as a function on char.

dimitrilw commented 4 months ago

Tracking. ❤️

dimitrilw commented 4 months ago

For ref, python 3 str.islower() and str.isupper():

image