Conversation
This adds support for `slice` to PersistentVector, matching the behavior of JavaScript's Array ([docs]) [docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
|
Just to be clear, this won't return a new PersistentVector, it will output an array? In rust a slice would defer until used (IIRC) so wouldn't it be best if slice returned a vector and then .toArray returned the array? |
|
@DanielRX: Yes, as written, it will return an array. My goal here was to simplify some code in a NEAR Studio template which looks like this: export function getMessages(): PostedMessage[] {
const numMessages = min(MESSAGE_LIMIT, messages.length);
const startIndex = messages.length - numMessages;
const result = new Array<PostedMessage>(numMessages);
for (let i = 0; i < numMessages; i++) {
result[i] = messages[i + startIndex];
}
return result;
}If export function getMessages(): PostedMessage[] {
return messages.slice(-MESSAGE_LIMIT)
}Which feels much more natural to me, coming from JavaScript. As to whether A third option: I think @willemneal may be working on a new pattern for serializing arbitrary data structures to contract storage, which could eventually obsolesce all I'd definitely love input from others. @potatodepaulo @evgenykuzyakov, what do y'all think? |
|
If you added myPersistentVector.slice(0, 10) => new PV which then can be cast to an array With this slice, there would be no way to do things like "Get a slice of the messages from x onwards, and loop over them until you find an item" because as the vector grows, the |
This adds support for
sliceto PersistentVector, matching the behavior of JavaScript's Array (docs)