From 5ab750b93b86b51284b5558b426a11a72651b083 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Tue, 14 Jun 2022 10:30:52 +0200 Subject: [PATCH] Array modification functions --- src/interpreter.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/interpreter.cc b/src/interpreter.cc index f0a924d..f0cdf34 100644 --- a/src/interpreter.cc +++ b/src/interpreter.cc @@ -373,6 +373,34 @@ Interpreter::Interpreter() return Value::from(std::move(array)); }); + global.force_define("sort", +[](Interpreter &i, std::vector args) -> Result { + auto array = Try(into_flat_array(i, std::move(args))); + std::sort(array.elements.begin(), array.elements.end()); + return Value::from(std::move(array)); + }); + + global.force_define("reverse", +[](Interpreter &i, std::vector args) -> Result { + auto array = Try(into_flat_array(i, std::move(args))); + std::reverse(array.elements.begin(), array.elements.end()); + return Value::from(std::move(array)); + }); + + global.force_define("min", +[](Interpreter &i, std::vector args) -> Result { + auto array = Try(into_flat_array(i, std::move(args))); + auto min = std::min_element(array.elements.begin(), array.elements.end()); + if (min == array.elements.end()) + return Value{}; + return *min; + }); + + global.force_define("max", +[](Interpreter &i, std::vector args) -> Result { + auto array = Try(into_flat_array(i, std::move(args))); + auto max = std::max_element(array.elements.begin(), array.elements.end()); + if (max == array.elements.end()) + return Value{}; + return *max; + }); + global.force_define("chord", +[](Interpreter &i, std::vector args) -> Result { Chord chord; Try(create_chord(chord.notes, i, std::move(args)));