Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic std functions to manipulate arrays #601

Closed
wants to merge 4 commits into from
Closed

Conversation

mks-h
Copy link
Member

@mks-h mks-h commented Nov 17, 2024

Implements some standard library functions that allow to manipulate arrays:

  1. last(array) to get the last element (merely a shorthand for array[len(array) - 1])
  2. remove_at(array, index) to remove an element at the index, translates to unset array[index]
  3. extract_at(array, index) — same as remove_at, but also returns the element
  4. pop(array) — remove and return the last element
  5. shift(array) — remove and return the first element

@Mte90
Copy link
Member

Mte90 commented Nov 18, 2024

So checking with the function we have right now they are very useful :-)

@Mte90
Copy link
Member

Mte90 commented Nov 21, 2024

This was reported on discord about this PR: The issue right now is that the length of an array doesn't update after remove_at operation this means that we need to test this code and maybe do some fixes.

@Mte90 Mte90 requested review from hdwalters and Ph0enixKM November 21, 2024 14:54
@hdwalters hdwalters self-assigned this Nov 21, 2024
@hdwalters hdwalters force-pushed the arrayfn branch 3 times, most recently from 388a184 to 7f0a77f Compare November 21, 2024 22:01
@Mte90
Copy link
Member

Mte90 commented Nov 22, 2024

@hdwalters some tests failed

@hdwalters
Copy link
Contributor

hdwalters commented Nov 22, 2024

@hdwalters some tests failed

Yes I know. It's because standard library function remove_at is failing to update the passed-by-ref array:

pub fun remove_at(ref array: [], index: Num): Null {
    trust $ unset {nameof array}[{index}] $
}

This is a flawed approach, because unset in Bash doesn't actually shift the following items up to fill the gap, it just marks the item at that index as missing (so removing at the same index is a no-op). I'm working on a better solution.

@hdwalters
Copy link
Contributor

I created a new pull request #625, as my changes did not show up when I pushed to this branch.

@hdwalters hdwalters closed this Dec 1, 2024
@hdwalters hdwalters deleted the arrayfn branch December 2, 2024 20:45
@hdwalters
Copy link
Contributor

I created a third pull request #626, sourcing a branch on my fork of the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants