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

Some tweaks following Steve's testings #10042

Merged
merged 8 commits into from
May 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import project.Panic.Panic
from project.Data.Boolean import Boolean, False, True
from project.Data.Index_Sub_Range import Index_Sub_Range
from project.Data.Range.Extensions import all
from project.Metadata import Display, Widget

## The type of primitive mutable arrays.
@Builtin_Type
Expand All @@ -46,8 +47,9 @@ type Array
Get the last element of an array.

[1, 2, 3].to_array.at -1 == 3
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
at : Integer -> Any ! Index_Out_Of_Bounds
at self index = Array_Like_Helpers.at self index
at self index:Integer=0 = Array_Like_Helpers.at self index

## GROUP Metadata
ICON metadata
Expand Down Expand Up @@ -733,8 +735,9 @@ type Array
also allowed be negative, then the elements are indexed from the back
of the array, i.e. -1 will correspond to the last element.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
get : Integer -> Any -> Any
get self index ~if_missing=Nothing =
get self index:Integer=0 ~if_missing=Nothing =
Array_Like_Helpers.get self index if_missing

## GROUP Logical
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ type Array_Proxy
methods.
from_proxy_object : Any -> Array
from_proxy_object proxy =
Array_Like_Helpers.new_array_proxy_builtin proxy.length proxy.at
Array_Like_Helpers.new_array_proxy_builtin proxy.length (i-> proxy.at i)
7 changes: 5 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/List.enso
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import project.Panic.Panic
from project.Data.Boolean import Boolean, False, True
from project.Data.Filter_Condition import unify_condition_or_predicate, unify_condition_predicate_or_element
from project.Data.List.List import Cons, Nil
from project.Metadata import Display, Widget

## The basic cons-list type.

Expand Down Expand Up @@ -76,8 +77,9 @@ type List
import Standard.Examples

example_length = Examples.list.length
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
at : Integer -> Any ! Index_Out_Of_Bounds
at self index =
at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index self.length))

## ICON parse3
Expand All @@ -93,8 +95,9 @@ type List
also allowed be negative, then the elements are indexed from the back
of the final item, i.e. -1 will correspond to the last element.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
get : Integer -> Any -> Any
get self index ~if_missing=Nothing = case index >= 0 of
get self index:Integer=0 ~if_missing=Nothing = case index >= 0 of
True ->
loop current index = case current of
Nil -> if_missing
Expand Down
7 changes: 5 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Pair.enso
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import project.Function.Function
import project.Nothing.Nothing
import project.Panic.Panic
from project.Data.Text.Extensions import all
from project.Metadata import Display, Widget

## A pair of elements.
type Pair
Expand Down Expand Up @@ -70,8 +71,9 @@ type Pair
- index: The location in the pair to get the element from. The index is
also allowed be negative, then the elements are indexed from the back
of the pair, i.e. -1 will correspond to the last element.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
at : Integer -> Any
at self index =
at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index 2))

## ICON parse3
Expand All @@ -83,8 +85,9 @@ type Pair
also allowed be negative, then the elements are indexed from the back
of the pair, i.e. -1 will correspond to the last element.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
get : Integer -> Any -> Any
get self index ~if_missing=Nothing =
get self index:Integer=0 ~if_missing=Nothing =
case index of
0 -> self.first
1 -> self.second
Expand Down
10 changes: 6 additions & 4 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 @@ -15,6 +15,7 @@ import project.Nothing.Nothing
import project.Panic.Panic
from project.Data.Boolean import Boolean, False, True
from project.Data.Filter_Condition import unify_condition_or_predicate
from project.Metadata import Display, Widget

## Represents a right-exclusive range of integer values.
type Range
Expand Down Expand Up @@ -120,8 +121,9 @@ type Range
Get the last element of a range with step.

0.up_to 10 . with_step 2 . get -1 == 8
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
at : Integer -> Any ! Index_Out_Of_Bounds
at self index =
at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index self.length))

## ICON parse3
Expand All @@ -133,8 +135,9 @@ type Range
also allowed be negative, then the elements are indexed from the back,
i.e. -1 will correspond to the last element.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
get : Integer -> Any -> Any
get self index ~if_missing=Nothing =
get self index:Integer=0 ~if_missing=Nothing =
len = self.length
used_index = if index < 0 then len + index else index
if used_index >= 0 && used_index < len then self.start + used_index * self.step else
Expand Down Expand Up @@ -532,10 +535,9 @@ type Range
1.up_to 6 . to_vector
to_vector : Vector Integer
to_vector self =
proxy = Array_Proxy.new self.length self.at
proxy = Array_Proxy.new self.length (self.at _)
Vector.from_polyglot_array proxy


## ICON preparation
Combines all the elements of a non-empty range using a binary operation.
If the range is empty, returns `if_empty`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ Text.each self function =
Get the individual characters in the text "건반(Korean)".

"건반(Korean)".at 1 == "반"
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length display=Display.Always)
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
Text.at : Integer -> Text ! Index_Out_Of_Bounds
Text.at self index =
Text.at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index self.length))

## ALIAS get character
Expand All @@ -154,9 +154,9 @@ Text.at self index =
Get the individual characters in the text "건반(Korean)".

"건반(Korean)".get 1 == "반"
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length display=Display.Always)
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
Text.get : Integer -> Any -> Any
Text.get self index ~if_missing=Nothing =
Text.get self index:Integer=0 ~if_missing=Nothing =
new_index = if index < 0 then index + self.length else index
if new_index < 0 then if_missing else
iterator = BreakIterator.getCharacterInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import project.Nothing.Nothing
import project.Panic.Panic
from project.Data.Boolean import Boolean, False, True
from project.Data.Range.Extensions import all
from project.Metadata import Display, Widget

type Match
## PRIVATE
Expand Down Expand Up @@ -299,8 +300,9 @@ type Match
Arguments:
- id: The integer index or name of that group.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.pattern.group_count display=Display.Always)
get : Integer -> Any -> Text | Any
get self index ~if_missing=Nothing =
get self index:Integer=0 ~if_missing=Nothing =
self.text index . catch No_Such_Group (_-> if_missing)

## GROUP Selections
Expand All @@ -312,6 +314,7 @@ type Match
Arguments:
- id: The integer index or name of that group.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.pattern.group_count display=Display.Always)
at : Integer -> Text ! Index_Out_Of_Bounds
at self index =
at self index:Integer=0 =
self.get index if_missing=(Error.throw (Index_Out_Of_Bounds.Error index self.pattern.group_count))
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import project.Nothing.Nothing
from project.Data.Boolean import Boolean, False, True
from project.Data.Filter_Condition import unify_condition_or_predicate, unify_condition_predicate_or_element
from project.Data.Range.Extensions import all
from project.Metadata import Display, Widget
from project.Runtime import assert

polyglot java import org.enso.base.Time_Utils
Expand Down Expand Up @@ -161,8 +162,9 @@ type Date_Range
Get the last element of a range with step.

(Date.new 2023 04 05) . up_to (Date.new 2023 10 07) . with_step Date_Period.Month . get -1 == (Date.new 2023 10 05)
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
at : Integer -> Any ! Index_Out_Of_Bounds
at self index =
at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index self.length))

## ICON parse3
Expand All @@ -174,8 +176,9 @@ type Date_Range
also allowed be negative, then the elements are indexed from the back,
i.e. -1 will correspond to the last element.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
get : Integer -> Any -> Any
get self index ~if_missing=Nothing =
get self index:Integer=0 ~if_missing=Nothing =
len = self.length
effective_index = if index < 0 then len + index else index
if effective_index >= 0 && effective_index < len then self.internal_at effective_index else
Expand Down Expand Up @@ -214,7 +217,7 @@ type Date_Range
(Date.new 2021 05 07).up_to (Date.new 2021 05 10) . to_vector
to_vector : Vector Date
to_vector self =
proxy = Array_Proxy.new self.length self.at
proxy = Array_Proxy.new self.length (self.at _)
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this change do? Naively, I thought self.at == x-> self.at x == self.at _.

Copy link
Member

Choose a reason for hiding this comment

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

Once the index argument of at got a default, calling self.at will force all default arguments and thus will return the first element, not the lambda. The equation above holds if there are no default arguments.

Btw.

Suggested change
proxy = Array_Proxy.new self.length (self.at _)
proxy = Array_Proxy.new self.length (self.at ...)

would probably work as well

Vector.from_polyglot_array proxy

## GROUP Logical
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ from project.Data.Filter_Condition import unify_condition_or_predicate, unify_co
from project.Data.Index_Sub_Range import Index_Sub_Range
from project.Data.Ordering import all
from project.Data.Range.Extensions import all
from project.Metadata import Display, Widget
from project.Runtime import assert

polyglot java import java.lang.IndexOutOfBoundsException
Expand Down Expand Up @@ -294,8 +295,9 @@ type Vector a
Get the last element of a vector.

[1, 2, 3].at -1 == 3
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
at : Integer -> Any ! Index_Out_Of_Bounds
at self index = Array_Like_Helpers.at self index
at self index:Integer=0 = Array_Like_Helpers.at self index

## ICON parse3
Gets an element from the vector at a specified index (0-based).
Expand All @@ -306,8 +308,9 @@ type Vector a
also allowed be negative, then the elements are indexed from the back
of the vector, i.e. -1 will correspond to the last element.
- if_missing: The value to return if the index is out of bounds.
@index (t-> Widget.Numeric_Input minimum=0 maximum=t.length-1 display=Display.Always)
get : Integer -> Any -> Any
get self index ~if_missing=Nothing =
get self index:Integer=0 ~if_missing=Nothing =
Array_Like_Helpers.get self index if_missing

## ICON dataframe_map_column
Expand Down Expand Up @@ -1440,7 +1443,7 @@ type Builder
also allowed be negative, then the elements are indexed from the back
of the vector, i.e. -1 will correspond to the last element.
at : Integer -> Any ! Index_Out_Of_Bounds
at self index =
at self index:Integer =
actual_index = if index < 0 then self.length + index else index
Panic.catch IndexOutOfBoundsException (self.elements_java_builder.get actual_index) _->
Error.throw (Index_Out_Of_Bounds.Error index self.length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ transpose vec_of_vecs =
Vector.from_polyglot_array proxy

map vector function on_problems =
vector_from_function vector.length (function << vector.at) on_problems
vector_from_function vector.length (i-> function (vector.at i)) on_problems

map_with_index vector function on_problems =
vector_from_function vector.length (i-> function i (vector.at i)) on_problems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ type Nothing
- key: The key to get.
- if_missing: The value to return if the key is not found.
get : Text | Integer -> Any -> Nothing
get self key ~if_missing=Nothing =
get self (key:(Text | Integer)=0) ~if_missing=Nothing =
_ = key
if_missing
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type File_By_Line
Arguments:
- line: The line to read (0 indexed).
get : Integer -> Text
get self line:Integer = if self.limit_lines.is_nothing.not && line>self.limit_lines then Error.throw (Index_Out_Of_Bounds.Error line self.limit_lines) else
get self line:Integer=0 = if self.limit_lines.is_nothing.not && line>self.limit_lines then Error.throw (Index_Out_Of_Bounds.Error line self.limit_lines) else
read_line self line

## GROUP Selections
Expand Down Expand Up @@ -207,7 +207,7 @@ type File_By_Line
Exports the row_map
row_positions : Vector Integer
row_positions self = Vector.from_polyglot_array <|
Array_Proxy.new self.row_map.getSize (i-> self.row_map.get i)
Array_Proxy.new self.row_map.getSize (self.row_map.get _)

## PRIVATE
Gets the Java_File for the backing file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type DB_Column

example_at = Examples.integer_column.at 0
at : Integer -> (Any | Nothing) ! Index_Out_Of_Bounds
at self (index : Integer) =
at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index self.length))

## GROUP Standard.Base.Selections
Expand All @@ -155,7 +155,7 @@ type DB_Column

example_at = Examples.integer_column.get 0 -1
get : Integer -> Any -> Any | Nothing
get self (index : Integer) (~default=Nothing) =
get self index:Integer=0 (~default=Nothing) =
self.read (..First index+1) . get index default

## GROUP Standard.Base.Metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ type DB_Table
- selector: The name or index of the column to get.
@selector Widget_Helpers.make_column_name_selector
at : Text | Integer -> DB_Column ! No_Such_Column | Index_Out_Of_Bounds
at self selector=0 = case selector of
at self (selector:(Integer | Text)=0) = case selector of
_ : Integer -> self.make_column (self.internal_columns.at selector)
_ -> self.get selector (Error.throw (No_Such_Column.Error selector))

Expand All @@ -148,11 +148,10 @@ type DB_Table
- if_missing: The value to use if the selector isn't present.
@selector Widget_Helpers.make_column_name_selector
get : Text | Integer -> Any -> DB_Column | Any
get self selector=0 ~if_missing=Nothing =
get self (selector:(Integer | Text)=0) ~if_missing=Nothing =
internal_column = case selector of
_ : Integer -> self.internal_columns.get selector if_missing=Nothing
_ : Text -> self.internal_columns.find (p -> p.name == selector) if_missing=Nothing
_ -> Error.throw (Illegal_Argument.Error "expected 'selector' to be either a Text or an Integer, but got "+(Meta.get_simple_type_name selector)+".")
if internal_column.is_nothing then if_missing else self.make_column internal_column

## ALIAS cell value, get cell
Expand Down
8 changes: 4 additions & 4 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ type Column
example_at = Examples.integer_column.at 0
@index (self-> Numeric_Input minimum=0 maximum=self.length-1)
at : Integer -> (Any | Nothing) ! Index_Out_Of_Bounds
at self (index : Integer) =
at self index:Integer=0 =
self.get index (Error.throw (Index_Out_Of_Bounds.Error index self.length))

## GROUP Standard.Base.Selections
Expand All @@ -2159,7 +2159,7 @@ type Column
example_at = Examples.integer_column.get 0 -1
@index (self-> Numeric_Input minimum=0 maximum=self.length-1)
get : Integer -> Any -> Any | Nothing
get self (index : Integer) (~default=Nothing) =
get self index:Integer=0 (~default=Nothing) =
valid_index = (index >= 0) && (index < self.length)
if valid_index.not then default else
storage = self.java_column.getStorage
Expand Down Expand Up @@ -2321,7 +2321,7 @@ type Column
@range Index_Sub_Range.default_widget
take : (Index_Sub_Range | Range | Integer) -> Column
take self range=(First 1) =
Index_Sub_Range_Module.take_helper self.length self.at self.slice (slice_ranges self) range
Index_Sub_Range_Module.take_helper self.length (self.at _) self.slice (slice_ranges self) range

## ALIAS skip, remove
GROUP Standard.Base.Selections
Expand All @@ -2334,7 +2334,7 @@ type Column
@range Index_Sub_Range.default_widget
drop : (Index_Sub_Range | Range | Integer) -> Column
drop self range=(First 1) =
Index_Sub_Range_Module.drop_helper self.length self.at self.slice (slice_ranges self) range
Index_Sub_Range_Module.drop_helper self.length (self.at _) self.slice (slice_ranges self) range

## PRIVATE
Returns a column with a continuous sub-range of rows taken.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Convertible_To_Columns

## PRIVATE
Convertible_To_Columns.from (that:JS_Object) =
Convertible_To_Columns.Value that.field_names that.get
Convertible_To_Columns.Value that.field_names (that.get _)

## PRIVATE
Convertible_To_Columns.from (that:Map) =
Expand All @@ -38,7 +38,7 @@ Convertible_To_Columns.from (that:Column) =

## PRIVATE
Convertible_To_Columns.from (that:Row) =
Convertible_To_Columns.Value that.column_names that.get
Convertible_To_Columns.Value that.column_names (that.get _)

## PRIVATE
Convertible_To_Columns.from (that:Vector) =
Expand Down
Loading
Loading