main
Ziyang Hu 2 years ago
parent 6537cfd01e
commit 82ad57901b

@ -124,6 +124,8 @@ The four _basic arithmetic operators_ `+`, `-`, `*`, and `/` do what you expect,
`prepend(l, x)`, `append(l, x)`: prepends / appends the element `x` to the list `l`.
`reverse(l)` reverses the list.
`sorted(l)`: returns the sorted list as defined by the total order detailed in [datatypes](datatypes.md).
`chunks(l, n)`: splits the list `l` into chunks of `n`, e.g. `chunks([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4], [5]]`.

@ -431,6 +431,7 @@ pub(crate) fn get_op(name: &str) -> Option<&'static Op> {
"is_nan" => &OP_IS_NAN,
"length" => &OP_LENGTH,
"sorted" => &OP_SORTED,
"reverse" => &OP_REVERSE,
"append" => &OP_APPEND,
"prepend" => &OP_PREPEND,
"unicode_normalize" => &OP_UNICODE_NORMALIZE,

@ -978,6 +978,16 @@ pub(crate) fn op_sorted(args: &[DataValue]) -> Result<DataValue> {
Ok(DataValue::List(arg))
}
define_op!(OP_REVERSE, 1, false);
pub(crate) fn op_reverse(args: &[DataValue]) -> Result<DataValue> {
let mut arg = args[0]
.get_list()
.ok_or_else(|| anyhow!("cannot apply 'reverse' to {:?}", args))?
.to_vec();
arg.reverse();
Ok(DataValue::List(arg))
}
define_op!(OP_HAVERSINE, 4, false);
pub(crate) fn op_haversine(args: &[DataValue]) -> Result<DataValue> {
let gen_err = || anyhow!("cannot computer haversine distance for {:?}", args);

@ -932,7 +932,7 @@ fn test_unicode_normalize() {
}
#[test]
fn test_sort() {
fn test_sort_reverse() {
assert_eq!(
op_sorted(&[DataValue::List(vec![
DataValue::from(2.0),
@ -947,6 +947,21 @@ fn test_sort() {
DataValue::from(2),
DataValue::from(2.0),
])
);
assert_eq!(
op_reverse(&[DataValue::List(vec![
DataValue::from(2.0),
DataValue::from(1),
DataValue::from(2),
DataValue::Null
])])
.unwrap(),
DataValue::List(vec![
DataValue::Null,
DataValue::from(2),
DataValue::from(1),
DataValue::from(2.0),
])
)
}

Loading…
Cancel
Save