-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
154 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 26 additions & 1 deletion
27
.../lib/Standard/Table/0.0.0-dev/src/Internal/Type_Refinements/Typed_Column_Conversions.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
private | ||
|
||
from Standard.Base import all | ||
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument | ||
|
||
import project.Column.Column | ||
import project.Internal.Type_Refinements.Typed_Column_Helpers | ||
import project.Refined_Types.Numeric_Column.Numeric_Column | ||
import project.Value_Type.Value_Type | ||
from project.Column import apply_unary_operation, naming_helper | ||
|
||
polyglot java import org.enso.table.data.column.operation.unary.AbsOperation | ||
polyglot java import org.enso.table.data.column.operation.unary.SignumOperation | ||
|
||
## This conversion is internal and should never be exported. | ||
Numeric_Column.from (that : Column) = | ||
... | ||
if that.value_type.is_numeric.not then | ||
Error.throw (Illegal_Argument.Error "Cannot convert a "+that.value_type.to_display_text+" column to a numeric column.") | ||
Typed_Column_Helpers.make_numeric_column that In_Memory_Numeric_Column_Implementation | ||
|
||
type In_Memory_Numeric_Column_Implementation | ||
abs column = | ||
Value_Type.expect_numeric column <| | ||
new_name = naming_helper.concat ["abs", naming_helper.to_expression_text column] | ||
apply_unary_operation column AbsOperation.INSTANCE new_name | ||
|
||
signum column = | ||
Value_Type.expect_numeric column <| | ||
new_name = naming_helper.concat ["signum", naming_helper.to_expression_text column] | ||
apply_unary_operation column SignumOperation.INSTANCE new_name |
7 changes: 7 additions & 0 deletions
7
...tion/lib/Standard/Table/0.0.0-dev/src/Internal/Type_Refinements/Typed_Column_Helpers.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import project.Refined_Types.Numeric_Column.Numeric_Column | ||
|
||
## PRIVATE | ||
For internal use only, this function should be used by implementations to | ||
provide a hidden conversion to `Numeric_Column` that allows the type refinement. | ||
make_numeric_column base_column numeric_operations_implementation = | ||
Numeric_Column.Value base_column numeric_operations_implementation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
distribution/lib/Standard/Table/0.0.0-dev/src/Refined_Types/Numeric_Column.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
## A column that contains numeric values. | ||
type Numeric_Column | ||
private Value column operations_implementation | ||
|
||
## GROUP Standard.Base.Operators | ||
ICON operators | ||
Computes the absolute value of each element in the column. | ||
|
||
> Example | ||
Compute the absolute value of values in a column. | ||
|
||
import Standard.Examples | ||
|
||
example_abs = Examples.decimal_column.abs | ||
abs self -> Numeric_Column = | ||
"TODO" | ||
self.operations_implementation.abs self.column | ||
|
||
## GROUP Standard.Base.Operators | ||
ICON operators | ||
Computes the sign of each element in the column. | ||
|
||
It will return -1, 0 or 1 depending on the sign of the value. | ||
|
||
> Example | ||
Compute the signum of values in a column. | ||
|
||
import Standard.Examples | ||
|
||
example_signum = Examples.decimal_column.signum | ||
signum self -> Numeric_Column = | ||
"TODO" | ||
self.operations_implementation.signum self.column |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
test/Table_Tests/src/Common_Table_Operations/Numeric_Column_Spec.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from Standard.Base import all | ||
import Standard.Base.Errors.Common.Type_Error | ||
|
||
from Standard.Table import all | ||
from Standard.Table.Errors import all | ||
|
||
import Standard.Database.Feature.Feature | ||
|
||
from Standard.Test import all | ||
|
||
import project.Common_Table_Operations.Util | ||
from project.Common_Table_Operations.Util import run_default_backend | ||
|
||
type Lazy_Ref | ||
Value ~get | ||
|
||
main filter=Nothing = run_default_backend add_specs filter | ||
|
||
add_specs suite_builder setup = | ||
if setup.is_feature_supported Feature.Column_Operations then (add_numeric_columns_specs suite_builder setup) | ||
|
||
add_numeric_columns_specs suite_builder setup = | ||
prefix = setup.prefix | ||
table_builder = setup.light_table_builder | ||
build_sorted_table = Util.build_sorted_table setup | ||
suite_builder.group prefix+"Numeric_Column" group_builder-> | ||
numeric_column = Lazy_Ref.Value <| build_sorted_table [["X", [-10, 0, 1, 12]]] . at "X" | ||
group_builder.specify "a numeric column can be cast to Numeric_Column" <| | ||
column = numeric_column.get | ||
column.value_type.is_numeric . should_be_true | ||
column:Numeric_Column | ||
|
||
group_builder.specify "a text column cannot be cast to Numeric_Column" <| | ||
column = table_builder [["X", ["txt"]]] . at "X" | ||
column.value_type.is_numeric . should_be_false | ||
Test.expect_panic Type_Error (column:Numeric_Column) | ||
|
||
group_builder.specify "a numeric column supports abs operation" <| | ||
column = numeric_column.get | ||
abs_column = (column:Numeric_Column).abs | ||
abs_column.to_vector . should_equal [10, 0, 1, 12] | ||
|
||
group_builder.specify "a numeric column supports signum operation" <| | ||
column = numeric_column.get | ||
signum_column = (column:Numeric_Column).signum | ||
signum_column.to_vector . should_equal [-1, 0, 1, 1] | ||
|
||
group_builder.specify "floating-point columns support abs and signum" <| | ||
column = build_sorted_table [["X", [-1.5, 0.0, -0.0, 1.0, 200.25]]] . at "X" | ||
abs_column = (column:Numeric_Column).abs | ||
abs_column.to_vector . should_equal [1.5, 0.0, 0.0, 1.0, 200.25] | ||
signum_column = (column:Numeric_Column).signum | ||
signum_column.to_vector . should_equal [-1, 0, 0, 1, 1] | ||
|
||
group_builder.specify "a single value column can also be Numeric_Column" <| | ||
column = table_builder [["X", [1]]] . at "X" | ||
column:Numeric_Column |