user3235765
user3235765

Reputation: 31

NetLogo: procedure for performing an operation on one item in a list in a compact way?

New to NetLogo... wondering if there's a procedure that performs an operation on one item in a list in a compact way (like map but for one item).

For example, say I wanted to add 3 to the item at index i in list blah.

Right now I'm doing this like:

set blah replace-item i blah (item i blah + 3)

It seems a little clunky, and like there would be a procedure that does that, but I haven't been able to find one. Just wanted to make sure I wasn't missing something.

Thank you! Taylor

Upvotes: 3

Views: 181

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30498

There isn't something built in that does this. But you could define it yourself as a procedure that takes a task as input:

;; replace item i of xs with the result of applying fn to that item
to-report mapping-replace-item [i xs fn]
  report replace-item i xs (runresult fn item i xs)
end

Sample usage:

observer> show mapping-replace-item 2 [10 20 30 40] task [? * ?]
observer: [10 20 900 40]

Upvotes: 1

Related Questions