import java.util.ArrayList;

public class Stack<E> {

  ArrayList<E> stack;

  public Stack(int capacity) {
    stack = new ArrayList<E>(capacity);
  }

  public E pop() {
    E e = stack.remove(stack.size() - 1);
    return e;
  }

  public void push(E e) {
    stack.add(e);
  }

  public boolean empty() {
    return stack.size() == 0;
  }

  <T> T reduce(Combinator<T, E> comb, T seed) {
    while (!empty()) {
      seed = comb.combine(seed, pop());
    }
    return seed;
  }
}
