Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Comments

feat(persistentvector): add slice function#123

Open
chadoh wants to merge 1 commit intomasterfrom
slice
Open

feat(persistentvector): add slice function#123
chadoh wants to merge 1 commit intomasterfrom
slice

Conversation

@chadoh
Copy link

@chadoh chadoh commented Feb 5, 2020

This adds support for slice to PersistentVector, matching the behavior of JavaScript's Array (docs)

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
@DanielRX
Copy link
Contributor

DanielRX commented Feb 6, 2020

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?
It means you could slice(100) Into a vector of length 1mil, loop over 10, without trying to get the last 1mil - 100 elements?

@chadoh
Copy link
Author

chadoh commented Feb 7, 2020

@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 PersistentArray supported slice, this function could be simplified to:

export function getMessages(): PostedMessage[] {
  return messages.slice(-MESSAGE_LIMIT)
}

Which feels much more natural to me, coming from JavaScript.

As to whether myPersistentVector.slice() ought to return an array or a new PersistentVector, I don't yet feel confident enough with the design goals of this library to feel equipped to judge.

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 Persistent* classes. If we think that might be the direction we go in, I would want to avoid merging any new improvements to PersistentVector, so people don't start depending on a feature just to have it deprecated soon after.

I'd definitely love input from others. @potatodepaulo @evgenykuzyakov, what do y'all think?

@DanielRX
Copy link
Contributor

DanielRX commented Feb 7, 2020

If you added start and end to PersistentVector and did @op("[]") as get(index + start) then

myPersistentVector.slice(0, 10) => new PV which then can be cast to an array
So slice would be like a data view, rather than a cast.

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 sliced array would grow and run OOG eventually.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants