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

Add annotation for table-from-raw-array(), checking arg is indeed a raw array of rows #1759

Open
wants to merge 3 commits into
base: horizon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/arr/trove/tables.arr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
provide:
* hiding (is-kv-pairs),
* hiding (is-kv-pairs, is-raw-array-of-rows),
type *
end

Expand Down Expand Up @@ -88,7 +88,11 @@ fun empty-table(col-names :: List<String>) -> Table:
end
end

fun table-from-raw-array(arr):
fun is-raw-array-of-rows(ra :: RawArray<Any>) -> Boolean:
raw-array-fold(lam(base, elt, _): base and is-row(elt) end, true, ra, 0)
end

fun table-from-raw-array(arr :: RawArray<Any>%(is-raw-array-of-rows)) -> Table:
col-names = raw-array-get(arr, 0).get-column-names()
with-cols = empty-table(col-names)
for raw-array-fold(t from with-cols, r from arr, _ from 0):
Expand Down
18 changes: 17 additions & 1 deletion tests/pyret/tests/test-tables.arr
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,24 @@ check "table-from-rows":
t4 = table-from-rows.make(raw-array-from-list(new-row-list))

nothing does-not-raise # Dummy test to avoid well-formedness errors in the previous row
end

[table-from-rows:
[list:
[raw-row: {"A"; 5}, {"B"; 7}, {"C"; 8}],
[raw-row: {"A"; 1}, {"B"; 2}, {"C"; 3}]]]
raises "is-raw-array-of-rows"

[table-from-rows:
1, [raw-row: {"A"; 1}, {"B"; 2}],
false, [raw-row: {"A"; 3}, {"B"; 4}],
"non-row string", [raw-row: {"A"; 5}, {"B"; 6}]]
raises "is-raw-array-of-rows"

[table-from-rows:
[raw-row: 5, {"B"; 7}, {"C"; 8}],
[raw-row: {"A"; 1}, {"B"; 2}, {"C"; 3}]]
raises "KeyValPair"
end

table-from-column = TS.table-from-column
table-from-columns = TS.table-from-columns
Expand Down