NetLogo:以紧凑的方式对列表中的一项执行操作的过程? [英] NetLogo: procedure for performing an operation on one item in a list in a compact way?

查看:39
本文介绍了NetLogo:以紧凑的方式对列表中的一项执行操作的过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NetLogo 新手...想知道是否有一种程序可以以紧凑的方式对列表中的一项执行操作(类似于 map 但对一项进行操作).

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).

例如,假设我想将 3 添加到列表 blah 中索引 i 处的项目.

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

现在我这样做:

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.

谢谢!泰勒

推荐答案

没有内置的东西可以做到这一点.但是您可以自己将其定义为将任务作为输入的过程:

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

示例用法:

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

这篇关于NetLogo:以紧凑的方式对列表中的一项执行操作的过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆