MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / count

Function count

aiscript-vm/src/builtins/array.rs:284–305  ·  view source on GitHub ↗

Return the number of times x appears in the list

(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
)

Source from the content-addressed store, hash-verified

282
283// Return the number of times x appears in the list
284fn count<'gc>(
285 _mc: &'gc Mutation<'gc>,
286 receiver: Value<'gc>,
287 args: Vec<Value<'gc>>,
288) -> Result<Value<'gc>, VmError> {
289 let list = receiver.as_array()?;
290
291 if args.is_empty() {
292 return Err(VmError::RuntimeError("count: expected 1 argument".into()));
293 }
294
295 let value_to_count = &args[0];
296
297 let count = list
298 .borrow()
299 .data
300 .iter()
301 .filter(|item| item.equals(value_to_count))
302 .count();
303
304 Ok(Value::Number(count as f64))
305}
306
307// Sort the items of the list in place
308fn sort<'gc>(

Callers

nothing calls this directly

Calls 5

RuntimeErrorClass · 0.85
as_arrayMethod · 0.80
countMethod · 0.80
borrowMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected