Difference between revisions of "Iterable Immutable List Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 1: Line 1:
=Code To Investigate=
+
=Code To Revisit=
 +
==DefaultImmutableList==
 +
[[ImmutableList_Assignment]]
 +
 
 +
=References=
 
==Iterable==
 
==Iterable==
 
:[https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html interface java.lang.Iterable<T>]
 
:[https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html interface java.lang.Iterable<T>]
Line 18: Line 22:
 
=Code To Implement=
 
=Code To Implement=
 
==DefaultImmutableList==
 
==DefaultImmutableList==
{{JavaToImplement|DefaultImmutableList|head<br>tail<br>isEmpty<br>iterator|immutable.list.assignment}}
+
{{JavaToImplement|DefaultImmutableList|iterator|immutable.list.assignment}}
===default constructor===
 
constructs a new empty immutable list
 
 
 
<nowiki>DefaultImmutableList()</nowiki>
 
 
 
===cons constructor===
 
construct a new immutable list with head and tail
 
 
 
<nowiki>DefaultImmutableList(E head, ImmutableList<E> tail)</nowiki>
 
 
 
===head===
 
<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===
 
<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===
 
<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>
 
==Lists==
 
<code>Lists</code> is a class which holds a number of static methods.  As we know, in [https://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html the Kingdom of Nouns], Verbs must always have an escort.
 
 
The first two static methods <code>nil</code> and <code>cons</code> will each call a constructor of the <code>DefaultImmutableList</code> class.  The third method <code>brackets</code> will use <code>nil</code> and <code>cons</code> to build up the desired ImmutableList.
 
 
{{JavaToImplement|Lists|nil<br>cons<br>brackets|immutable.list.assignment}}
 
===nil===
 
<nowiki>public static <E> ImmutableList<E> nil()</nowiki>
 
 
Analogous to the [http://sml-family.org/Basis/list.html SML List] [http://sml-family.org/Basis/list.html#SIG:LIST.nil:TY nil constructor].
 
 
Note: often SML programs use empty brackets <code>[]</code> instead of <code>nil</code>.
 
 
===cons===
 
<nowiki>public static <E> ImmutableList<E> cons(E head, 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.:::TY :: constructor].
 
 
===brackets===
 
<nowiki>@SafeVarargs
 
public static <E> ImmutableList<E> brackets(E... elements)</nowiki>
 
 
<code>brackets</code> accepts [https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html variable length arguments] as specified by the <code>...</code> syntax.  When implementing the <code>brackets</code> method you can treat <code>elements</code> 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 [https://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html @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 <code>cons</code> the elements on from the back of the <code>elements</code> array to the front.
 
  
 
=Example=
 
=Example=

Revision as of 16:28, 24 April 2020

Code To Revisit

DefaultImmutableList

ImmutableList_Assignment

References

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: iterator
package: immutable.list.assignment
source folder: src/main/java

iterator

public Iterator<E> iterator()

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: IterableAssignmentTestSuite.java Junit.png
package: immutable.list.assignment
source folder: src/test/java