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

Adding Integer.Parse #3283

Merged
merged 8 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- [Implemented `Vector.flatten`][3259]
- [Significant performance improvement in `Natural_Order` and new `Faker`
methods added to `Standard.Test`][3276]
- [Implemented `Integer.parse`][3283]

[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
Expand All @@ -68,6 +69,7 @@
[3259]: https://github.com/enso-org/enso/pull/3259
[3273]: https://github.com/enso-org/enso/pull/3273
[3276]: https://github.com/enso-org/enso/pull/3276
[3283]: https://github.com/enso-org/enso/pull/3283

#### Enso Compiler

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from Standard.Base import all hiding Parse_Error

polyglot java import java.lang.Long
polyglot java import java.lang.Double
polyglot java import java.lang.Math
polyglot java import java.lang.String
Expand Down Expand Up @@ -252,6 +253,24 @@ Number.max that = if this > that then this else that
Number.to_json : Json.Number
Number.to_json = Json.Number this

## ALIAS From Text

Parses a textual representation of an integer into an integer number, returning
a `Parse_Error` if the text does not represent a valid integer.

Arguments:
- text: The text to parse into a integer.
- radix: The number base to use for parsing (defaults to 10).

> Example
Parse the text "20220216" into an integer number.

Integer.parse "20220216"
Integer.parse : Text -> Text -> Integer ! Parse_Error
Integer.parse text (radix=10) =
Panic.recover (Long.parseLong text radix) . catch _->
Error.throw (Parse_Error text)

## ALIAS From Text

Parses a textual representation of a decimal into a decimal number, returning
Expand All @@ -263,7 +282,7 @@ Number.to_json = Json.Number this
> Example
Parse the text "7.6" into a decimal number.

Decimal.parse 7.6
Decimal.parse "7.6"
Decimal.parse : Text -> Decimal ! Parse_Error
Decimal.parse text =
Panic.recover (Double.parseDouble text) . catch _->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ compare text1 text2 =

pair = loop text next iter
substring = Text_Utils.substring text prev pair.first

## TODO [RW] Currently there is no `Integer.parse` method, so we
parse a decimal and convert it to an integer. Once
https://www.pivotaltracker.com/story/show/181176522 is
implemented, this should be changed to use `Integer.parse`.
decimal = Decimal.parse substring . floor

decimal = Integer.parse substring

next_index = if pair.second then -1 else iter.current
[substring, decimal, pair.first, next_index]
Expand Down
2 changes: 1 addition & 1 deletion test/Benchmarks/src/Natural_Order_Sort.enso
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from Standard.Base import all

import Standard.Test.Bench

import Standard.Test.Faker

import Standard.Base.Data.Ordering.Natural_Order

## Bench Utilities ============================================================
Expand Down
41 changes: 41 additions & 0 deletions test/Benchmarks/src/Number_Parse.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from Standard.Base import all

import Standard.Test.Bench
import Standard.Test.Faker

## Bench Utilities ============================================================

make_double_strings : Integer -> Any -> Integer -> Integer -> Vector
make_double_strings count generator (min = -1000000000) (max = 1000000000) =
range = max - min
output = Array.new count
0.up_to count . each i->
v = generator.nextDouble * range - min
output.set_at i v.to_text
Vector.Vector output

make_integer_strings : Integer -> Any -> Integer -> Integer -> Vector
make_integer_strings count generator (min = -1000000000) (max = 1000000000) =
range = max - min
output = Array.new count
0.up_to count . each i->
v = (generator.nextInt range - min)
output.set_at i v.to_text
Vector.Vector output

vector_size = 1000000
iter_size = 100
num_iterations = 10

# The Benchmarks ==============================================================

main =
## No specific significance to this constant, just fixed to make generated set deterministic
fixed_random_seed = 1644575867
random_generator = Faker.make_generator fixed_random_seed

double_string = here.make_double_strings here.vector_size random_generator
Bench.measure (double_string.map Decimal.parse) "Decimal.parse" here.iter_size here.num_iterations

int_strings = here.make_integer_strings here.vector_size random_generator
Bench.measure (int_strings.map Integer.parse) "Integer.parse" here.iter_size here.num_iterations
5 changes: 5 additions & 0 deletions test/Tests/src/Data/Numbers_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ spec =
(negative_big_bits.bit_shift_r negative_big_bits).should_fail_with Arithmetic_Error
negative_big_bits.bit_shift_r positive_big_bits . should_equal -1

Test.specify "should be able to be parsed" <|
Integer.parse "1245623" . should_equal 1245623
Integer.parse "123.45" . should_fail_with Parse_Error
Integer.parse "aaaa" . should_fail_with Parse_Error
jdunkerley marked this conversation as resolved.
Show resolved Hide resolved

Test.group "Decimals" <|

Test.specify "should exist and expose basic arithmetic operations" <|
Expand Down