为了更好地理解Pony,我一直在尝试使用Pony数组,并希望为任何数组编写map函数。
我说的是现在大多数语言都有的用于转换集合元素的标准map函数,比如Clojure:
(map #(+ 1 %) [1 2 3]) ; => [2 3 4]但我希望它实际修改给定的数组,而不是返回新的数组。
到目前为止,我的尝试由于功能的原因遇到了许多错误:
// array is "iso" so I can give it to another actor and change it
let my_array: Array[U64] iso = [1; 2; 3; 4]
// other actor tries to recover arrays as "box" just to call pairs() on it
let a = recover box my_array end // ERROR: can't recover to this capability
for (i, item) in a.pairs() do
// TODO set item at i to some other mapped value
try my_array.update(i, fun(item))? end
end任何人都知道如何做到这一点。
发布于 2019-02-24 17:59:54
好吧,花了我一段时间,但我能让事情运转起来。
这是我对发生的事情的基本理解(如果我错了,请纠正我)!
第一步是理解我们需要使用别名来更改Pony中变量的功能。
因此,为了使iso变量可以作为框使用,基本上必须通过将其消费到另一个变量中来对其进行别名:
let a: Array[U64] ref = consume array // array is "iso"
for (i, item) in a.pairs() do
try a.update(i, item + n)? end
end这行得通!!
我遇到的另一个问题是,我不能对生成的Array[U64] ref做很多事情。例如,不能将其传递给任何人。
所以我把所有东西包装到一个recover块中,以得到相同的数组,但作为一个val (对数组的不可变引用),这更有用,因为我可以将它发送给其他参与者:
let result = recover val
let a: Array[U64] ref = consume array
for (i, item) in a.pairs() do
try a.update(i, item + n)? end
end
a
end现在我可以将result发送给任何人了!
https://stackoverflow.com/questions/54850238
复制相似问题