diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-08-31 12:59:53 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-08-31 12:59:53 +0200 |
| commit | 3481d8cc81a2b3a14118869c7f0ffe204ff3efc8 (patch) | |
| tree | 907efa2e092366a24e25243854b1a4e088cc04a9 /src/eval/array.rs | |
| parent | ee84bf74083f5b9cc88a2a0a968dc905b1eef22c (diff) | |
More utility functions
- join("a", "b", "c", sep: ", ")
- int("12")
- float("31.4e-1")
- str(10)
- sorted((3, 2, 1))
Diffstat (limited to 'src/eval/array.rs')
| -rw-r--r-- | src/eval/array.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs index acf44ab2..bae89c4b 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::convert::TryFrom; use std::fmt::{self, Debug, Display, Formatter, Write}; use std::iter::FromIterator; @@ -80,6 +81,26 @@ impl Array { self.0.iter() } + /// Return a sorted version of this array. + /// + /// Returns an error if two values could not be compared. + pub fn sorted(mut self) -> StrResult<Self> { + let mut result = Ok(()); + Rc::make_mut(&mut self.0).sort_by(|a, b| { + a.partial_cmp(b).unwrap_or_else(|| { + if result.is_ok() { + result = Err(format!( + "cannot compare {} with {}", + a.type_name(), + b.type_name(), + )); + } + Ordering::Equal + }) + }); + result.map(|_| self) + } + /// Repeat this array `n` times. pub fn repeat(&self, n: i64) -> StrResult<Self> { let count = usize::try_from(n) |
