Skip to content

Commit

Permalink
Do Standard.Table.Data.Table
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrecursion committed May 6, 2021
1 parent 71e6791 commit 521a839
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 93 deletions.
18 changes: 9 additions & 9 deletions distribution/std-lib/Standard/data/food_shop_inventory.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
item_name , price , stock
Pavlova Cake (Whole) , 10.50 , 10
Gruyere (Wheel) , 50 , 3
Strawberries (Punnett) , 4.50 , 50
Tomatoes (Whole) , 0.5 , 100
Tomato and Egg Sandwich , 1.75 , 20
Lobster Thermidor , 15.50 , 10
Syrniki , 1.99 , 25
Bacon and Sausage Sandwich , 1.75 , 15
item_id , item_name , price , total_stock , sold_stock
001 , Pavlova Cake (Whole) , 10.50 , 10 , 7
002 , Gruyere (Wheel) , 50 , 3 , 1
003 , Strawberries (Punnett) , 4.50 , 50 , 47
004 , Tomatoes (Whole) , 0.5 , 100 , 32
005 , Tomato and Egg Sandwich , 1.75 , 20 , 7
006 , Lobster Thermidor , 15.50 , 10 , 9
007 , Syrniki , 1.99 , 25 , 11
008 , Bacon and Sausage Sandwich , 1.75 , 15 , 12

9 changes: 9 additions & 0 deletions distribution/std-lib/Standard/data/food_shop_popularity.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
item_id , popularity
001 , 5
002 , 2
003 , 5
004 , 3
005 , 1
006 , 2
007 , 3
008 , 4
13 changes: 13 additions & 0 deletions distribution/std-lib/Standard/data/food_shop_transactions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
transaction_id , item_id , quantity
001 , 001 , 1
001 , 008 , 2
002 , 005 , 4
003 , 003 , 2
003 , 004 , 1
003 , 007 , 8
004 , 002 , 1
005 , 006 , 2
006 , 001 , 1
007 , 005 , 2
008 , 003 , 5

47 changes: 28 additions & 19 deletions distribution/std-lib/Standard/src/Examples.enso
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@ import Standard.Base.Network.Uri as Enso_Uri
import Standard.Image as Enso_Image
import Standard.Image.Data.Matrix as Enso_Matrix

# Can do the top-level examples directory.

# Fine to not provide examples for some APIs (e.g. Database).

# Examples with HTTP should note this clearly in the example text.

# Image source should only download if it doesn't exist. We can say that the #
# user can put it there.

# > Example
# Frobnicate the doobly do
#
# import Standard.Examples
#
# my_example =
# x = dlksaldjks
# y = x + 1
# ...

## An example error type used in a number of examples.

Arguments:
Expand Down Expand Up @@ -247,3 +228,31 @@ text_column_2 = Table.Column.from_vector "Text" ["He,", "he", "he", "i", "so"]

## A simple aggregate column.
aggregate_column : Table.Column.Aggregate_Column

## A simple table that contains basic shop inventory data for the food shop.
inventory_table : Table.Table
inventory_table = Table.from_csv here.csv . set_index "item_id"

## A simple table that contains basic item popularity data for the food shop.
popularity_table : Table.Table
popularity_table =
table = Table.from_csv <| Enso_Project.data / "food_shop_popularity.csv"
table.set_index "item_id"

## A simple tablethat contains basic transaction data for the food shop.
transactions_table : Table.Table
transactions_table =
Table.from_csv <| Enso_Project.data / "food_shop_transactions.csv"

## An aggregate table for the relevant examples.
aggregate_table : Table.Aggregate_Table
aggregate_table =
transactions = Examples.transactions_table
item_names = Examples.inventory_table.at "item_name"
with_names = transactions.join item_names on="item_id"
with_names.group by="item_name"

## An aggregate column for the relevant examples.
aggregate_column : Table.Column.Aggregate_Column
aggregate_column = here.aggregate_table.at "transaction_id"

68 changes: 68 additions & 0 deletions distribution/std-lib/Standard/src/Table/Data/Column.enso
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,15 @@ type Aggregate_Column
groups.
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Convert an aggregate column of transaction ids into a column by
counting the number of transactions. per item.

import Standard.Examples

example_reduce =
Examples.aggregate_column.reduce .length . rename "transaction_count"
reduce : (Vector.Vector -> Any) -> Boolean -> Text -> Column
reduce function skip_missing=True name_suffix="_result" =
f arr = function (Vector.Vector arr)
Expand All @@ -1043,6 +1052,14 @@ type Aggregate_Column
Arguments:
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Convert an aggregate column of transaction ids into a column by summing
the transaction IDs together.

import Standard.Examples

example_sum = Examples.aggregate_column.sum . rename "id_sum"
sum : Text -> Column
sum name_suffix='_sum' =
r = this.java_column.aggregate 'sum' name_suffix (x-> Vector.Vector x . reduce (+)) True
Expand All @@ -1053,6 +1070,13 @@ type Aggregate_Column
Arguments:
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Get the latest (maximum ID) transaction id for each item.

import Standard.Examples

example_max = Examples.aggregate_column.max . rename "latest_transaction"
max : Text -> Column
max name_suffix='_max' =
r = this.java_column.aggregate 'max' name_suffix (x-> Vector.Vector x . reduce Math.max) True
Expand All @@ -1063,6 +1087,13 @@ type Aggregate_Column
Arguments:
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Get the earliest (mimumum ID) transaction id for each item.

import Standard.Examples

example_min = Examples.aggregate_column.min . rename "first_transaction"
min : Text -> Column
min name_suffix='_min' =
r = this.java_column.aggregate 'min' name_suffix (x-> Vector.Vector x . reduce Math.min) True
Expand All @@ -1073,6 +1104,14 @@ type Aggregate_Column
Arguments:
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Count the number of non-missing elements in each group in the column,
which gives the number of transactions in which a given item was sold.

import Standard.Examples

example_count = Examples.aggregate_column.count . rename "transaction_count"
count : Text -> Column
count name_suffix='_count' =
r = this.java_column.aggregate 'count' name_suffix (x-> x.length) True
Expand All @@ -1083,6 +1122,13 @@ type Aggregate_Column
Arguments:
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Get the mean transaction id for each item.

import Standard.Examples

example_mean = Examples.aggregate_column.mean
mean : Text -> Column
mean name_suffix='_mean' =
vec_mean v = if v.length == 0 then Nothing else
Expand All @@ -1096,11 +1142,33 @@ type Aggregate_Column
Arguments:
- name_suffix: a suffix that will be appended to the original column name
to generate the resulting column name.

> Example
Gather the values of the aggregate column together.

import Standard.Examples

example_values = Examples.aggregate_column.values
values : Text -> Column
values name_suffix='_values' =
r = this.java_column.aggregate Nothing name_suffix Vector.Vector False
Column r

## Prints an ASCII-art column with this data to the standard output.

Arguments:
- show_rows: the number of initial rows that should be displayed.

> Example
Pretty-print an aggregate column and display it using the standard
output.

import Standard.Examples

example_print = Examples.aggregate_column.print
print : Nothing
print = this.values.print

## UNSTABLE

A type representing an error for an out-of-bounds index in a column.
Expand Down
Loading

0 comments on commit 521a839

Please sign in to comment.