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

Function remove

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

Remove the first item from the list whose value is equal to x

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

Source from the content-addressed store, hash-verified

110
111// Remove the first item from the list whose value is equal to x
112fn remove<'gc>(
113 mc: &'gc Mutation<'gc>,
114 receiver: Value<'gc>,
115 args: Vec<Value<'gc>>,
116) -> Result<Value<'gc>, VmError> {
117 let list = receiver.as_array()?;
118
119 if args.is_empty() {
120 return Err(VmError::RuntimeError("remove: expected 1 argument".into()));
121 }
122
123 let value_to_remove = &args[0];
124
125 let mut list_mut = list.borrow_mut(mc);
126 if let Some(index) = list_mut
127 .data
128 .iter()
129 .position(|item| item.equals(value_to_remove))
130 {
131 list_mut.data.remove(index);
132 Ok(receiver)
133 } else {
134 Err(VmError::RuntimeError(format!(
135 "remove: value {} not found in list",
136 value_to_remove
137 )))
138 }
139}
140
141// Remove the item at the given position and return it
142fn pop<'gc>(

Callers

nothing calls this directly

Calls 4

RuntimeErrorClass · 0.85
as_arrayMethod · 0.80
borrow_mutMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected