Since the documentation and tutorials on collections in Ponylang are lacking, I'm really struggling in using the Array class and working with indices.
I have an overenginered actor-based fizzbuzz where there's an actor in charge of requesting fizzbuzz operations to other actors and collecting the results. Probably the pattern I'm using is not the best but right now I would like to iterate in this way to learn more.
My problem is with the following code:
actor FizzBuzzer
var _results:Array[String]
let _main: Main tag
fun list_to_string(l:List[String]):String=>
l.fold[String]({(a:String,b:String)=>a+"\n"+b},"")
new create(n:String, main:Main tag)=>
let num:USize = try (consume n).usize()? else 0 end
_main = main
_results = recover Array[String] end
try
let result = this.fizzbuzz(num)?
let message:String = list_to_string(result)
main.print(message)
else
main.print("Invalid argument: "+ num.string())
end
fun fizzbuzz(n:USize, acc:List[String]=List[String]()):List[String] ?=>
if n <=0 then error end
let res = List[String]()
for i in Range[USize](1,n+1) do
FizzBuzzerino.process(i,this)
end
res
be collect_result(result:String,num:USize)=>
try
_results.insert(num,result)?
else
_main.print("Error processing element: "+num.string())
end
`
In the function collect_result the insertion always fails. Using update is the same. The result and num I receive from the processing actor are correct but I cannot insert them in the array. Any insertion in the Array in this class fails. Is it a matter of capabilities? Or am I doing something wrong?