expr-lang 2.0.0

Implementation of expr language in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::{bail, Environment, Value};

fn add_sum(total: Value, value: Value) -> crate::Result<Value> {
    match (total, value) {
        (Value::Integer(total), Value::Integer(value)) => {
            Ok(Value::Integer(total.wrapping_add(value)))
        }
        (Value::Integer(total), Value::Float(value)) => Ok(Value::Float(total as f64 + value)),
        (Value::Float(total), Value::Integer(value)) => Ok(Value::Float(total + value as f64)),
        (Value::Float(total), Value::Float(value)) => Ok(Value::Float(total + value)),
        _ => bail!("sum() values must be numbers"),
    }
}

pub fn add_array_functions(env: &mut Environment) {
    env.add_function("count", |c| {
        if c.args.len() != 1 {
            bail!("count() takes exactly one array argument");
        }
        let Value::Array(values) = &c.args[0] else {
            bail!("count() takes an array as the first argument");
        };
        let mut count = 0;
        if let Some(predicate) = c.predicate {
            for value in values {
                match c
                    .env
                    .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    Value::Bool(true) => count += 1,
                    Value::Bool(false) => {}
                    _ => bail!("count() predicate must return a boolean"),
                }
            }
        } else {
            for value in values {
                match value {
                    Value::Bool(true) => count += 1,
                    Value::Bool(false) => {}
                    _ => bail!("count() without a predicate requires booleans"),
                }
            }
        }
        Ok(Value::Integer(count))
    });

    env.add_function("sum", |c| {
        if c.args.len() != 1 {
            bail!("sum() takes exactly one array argument");
        }
        let Value::Array(values) = &c.args[0] else {
            bail!("sum() takes an array as the first argument");
        };
        let mut total = Value::Integer(0);
        for value in values {
            let value = if let Some(predicate) = &c.predicate {
                c.env
                    .run_with_binding(predicate, c.ctx, "#", value.clone())?
            } else {
                value.clone()
            };
            total = add_sum(total, value)?;
        }
        Ok(total)
    });

    env.add_function("reduce", |c| {
        if c.args.is_empty() || c.args.len() > 2 {
            bail!("reduce() takes an array and optional initial value");
        }
        let Value::Array(values) = &c.args[0] else {
            bail!("reduce() takes an array as the first argument");
        };
        let Some(predicate) = c.predicate else {
            bail!("reduce() requires a predicate");
        };
        let (mut accumulator, start) = match c.args.get(1) {
            Some(initial) => (initial.clone(), 0),
            None => match values.first() {
                Some(first) => (first.clone(), 1),
                None => bail!("reduce() of an empty array requires an initial value"),
            },
        };
        for (index, value) in values.iter().enumerate().skip(start) {
            accumulator = c.env.run_with_bindings(
                predicate,
                c.ctx,
                [
                    ("#", value.clone()),
                    ("#acc", accumulator),
                    ("#index", index.into()),
                ],
            )?;
        }
        Ok(accumulator)
    });

    env.add_function("all", |c| {
        if c.args.len() != 1 {
            bail!("all() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for value in a {
                if let Value::Bool(false) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(false.into());
                }
            }
            Ok(true.into())
        } else {
            bail!("all() takes an array as the first argument");
        }
    });

    env.add_function("any", |c| {
        if c.args.len() != 1 {
            bail!("any() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for value in a {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(true.into());
                }
            }
            Ok(false.into())
        } else {
            bail!("any() takes an array as the first argument");
        }
    });

    env.add_function("one", |c| {
        if c.args.len() != 1 {
            bail!("one() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            let mut found = false;
            for value in a {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    if found {
                        return Ok(false.into());
                    }
                    found = true;
                }
            }
            Ok(found.into())
        } else {
            bail!("one() takes an array as the first argument");
        }
    });

    env.add_function("none", |c| {
        if c.args.len() != 1 {
            bail!("none() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for value in a {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(false.into());
                }
            }
            Ok(true.into())
        } else {
            bail!("none() takes an array as the first argument");
        }
    });

    env.add_function("map", |c| {
        let mut result = Vec::new();
        if c.args.len() != 1 {
            bail!("map() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for (index, value) in a.iter().enumerate() {
                result.push(c.env.run_with_bindings(
                    predicate,
                    c.ctx,
                    [("#", value.clone()), ("#index", index.into())],
                )?);
            }
        } else {
            bail!("map() takes an array as the first argument");
        }
        Ok(result.into())
    });

    env.add_function("filter", |c| {
        let mut result = Vec::new();
        if c.args.len() != 1 {
            bail!("filter() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for value in a {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    result.push(value.clone());
                }
            }
        } else {
            bail!("filter() takes an array as the first argument");
        }
        Ok(result.into())
    });

    env.add_function("find", |c| {
        if c.args.len() != 1 {
            bail!("find() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for value in a {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(value.clone());
                }
            }
            Ok(Value::Nil)
        } else {
            bail!("find() takes an array as the first argument");
        }
    });

    env.add_function("findIndex", |c| {
        if c.args.len() != 1 {
            bail!("findIndex() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for (i, value) in a.iter().enumerate() {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(i.into());
                }
            }
            Ok(Value::Integer(-1))
        } else {
            bail!("findIndex() takes an array as the first argument");
        }
    });

    env.add_function("findLast", |c| {
        if c.args.len() != 1 {
            bail!("findLast() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for value in a.iter().rev() {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(value.clone());
                }
            }
            Ok(Value::Nil)
        } else {
            bail!("findLast() takes an array as the first argument");
        }
    });

    env.add_function("findLastIndex", |c| {
        if c.args.len() != 1 {
            bail!("findLastIndex() takes exactly one argument and a predicate");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            for (i, value) in a.iter().enumerate().rev() {
                if let Value::Bool(true) =
                    c.env
                        .run_with_binding(predicate, c.ctx, "#", value.clone())?
                {
                    return Ok(i.into());
                }
            }
            Ok(Value::Integer(-1))
        } else {
            bail!("findLastIndex() takes an array as the first argument");
        }
    });
    env.add_function("groupBy", |c| {
        if c.args.len() != 1 {
            bail!("groupBy() takes exactly two arguments");
        }
        if let (Value::Array(a), Some(predicate)) = (&c.args[0], c.predicate) {
            let mut groups: Vec<(Value, Vec<Value>)> = Vec::new();
            for value in a {
                let key = c
                    .env
                    .run_with_binding(predicate, c.ctx, "#", value.clone())?;
                if !crate::ast::operator::is_comparable_map_key(&key) {
                    bail!("groupBy() predicate returned a non-comparable key");
                }
                if let Some((_, group)) = groups.iter_mut().find(|(candidate, _)| {
                    crate::ast::operator::map_keys_equal(candidate, &key)
                }) {
                    group.push(value.clone());
                } else {
                    groups.push((key, vec![value.clone()]));
                }
            }
            Ok(Value::KeyedMap(
                groups
                    .into_iter()
                    .map(|(key, group)| (key, Value::Array(group)))
                    .collect(),
            ))
        } else {
            bail!("groupBy() takes an array as the first argument and a predicate as the second argument");
        }
    });

    env.add_function("sort", |c| {
        if c.args.is_empty() || c.args.len() > 2 {
            bail!("sort() takes one or two arguments");
        }
        let Value::Array(a) = &c.args[0] else {
            bail!("sort() takes an array as the first argument");
        };
        let desc = if c.args.len() == 2 {
            match &c.args[1] {
                Value::String(s) if s == "desc" => true,
                Value::String(s) if s == "asc" => false,
                _ => bail!("sort() second argument must be \"asc\" or \"desc\""),
            }
        } else {
            false
        };
        if let Err(error) = validate_sort_values(a.iter()) {
            bail!("sort() {error}");
        }
        let mut result = a.clone();
        result.sort_by(|a, b| {
            let cmp = compare_sort_values(a, b);
            if desc {
                cmp.reverse()
            } else {
                cmp
            }
        });
        Ok(result.into())
    });

    env.add_function("sortBy", |c| {
        if c.args.is_empty() || c.args.len() > 2 {
            bail!("sortBy() takes one or two arguments and a predicate");
        }
        let Value::Array(a) = &c.args[0] else {
            bail!("sortBy() takes an array as the first argument");
        };
        let Some(predicate) = c.predicate else {
            bail!("sortBy() requires a predicate");
        };
        let desc = if c.args.len() == 2 {
            match &c.args[1] {
                Value::String(s) if s == "desc" => true,
                Value::String(s) if s == "asc" => false,
                _ => bail!("sortBy() second argument must be \"asc\" or \"desc\""),
            }
        } else {
            false
        };
        // Compute keys for each element
        let mut keyed: Vec<(Value, Value)> = Vec::new();
        for value in a {
            let key = c
                .env
                .run_with_binding(predicate, c.ctx, "#", value.clone())?;
            keyed.push((key, value.clone()));
        }
        if let Err(error) = validate_sort_values(keyed.iter().map(|(key, _)| key)) {
            bail!("sortBy() {error}");
        }
        keyed.sort_by(|(a, _), (b, _)| {
            let cmp = compare_sort_values(a, b);
            if desc {
                cmp.reverse()
            } else {
                cmp
            }
        });
        Ok(keyed.into_iter().map(|(_, v)| v).collect::<Vec<_>>().into())
    });
}

fn validate_sort_values<'a>(values: impl Iterator<Item = &'a Value>) -> Result<(), String> {
    let mut kind = None;
    for value in values {
        let value_kind = match value {
            Value::Integer(_) => 0,
            Value::Float(value) if value.is_nan() => {
                return Err("cannot compare NaN values".to_string());
            }
            Value::Float(_) => 0,
            Value::String(_) => 1,
            _ => return Err("values must all be numbers or all be strings".to_string()),
        };
        if kind.is_some_and(|kind| kind != value_kind) {
            return Err("values must all be numbers or all be strings".to_string());
        }
        kind = Some(value_kind);
    }
    Ok(())
}

fn compare_sort_values(left: &Value, right: &Value) -> std::cmp::Ordering {
    match (left, right) {
        (Value::Integer(left), Value::Integer(right)) => left.cmp(right),
        (Value::Float(left), Value::Float(right)) => left
            .partial_cmp(right)
            .unwrap_or(std::cmp::Ordering::Equal),
        (Value::Integer(left), Value::Float(right)) => (*left as f64)
            .partial_cmp(right)
            .unwrap_or(std::cmp::Ordering::Equal),
        (Value::Float(left), Value::Integer(right)) => left
            .partial_cmp(&(*right as f64))
            .unwrap_or(std::cmp::Ordering::Equal),
        (Value::String(left), Value::String(right)) => left.cmp(right),
        _ => unreachable!("sort values validated before comparison"),
    }
}