Interface PStack<E>

Type Parameters:
E - the type of elements maintained by this stack

public interface PStack<E>
Persistent (functional) Stack.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    anyMatch​(Predicate<E> predicate)
    Test given predicate on elements and return true if any of elements matches the predicate
    void
    forEach​(Consumer<E> action)
    Performs the given action for each element in this stack until all elements have been processed or the action throws an exception.
    boolean
     
     
    peek​(int i)
     
    pop()
     
    push​(E e)
     
    int
    Naive implementation has O(n) time complexity, where n is number of elements.
     
  • Method Details

    • push

      PStack<E> push(E e)
      Returns:
      new stack with added element
    • peek

      E peek()
      Returns:
      element at the top of this stack
      Throws:
      IllegalStateException - if this stack is empty.
    • peek

      E peek(int i)
      Parameters:
      i - - index of element to be returned, 0 means top of the stack
      Returns:
      i-th element from top of the stack
      Throws:
      IllegalStateException - if stack has less than i elements
    • pop

      PStack<E> pop()
      Returns:
      new stack with removed element
      Throws:
      IllegalStateException - if this stack is empty.
    • isEmpty

      boolean isEmpty()
      Returns:
      true if this stack contains no elements
    • forEach

      void forEach(Consumer<E> action)
      Performs the given action for each element in this stack until all elements have been processed or the action throws an exception.
    • anyMatch

      boolean anyMatch(Predicate<E> predicate)
      Test given predicate on elements and return true if any of elements matches the predicate
      Parameters:
      predicate - predicate to be tested
      Returns:
      true if any of the stack elements satisfies the predicate
    • size

      int size()
      Naive implementation has O(n) time complexity, where n is number of elements. More clever implementation could take advantage of PStack's immutability
      Returns:
      number of elements in the stack
    • toString

      String toString()
      Overrides:
      toString in class Object
      Returns:
      a string representation of this stack