feat(core): higher-order sort + matching list replace via Applier seam (ADR-37)
public
feat(core): higher-order sort + matching list replace via Applier seam (ADR-37) Add an `Applier` trait to `function.rs` and implement it on `Evaluator`, so higher-order built-ins can invoke a function-valued argument through the same path as a direct FEEL call — including the recursion-depth guard (ADR-23) and lexical-scope binding. Thread `&dyn Applier` through `builtins::call` to: - `sort_fn(list, precedes)` — stable insertion sort (spec §10.3.4.9). `precedes(x, y)` must return `Boolean(true)` to keep `x` before `y`; a non-boolean result propagates as error-null. Closure arity guard rejects a non-2-param predicate before invoking. - `list_replace_fn(list, match, newItem)` — matching form (spec §10.3.4.4). When `args[1]` is `Value::Function`, replace each item for which `match(item, newItem)` is `true`; otherwise falls through to the existing positional form. Named `match:` overload routed in `reorder_named` via dual param-table logic. Also register `"sort"` in `is_builtin` / `call` / `builtin_param_names` (names: `list`, `precedes`), fix missing `Func::Builtin` arms in the three wrapper-crate function-value marshal paths (feel-wasm/feel-py/feel-lua — gap left by ADR-36), and add unit tests for ascending/descending sort, sort-by-field, stable order, named args, first-class `sort` value, error cases, 0092-017/018 closure-over-input patterns, and the full matching-form `list replace` path including named `match:` and arity errors. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| Changeset | f6096999698c9e47456b13908baf4e6e97d411e9 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 581403521397 |
| Children | |
| Pushlog | by timo via ssh |
modified crates/feel-core/src/builtins.rs +143 −24
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | use std::collections::BTreeMap; |
| 25 | 25 | |
| 26 | 26 | use crate::eval::{as_number, feel_order}; |
| 27 | +use crate::function::{Applier, Args, Func}; | |
| 27 | 28 | use crate::regex_provider::{match_value, FeelRegex}; |
| 28 | 29 | use crate::{EvalEnv, Value}; |
| 29 | 30 | |
| @@ -63,7 +64,7 @@ | ||
| 63 | 64 | "median" | "stddev" | "mode" | "all" | "any" | |
| 64 | 65 | "append" | "concatenate" | "union" | "remove" | "reverse" | |
| 65 | 66 | "flatten" | "sublist" | "list contains" | "index of" | |
| 66 | - "distinct values" | "insert before" | "list replace" | | |
| 67 | + "distinct values" | "insert before" | "list replace" | "sort" | | |
| 67 | 68 | // context functions |
| 68 | 69 | "get value" | "get entries" | "context" | "context put" | "context merge" | |
| 69 | 70 | // numeric functions |
| @@ -88,10 +89,19 @@ | ||
| 88 | 89 | /// Dispatch a single- or multi-word built-in call. Arguments are already |
| 89 | 90 | /// evaluated (but unforced). The `env` carries the regex provider (consulted only |
| 90 | 91 | /// by `matches`/`replace`/`split`) and the instant `now()`/`today()` read (ADR-18). |
| 92 | +/// `applier` is the evaluator's function-apply capability, threaded in so that | |
| 93 | +/// higher-order built-ins (`sort`, matching `list replace`) can invoke a | |
| 94 | +/// function-valued argument through the same call path as a direct FEEL call | |
| 95 | +/// — including the recursion-depth guard (ADR-37). | |
| 91 | 96 | /// |
| 92 | 97 | /// Returns `None` for a name this module does not implement, so the caller can fall |
| 93 | 98 | /// through to a host function (ADR-23); every recognised built-in returns `Some`. |
| 94 | -pub(crate) fn call(name: &str, args: crate::function::Args, env: &EvalEnv) -> Option<Value> { | |
| 99 | +pub(crate) fn call( | |
| 100 | + name: &str, | |
| 101 | + args: crate::function::Args, | |
| 102 | + env: &EvalEnv, | |
| 103 | + applier: &dyn Applier, | |
| 104 | +) -> Option<Value> { | |
| 95 | 105 | // Named arguments must be placed into this built-in's positional parameter |
| 96 | 106 | // order before dispatch (FEEL spec §10.3.2.13.5). That reordering needs a |
| 97 | 107 | // per-built-in parameter-name table (the Sonnet-Teilaufgabe in the plan); see |
| @@ -157,7 +167,8 @@ | ||
| 157 | 167 | "index of" => index_of_fn(args), |
| 158 | 168 | "distinct values" => distinct_values_fn(args), |
| 159 | 169 | "insert before" => insert_before_fn(args), |
| 160 | - "list replace" => list_replace_fn(args), | |
| 170 | + "list replace" => list_replace_fn(args, applier), | |
| 171 | + "sort" => sort_fn(args, applier), | |
| 161 | 172 | |
| 162 | 173 | // --- context --- |
| 163 | 174 | "get value" => get_value_fn(args), |
| @@ -289,7 +300,13 @@ | ||
| 289 | 300 | "index of" => &["list", "match"], |
| 290 | 301 | "distinct values" => &["list"], |
| 291 | 302 | "insert before" => &["list", "position", "newItem"], |
| 303 | + // `list replace` has two 3-parameter overloads: positional (`position`) and | |
| 304 | + // matching-function (`match`). `reorder_named` selects the right table based | |
| 305 | + // on which slot-1 key appears in the named arguments (see its implementation). | |
| 306 | + // This entry covers the positional overload; the matching overload is handled | |
| 307 | + // directly in `reorder_named`. | |
| 292 | 308 | "list replace" => &["list", "position", "newItem"], |
| 309 | + "sort" => &["list", "precedes"], | |
| 293 | 310 | |
| 294 | 311 | // --- context functions (spec §10.3.4.10) --- |
| 295 | 312 | "get value" => &["m", "key"], |
| @@ -336,7 +353,19 @@ | ||
| 336 | 353 | /// `None` propagates as an error-null at the call site (the caller tries the host |
| 337 | 354 | /// function registry next; an unrecognised name still falls through correctly). |
| 338 | 355 | fn reorder_named(name: &str, args: crate::function::Args) -> Option<Vec<Value>> { |
| 339 | - let params = builtin_param_names(name)?; | |
| 356 | + // `list replace` has two 3-parameter overloads with different slot-1 names: | |
| 357 | + // position form: (list, position, newItem) — spec Table 72 §10.3.4.4 | |
| 358 | + // match form: (list, match, newItem) — spec Table 72, function predicate | |
| 359 | + // Route to the right table based on which slot-1 key appears in the named args. | |
| 360 | + let params: &[&str] = if name == "list replace" { | |
| 361 | + if args.named.iter().any(|(k, _)| k == "match") { | |
| 362 | + &["list", "match", "newItem"] | |
| 363 | + } else { | |
| 364 | + &["list", "position", "newItem"] | |
| 365 | + } | |
| 366 | + } else { | |
| 367 | + builtin_param_names(name)? | |
| 368 | + }; | |
| 340 | 369 | |
| 341 | 370 | let crate::function::Args { positional, named } = args; |
| 342 | 371 | |
| @@ -1656,31 +1685,121 @@ | ||
| 1656 | 1685 | } |
| 1657 | 1686 | } |
| 1658 | 1687 | |
| 1659 | -/// `list replace(list, position, newItem)` — the positional form only; the | |
| 1660 | -/// matching-function form needs function values and stays deferred (ADR-15). A | |
| 1661 | -/// scalar `list` is treated as a one-element list (per DMN-TCK 1155). | |
| 1662 | -fn list_replace_fn(args: Vec<Value>) -> Value { | |
| 1663 | - let [list_v, pos_v, new_item] = args.as_slice() else { | |
| 1688 | +/// `list replace(list, match, newItem)` — matching form: replace each element for | |
| 1689 | +/// which `match(item, newItem)` returns `true` (spec §10.3.4.4 Table 72). | |
| 1690 | +/// `list replace(list, position, newItem)` — positional form: replace the element | |
| 1691 | +/// at the 1-based `position` (supports negative indexing). Dispatches on whether | |
| 1692 | +/// `match`/`position` is a `Value::Function` (matching form) or not (positional). | |
| 1693 | +/// A scalar `list` is treated as a one-element list (per DMN-TCK 1155, ADR-37). | |
| 1694 | +fn list_replace_fn(args: Vec<Value>, applier: &dyn Applier) -> Value { | |
| 1695 | + if args.len() != 3 { | |
| 1696 | + return Value::Error; | |
| 1697 | + } | |
| 1698 | + if let Value::Function(f) = &args[1] { | |
| 1699 | + // Matching form: replace items where match(item, newItem) is Boolean(true). | |
| 1700 | + let f = f.clone(); | |
| 1701 | + // Arity guard: a closure with a parameter count other than 2 is not a valid | |
| 1702 | + // (item, newItem) predicate (spec §10.3.4.4); reject before invoking (ADR-37). | |
| 1703 | + if let Func::Closure(c) = f.as_ref() { | |
| 1704 | + if c.params.len() != 2 { | |
| 1705 | + return Value::Error; | |
| 1706 | + } | |
| 1707 | + } | |
| 1708 | + let new_item = args[2].clone(); | |
| 1709 | + let mut items = match &args[0] { | |
| 1710 | + Value::List(items) => items.clone(), | |
| 1711 | + Value::Null => return Value::Null, | |
| 1712 | + other => vec![other.clone()], | |
| 1713 | + }; | |
| 1714 | + for item in &mut items { | |
| 1715 | + match applier.apply( | |
| 1716 | + &f, | |
| 1717 | + Args { positional: vec![item.clone(), new_item.clone()], named: vec![] }, | |
| 1718 | + ) { | |
| 1719 | + Value::Boolean(true) => *item = new_item.clone(), | |
| 1720 | + Value::Boolean(false) => {} // keep item unchanged | |
| 1721 | + _ => return Value::Error, // null, non-boolean, or error-null | |
| 1722 | + } | |
| 1723 | + } | |
| 1724 | + Value::List(items) | |
| 1725 | + } else { | |
| 1726 | + // Positional form: replace the element at the 1-based position. | |
| 1727 | + let [list_v, pos_v, new_item] = args.as_slice() else { | |
| 1728 | + return Value::Error; | |
| 1729 | + }; | |
| 1730 | + let mut items = match list_v { | |
| 1731 | + Value::List(items) => items.clone(), | |
| 1732 | + Value::Null => return Value::Null, | |
| 1733 | + other => vec![other.clone()], | |
| 1734 | + }; | |
| 1735 | + let pos = match num_arg(pos_v) { | |
| 1736 | + NumArg::Num(p) => p.trunc() as i64, | |
| 1737 | + NumArg::Null => return Value::Null, | |
| 1738 | + NumArg::Err => return Value::Error, | |
| 1739 | + }; | |
| 1740 | + let n = items.len() as i64; | |
| 1741 | + match resolve_index(pos, n) { | |
| 1742 | + Some(idx) if idx < n => { | |
| 1743 | + items[idx as usize] = new_item.clone(); | |
| 1744 | + Value::List(items) | |
| 1745 | + } | |
| 1746 | + _ => Value::Error, | |
| 1747 | + } | |
| 1748 | + } | |
| 1749 | +} | |
| 1750 | + | |
| 1751 | +/// `sort(list, precedes)` — stable sort using a FEEL function as the strict | |
| 1752 | +/// ordering predicate (spec §10.3.4.9). `precedes(x, y)` must return | |
| 1753 | +/// `Boolean(true)` when `x` should come before `y` and `Boolean(false)` when it | |
| 1754 | +/// should not; any other result (null, non-boolean, error-null) makes the whole | |
| 1755 | +/// call an error-null. Implemented as a stable insertion sort — O(n²) but | |
| 1756 | +/// correct and naturally stable for the small lists typical in DMN rules. The | |
| 1757 | +/// comparator is invoked through `applier` so closures and built-in refs both | |
| 1758 | +/// work and the recursion-depth guard is respected (ADR-37). | |
| 1759 | +fn sort_fn(args: Vec<Value>, applier: &dyn Applier) -> Value { | |
| 1760 | + let [list_v, precedes_v] = args.as_slice() else { | |
| 1664 | 1761 | return Value::Error; |
| 1665 | 1762 | }; |
| 1666 | 1763 | let mut items = match list_v { |
| 1667 | 1764 | Value::List(items) => items.clone(), |
| 1668 | 1765 | Value::Null => return Value::Null, |
| 1669 | - other => vec![other.clone()], | |
| 1670 | - }; | |
| 1671 | - let pos = match num_arg(pos_v) { | |
| 1672 | - NumArg::Num(p) => p.trunc() as i64, | |
| 1673 | - NumArg::Null => return Value::Null, | |
| 1674 | - NumArg::Err => return Value::Error, | |
| 1675 | - }; | |
| 1676 | - let n = items.len() as i64; | |
| 1677 | - match resolve_index(pos, n) { | |
| 1678 | - Some(idx) if idx < n => { | |
| 1679 | - items[idx as usize] = new_item.clone(); | |
| 1680 | - Value::List(items) | |
| 1681 | - } | |
| 1682 | - _ => Value::Error, | |
| 1683 | - } | |
| 1766 | + _ => return Value::Error, | |
| 1767 | + }; | |
| 1768 | + let precedes_fn = match precedes_v { | |
| 1769 | + Value::Function(f) => { | |
| 1770 | + // Arity guard: a closure with a parameter count other than 2 cannot be a | |
| 1771 | + // valid (x, y) strict-ordering predicate (spec §10.3.4.9); ADR-37. | |
| 1772 | + if let Func::Closure(c) = f.as_ref() { | |
| 1773 | + if c.params.len() != 2 { | |
| 1774 | + return Value::Error; | |
| 1775 | + } | |
| 1776 | + } | |
| 1777 | + f.clone() | |
| 1778 | + } | |
| 1779 | + _ => return Value::Error, // second argument is not a function value | |
| 1780 | + }; | |
| 1781 | + // Stable insertion sort: move items[i] left while precedes(items[i], items[i-1]). | |
| 1782 | + // Cloning before each comparison is safe because the `swap` does not invalidate | |
| 1783 | + // the borrow (we clone first, then swap). | |
| 1784 | + for i in 1..items.len() { | |
| 1785 | + let mut j = i; | |
| 1786 | + while j > 0 { | |
| 1787 | + let key = items[j].clone(); | |
| 1788 | + let neighbor = items[j - 1].clone(); | |
| 1789 | + match applier.apply( | |
| 1790 | + &*precedes_fn, | |
| 1791 | + Args { positional: vec![key, neighbor], named: vec![] }, | |
| 1792 | + ) { | |
| 1793 | + Value::Boolean(true) => { | |
| 1794 | + items.swap(j, j - 1); | |
| 1795 | + j -= 1; | |
| 1796 | + } | |
| 1797 | + Value::Boolean(false) => break, // key is in place, move to next element | |
| 1798 | + _ => return Value::Error, // null / non-boolean / error-null | |
| 1799 | + } | |
| 1800 | + } | |
| 1801 | + } | |
| 1802 | + Value::List(items) | |
| 1684 | 1803 | } |
| 1685 | 1804 | |
| 1686 | 1805 | // --------------------------------------------------------------------------- // |
modified crates/feel-core/src/eval.rs +15 −3
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | |
| 15 | 15 | use crate::ast::{ArithOp, CallArgs, CmpOp, Expr, FeelType, IterSource, Quantifier, UnaryTest}; |
| 16 | 16 | use crate::builtins::resolve_index; |
| 17 | -use crate::function::{Args, Closure, Func}; | |
| 17 | +use crate::function::{Applier, Args, Closure, Func}; | |
| 18 | 18 | use crate::{temporal, Context, EvalEnv, FeelError, Value}; |
| 19 | 19 | |
| 20 | 20 | type EvalResult = Result<Value, FeelError>; |
| @@ -256,7 +256,7 @@ | ||
| 256 | 256 | // 2. A built-in. `builtins::call` returns `None` for a name it does not |
| 257 | 257 | // implement, so the call can fall through to the host registry. |
| 258 | 258 | let evaluated = self.eval_args(args, ctx)?; |
| 259 | - if let Some(result) = crate::builtins::call(name, evaluated, self.env) { | |
| 259 | + if let Some(result) = crate::builtins::call(name, evaluated, self.env, self) { | |
| 260 | 260 | return Ok(result); |
| 261 | 261 | } |
| 262 | 262 | // 3. An ambient host function (the arguments were consumed by the built-in |
| @@ -294,7 +294,7 @@ | ||
| 294 | 294 | // table. `call` returns `None` only for names not in the table — which |
| 295 | 295 | // cannot happen here (the name was validated by `is_builtin` when the |
| 296 | 296 | // value was created) — so `unwrap_or(Error)` is a safety net (ADR-36). |
| 297 | - Func::Builtin(name) => Ok(crate::builtins::call(name, args, self.env) | |
| 297 | + Func::Builtin(name) => Ok(crate::builtins::call(name, args, self.env, self) | |
| 298 | 298 | .unwrap_or(Value::Error)), |
| 299 | 299 | } |
| 300 | 300 | } |
| @@ -319,6 +319,18 @@ | ||
| 319 | 319 | let result = self.eval(&closure.body, &scope); |
| 320 | 320 | self.call_depth.set(depth); |
| 321 | 321 | result |
| 322 | + } | |
| 323 | +} | |
| 324 | + | |
| 325 | +/// Wire up the `Applier` seam so higher-order built-ins can invoke a | |
| 326 | +/// function-valued argument through the same path as a direct FEEL call — | |
| 327 | +/// including the recursion-depth guard (ADR-37). | |
| 328 | +impl Applier for Evaluator<'_> { | |
| 329 | + fn apply(&self, f: &Func, args: Args) -> Value { | |
| 330 | + // `invoke` returns `EvalResult = Result<Value, FeelError>`. A genuine | |
| 331 | + // `FeelError` (parse failure) cannot occur here — we are already inside | |
| 332 | + // the evaluator — so `unwrap_or(Error)` is purely a type-level safety net. | |
| 333 | + self.invoke(f, args).unwrap_or(Value::Error) | |
| 322 | 334 | } |
| 323 | 335 | } |
| 324 | 336 | |
modified crates/feel-core/src/function.rs +15 −0
| @@ -29,6 +29,21 @@ | ||
| 29 | 29 | pub struct Args { |
| 30 | 30 | pub positional: Vec<Value>, |
| 31 | 31 | pub named: Vec<(String, Value)>, |
| 32 | +} | |
| 33 | + | |
| 34 | +/// Apply capability threaded into higher-order built-ins (ADR-37). Implemented by | |
| 35 | +/// `Evaluator` so `sort`, `list replace` (matching form), and any future higher-order | |
| 36 | +/// built-in can invoke a function-valued argument through the same path as a direct | |
| 37 | +/// call — including the recursion-depth guard and lexical-scope binding. | |
| 38 | +/// | |
| 39 | +/// `pub(crate)` mirrors [`ForeignFunction`]: only the core crate wires up the | |
| 40 | +/// implementation; host crates never need to touch it. | |
| 41 | +pub(crate) trait Applier { | |
| 42 | + /// Invoke `f` with `args` exactly as a direct FEEL call would. A `FeelError` | |
| 43 | + /// (parse failure — not reachable here) or any other error-path collapses to | |
| 44 | + /// `Value::Error` (error-null, ADR-14), because built-ins return `Value`, not | |
| 45 | + /// `Result<Value, FeelError>`. | |
| 46 | + fn apply(&self, f: &Func, args: Args) -> Value; | |
| 32 | 47 | } |
| 33 | 48 | |
| 34 | 49 | /// A host callable resolved lazily, implemented per target (feel-py's `PyForeignFn`, |
modified crates/feel-core/src/lib.rs +144 −0
| @@ -2416,6 +2416,150 @@ | ||
| 2416 | 2416 | ); |
| 2417 | 2417 | } |
| 2418 | 2418 | |
| 2419 | + // --- higher-order built-ins: sort + matching list replace (ADR-37) -------- // | |
| 2420 | + | |
| 2421 | + fn num(v: f64) -> Value { | |
| 2422 | + Value::Number(v) | |
| 2423 | + } | |
| 2424 | + fn str_val(v: &str) -> Value { | |
| 2425 | + Value::String(v.into()) | |
| 2426 | + } | |
| 2427 | + | |
| 2428 | + #[test] | |
| 2429 | + fn sort_ascending_descending() { | |
| 2430 | + // Numeric ascending. | |
| 2431 | + assert_eq!( | |
| 2432 | + ev("sort([3,1,4,5,2], function(x,y) x<y)"), | |
| 2433 | + Value::List(vec![num(1.0), num(2.0), num(3.0), num(4.0), num(5.0)]) | |
| 2434 | + ); | |
| 2435 | + // Numeric descending (0013-sort sort1: sort(listA, function(x,y) x>y)). | |
| 2436 | + assert_eq!( | |
| 2437 | + ev("sort([3,1,5,4], function(x,y) x>y)"), | |
| 2438 | + Value::List(vec![num(5.0), num(4.0), num(3.0), num(1.0)]) | |
| 2439 | + ); | |
| 2440 | + // String ascending (basic latin). | |
| 2441 | + assert_eq!( | |
| 2442 | + ev(r#"sort(["c","a","b"], function(x,y) x<y)"#), | |
| 2443 | + Value::List(vec![str_val("a"), str_val("b"), str_val("c")]) | |
| 2444 | + ); | |
| 2445 | + // Empty list. | |
| 2446 | + assert_eq!(ev("sort([], function(x,y) x<y)"), Value::List(vec![])); | |
| 2447 | + // Single-element list. | |
| 2448 | + assert_eq!( | |
| 2449 | + ev("sort([42], function(x,y) x<y)"), | |
| 2450 | + Value::List(vec![num(42.0)]) | |
| 2451 | + ); | |
| 2452 | + } | |
| 2453 | + | |
| 2454 | + #[test] | |
| 2455 | + fn sort_by_context_field() { | |
| 2456 | + // Sort contexts by a numeric field — mirrors 0013-sort sort2. | |
| 2457 | + assert_eq!( | |
| 2458 | + ev("{t: sort([{v:3},{v:1},{v:2}], function(x,y) x.v < y.v), r: t[1].v}.r"), | |
| 2459 | + num(1.0) | |
| 2460 | + ); | |
| 2461 | + } | |
| 2462 | + | |
| 2463 | + #[test] | |
| 2464 | + fn sort_stable() { | |
| 2465 | + // Elements comparing equal (precedes returns false both ways) must stay in | |
| 2466 | + // their original relative order (stable sort). | |
| 2467 | + assert_eq!( | |
| 2468 | + ev("sort([1,1,2], function(x,y) x<y)"), | |
| 2469 | + Value::List(vec![num(1.0), num(1.0), num(2.0)]) | |
| 2470 | + ); | |
| 2471 | + } | |
| 2472 | + | |
| 2473 | + #[test] | |
| 2474 | + fn sort_named_args() { | |
| 2475 | + // Named-argument form: sort(list: …, precedes: …). | |
| 2476 | + assert_eq!( | |
| 2477 | + ev("sort(list: [3,1,2], precedes: function(x,y) x<y)"), | |
| 2478 | + Value::List(vec![num(1.0), num(2.0), num(3.0)]) | |
| 2479 | + ); | |
| 2480 | + } | |
| 2481 | + | |
| 2482 | + #[test] | |
| 2483 | + fn sort_as_first_class_builtin() { | |
| 2484 | + // `sort` can be referenced as a first-class value and passed around. | |
| 2485 | + assert!(matches!(ev("sort"), Value::Function(_))); | |
| 2486 | + } | |
| 2487 | + | |
| 2488 | + #[test] | |
| 2489 | + fn sort_error_cases() { | |
| 2490 | + // Null list -> null (value-null from sort_fn). | |
| 2491 | + assert_eq!(ev("sort(null, function(x,y) x<y)"), Value::Null); | |
| 2492 | + // Non-list first arg -> error-null -> null (lenient). | |
| 2493 | + assert_eq!(ev("sort(1, function(x,y) x<y)"), Value::Null); | |
| 2494 | + // Non-function second arg -> error-null -> null. | |
| 2495 | + assert_eq!(ev("sort([1,2], 3)"), Value::Null); | |
| 2496 | + // Closure with wrong arity (1 param instead of 2) -> error-null -> null. | |
| 2497 | + assert_eq!(ev("sort([1,2], function(x) x)"), Value::Null); | |
| 2498 | + // Comparator returning non-boolean -> error-null -> null. | |
| 2499 | + assert_eq!(ev("sort([1,2], function(x,y) null)"), Value::Null); | |
| 2500 | + } | |
| 2501 | + | |
| 2502 | + #[test] | |
| 2503 | + fn sort_higher_order_closure_over_input() { | |
| 2504 | + // 0092-017: comparator closes over an input value. | |
| 2505 | + assert_eq!( | |
| 2506 | + ev(r#"{input_017_1: "a", bkm: function (fn1) sort(["a","z","a","z"], fn1), r: bkm(function (a: string, b: string) if a = input_017_1 then true else false)}.r"#), | |
| 2507 | + Value::List(vec![str_val("a"), str_val("a"), str_val("z"), str_val("z")]) | |
| 2508 | + ); | |
| 2509 | + // 0092-018: BKM passed as the comparator. | |
| 2510 | + assert_eq!( | |
| 2511 | + ev(r#"{bkm_018: function (p1, p2) if p1 = "a" then true else false, r: sort(["a","z","a","z"], bkm_018)}.r"#), | |
| 2512 | + Value::List(vec![str_val("a"), str_val("a"), str_val("z"), str_val("z")]) | |
| 2513 | + ); | |
| 2514 | + } | |
| 2515 | + | |
| 2516 | + #[test] | |
| 2517 | + fn list_replace_matching_form() { | |
| 2518 | + // Replace items where match(item, newItem) returns true. | |
| 2519 | + assert_eq!( | |
| 2520 | + ev("list replace([2,4,7,8], function(item,newItem) item<newItem, 5)"), | |
| 2521 | + Value::List(vec![num(5.0), num(5.0), num(7.0), num(8.0)]) | |
| 2522 | + ); | |
| 2523 | + // All items match. | |
| 2524 | + assert_eq!( | |
| 2525 | + ev("list replace([1,2,3], function(item,newItem) true, 5)"), | |
| 2526 | + Value::List(vec![num(5.0), num(5.0), num(5.0)]) | |
| 2527 | + ); | |
| 2528 | + // No items match. | |
| 2529 | + assert_eq!( | |
| 2530 | + ev("list replace([1,2,3], function(item,newItem) false, 5)"), | |
| 2531 | + Value::List(vec![num(1.0), num(2.0), num(3.0)]) | |
| 2532 | + ); | |
| 2533 | + } | |
| 2534 | + | |
| 2535 | + #[test] | |
| 2536 | + fn list_replace_matching_named_args() { | |
| 2537 | + // Named `match:` overload (1155 test 117). | |
| 2538 | + assert_eq!( | |
| 2539 | + ev("list replace(match: function(item, newItem) item = 2, newItem: 4, list: [1,2,3])"), | |
| 2540 | + Value::List(vec![num(1.0), num(4.0), num(3.0)]) | |
| 2541 | + ); | |
| 2542 | + } | |
| 2543 | + | |
| 2544 | + #[test] | |
| 2545 | + fn list_replace_matching_error_cases() { | |
| 2546 | + // Comparator returning a non-boolean value -> error-null -> null (1155 test 165). | |
| 2547 | + assert_eq!( | |
| 2548 | + ev("list replace([2,4], function(item,newItem) item, 5)"), | |
| 2549 | + Value::Null | |
| 2550 | + ); | |
| 2551 | + // Closure with 3 params instead of 2 -> error-null -> null (1155 test 149). | |
| 2552 | + assert_eq!( | |
| 2553 | + ev("list replace([2,4], function(item,newItem,extra) item=2, 5)"), | |
| 2554 | + Value::Null | |
| 2555 | + ); | |
| 2556 | + // Closure with 1 param instead of 2 -> error-null -> null (1155 test 157). | |
| 2557 | + assert_eq!( | |
| 2558 | + ev("list replace([2,4], function(item) item=2, 5)"), | |
| 2559 | + Value::Null | |
| 2560 | + ); | |
| 2561 | + } | |
| 2562 | + | |
| 2419 | 2563 | // --- host callables (ForeignFunction) ------------------------------------ // |
| 2420 | 2564 | |
| 2421 | 2565 | /// A host function that sums all of its numeric arguments (positional + named), |
modified crates/feel-lua/src/lib.rs +1 −1
| @@ -412,7 +412,7 @@ | ||
| 412 | 412 | Some(lf) => LuaValue::Function(lf.func.clone()), |
| 413 | 413 | None => LuaValue::Nil, |
| 414 | 414 | }, |
| 415 | - Func::Closure(_) => LuaValue::Nil, | |
| 415 | + Func::Closure(_) | Func::Builtin(_) => LuaValue::Nil, | |
| 416 | 416 | }, |
| 417 | 417 | // An opaque comparison-test value `(< 10)` (ADR-32) has no native Lua type; |
| 418 | 418 | // like a native FEEL closure it crosses out as `nil`. |
modified crates/feel-py/src/lib.rs +1 −1
| @@ -541,7 +541,7 @@ | ||
| 541 | 541 | Some(pf) => pf.obj.clone_ref(py).into_bound(py), |
| 542 | 542 | None => py.None().into_bound(py), |
| 543 | 543 | }, |
| 544 | - Func::Closure(_) => py.None().into_bound(py), | |
| 544 | + Func::Closure(_) | Func::Builtin(_) => py.None().into_bound(py), | |
| 545 | 545 | }, |
| 546 | 546 | // An opaque comparison-test value `(< 10)` (ADR-32) has no native Python type; |
| 547 | 547 | // like a native FEEL closure it crosses out as `None`. |
modified crates/feel-wasm/src/lib.rs +1 −1
| @@ -184,7 +184,7 @@ | ||
| 184 | 184 | Some(jf) => Ok(jf.obj.clone()), |
| 185 | 185 | None => Ok(JsValue::NULL), |
| 186 | 186 | }, |
| 187 | - Func::Closure(_) => Ok(JsValue::NULL), | |
| 187 | + Func::Closure(_) | Func::Builtin(_) => Ok(JsValue::NULL), | |
| 188 | 188 | }, |
| 189 | 189 | // Scalars (number/bool/string/null) go through the JSON-compatible path so |
| 190 | 190 | // integral numbers stay JS numbers (`1 + 1` -> `2`, not `2.0`). |