Skip to main content

rl_lang/docs/entries/tutorial/t1/
p11.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2pub static STEP_LAMBDAS: ConceptEntry = ConceptEntry {
3    name: "11. functions as values",
4    summary: "functions as values",
5    category: ConceptCategory::Functions,
6    prerequisites: &[],
7    descriptions: &[
8        DescriptionEntry {
9            kind: DescriptionKind::Explanation,
10            title: None,
11            description: "in rl functions are values just like numbers and strings. you can store a function in a variable with dec fn and call it through that variable",
12            examples: &[
13                "fn double(int x) -> int {\n    return x * 2\n}\n\ndec fn f = double\nprintln(f(5)) // 10",
14            ],
15            expected_output: &[],
16        },
17        DescriptionEntry {
18            kind: DescriptionKind::Explanation,
19            title: None,
20            description: "a lambda is an anonymous function defined inline without a name. useful when you need a short function just once and do not want to name it",
21            examples: &[
22                "dec fn square = fn(int x) -> int {\n    return x * x\n}\n\nprintln(square(4)) // 16",
23            ],
24            expected_output: &[],
25        },
26        DescriptionEntry {
27            kind: DescriptionKind::Explanation,
28            title: None,
29            description: "lambdas capture variables from the surrounding scope automatically",
30            examples: &[
31                "dec int base = 10\n\ndec fn add_base = fn(int x) -> int {\n    return x + base\n}\n\nprintln(add_base(5))  // 15\nprintln(add_base(20)) // 30",
32            ],
33            expected_output: &[],
34        },
35        DescriptionEntry {
36            kind: DescriptionKind::Explanation,
37            title: None,
38            description: "the real power of lambdas is passing them to functions like arr_map and arr_filter which apply your function to every element of an array",
39            examples: &[
40                "get arr_map, arr_filter from std::array\n\ndec arr[int] nums   = [1, 2, 3, 4, 5, 6]\ndec arr[int] evens  = arr_filter(nums, fn(int x) -> bool { return x > 3 })\ndec arr[int] doubled = arr_map(evens, fn(int x) -> int { return x * 2 })\nprintln(doubled) // [8, 10, 12]",
41            ],
42            expected_output: &[],
43        },
44        DescriptionEntry {
45            kind: DescriptionKind::Explanation,
46            title: None,
47            description: "exercise: use arr_filter and a lambda to count how many of the player's guesses were below the secret number, and how many were above\n\nexpected output:\n  guesses below: 2\n  guesses above: 1",
48            examples: &[
49                "get arr_filter, len from std::array\nget format          from std::str\n\n// assume secret and guesses exist from the game\ndec arr[int] below = arr_filter(guesses, fn(int g) -> bool { return g < secret })\ndec arr[int] above = arr_filter(guesses, fn(int g) -> bool { return g > secret })\n\nprintln(format(\"guesses below: {}\", len(below)))\nprintln(format(\"guesses above: {}\", len(above)))",
50            ],
51            expected_output: &[],
52        },
53    ],
54    pitfalls: &[],
55    related: &[],
56    related_stdlib: &[],
57    since: None,
58};