MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / min

Function min

aiscript-vm/src/builtins/mod.rs:190–233  ·  view source on GitHub ↗
(_state: &mut State<'gc>, args: Vec<Value<'gc>>)

Source from the content-addressed store, hash-verified

188}
189
190fn min<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
191 if args.is_empty() {
192 return Err(VmError::RuntimeError(
193 "min() takes at least one argument.".into(),
194 ));
195 }
196
197 if args.len() == 1 {
198 // If single argument, it should be an array
199 match &args[0] {
200 Value::List(arr) => {
201 let arr = &arr.borrow().data;
202 if arr.is_empty() {
203 return Err(VmError::RuntimeError("min() arg is an empty array".into()));
204 }
205 arr.iter()
206 .min_by(|a, b| {
207 if let (Value::Number(x), Value::Number(y)) = (a, b) {
208 x.partial_cmp(y).unwrap()
209 } else {
210 panic!("min() array elements must be numbers")
211 }
212 })
213 .copied()
214 .ok_or_else(|| VmError::RuntimeError("min() array must not be empty".into()))
215 }
216 _ => Err(VmError::RuntimeError(
217 "single argument to min() must be an array.".into(),
218 )),
219 }
220 } else {
221 // Multiple arguments case
222 args.iter()
223 .min_by(|a, b| {
224 if let (Value::Number(x), Value::Number(y)) = (a, b) {
225 x.partial_cmp(y).unwrap()
226 } else {
227 panic!("min() arguments must be numbers")
228 }
229 })
230 .copied()
231 .ok_or_else(|| VmError::RuntimeError("min() arguments must be numbers".into()))
232 }
233}
234
235fn max<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
236 if args.is_empty() {

Callers

nothing calls this directly

Calls 4

RuntimeErrorClass · 0.85
lenMethod · 0.80
borrowMethod · 0.45
partial_cmpMethod · 0.45

Tested by

no test coverage detected