rl_lang/docs/entries/tutorial/t2/
p_a6.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static ADV_CSV_QUERY: ConceptEntry = ConceptEntry {
4 name: "6. querying CSV data",
5 summary: "querying CSV data",
6 category: ConceptCategory::Tooling,
7 prerequisites: &[],
8 descriptions: &[
9 DescriptionEntry {
10 kind: DescriptionKind::Explanation,
11 title: None,
12 description: "raw rows are just arrays of strings. to query them usefully you need helper functions that know which column index means what. define column constants so you never use magic numbers",
13 examples: &[
14 "// csv.rl\nCONST int COL_ID = 0\nCONST int COL_STATUS = 1\nCONST int COL_CREATED_AT = 2\nCONST int COL_TEXT = 3\n\n// now instead of row[1] you write row[COL_STATUS]\n// readable and safe if columns ever change",
15 ],
16 expected_output: &[],
17 },
18 DescriptionEntry {
19 kind: DescriptionKind::Explanation,
20 title: None,
21 description: "arr_filter with a lambda is how you query rows. the lambda receives a row and returns true if it matches. this is the pattern you will use for every filtered view",
22 examples: &[
23 "get arr_filter from std::array\n\n// get all pending tasks\ndec arr[arr[string]] pending = arr_filter(rows, fn(arr[string] row) -> bool {\n return row[COL_STATUS] == \"pending\"\n})\n\n// get all done tasks\ndec arr[arr[string]] done = arr_filter(rows, fn(arr[string] row) -> bool {\n return row[COL_STATUS] == \"done\"\n})",
24 ],
25 expected_output: &[],
26 },
27 DescriptionEntry {
28 kind: DescriptionKind::Explanation,
29 title: None,
30 description: "arr_find lets you locate a single row by id. it returns the first matching row or null if nothing matches - always check with is_null before using the result",
31 examples: &[
32 "get arr_find from std::array\nget is_null from std::types\n\nfn csv_find_by_id(arr[arr[string]] rows, string id) -> arr[string] {\n return arr_find(rows, fn(arr[string] row) -> bool {\n return row[COL_ID] == id\n })\n}\n\ndec arr[string] task = csv_find_by_id(rows, \"2\")\nif (is_null(task)) {\n println(\"not found\")\n} else {\n println(task[COL_TEXT])\n}",
33 ],
34 expected_output: &[],
35 },
36 DescriptionEntry {
37 kind: DescriptionKind::Explanation,
38 title: None,
39 description: "exercise: add these query functions to csv.rl:\n csv_filter_by(rows, col, value) -> arr[arr[string]]\n csv_find_by_id(rows, id) -> arr[string]\n\nthen test them in main.rl against a hardcoded set of rows",
40 examples: &[
41 "// csv.rl\nget arr_filter, arr_find from std::array\n\nfn csv_filter_by(arr[arr[string]] rows, int col, string value) -> arr[arr[string]] {\n return arr_filter(rows, fn(arr[string] row) -> bool {\n return row[col] == value\n })\n}\n\nfn csv_find_by_id(arr[arr[string]] rows, string id) -> arr[string] {\n return arr_find(rows, fn(arr[string] row) -> bool {\n return row[COL_ID] == id\n })\n}\n\n// main.rl test\ndec arr[arr[string]] rows = [\n [\"1\", \"pending\", \"1750000000\", \"buy milk\"],\n [\"2\", \"done\", \"1750000100\", \"write code\"],\n [\"3\", \"pending\", \"1750000200\", \"fix bug\"],\n]\n\ndec arr[arr[string]] pending = csv_filter_by(rows, COL_STATUS, \"pending\")\nprintln(len(pending)) // 2\n\ndec arr[string] task = csv_find_by_id(rows, \"2\")\nprintln(task[COL_TEXT]) // write code",
42 ],
43 expected_output: &[],
44 },
45 ],
46 pitfalls: &[],
47 related: &[],
48 related_stdlib: &[],
49 since: None,
50};