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

First few changes from the Churn workflow #8782

Merged
merged 6 commits into from
Jan 17, 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
13 changes: 5 additions & 8 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Range.enso
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Range
- end: The right boundary of the range. Its value is excluded.
- step: The step between values. Must be a positive value.
new : Integer -> Integer -> Integer -> Range
new start=0 end=100 step=1 =
new start:Integer=0 end:Integer=100 step:Integer=1 =
Range.Between start end (if start > end then -1 else 1) . with_step step

## PRIVATE
Expand All @@ -50,13 +50,10 @@ type Range

10.down_to 0 . with_step 2 . to_vector == [10, 8, 6, 4, 2]
with_step : Integer -> Range
with_step self new_step = case new_step of
_ : Integer ->
if new_step == 0 then throw_zero_step_error else
if new_step < 0 then Error.throw (Illegal_Argument.Error "The step should be positive. A decreasing sequence will remain decreasing after updating it with positive step, as this operation only sets the magnitude without changing the sign.") else
Range.Between self.start self.end self.step.signum*new_step
_ ->
Error.throw (Illegal_Argument.Error "Range step should be an integer.")
with_step self new_step:Integer =
if new_step == 0 then throw_zero_step_error else
if new_step < 0 then Error.throw (Illegal_Argument.Error "The step should be positive. A decreasing sequence will remain decreasing after updating it with positive step, as this operation only sets the magnitude without changing the sign.") else
Range.Between self.start self.end self.step.signum*new_step

## GROUP Selections
Returns the first element that is included within the range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ from project.Data.Boolean import Boolean, False, True

Arguments:
- n: The end of the range.
- include_end: Specifies if the right end of the range should be included. By
default, the range is right-exclusive.
- include_end: Specifies if the right end of the range should be included.
By default, the range is right-exclusive.
- step: The step between each element in the range. By default, the step is
1.

> Example
Create a range containing the numbers 0, 1, 2, 3, 4.
Expand All @@ -23,12 +25,11 @@ from project.Data.Boolean import Boolean, False, True
Create a range containing elements 1, 2, 3.

1.up_to 3 include_end=True
Integer.up_to : Integer -> Boolean -> Range
Integer.up_to self n include_end=False = case n of
_ : Integer ->
effective_end = if include_end then n+1 else n
Range.Between self effective_end 1
_ -> Error.throw (Illegal_Argument.Error "Expected range end to be an Integer.")
Integer.up_to : Integer -> Boolean -> Integer -> Range
Integer.up_to self n:Integer include_end:Boolean=False step:Integer=1 =
effective_end = if include_end then n+1 else n
unit_range = Range.Between self effective_end 1
if step == 1 then unit_range else unit_range.with_step step

## ALIAS range
GROUP Input
Expand All @@ -37,8 +38,10 @@ Integer.up_to self n include_end=False = case n of

Arguments:
- n: The end of the range.
- include_end: Specifies if the right end of the range should be included. By
default, the range is right-exclusive.
- include_end: Specifies if the right end of the range should be included.
By default, the range is right-exclusive.
- step: The step between each element in the range. By default, the step is
1. Step should be a positive integer and will be subtracted between each.

> Example
Create a range containing the numbers 5, 4, 3, 2, 1.
Expand All @@ -49,9 +52,8 @@ Integer.up_to self n include_end=False = case n of
Create a range containing elements 3, 2, 1.

3.down_to 1 include_end=True
Integer.down_to : Integer -> Boolean -> Range
Integer.down_to self n include_end=False = case n of
_ : Integer ->
effective_end = if include_end then n-1 else n
Range.Between self effective_end -1
_ -> Error.throw (Illegal_Argument.Error "Expected range end to be an Integer.")
Integer.down_to : Integer -> Boolean -> Integer -> Range
Integer.down_to self n:Integer include_end:Boolean=False step:Integer=1 =
effective_end = if include_end then n-1 else n
unit_range = Range.Between self effective_end -1
if step == 1 then unit_range else unit_range.with_step step
31 changes: 17 additions & 14 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ new_builtin year month day = @Builtin_Method "Date.new_builtin"
type Date
## ALIAS current date, now
GROUP DateTime
ICON calendar

Obtains the current date from the system clock in the system timezone.

Expand All @@ -71,6 +72,7 @@ type Date
today = @Builtin_Method "Date.today"

## GROUP DateTime
ICON calendar
Constructs a new Date from a year, month, and day.

Arguments
Expand Down Expand Up @@ -100,6 +102,7 @@ type Date

## ALIAS date from text
GROUP Conversions
ICON calendar

Converts text containing a date into a Date object.

Expand Down Expand Up @@ -502,8 +505,10 @@ type Date

Arguments:
- end: The end of the range.
- include_end: Specifies if the right end of the range should be included. By
default, the range is right-exclusive.
- include_end: Specifies if the right end of the range should be
included. By default, the range is right-exclusive.
- step: The step of the range. It can be a `Date_Period` or `Period`.
By default, the step is one day.

> Example
Create a range of dates.
Expand All @@ -514,12 +519,11 @@ type Date
Create a range containing dates [2021-12-05, 2021-12-06].

(Date.new 2021 12 05).up_to (Date.new 2021 12 06) include_end=True
up_to : Date -> Boolean -> Date_Range
up_to self end include_end=False = case end of
_ : Date ->
effective_end = if include_end then end.next else end
Date_Range.new_internal self effective_end increasing=True step=(Period.new days=1)
_ -> Error.throw (Type_Error.Error Date end "end")
up_to : Date -> Boolean -> Date_Period|Period -> Date_Range
up_to self end:Date include_end:Boolean=False step:Date_Period|Period=Date_Period.Day =
effective_end = if include_end then end.next else end
day_range = Date_Range.new_internal self effective_end increasing=True step=(Period.new days=1)
if step == Date_Period.Day then day_range else day_range.with_step step

## ALIAS date range
GROUP Input
Expand All @@ -539,12 +543,11 @@ type Date
Create a range containing dates [2021-12-06, 2021-12-05].

(Date.new 2021 12 06).down_to (Date.new 2021 12 05) include_end=True
down_to : Date -> Boolean -> Date_Range
down_to self end include_end=False = case end of
_ : Date ->
effective_end = if include_end then end.previous else end
Date_Range.new_internal self effective_end increasing=False step=(Period.new days=1)
_ -> Error.throw (Type_Error.Error Date end "end")
down_to : Date -> Boolean -> Date_Period|Period -> Date_Range
down_to self end:Date include_end:Boolean=False step:Date_Period|Period=Date_Period.Day =
effective_end = if include_end then end.previous else end
day_range = Date_Range.new_internal self effective_end increasing=False step=(Period.new days=1)
if step == Date_Period.Day then day_range else day_range.with_step step

## GROUP DateTime
Shift the date by the specified amount of business days.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Date_Range

(Date.new 2022 10 23).down_to (Date.new 2022 10 1) . with_step (Period.new days=2)
with_step : Date_Period|Period -> Date_Range ! Illegal_Argument
with_step self new_step = case new_step of
with_step self new_step:Date_Period|Period = case new_step of
_ : Period ->
effective_length = compute_length_and_verify self.start self.end new_step self.increasing
Date_Range.Internal_Constructor self.start self.end new_step self.increasing effective_length
Expand Down Expand Up @@ -487,6 +487,14 @@ type Date_Range
(1.up_to length).fold self.start acc-> ix->
function acc (self.internal_at ix)

## PRIVATE
Default column name based on step.
default_column_name : Text
default_column_name self =
periods = [Date_Period.Year, Date_Period.Quarter, Date_Period.Month, Date_Period.Week, Date_Period.Day].map _.to_period
names = ["Year", "Quarter", "Month", "Week", "Date"]
index = periods.index_of self.step
if index == Nothing then "Date" else names.get index

## PRIVATE
Computes the length of the range and verifies its invariants.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2656,4 +2656,4 @@ Column.from (that:Range) (name:Text="Range") = Column.from_vector name that.to_v

## PRIVATE
Conversion method to a Column from a Vector.
Column.from (that:Date_Range) (name:Text="Date Range") = Column.from_vector name that.to_vector
Column.from (that:Date_Range) (name:Text=that.default_column_name) = Column.from_vector name that.to_vector
15 changes: 11 additions & 4 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -2729,10 +2729,9 @@ Table.from (that : Text) (format:Delimited_Format = Delimited_Format.Delimited '
Arguments:
- that: The table to convert.
- format: The format of the text.
Text.from (that : Table) (format:Delimited_Format = Delimited_Format.Delimited '\t') =
case format of
_ : Delimited_Format -> Delimited_Writer.write_text that format
_ -> Unimplemented.throw "Text.from is currently only implemented for Delimited_Format."
Text.from (that : Table) (format:Delimited_Format = Delimited_Format.Delimited '\t') = case format of
_ : Delimited_Format -> Delimited_Writer.write_text that format
_ -> Unimplemented.throw "Text.from is currently only implemented for Delimited_Format."

## PRIVATE
Conversion method to a Table from a Vector.
Expand All @@ -2743,6 +2742,14 @@ Table.from (that:Vector) (fields : (Vector | Nothing) = Nothing) = that.to_table
Table.from (that:JS_Object) (fields : (Vector | Nothing) = Nothing) =
Table.from_objects that fields

## PRIVATE
Conversion method to a Column from a Vector.
Table.from (that:Range) (name:Text="Range") = Table.new [Column.from_vector name that.to_vector]

## PRIVATE
Conversion method to a Column from a Vector.
Table.from (that:Date_Range) (name:Text=that.default_column_name) = Table.new [Column.from_vector name that.to_vector]

## PRIVATE
Convert an `XML_Element` into a `Table`

Expand Down
23 changes: 19 additions & 4 deletions test/Base_Tests/src/Data/Range_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ spec = Test.group "Range" <|
range_3.end . should_equal 0
range_3.step . should_equal -1

range_4 = 0.up_to 10 step=2
range_4.start . should_equal 0
range_4.end . should_equal 10
range_4.step . should_equal 2

range_5 = 10.down_to 2 step=2
range_5.start . should_equal 10
range_5.end . should_equal 2
range_5.step . should_equal -2

Test.specify "should allow to include the end" <|
1.up_to 3 include_end=True . to_vector . should_equal [1, 2, 3]
3.down_to 1 include_end=True . to_vector . should_equal [3, 2, 1]
Expand All @@ -50,12 +60,17 @@ spec = Test.group "Range" <|
10.down_to 0 . with_step 2 . should_equal (Range.Between 10 0 -2)
10.down_to 0 . with_step 2 . to_vector . should_equal [10, 8, 6, 4, 2]

1.up_to 2 . with_step 0.5 . should_fail_with Illegal_Argument
1.up_to 2 . with_step -1 . should_fail_with Illegal_Argument
0.up_to 2.0 . should_fail_with Illegal_Argument
0.down_to 2.0 . should_fail_with Illegal_Argument
Test.specify "should fail with type errors if not the wrong type" <|
Test.expect_panic_with (0.0.up_to 2) No_Such_Method
Test.expect_panic_with (0.0.down_to 2) No_Such_Method
Test.expect_panic_with (0.up_to 2.0) Type_Error
Test.expect_panic_with (5.down_to 2.0) Type_Error
Test.expect_panic_with (1.up_to 2 step=0.5) Type_Error
Test.expect_panic_with (5.down_to 2 step=0.5) Type_Error
Test.expect_panic_with (1.up_to 2 . with_step 0.5) Type_Error
Test.expect_panic_with (5.down_to 2 . with_step 0.5) Type_Error
1.up_to 2 . with_step 0 . should_fail_with Illegal_State
1.up_to 2 . with_step -1 . should_fail_with Illegal_Argument

Test.specify "should have a length" <|
0.up_to 100 . length . should_equal 100
Expand Down
3 changes: 3 additions & 0 deletions test/Base_Tests/src/Data/Time/Date_Range_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ spec =
(Date.new 2023 10 04).up_to (Date.new 2023 10 01) . with_step Date_Period.Month . to_vector . should_equal []

Test.specify "should allow setting a custom step" <|
(Date.new 2020 01 10).up_to (Date.new 2020 01 31) step=(Period.new days=5) . to_vector . should_equal [Date.new 2020 01 10, Date.new 2020 01 15, Date.new 2020 01 20, Date.new 2020 01 25, Date.new 2020 01 30]
(Date.new 2020 01 10).up_to (Date.new 2020 01 31) . with_step (Period.new days=5) . to_vector . should_equal [Date.new 2020 01 10, Date.new 2020 01 15, Date.new 2020 01 20, Date.new 2020 01 25, Date.new 2020 01 30]
(Date.new 2020 01 10).up_to (Date.new 2020 01 30) . with_step (Period.new days=5) . to_vector . should_equal [Date.new 2020 01 10, Date.new 2020 01 15, Date.new 2020 01 20, Date.new 2020 01 25]
(Date.new 2020 01 10).up_to (Date.new 2020 01 30) include_end=True . with_step (Period.new days=5) . to_vector . should_equal [Date.new 2020 01 10, Date.new 2020 01 15, Date.new 2020 01 20, Date.new 2020 01 25, Date.new 2020 01 30]

(Date.new 2020 01 10).down_to (Date.new 2020 01 01) step=Date_Period.Week . to_vector . should_equal [Date.new 2020 01 10, Date.new 2020 01 03]
(Date.new 2020 01 10).down_to (Date.new 2020 01 01) . with_step Date_Period.Week . to_vector . should_equal [Date.new 2020 01 10, Date.new 2020 01 03]

(Date.new 2020 01 01).up_to (Date.new 2020 12 31) . with_step Date_Period.Month . to_vector . should_equal [Date.new 2020 01 01, Date.new 2020 02 01, Date.new 2020 03 01, Date.new 2020 04 01, Date.new 2020 05 01, Date.new 2020 06 01, Date.new 2020 07 01, Date.new 2020 08 01, Date.new 2020 09 01, Date.new 2020 10 01, Date.new 2020 11 01, Date.new 2020 12 01]
(Date.new 2020 01 01).up_to (Date.new 2020 12 31) step=Date_Period.Month . to_vector . should_equal [Date.new 2020 01 01, Date.new 2020 02 01, Date.new 2020 03 01, Date.new 2020 04 01, Date.new 2020 05 01, Date.new 2020 06 01, Date.new 2020 07 01, Date.new 2020 08 01, Date.new 2020 09 01, Date.new 2020 10 01, Date.new 2020 11 01, Date.new 2020 12 01]
(Date.new 2020 01 01).up_to (Date.new 2026) . with_step (Period.new years=2) . to_vector . should_equal [Date.new 2020 01 01, Date.new 2022 01 01, Date.new 2024 01 01]
(Date.new 2020 01 01).up_to (Date.new 2026) include_end=True . with_step (Period.new years=2) . to_vector . should_equal [Date.new 2020 01 01, Date.new 2022 01 01, Date.new 2024 01 01, Date.new 2026 01 01]
(Date.new 2060 11 25).down_to (Date.new 2020 11 24) . with_step (Period.new years=20) . to_vector . should_equal [Date.new 2060 11 25, Date.new 2040 11 25, Date.new 2020 11 25]
Expand Down
8 changes: 6 additions & 2 deletions test/Table_Tests/src/In_Memory/Table_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,12 @@ spec =
t_range.at "Range" . to_vector . should_equal [100, 101, 102]

t_date_range = t.set ((Date.new 2020 1 1).up_to (Date.new 2020 1 4))
t_date_range.column_names.should_equal ["X", "Date Range"]
t_date_range.at "Date Range" . to_vector . should_equal [Date.new 2020 1 1, Date.new 2020 1 2, Date.new 2020 1 3]
t_date_range.column_names.should_equal ["X", "Date"]
t_date_range.at "Date" . to_vector . should_equal [Date.new 2020 1 1, Date.new 2020 1 2, Date.new 2020 1 3]

t_month_range = t.set ((Date.new 2020 1 1).up_to (Date.new 2020 4 1) step=Date_Period.Month)
t_month_range.column_names.should_equal ["X", "Month"]
t_month_range.at "Month" . to_vector . should_equal [Date.new 2020 1 1, Date.new 2020 2 1, Date.new 2020 3 1]

Test.specify "should fail if there is a length mismatch on a new column" <|
t = Table.new [["X", [1, 2, 3]]]
Expand Down
Loading