diff options
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/array.rs | 12 | ||||
| -rw-r--r-- | src/eval/methods.rs | 2 |
2 files changed, 14 insertions, 0 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs index 1166ce94..f6e2f2d4 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -306,6 +306,18 @@ impl Array { Ok(result) } + /// Zips the array with another array. If the two arrays are of unequal length, it will only + /// zip up until the last element of the smaller array and the remaining elements will be + /// ignored. The return value is an array where each element is yet another array of size 2. + pub fn zip(&self, other: Array) -> Array { + self.iter() + .zip(other) + .map(|(first, second)| { + Value::Array(Array::from_vec(eco_vec![first.clone(), second])) + }) + .collect() + } + /// Return a sorted version of this array, optionally by a given key function. /// /// Returns an error if two values could not be compared or if the key function (if given) diff --git a/src/eval/methods.rs b/src/eval/methods.rs index 29b729cb..3ee4599c 100644 --- a/src/eval/methods.rs +++ b/src/eval/methods.rs @@ -118,6 +118,7 @@ pub fn call( array.join(sep, last).at(span)? } "sorted" => Value::Array(array.sorted(vm, span, args.named("key")?)?), + "zip" => Value::Array(array.zip(args.expect("other")?)), "enumerate" => Value::Array(array.enumerate()), _ => return missing(), }, @@ -319,6 +320,7 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] { ("slice", true), ("sorted", false), ("enumerate", false), + ("zip", true), ], "dictionary" => &[ ("at", true), |
