Skip to content

Commit

Permalink
Add Stats tests to Main.enso.
Browse files Browse the repository at this point in the history
Create helpers for promoting dataflow errors as panics.
Remove set_at where possible.
  • Loading branch information
jdunkerley committed May 26, 2022
1 parent f06588e commit 3b80fb9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 29 deletions.
28 changes: 12 additions & 16 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Statistics.enso
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from Standard.Base import Boolean, True, False, Nothing, Vector, Number, Any, Error, Array, Panic, Illegal_Argument_Error, Unsupported_Argument_Types, IO
import Standard.Base.Runtime.Ref
from Standard.Base import Boolean, True, False, Nothing, Vector, Number, Any, Error, Array, Panic, Illegal_Argument_Error, Unsupported_Argument_Types

from Standard.Base.Data.Vector import Empty_Error

Expand Down Expand Up @@ -166,19 +165,18 @@ pearson_correlation data =
- data: The input data sets
spearman_correlation : [Vector] -> [Vector]
spearman_correlation data =
output_array = Array.new data.length
ref_error = Ref.new Nothing
Panic.handle_wrapped_dataflow_error <|
output = Vector.new_builder data.length

0.up_to data.length . each i->
output_array.set_at i <|
Vector.new data.length j->
if j == i then 1 else
if j < i then (output_array.at j . at i) else
value = here.calculate_spearman_rank (data.at i) (data.at j)
if value.is_error then ref_error.put value
value
0.up_to data.length . each i->
output.append <|
Vector.new data.length j->
if j == i then 1 else
if j < i then (output.at j . at i) else
Panic.throw_wrapped_if_error <|
here.calculate_spearman_rank (data.at i) (data.at j)

ref_error.get.if_nothing (Vector.Vector output_array)
output.to_vector


## PRIVATE
Expand Down Expand Up @@ -211,9 +209,7 @@ calculate_spearman_rank x_data y_data =
Given a set of series get CorrelationStatistics objects
calculate_correlation_statistics_matrix : [Vector] -> [CorrelationStatistics]
calculate_correlation_statistics_matrix data =
data_array = Array.new data.length
0.up_to data.length . each i->(data_array.set_at i (data.at i).to_array)

data_array = Vector.new data.length i->(data.at i).to_array . to_array
stats_array = here.wrap_java_call <| CorrelationStatistics.computeMatrix data_array
Vector.new stats_array.length i->(Vector.Vector (stats_array.at i))

Expand Down
34 changes: 23 additions & 11 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ fill length ~item =
A vector allows to store an arbitrary number of elements in linear memory. It
is the recommended data structure for most applications.

Arguments:
- capacity: Initial capacity of the Vector.Builder

> Example
Construct a vector using a builder that contains the items 1 to 10.

Expand All @@ -66,8 +69,8 @@ fill length ~item =
@Tail_Call do_build start+1 stop
do_build 1 10
builder.to_vector
new_builder : Builder
new_builder = Builder.new
new_builder : Integer -> Builder
new_builder (capacity=1) = Builder.new capacity

## ADVANCED

Expand Down Expand Up @@ -141,13 +144,7 @@ type Vector
at : Integer -> Any ! Index_Out_Of_Bounds_Error
at index =
actual_index = if index < 0 then this.length + index else index
## TODO [RW] Ideally we do not want an additional check here, but we
should catch a Invalid_Array_Index_Error panic. However, such a catch
should still properly forward any other panics or dataflow errors
which is not fully possible until the approach to handling Panics is
improved, as described in the following Pivotal ticket:
https://www.pivotaltracker.com/n/projects/2539304/stories/181029230
if actual_index>=0 && actual_index<this.length then this.unsafe_at actual_index else
Panic.catch Invalid_Array_Index_Error (this.unsafe_at actual_index) _->
Error.throw (Index_Out_Of_Bounds_Error index this.length)

## ADVANCED
Expand Down Expand Up @@ -1015,12 +1012,15 @@ type Builder

## Creates a new builder.

Arguments:
- capacity: Initial capacity of the Vector.Builder

> Example
Make a new builder

Vector.new_builder
new : Builder
new = Builder (Array.new 1) 0
new : Integer->Builder
new (capacity=1) = Builder (Array.new capacity) 0

## Returns the current capacity (i.e. the size of the underlying storage)
of this builder.
Expand Down Expand Up @@ -1088,6 +1088,18 @@ type Builder
this.append item
Nothing

## Gets an element from the vector at a specified index (0-based).

Arguments:
- index: The location in the vector to get the element from. The index is
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_Error
at index =
actual_index = if index < 0 then this.length + index else index
Panic.catch Invalid_Array_Index_Error (this.to_array.at actual_index) _->
Error.throw (Index_Out_Of_Bounds_Error index this.length)

## Checks whether a predicate holds for at least one element of this builder.

Arguments:
Expand Down
17 changes: 17 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Error/Common.enso
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ type Panic
True -> caught_panic.convert_to_dataflow_error
False -> Panic.throw caught_panic

## If a dataflow error had occurred, promote to a Panic and wrap in a Wrapped_Dataflow_Error

Arguments:
- value: value to return if not an error, or rethrow as a Panic.
throw_wrapped_if_error : Any -> Any
throw_wrapped_if_error ~value =
if value.is_error then Panic.throw (Wrapped_Dataflow_Error value.catch) else value

## Catch any Wrapped_Dataflow_Error Panic and rethrow as a dataflow error

Arguments:
- action: The code to execute that potentially raised a Wrapped_Dataflow_Error.
handle_wrapped_dataflow_error : Any -> Any
handle_wrapped_dataflow_error ~action =
Panic.catch Wrapped_Dataflow_Error action caught_panic->
Error.throw caught_panic.payload.payload

## The runtime representation of a syntax error.

Arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ type Vector_Builder
array = Array.new this.length
go ix elem = case elem of
Leaf vec ->
vec.map_with_index vi-> elem->
array.set_at ix+vi elem
Array.copy vec.to_array 0 array ix vec.length
ix + vec.length
Append l r _ ->
ix2 = go ix l
Expand Down
2 changes: 2 additions & 0 deletions test/Tests/src/Main.enso
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import project.Data.Ref_Spec
import project.Data.Text_Spec
import project.Data.Time.Spec as Time_Spec
import project.Data.Vector_Spec
import project.Data.Statistics_Spec
import project.Data.Text.Regex_Spec
import project.Data.Text.Utils_Spec
import project.Data.Text.Default_Regex_Engine_Spec
Expand Down Expand Up @@ -104,4 +105,5 @@ main = Test.Suite.run_main <|
Time_Spec.spec
Uri_Spec.spec
Vector_Spec.spec
Statistics_Spec.spec
Warnings_Spec.spec

0 comments on commit 3b80fb9

Please sign in to comment.