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

浪尽此生 提交于 2019-12-10 18:37:16

问题


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


回答1:


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]


来源:https://stackoverflow.com/questions/21354589/netlogo-procedure-for-performing-an-operation-on-one-item-in-a-list-in-a-compac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!