ConsList.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package migl.util;
  2. import java.util.function.BinaryOperator;
  3. import java.util.function.Function;
  4. /**
  5. * Implementation of a list using the {@link migl.util.Cons} data structure.
  6. *
  7. * The implementation of ConsList must be immutable, i.e. each call to the
  8. * {@link #append(Object)} or {@link #prepend(Object)} methods must return a new
  9. * list without changing the state of the current list. This is unlike the
  10. * default behavior of {@link java.util.List} behavior.
  11. *
  12. * @author leberre
  13. *
  14. * @param <E>
  15. * the type of the elements in the list
  16. */
  17. public interface ConsList<E> extends Iterable<E> {
  18. /**
  19. * Insert a new element e in front of the list.
  20. *
  21. * @param e
  22. * an element.
  23. * @return a new list containing e in front of the current one.
  24. */
  25. ConsList<E> prepend(E e);
  26. /**
  27. * Insert a new element e at the end of the list
  28. *
  29. * @param e
  30. * an element
  31. * @return a new list containing e at the end of the current one.
  32. */
  33. ConsList<E> append(E e);
  34. /**
  35. * Check if the list is empty or not.
  36. *
  37. * @return true if the list is empty, else false.
  38. */
  39. boolean isEmpty();
  40. /**
  41. * Retrieve the first element of the list.
  42. *
  43. * @return the first element of the list.
  44. */
  45. E car();
  46. /**
  47. * Return the sublist corresponding to all but the first element.
  48. *
  49. * @return all but the first element of the list.
  50. */
  51. ConsList<E> cdr();
  52. /**
  53. * Returns the size of the list (the number of elements it contains).
  54. *
  55. * @return the number of elements in the list.
  56. */
  57. int size();
  58. /**
  59. * Create a new list by applying a function to each element of the list.
  60. *
  61. * @param f
  62. * a function
  63. * @return a list where each element is the result of applying f to an element
  64. * of the original list.
  65. */
  66. <T> ConsList<T> map(Function<E, T> f);
  67. /**
  68. * Performs a reduction on the elements of this list, using the provided
  69. * identity value and an associative accumulation function, and returns the
  70. * reduced value.
  71. *
  72. * @param identity
  73. * the identity value for the accumulating function
  74. * @param accumulator
  75. * an associative, stateless function for combining two values
  76. * @return the result of the reduction
  77. */
  78. default E reduce(E identity, BinaryOperator<E> accumulator) {
  79. E result = identity;
  80. for (E element : this) {
  81. result = accumulator.apply(result, element);
  82. }
  83. return result;
  84. }
  85. /**
  86. * Translates the ConsList as an array of Object. The type of the
  87. *
  88. * @return all the elements of the list in an array of objects
  89. */
  90. default Object[] toArray() {
  91. Object[] array = new Object[size()];
  92. int i = 0;
  93. for (Object o : this) {
  94. array[i++] = o;
  95. }
  96. return array;
  97. }
  98. }