aimx/info_library/
functional_cards.rs

1//! Functional programming function documentation for AIMX expressions.
2//!
3//! Provides comprehensive documentation for higher-order functions and functional
4//! programming patterns used in AIMX arrays and closures.
5
6use crate::info_library::{FunctionCard, ArgumentInfo};
7
8/// Documentation for the `map` function.
9///
10/// Transforms each element of an array by applying a closure function.
11/// Returns a new array with the transformed elements.
12pub const MAP_CARD: FunctionCard = FunctionCard {
13    identifier: "map",
14    signature: "map(array, closure)",
15    brief: "Transform each element of an array using a closure function.",
16    description: "The map function applies a closure to each element of an array, producing a new array with the transformed values. This is a fundamental functional programming operation that enables data transformation pipelines. The closure receives each array element as its first parameter and should return the transformed value.",
17    arguments: &[
18        &ArgumentInfo {
19            label: "array",
20            description: "Input array to transform",
21            type_hint: "Array",
22            optional: false,
23        },
24        &ArgumentInfo {
25            label: "closure",
26            description: "Function to apply to each element",
27            type_hint: "Closure",
28            optional: false,
29        },
30    ],
31    returns: "Array containing transformed elements",
32    errors: "Returns error if closure cannot be applied or array is not provided",
33    categories: &["functional", "higher-order", "transformation"],
34    examples: &[
35        "map((1, 2, 3), x => x * 2) // Returns (2, 4, 6)",
36        "map((\"a\", \"b\", \"c\"), x => upper(x)) // Returns (\"A\", \"B\", \"C\")",
37        "map((1, 2, 3), x => x > 1) // Returns (false, true, true)",
38    ],
39};
40
41/// Documentation for the `filter` function.
42///
43/// Filters array elements based on a predicate closure.
44/// Returns a new array containing only elements that satisfy the predicate.
45pub const FILTER_CARD: FunctionCard = FunctionCard {
46    identifier: "filter",
47    signature: "filter(array, closure)",
48    brief: "Filter array elements using a predicate closure.",
49    description: "The filter function creates a new array containing only the elements that satisfy a given predicate. The predicate is a closure that takes an array element and returns a boolean value. Elements for which the predicate returns true are included in the result array.",
50    arguments: &[
51        &ArgumentInfo {
52            label: "array",
53            description: "Input array to filter",
54            type_hint: "Array",
55            optional: false,
56        },
57        &ArgumentInfo {
58            label: "closure",
59            description: "Predicate function to test each element",
60            type_hint: "Closure",
61            optional: false,
62        },
63    ],
64    returns: "Array containing elements that satisfy the predicate",
65    errors: "Returns error if predicate cannot be evaluated or array is not provided",
66    categories: &["functional", "higher-order", "filtering"],
67    examples: &[
68        "filter((1, 2, 3, 4, 5), x => x > 3) // Returns (4, 5)",
69        "filter((\"apple\", \"banana\", \"cherry\"), x => length(x) > 5) // Returns (\"banana\", \"cherry\")",
70        "filter((1, 2, 3, 4, 5), x => x % 2 == 0) // Returns (2, 4)",
71    ],
72};
73
74/// Documentation for the `reduce` function.
75///
76/// Reduces an array to a single value using an accumulator closure.
77/// Applies the closure cumulatively to array elements.
78pub const REDUCE_CARD: FunctionCard = FunctionCard {
79    identifier: "reduce",
80    signature: "reduce(array, initial, closure)",
81    brief: "Reduce array to single value using accumulator closure.",
82    description: "The reduce function combines all elements of an array into a single value by repeatedly applying an accumulator function. It starts with an initial value and applies the closure to the accumulator and each array element in sequence. This is useful for computing sums, products, concatenations, or any cumulative operation.",
83    arguments: &[
84        &ArgumentInfo {
85            label: "array",
86            description: "Input array to reduce",
87            type_hint: "Array",
88            optional: false,
89        },
90        &ArgumentInfo {
91            label: "initial",
92            description: "Initial accumulator value",
93            type_hint: "Any",
94            optional: false,
95        },
96        &ArgumentInfo {
97            label: "closure",
98            description: "Accumulator function (acc, element) => new_acc",
99            type_hint: "Closure",
100            optional: false,
101        },
102    ],
103    returns: "Single value resulting from reduction",
104    errors: "Returns error if closure cannot be applied or array is not provided",
105    categories: &["functional", "higher-order", "aggregation"],
106    examples: &[
107        "reduce((1, 2, 3, 4, 5), 0, (acc, x) => acc + x) // Returns 15",
108        "reduce((1, 2, 3, 4, 5), 1, (acc, x) => acc * x) // Returns 120",
109        "reduce((\"a\", \"b\", \"c\"), \"\", (acc, x) => acc + x) // Returns \"abc\"",
110    ],
111};
112
113/// Documentation for the `scan` function.
114///
115/// Like reduce but returns intermediate results as an array.
116/// Provides running accumulation values throughout the reduction process.
117pub const SCAN_CARD: FunctionCard = FunctionCard {
118    identifier: "scan",
119    signature: "scan(array, initial, closure)",
120    brief: "Reduce array while keeping intermediate results.",
121    description: "The scan function is similar to reduce but returns an array of all intermediate accumulator values instead of just the final result. This is useful for tracking the progression of cumulative operations, such as running totals, partial sums, or step-by-step transformations. Each element in the result array represents the accumulator value after processing the corresponding input element.",
122    arguments: &[
123        &ArgumentInfo {
124            label: "array",
125            description: "Input array to scan",
126            type_hint: "Array",
127            optional: false,
128        },
129        &ArgumentInfo {
130            label: "initial",
131            description: "Initial accumulator value",
132            type_hint: "Any",
133            optional: false,
134        },
135        &ArgumentInfo {
136            label: "closure",
137            description: "Accumulator function (acc, element) => new_acc",
138            type_hint: "Closure",
139            optional: false,
140        },
141    ],
142    returns: "Array of intermediate accumulator values",
143    errors: "Returns error if closure cannot be applied or array is not provided",
144    categories: &["functional", "higher-order", "aggregation"],
145    examples: &[
146        "scan((1, 2, 3, 4, 5), 0, (acc, x) => acc + x) // Returns (0, 1, 3, 6, 10, 15)",
147        "scan((1, 2, 3), 1, (acc, x) => acc * x) // Returns (1, 1, 2, 6)",
148        "scan((\"a\", \"b\", \"c\"), \"\", (acc, x) => acc + x) // Returns (\"\", \"a\", \"ab\", \"abc\")",
149    ],
150};
151
152/// Documentation for the `some` function.
153///
154/// Tests if any element in the array satisfies the predicate.
155/// Returns true if at least one element matches the condition.
156pub const SOME_CARD: FunctionCard = FunctionCard {
157    identifier: "some",
158    signature: "some(array, closure)",
159    brief: "Check if any element satisfies the predicate.",
160    description: "The some function tests whether at least one element in the array satisfies a given predicate. It returns true as soon as the first matching element is found, making it efficient for existence checks. The predicate is a closure that takes an array element and returns a boolean value.",
161    arguments: &[
162        &ArgumentInfo {
163            label: "array",
164            description: "Input array to test",
165            type_hint: "Array",
166            optional: false,
167        },
168        &ArgumentInfo {
169            label: "closure",
170            description: "Predicate function to test each element",
171            type_hint: "Closure",
172            optional: false,
173        },
174    ],
175    returns: "Boolean indicating if any element satisfies the predicate",
176    errors: "Returns error if predicate cannot be evaluated or array is not provided",
177    categories: &["functional", "higher-order", "testing"],
178    examples: &[
179        "some((1, 2, 3, 4, 5), x => x > 4) // Returns true",
180        "some((1, 2, 3, 4, 5), x => x > 5) // Returns false",
181        "some((\"apple\", \"banana\", \"cherry\"), x => contains(x, \"z\")) // Returns false",
182    ],
183};
184
185/// Documentation for the `every` function.
186///
187/// Tests if all elements in the array satisfy the predicate.
188/// Returns true only if every element matches the condition.
189pub const EVERY_CARD: FunctionCard = FunctionCard {
190    identifier: "every",
191    signature: "every(array, closure)",
192    brief: "Check if all elements satisfy the predicate.",
193    description: "The every function tests whether all elements in the array satisfy a given predicate. It returns true only if every single element matches the condition. The function stops evaluation and returns false as soon as the first non-matching element is found, providing early termination for efficiency.",
194    arguments: &[
195        &ArgumentInfo {
196            label: "array",
197            description: "Input array to test",
198            type_hint: "Array",
199            optional: false,
200        },
201        &ArgumentInfo {
202            label: "closure",
203            description: "Predicate function to test each element",
204            type_hint: "Closure",
205            optional: false,
206        },
207    ],
208    returns: "Boolean indicating if all elements satisfy the predicate",
209    errors: "Returns error if predicate cannot be evaluated or array is not provided",
210    categories: &["functional", "higher-order", "testing"],
211    examples: &[
212        "every((1, 2, 3, 4, 5), x => x > 0) // Returns true",
213        "every((1, 2, 3, 4, 5), x => x > 1) // Returns false",
214        "every((\"apple\", \"banana\", \"cherry\"), x => length(x) > 3) // Returns true",
215    ],
216};
217
218/// Documentation for the `none` function.
219///
220/// Tests if no elements in the array satisfy the predicate.
221/// Returns true if zero elements match the condition.
222pub const NONE_CARD: FunctionCard = FunctionCard {
223    identifier: "none",
224    signature: "none(array, closure)",
225    brief: "Check if no elements satisfy the predicate.",
226    description: "The none function tests whether no elements in the array satisfy a given predicate. It returns true only if zero elements match the condition. This is the logical opposite of the some function. The function stops evaluation and returns false as soon as the first matching element is found.",
227    arguments: &[
228        &ArgumentInfo {
229            label: "array",
230            description: "Input array to test",
231            type_hint: "Array",
232            optional: false,
233        },
234        &ArgumentInfo {
235            label: "closure",
236            description: "Predicate function to test each element",
237            type_hint: "Closure",
238            optional: false,
239        },
240    ],
241    returns: "Boolean indicating if no elements satisfy the predicate",
242    errors: "Returns error if predicate cannot be evaluated or array is not provided",
243    categories: &["functional", "higher-order", "testing"],
244    examples: &[
245        "none((1, 2, 3, 4, 5), x => x > 5) // Returns true",
246        "none((1, 2, 3, 4, 5), x => x > 4) // Returns false",
247        "none((\"apple\", \"banana\", \"cherry\"), x => contains(x, \"z\")) // Returns true",
248    ],
249};
250
251/// Documentation for the `exactly_one` function.
252///
253/// Tests if exactly one element in the array satisfies the predicate.
254/// Returns true only if precisely one element matches the condition.
255pub const EXACTLY_ONE_CARD: FunctionCard = FunctionCard {
256    identifier: "exactly_one",
257    signature: "exactly_one(array, closure)",
258    brief: "Check if exactly one element satisfies the predicate.",
259    description: "The exactly_one function tests whether precisely one element in the array satisfies a given predicate. It returns true only if exactly one element matches the condition. The function stops evaluation and returns false as soon as a second matching element is found, providing early termination for efficiency.",
260    arguments: &[
261        &ArgumentInfo {
262            label: "array",
263            description: "Input array to test",
264            type_hint: "Array",
265            optional: false,
266        },
267        &ArgumentInfo {
268            label: "closure",
269            description: "Predicate function to test each element",
270            type_hint: "Closure",
271            optional: false,
272        },
273    ],
274    returns: "Boolean indicating if exactly one element satisfies the predicate",
275    errors: "Returns error if predicate cannot be evaluated or array is not provided",
276    categories: &["functional", "higher-order", "testing"],
277    examples: &[
278        "exactly_one((1, 2, 3, 4, 5), x => x == 3) // Returns true",
279        "exactly_one((1, 2, 3, 4, 5), x => x > 3) // Returns false",
280        "exactly_one((\"apple\", \"banana\", \"cherry\"), x => length(x) == 5) // Returns true",
281    ],
282};
283
284/// Documentation for the `select` function.
285///
286/// Like map but provides both element and index to the closure.
287/// Enables index-aware transformations of array elements.
288pub const SELECT_CARD: FunctionCard = FunctionCard {
289    identifier: "select",
290    signature: "select(array, closure)",
291    brief: "Transform array elements with access to their indices.",
292    description: "The select function is similar to map but provides both the array element and its index to the closure. This enables index-aware transformations where the position of elements matters. The closure receives the element as the first parameter and the zero-based index as the second parameter.",
293    arguments: &[
294        &ArgumentInfo {
295            label: "array",
296            description: "Input array to transform",
297            type_hint: "Array",
298            optional: false,
299        },
300        &ArgumentInfo {
301            label: "closure",
302            description: "Function to apply with element and index parameters",
303            type_hint: "Closure",
304            optional: false,
305        },
306    ],
307    returns: "Array containing transformed elements",
308    errors: "Returns error if closure cannot be applied or array is not provided",
309    categories: &["functional", "higher-order", "transformation"],
310    examples: &[
311        "select((\"a\", \"b\", \"c\"), (x, i) => x + string(i)) // Returns (\"a0\", \"b1\", \"c2\")",
312        "select((1, 2, 3), (x, i) => x * (i + 1)) // Returns (1, 4, 9)",
313        "select((\"first\", \"second\", \"third\"), (x, i) => i == 0 ? upper(x) : x) // Returns (\"FIRST\", \"second\", \"third\")",
314    ],
315};
316
317/// Array of all functional programming function cards.
318pub const FUNCTIONAL_CARDS: &[&FunctionCard] = &[
319    &MAP_CARD,
320    &FILTER_CARD,
321    &REDUCE_CARD,
322    &SCAN_CARD,
323    &SOME_CARD,
324    &EVERY_CARD,
325    &NONE_CARD,
326    &EXACTLY_ONE_CARD,
327    &SELECT_CARD,
328];