Skip to content

Commit

Permalink
Revert "Revert array helpers refactor"
Browse files Browse the repository at this point in the history
This reverts commit a817116
  • Loading branch information
radeusgd committed Feb 13, 2024
1 parent 651931d commit f29f313
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 237 deletions.
90 changes: 55 additions & 35 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import project.Data.Pair.Pair
import project.Data.Range.Range
import project.Data.Sort_Direction.Sort_Direction
import project.Data.Text.Text
import project.Data.Vector.Empty_Error
import project.Data.Vector.No_Wrap
import project.Data.Vector.Vector
import project.Error.Error
import project.Errors.Common.Incomparable_Values
import project.Errors.Common.Index_Out_Of_Bounds
import project.Errors.Common.Not_Found
import project.Errors.Empty_Error.Empty_Error
import project.Errors.Problem_Behavior.Problem_Behavior
import project.Errors.Unimplemented.Unimplemented
import project.Internal.Array_Like_Helpers
Expand Down Expand Up @@ -144,7 +144,7 @@ type Array
[My_Type.Value 'hello', 1].to_array.sort == [1, My_Type.Value 'hello'].to_array
sort : Sort_Direction -> (Any -> Any)|Nothing -> (Any -> Any -> (Ordering|Nothing))|Nothing -> Problem_Behavior -> Vector Any ! Incomparable_Values
sort self (order = Sort_Direction.Ascending) on=Nothing by=Nothing on_incomparable=Problem_Behavior.Ignore =
(Vector.from_polyglot_array self).sort order on by on_incomparable
Array_Like_Helpers.sort self order on by on_incomparable

## ALIAS first, last, sample, slice
GROUP Selections
Expand All @@ -159,7 +159,8 @@ type Array
If a `Range`, the selection is specified by two indices, from and to.
@range Index_Sub_Range.default_widget
take : (Index_Sub_Range | Range | Integer) -> Vector Any
take self range=(Index_Sub_Range.First 1) = (Vector.from_polyglot_array self).take range
take self range=(Index_Sub_Range.First 1) =
Array_Like_Helpers.take self range

## ALIAS skip
GROUP Selections
Expand All @@ -173,7 +174,8 @@ type Array
If a `Range`, the selection is specified by two indices, from and to.
@range Index_Sub_Range.default_widget
drop : (Index_Sub_Range | Range | Integer) -> Vector Any
drop self range=(Index_Sub_Range.First 1) = (Vector.from_polyglot_array self).drop range
drop self range=(Index_Sub_Range.First 1) =
Array_Like_Helpers.drop self range

## GROUP Calculations
Inserts the given item into the array at the given index.
Expand All @@ -192,7 +194,8 @@ type Array
['a', 'b', 'c'].to_array.insert -1 'X' == ['a', 'b', 'X', 'c'].to_array
['a', 'b', 'c'].to_array.insert item='X' == ['a', 'b', 'c', 'X'].to_array
insert : Integer -> Any -> Vector ! Index_Out_Of_Bounds
insert self at=self.length item=Nothing = (Vector.from_polyglot_array self).insert at item
insert self at=self.length item=Nothing =
Array_Like_Helpers.insert self at item

## GROUP Selections
Removes the item at the given index from the array.
Expand All @@ -202,7 +205,7 @@ type Array
If the index is less than 0, the index will be counted back from the
end.
remove : Integer -> Vector
remove self at=-1 = (Vector.from_polyglot_array self).remove at
remove self at=-1 = Array_Like_Helpers.remove self at

## GROUP Selections
ICON select_row
Expand Down Expand Up @@ -262,7 +265,8 @@ type Array

["ab", "abab", "aba", "bbb"].to_array.index_of (s-> s == s.reverse) == 2
index_of : (Any | Filter_Condition | (Any -> Boolean)) -> Integer -> Integer | Nothing
index_of self condition start=0 = (Vector.from_polyglot_array self).index_of condition start
index_of self condition (start : Integer = 0) =
Array_Like_Helpers.index_of self condition start

## GROUP Values
Returns the last index of an element in the array.
Expand All @@ -284,7 +288,8 @@ type Array

["ab", "abab", "aba", "bbb"].to_array.last_index_of (s-> s == s.reverse) == 3
last_index_of : (Any | Filter_Condition | (Any -> Boolean)) -> Integer -> Integer | Nothing
last_index_of self condition start=-1 = (Vector.from_polyglot_array self).last_index_of condition start
last_index_of self condition (start : Integer = -1) =
Array_Like_Helpers.last_index_of self condition start

## GROUP Logical
ICON metadata
Expand All @@ -299,7 +304,7 @@ type Array

## Converts the array to a list with the same elements.
to_list : List
to_list self = (Vector.from_polyglot_array self).to_list
to_list self = Array_Like_Helpers.to_list self

## GROUP Selections
ICON preparation
Expand All @@ -325,7 +330,7 @@ type Array

[Pair 1 "a", Pair 2 "b", Pair 1 "c"].to_array . distinct (on = _.first) == [Pair 1 "a", Pair 2 "b"].to_array
distinct : (Any -> Any) -> Vector Any
distinct self (on = x->x) = (Vector.from_polyglot_array self).distinct on
distinct self (on = x->x) = Array_Like_Helpers.distinct self on

## ICON dataframe_map_column
Applies a function to each element of the array, returning the `Vector` of
Expand Down Expand Up @@ -363,7 +368,8 @@ type Array

[1, 2, 3].to_array . map +1
map : (Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
map self function on_problems=Problem_Behavior.Report_Error = (Vector.from_polyglot_array self).map function on_problems
map self function on_problems=Problem_Behavior.Report_Error =
Array_Like_Helpers.map self function on_problems

## Applies a function to each element of the array, returning the `Vector`
that contains all results concatenated.
Expand Down Expand Up @@ -400,7 +406,8 @@ type Array

[0, 1, 2].to_array . flat_map (n -> Vector.fill n n)
flat_map : (Any -> Vector Any) -> Problem_Behavior | No_Wrap -> Vector Any
flat_map self function on_problems=Problem_Behavior.Report_Error = (Vector.from_polyglot_array self).flat_map function on_problems
flat_map self function on_problems=Problem_Behavior.Report_Error =
Array_Like_Helpers.flat_map self function on_problems

## GROUP Selections
ICON preparation
Expand All @@ -418,7 +425,7 @@ type Array
[1, 2, 3, 4, 5].to_array.filter (> 3)
[1, 2, 3, 4, 5].to_array.filter (Filter_Condition.Greater than=3)
filter : (Filter_Condition | (Any -> Boolean)) -> Vector Any
filter self filter = (Vector.from_polyglot_array self).filter filter
filter self filter = Array_Like_Helpers.filter self filter

## GROUP Calculations
Transforms an array of arrays into a `Vector` of inner elements - removes
Expand All @@ -429,12 +436,13 @@ type Array

[[1, 2, 3].to_array, [4, 10].to_array, [].to_array, [0].to_array, [0].to_array].to_array . flatten == [1, 2, 3, 4, 10, 0, 0].to_array
flatten : Vector Any
flatten self = (Vector.from_polyglot_array self).flatten
flatten self = Array_Like_Helpers.flatten self

## PRIVATE
ADVANCED
short_display_text : Integer -> Text
short_display_text self max_entries=10 = (Vector.from_polyglot_array self).short_display_text max_entries
short_display_text self max_entries=10 =
Array_Like_Helpers.short_display_text self max_entries

## Combines all the elements of the array, by iteratively applying the
passed function with the next element of the array. After each step the
Expand All @@ -449,7 +457,8 @@ type Array

[1, 2, 3].to_array.running_fold 0 (+)
running_fold : Any -> (Any -> Any -> Any) -> Vector Any
running_fold self init function = (Vector.from_polyglot_array self).running_fold init function
running_fold self init function =
Array_Like_Helpers.running_fold self init function

## ICON dataframe_map_column
Combines all the elements of the array, by iteratively applying the
Expand All @@ -469,7 +478,8 @@ type Array

[0, 1, 2].to_array . fold 0 (+)
fold : Any -> (Any -> Any -> Any) -> Any
fold self init function = (Vector.from_polyglot_array self).fold init function
fold self init function =
Array_Like_Helpers.fold self init function

## ICON dataframe_map_column
Combines all the elements of the array, by iteratively applying the
Expand All @@ -485,7 +495,8 @@ type Array

[0, 1, 2].to_array . fold_with_index 0 (s->i->e->s+i+e)
fold_with_index : Any -> (Any -> Integer -> Any -> Any) -> Any
fold_with_index self init function = (Vector.from_polyglot_array self).fold_with_index init function
fold_with_index self init function =
Array_Like_Helpers.fold_with_index self init function

## GROUP Calculations
Extend `self` array to the length of `n` appending elements `elem` to
Expand All @@ -508,7 +519,7 @@ type Array

[1, 2, 3, 4, 5].to_array.pad 5 0 == [1, 2, 3, 4, 5].to_array
pad : Integer -> Any -> Vector Any
pad self n elem = (Vector.from_polyglot_array self).pad n elem
pad self n elem = Array_Like_Helpers.pad self n elem

## GROUP Selections
ICON split_text
Expand Down Expand Up @@ -536,7 +547,8 @@ type Array

[1, 2, 3, 4, 5].to_array.partition (x -> x % 2 == 0) == (Pair [2, 4].to_array [1, 3, 5].to_array)
partition : (Filter_Condition | (Any -> Boolean)) -> Pair (Vector Any) (Vector Any)
partition self condition = (Vector.from_polyglot_array self).partition condition
partition self condition =
Array_Like_Helpers.partition self condition

## Partitions the array into `Vector`s of elements which satisfy a given
predicate and ones that do not.
Expand All @@ -557,7 +569,8 @@ type Array

["a", "b", "c", "d"].to_array.partition_with_index (ix -> _ -> ix % 2 == 0) == (Pair ["a", "c"].to_array ["b", "d"].to_array)
partition_with_index : (Integer -> Any -> Boolean) -> Pair (Vector Any) (Vector Any)
partition_with_index self predicate = (Vector.from_polyglot_array self).partition_with_index predicate
partition_with_index self predicate =
Array_Like_Helpers.partition_with_index self predicate

## Applies a function to each element of the array, returning the `Vector`
of results.
Expand Down Expand Up @@ -597,7 +610,8 @@ type Array

[1, 2, 3].to_array.map_with_index (+)
map_with_index : (Integer -> Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
map_with_index self function on_problems=Problem_Behavior.Report_Error = (Vector.from_polyglot_array self).map_with_index function on_problems
map_with_index self function on_problems=Problem_Behavior.Report_Error =
Array_Like_Helpers.map_with_index self function on_problems

## PRIVATE
Creates a new array with the skipping elements until `start` and then
Expand All @@ -612,7 +626,8 @@ type Array

[1, 2, 3, 4, 5, 6, 7, 8].to_array.slice 2 5 == [3, 4, 5].to_array
slice : Integer -> Integer -> Vector Any
slice self start end = (Vector.from_polyglot_array self).slice start end
slice self start end =
Array_Like_Helpers.slice self start end

## GROUP Selections
Returns the first element of the array that satisfies the condition or
Expand All @@ -630,7 +645,8 @@ type Array

[1, 2, 3, 4, 5].to_array.find (> 3)
find : (Filter_Condition | (Any -> Boolean)) -> Integer -> Any -> Any
find self condition start=0 ~if_missing=(Error.throw Not_Found) = (Vector.from_polyglot_array self).find condition start if_missing
find self condition (start : Integer = 0) ~if_missing=(Error.throw Not_Found) =
Array_Like_Helpers.find self condition start if_missing

## ICON select_row
Gets an element from the array at a specified index (0-based).
Expand All @@ -642,7 +658,8 @@ type Array
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.
get : Integer -> Any -> Any
get self index ~if_missing=Nothing = (Vector.from_polyglot_array self).get index if_missing
get self index ~if_missing=Nothing =
Array_Like_Helpers.get self index if_missing

## GROUP Logical
ICON metadata
Expand All @@ -669,7 +686,8 @@ type Array

[0, 10, 2, 2].to_array.filter (==) == [0, 2].to_array
filter_with_index : (Integer -> Any -> Boolean) -> Vector Any
filter_with_index self predicate = (Vector.from_polyglot_array self).filter_with_index predicate
filter_with_index self predicate =
Array_Like_Helpers.filter_with_index self predicate

## GROUP Calculations
ICON join
Expand All @@ -686,7 +704,8 @@ type Array

["foo", "bar", "baz"].to_array.join ", "
join : Text -> Text -> Text -> Text
join self separator="" prefix="" suffix="" = (Vector.from_polyglot_array self).join separator prefix suffix
join self separator:Text="" prefix:Text="" suffix:Text="" =
Array_Like_Helpers.join self separator prefix suffix

## PRIVATE
Generates a human-readable text representation of the array.
Expand All @@ -710,7 +729,8 @@ type Array

[0, 1, 2].to_array . reduce (+)
reduce : (Any -> Any -> Any) -> Any -> Any
reduce self function ~if_empty=(Error.throw (Empty_Error.Error Array)) = (Vector.from_polyglot_array self).reduce function if_empty
reduce self function ~if_empty=(Error.throw Empty_Error) =
Array_Like_Helpers.reduce self function if_empty

## GROUP Logical
ICON preparation
Expand All @@ -730,7 +750,7 @@ type Array

[1, 2, 3, 4, 5].to_array.any (x-> x%2 == 0)
any : (Filter_Condition | (Any -> Boolean)) -> Boolean
any self condition = (Vector.from_polyglot_array self).any condition
any self condition = Array_Like_Helpers.any self condition

## GROUP Logical
ICON preparation
Expand All @@ -750,7 +770,7 @@ type Array

[-1, 1, 5, 8].to_array.all (x-> x%2 == 0)
all : (Filter_Condition | (Any -> Boolean)) -> Boolean
all self condition = (Vector.from_polyglot_array self).all condition
all self condition = Array_Like_Helpers.all self condition

## GROUP Logical
ICON preparation
Expand Down Expand Up @@ -820,7 +840,7 @@ type Array

## Returns the array as a `Vector`.
to_vector : Vector
to_vector self = (Vector.from_polyglot_array self).from_polyglot_array
to_vector self = Vector.from_polyglot_array self

## GROUP Selections
Reverses the array, returning a `Vector` with the same elements, but in
Expand All @@ -831,7 +851,7 @@ type Array

[1, 2].to_array.reverse
reverse : Vector Any
reverse self = (Vector.from_polyglot_array self).reverse
reverse self = Array_Like_Helpers.reverse self

## PRIVATE
ADVANCED
Expand All @@ -848,7 +868,7 @@ type Array

[1, 2, 3, 4, 5].to_array . each IO.println
each : (Any -> Any) -> Nothing
each self f = (Vector.from_polyglot_array self).each f
each self f = Array_Like_Helpers.each self f

## PRIVATE
ADVANCED
Expand All @@ -868,7 +888,7 @@ type Array

[1, 2, 3, 4, 5].to_array . each_with_index (ix->elem-> IO.println Pair ix elem)
each_with_index : (Integer -> Any -> Any) -> Nothing
each_with_index self f = (Vector.from_polyglot_array self).each_with_index f
each_with_index self f = Array_Like_Helpers.each_with_index self f

## ALIAS append, concatenate, union
GROUP Operators
Expand All @@ -884,4 +904,4 @@ type Array

[1].to_array + [2].to_array
+ : Vector Any -> Vector Any
+ self that = (Vector.from_polyglot_array self).+ that
+ self that = Array_Like_Helpers.plus self that
6 changes: 3 additions & 3 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 @@ -555,7 +555,7 @@ type List

example_first = Examples.list.find (> 2)
find : (Filter_Condition | (Any -> Boolean)) -> Integer -> Any -> Any
find self condition start=0 ~if_missing=(Error.throw Not_Found) =
find self condition (start : Integer = 0) ~if_missing=(Error.throw Not_Found) =
predicate = unify_condition_or_predicate condition
case start.signum of
-1 ->
Expand Down Expand Up @@ -583,7 +583,7 @@ type List

[1, 2, 3, 4, 5].find (> 3)
index_of : (Any | Filter_Condition | (Any -> Boolean)) -> Integer -> Integer | Nothing
index_of self condition start=0 = case start.signum of
index_of self condition (start : Integer = 0) = case start.signum of
-1 ->
node_and_index = find_node_from_end self start
found = node_and_index.first.index_of condition 0
Expand Down Expand Up @@ -611,7 +611,7 @@ type List

[1, 2, 3, 4, 5].find (> 3)
last_index_of : (Any | Filter_Condition | (Any -> Boolean)) -> Integer -> Integer | Nothing
last_index_of self condition start=-1 = case self of
last_index_of self condition (start : Integer = -1) = case self of
Nil -> if start == -1 || start == 0 then Nothing else Error.throw (Index_Out_Of_Bounds.Error start 0)
Cons _ _ ->
length = self.length
Expand Down
6 changes: 3 additions & 3 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 @@ -147,7 +147,7 @@ type Pair

Pair.new 1 6 .find (> 3)
find : (Any -> Boolean) -> Integer -> Any -> Any
find self predicate start=0 ~if_missing=(Error.throw Not_Found) =
find self predicate (start : Integer = 0) ~if_missing=(Error.throw Not_Found) =
check_start_valid start used_start->
if used_start<1 && predicate self.first then self.first else
if used_start<2 && predicate self.second then self.second else
Expand All @@ -169,7 +169,7 @@ type Pair
Pair.new 1 2 . index_of 2 == 1
Pair.new 2 2 . index_of 2 == 0
index_of : (Any | (Any -> Boolean)) -> Integer -> Integer | Nothing
index_of self element start=0 = check_start_valid start used_start->
index_of self element (start : Integer = 0) = check_start_valid start used_start->
predicate = case element of
_ : Function -> element
_ -> (==element)
Expand All @@ -192,7 +192,7 @@ type Pair

Pair.new 2 2 . last_index_of 2 == 1
last_index_of : (Any | (Any -> Boolean)) -> Integer -> Integer | Nothing
last_index_of self element start=-1 = check_start_valid start max=2 used_start->
last_index_of self element (start : Integer = -1) = check_start_valid start max=2 used_start->
predicate = case element of
_ : Function -> element
_ -> (==element)
Expand Down
Loading

0 comments on commit f29f313

Please sign in to comment.