Skip to content

Commit 09b840f

Browse files
committed
✨ feat(table): add map_rows, filter_rows, and sort_rows functions
Add row transformation operations to the table module: - map_rows: applies a function to each row - filter_rows: filters rows based on a predicate - sort_rows: sorts rows by column index or default ordering Also registers the table module in the resolver for static lookup.
1 parent b609992 commit 09b840f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

crates/mq-lang/modules/module_tests.mq

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ def test_table_to_markdown():
293293
| assert(len(md_result) > 0)
294294
end
295295

296+
def test_table_map_rows():
297+
let t = first(table::tables(table_nodes))
298+
| let md_result = table::map_rows(t, fn(row): map(row, upcase);)
299+
| assert_eq(to_string(md_result[:rows][0][0]), "ALICE")
300+
| assert_eq(to_string(md_result[:rows][1][0]), "BOB")
301+
end
302+
303+
def test_table_filter_rows():
304+
let t = first(table::tables(table_nodes))
305+
| let md_result = table::filter_rows(t, fn(row): to_string(row[0]) == "Alice";)
306+
| assert_eq(len(md_result[:rows]), 1)
307+
end
308+
309+
def test_table_sort_rows():
310+
let t = first(table::tables(table_nodes))
311+
| let md_result = table::sort_rows(t, 1)
312+
| assert_eq(to_string(md_result[:rows][0][0]), "Bob")
313+
314+
| let md_result = table::sort_rows(t)
315+
| assert_eq(to_string(md_result[:rows][0][0]), "Alice")
316+
end
317+
296318
| run_tests([
297319
# csv
298320
test_case("CSV Parse for Dict", test_csv_parse_for_dict),
@@ -340,4 +362,8 @@ end
340362
test_case("Table Remove Row", test_table_remove_row),
341363
test_case("Table Remove Column", test_table_remove_column),
342364
test_case("Table To Markdown", test_table_to_markdown),
365+
test_case("Table Map Rows", test_table_map_rows),
366+
test_case("Table Filter Rows", test_table_filter_rows),
367+
test_case("Table Sort Rows", test_table_sort_rows),
343368
])
369+

crates/mq-lang/modules/table.mq

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ def remove_column(table, col_index):
9090
| set(table, :rows, rows)
9191
end
9292

93+
# Map a function over each row in the table.
94+
def map_rows(table, f):
95+
set(table, :rows, map(table[:rows], f))
96+
end
97+
98+
# Filter rows in the table based on a predicate function.
99+
def filter_rows(table, f):
100+
set(table, :rows, filter(table[:rows], f))
101+
end
102+
103+
# Sort rows in the table by a specified column index or default sorting.
104+
def sort_rows(table, column_index = None):
105+
let new_rows = if (column_index == None):
106+
sort(table[:rows])
107+
else:
108+
sort_by(table[:rows], fn(row): row[column_index];)
109+
| set(table, :rows, new_rows)
110+
end
111+
93112
# Convert a table structure back into a list of markdown nodes.
94113
def to_markdown(table)
95114
table[:header] + table[:align] + flatten(table[:rows])

crates/mq-lang/src/module/resolver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn module_name(name: &str) -> Cow<'static, str> {
2222
"test" => Cow::Borrowed("test.mq"),
2323
"section" => Cow::Borrowed("section.mq"),
2424
"fuzzy" => Cow::Borrowed("fuzzy.mq"),
25+
"table" => Cow::Borrowed("table.mq"),
2526
_ => Cow::Owned(format!("{}.mq", name)),
2627
}
2728
}

0 commit comments

Comments
 (0)