#Removing Elements from Lists ?

1 messages · Page 1 of 1 (latest)

devout solstice
#

Hello folks. How do i idiomatically remove elements from an ArrayList based on certain predicates? . Something like filter from the functional paradigm ?

buoyant quest
#

mentioning functional programming in the zig discord server

latent forge
#
var read_index: usize = 0;
var write_index: usize = 0;
while (read_index < list.items.len) : (read_index += 1) {
    if (pred(list.items[read_index])) {
        list.items[write_index] = list.items[read_index];
        write_index += 1;
    }
}
list.shrinkRetainingCapacity(write_index);
fair stag
fair stag
devout solstice
fair stag
#

it would

devout solstice
fair stag
#

technically you could borrow the arraylist's backing allocator but that's pretty bad UX

devout solstice
#

hmm.... got it... i did not even think that you could use the allocator from the list itself

fair stag
#

yep!
all fields are public so you can do whatever :p

#

I just wouldnt recommend it