Difference between revisions of "Iterable Immutable List Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 31: Line 31:
 
===head===
 
===head===
 
  <nowiki>public E head()</nowiki>
 
  <nowiki>public E head()</nowiki>
 +
 +
Analogous to the [http://sml-family.org/Basis/list.html SML List] [http://sml-family.org/Basis/list.html#SIG:LIST.hd:VAL hd method].
 
===tail===
 
===tail===
 
  <nowiki>public ImmutableList<E> tail()</nowiki>
 
  <nowiki>public ImmutableList<E> tail()</nowiki>
 +
 +
Analogous to the [http://sml-family.org/Basis/list.html SML List] [http://sml-family.org/Basis/list.html#SIG:LIST.tl:VAL tl method].
 
===isEmpty===
 
===isEmpty===
 
  <nowiki>public boolean isEmpty()</nowiki>
 
  <nowiki>public boolean isEmpty()</nowiki>
 +
 +
Analogous to the [http://sml-family.org/Basis/list.html SML List] [http://sml-family.org/Basis/list.html#SIG:LIST.null:VAL null method].
 
===iterator===
 
===iterator===
 
  <nowiki>public Iterator<E> iterator()</nowiki>
 
  <nowiki>public Iterator<E> iterator()</nowiki>

Revision as of 03:37, 31 July 2019

Code To Investigate

Iterable

interface java.lang.Iterable<T>
Iterator<T> iterator()

Iterator

interface java.util.Iterator<T>
boolean hasNext()
T next()

ImmutableList

public interface ImmutableList<E> extends Iterable<E> {
	E head();
	ImmutableList<E> tail();
	boolean isEmpty();
}

Code To Implement

DefaultImmutableList

class: DefaultImmutableList.java Java.png
methods: head
tail
isEmpty
iterator
package: immutable.list.assignment
source folder: src/main/java

default constructor

constructs a new empty immutable list

DefaultImmutableList()

cons constructor

construct a new immutable list with head and tail

DefaultImmutableList(E head, ImmutableList<E> tail)

head

public E head()

Analogous to the SML List hd method.

tail

public ImmutableList<E> tail()

Analogous to the SML List tl method.

isEmpty

public boolean isEmpty()

Analogous to the SML List null method.

iterator

public Iterator<E> iterator()

Lists

Lists is a class which holds a number of static methods. As we know, in the Kingdom of Nouns, Verbs must always have an escort.

The first two static methods nil and cons will each call a constructor of the DefaultImmutableList class. The third method brackets will use nil and cons to build up the desired ImmutableList.

class: Lists.java Java.png
methods: nil
cons
brackets
package: immutable.list.assignment
source folder: src/main/java

nil

public static <E> ImmutableList<E> nil()

Analogous to the SML List nil constructor.

Note: often SML programs use empty brackets [] instead of nil.

cons

public static <E> ImmutableList<E> cons(E head, ImmutableList<E> tail)

Analogous to the SML List :: constructor.

brackets

@SafeVarargs
public static <E> ImmutableList<E> brackets(E... elements)

brackets accepts variable length arguments as specified by the ... syntax. When implementing the brackets method you can treat elements as an array. Combining generics and varargs https://stackoverflow.com/questions/14231037/java-safevarargs-annotation-does-a-standard-or-best-practice-exist can be problematic] if the implementation lets the E[] escape since it will actually be an Object[]. The @SafeVarargs annotation indicates a promise that you will rely only on the elements of the array being of type E and not the array being of type E[]. This should not be a problem since you should not require any unsafe use of varargs.

Note: be sure to cons the elements on from the back of the elements array to the front.

Example

class: ListsExample.java Presentation icon-72a7cf.svg
package: immutable.list.example
source folder: src/main/java

The code:

ImmutableList<Integer> numbers = Lists.brackets(4, 66, 99);
for (int i : numbers) {
	System.out.println(i);
}

produces:

4
66
99

Test

class: ListsTestSuite.java Junit.png
package: immutable.list.assignment
source folder: src/test/java