Monkey container filtering

Home Forums General Programming Discussion Monkey container filtering

Tagged: 

This topic contains 5 replies, has 3 voices, and was last updated by  Pixel_Outlaw 3 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #9921

    Pixel_Outlaw
    Participant

    So  I’ve been confused a bit since Monkey X, but what is generally the best way to clear all dead items from a container from various positions?

    Do you form a deque and push(pop()) only the alive items to the back on the dequeue [length] many times??

    Or do you simply create a new list and add the living items onto it then reassign it back to the old list like in the firepaint example? This seems like it will really hammer memory if you’re clearing dead items out each cycle and creating a new data structure.

    Currently I’m working on a Monkey X project (I’ll migrate to Monkey 2 when done) and I need the best container and approach for filtering out many dead instances from the middle.

    In many genres of game you’ll have enemies that suddenly have been killed and need to be filtered out from various positions in the container.

    I actually think things like Map, Filter, and Reduce would be really nice to have for the containers so you can know they’re implemented efficiently. I’d make them destructive/mutating if possible. 🙂

    Thanks!
    P_O

    #9923

    nerobot
    Participant

    For MonkeyX you can use List<T> container and call its Remove() method like this:

    In Monkey2 we can write more powerfull code – because we can pass functions as parameters of other functions.

    And we can write generic function to remove items by filter for any instance type.

    One bad thing here – we can’t write the only universal function for all containers (list, stack, map, etc…) because of language limitation:

    Finally, IContainer is not a ‘real’ interface because Monkey2 does not yet support generic interface methods. This feature is planned for a future version of monkey2.

    #9924

    Pixel_Outlaw
    Participant

    Ah, that’s really interesting.
    Thanks for the insight!

    For your Monkey X example:
    Are you sure calling remove on the iterator is safe?
    It’s been a source of dangerous and undefined behavior in many programming languages.
    Just a surprising pattern to see if so. 🙂

    As far as passing functions:
    Yep! I anticipate using passed functions quite a bit in Monkey 2.  I’m very familiar with that style of programming.
    I do it quite a bit in C++, Python, Common Lisp. 🙂

    #9930

    Simon Armstrong
    Participant

    Remove seems safe inside a for next, not sure if there are other iterations available for monkey containers.

    #9934

    nerobot
    Participant

    Are you sure calling remove on the iterator is safe?

    I know that other langs deny iterator changes, but I just tried it in monkey-x and it works. 🙂

    In monkey2 don’t work but we have iterator.Erase() here.

    #9936

    Pixel_Outlaw
    Participant

    Ok, thanks for the help guys!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.