Skip to content

feature(stack): add TryPop and TryPeek methods - #60

Merged
zyedidia merged 2 commits into
zyedidia:masterfrom
mohitjoshi-hey:feat/stack-try-methods
Oct 12, 2026
Merged

zyedidia merged 2 commits into
zyedidia:masterfrom
mohitjoshi-hey:feat/stack-try-methods

Conversation

@mohitjoshi-hey

Copy link
Copy Markdown
Contributor

Description

This PR addresses the ambiguity in stack operations when the stack is empty and the returned value happens to be the zero value of the generic type.

Changes

  • Added TryPop() (T, bool) to return the top element and indicate whether the operation succeeded.
  • Added TryPeek() (T, bool) to retrieve the top element without removing it and indicate whether the operation succeeded.
  • Added tests covering empty stacks, populated stacks, and zero-valued elements.

Testing

  • Tests pass using go test ./stack/....
  • Existing stack functionality remains unchanged.

Closes #59

@zyedidia

Copy link
Copy Markdown
Owner

Please fix the formatting issue and add a test that covers the case where 0 is pushed and then popped with TryPop.

@mohitjoshi-hey

Copy link
Copy Markdown
Contributor Author

I added those missing test cases covering TryPeek and TryPop when 0 is pushed/popped. And also formatted the files with gofmt

@zyedidia

Copy link
Copy Markdown
Owner

Thanks, please resolve the conflicts (caused by merging the other PR), and then I will merge.

@mohitjoshi-hey

Copy link
Copy Markdown
Contributor Author

I hope this helps. Thanks!!

@zyedidia
zyedidia merged commit 9d1edee into zyedidia:master Oct 12, 2026
1 check passed
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.

Stack operations cannot distinguish zero values from an empty stack

2 participants