Ponylang 为小马阵列实现map函数
我一直在玩Pony数组,以便更好地理解Pony,并希望为任何数组编写map函数 我所说的类似于现在大多数语言用于转换集合元素的标准映射函数,如Clojure:Ponylang 为小马阵列实现map函数,ponylang,Ponylang,我一直在玩Pony数组,以便更好地理解Pony,并希望为任何数组编写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: Ar
(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
任何人都知道如何做到这一点好吧,我花了一段时间,但我能够让事情正常进行 这是我对所发生事情的基本理解(如果我错了,请纠正我) 第一步是理解我们需要使用别名来更改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
现在我可以向任何人发送结果