Re: [elixir-core:9638] [Proposal] Add a General Number Module

39 views
Skip to first unread message

José Valim

unread,
Jul 25, 2020, 4:12:04 AM7/25/20
to elixir-l...@googlegroups.com
Hi Julian,

Thanks for the proposal! While I believe this functionality can be helpful, I would be skeptical about adding a new module to the standard library where only a single function has been planned. I personally wouldn't add is_number/2, as one can simply match on the result of parse/2 instead.

However, if there is a desire to improve Integer.parse/2 and Float.parse/2 with new options, I believe those will be welcome.

Thank you!

On Sat, Jul 25, 2020 at 5:10 AM Julian Gomez <juln...@gmail.com> wrote:
Hello Everybody,

I picked up Elixir this May and It has become one of my favorite programming languages. I played with it back in 2014 and I regret dropping it.

This is my proposal for a new general Number Module. Provides a clean abstraction.

* SEE THE ATTACHED FILES FOR EXAMPLES AND MODULE PROTOTYPE*

To my knowledge, If we want to parse a string to a number we currently have four options:
  • String.to_float/1
  • String.to_integer/1
  • Float.parse/1
  • Integer.parse/1 or Integer.parse/2
These are great if you already know that the string contains a valid number. 

At the moment, The Number module only contains the following functions:
  • parse/1 - automatic number parsing to int or float
  • parse/2 - explicit number parsing to int or float
  • parse/3 - explicit number parsing to int or float but has an extra step.
  • is_number/1 - does the string contain a valid number or start with a valid number. Compared to Kernel.is_number/1 where the value has to be in its proper form already
parse/3 accepts three arguments:
  • binary - the string you want to convert to a number, implicit parsing.
  • data type (atom) - :integer or :float, explicit parsing.
  • options - can be a function in the Float and Integer module or pass in the `base` for the Integer.parse/2 function.

My proposal is a one-liner and since it's built on top of the Integer and Float modules
we can utilize their functions in conjunction with parsing the string to a number.

Example:

Parsing user input:
{ bill_amount, _ } = IO.gets("What is the bill amount? ") |> Number.parse()

{ tip_rate, _ ) = IO.gets("What is the tip rate? ") |> Number.parse()

Number.parse( "45.565", :float, ceil: 2 ) would result in {45.57, ""}
Number.parse( "45.565%", :float, ceil: 2 ) would result in {45.57, "%"}

Valid strings return {parsed_number, rest} and invalid strings return :error in tradition to the Integer and Float modules.

* SEE THE ATTACHED FILES FOR MORE EXAMPLES AND MODULE PROTOTYPE*

Julian Gomez

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/444b3079-20a8-4d81-8baf-918b9ba937ccn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages