Difference between revisions of "ImmutableList Assignment"
(→Apps) |
|||
Line 66: | Line 66: | ||
<pre>public static <E> int length(ImmutableList<E> list)</pre> | <pre>public static <E> int length(ImmutableList<E> list)</pre> | ||
+ | |||
+ | Compute the length of an arbitrary ImmutableList<E>. | ||
===SumProductCountdownFactorial=== | ===SumProductCountdownFactorial=== | ||
Line 72: | Line 74: | ||
====sum==== | ====sum==== | ||
<pre>public static int sum(ImmutableList<Integer> xs)</pre> | <pre>public static int sum(ImmutableList<Integer> xs)</pre> | ||
+ | |||
+ | Compute the sum of all the items in an ImmutableList<Integer>. | ||
====product==== | ====product==== | ||
<pre>public static int product(ImmutableList<Integer> xs)</pre> | <pre>public static int product(ImmutableList<Integer> xs)</pre> | ||
+ | |||
+ | Compute the produce all the items in an ImmutableList<Integer>. | ||
====countdown==== | ====countdown==== | ||
<pre>public static ImmutableList<Integer> countdown(int n)</pre> | <pre>public static ImmutableList<Integer> countdown(int n)</pre> | ||
+ | |||
+ | Produce a new list which contains the values from n down to 1. For example, countdown(5) would produce the list [5,4,3,2,1]. | ||
====factorial==== | ====factorial==== | ||
<pre>public static int factorial(int n)</pre> | <pre>public static int factorial(int n)</pre> | ||
+ | |||
+ | Can you implement this method using the methods you have already created? | ||
===Concat=== | ===Concat=== | ||
Line 86: | Line 96: | ||
<pre>public static <E> ImmutableList<E> concat(ImmutableList<E> xs, ImmutableList<E> ys)</pre> | <pre>public static <E> ImmutableList<E> concat(ImmutableList<E> xs, ImmutableList<E> ys)</pre> | ||
+ | |||
+ | Concatenate one list on to another. | ||
=Test= | =Test= |
Revision as of 14:50, 15 September 2020
Code To Investigate
interface ImmutableList<E>
public interface ImmutableList<E> extends Iterable<E> { /** * @return the head value if it is present * @throws NoSuchElementException if empty */ E head(); /** * @return the tail if it is present * @throws NoSuchElementException if empty */ ImmutableList<E> tail(); /** * @return true if empty, false otherwise */ boolean isEmpty(); }
Code To Implement
Utilities
Lists
class: | Lists.java | ![]() |
methods: | nil cons brackets |
|
package: | immutable.list.assignment | |
source folder: | src/main/java |
nil
public static <E> ImmutableList<E> nil()
Constructs a new empty list, analogous to the nil constructor for SML List.
cons
public static <E> ImmutableList<E> cons(E head, ImmutableList<E> tail)
Constructs a new list comprised of head::tail, analogous to the :: constructor for SML List.
brackets
public static <E> ImmutableList<E> brackets(E... elements)
Constructs an ImmutableList<E> whose contents are defined by the elements passed in.
The parameter E... arguments is an example usage of varargs in Java. You can treat parameter elements as if it is an E[]. The varargs ... simply allows for more convenient invocations of the brackets method.
In order to build this method, consider added a private static helper method to perform the actual work.
It is suggested that you adopt a recursive approach to solving this problem. What is the base case? What is the recursive case?
enum EmptyImmutableList
class: | EmptyImmutableList.java | ![]() |
methods: | constructor isEmpty head tail |
|
package: | immutable.list.assignment | |
source folder: | src/main/java |
/* package-private */ enum EmptyImmutableList implements ImmutableList<Object>
WARNING: you need NOT implement iterator() now. That will be the subject of a future studio.
class NonEmptyImmutableList<E>
class: | NonEmptyImmutableList.java | ![]() |
methods: | constructor isEmpty head tail |
|
package: | immutable.list.assignment | |
source folder: | src/main/java |
/* package-private */ enum EmptyImmutableList implements ImmutableList<Object>
WARNING: you need NOT implement iterator() now. That will be the subject of a future studio.
Apps
NOTE: Since the constructors on our implementations are package protected, you will need to use nil() and cons(head,tail).
Length
class: | Length.java | ![]() |
methods: | length | |
package: | immutable.list.apps.assignment | |
source folder: | src/main/java |
public static <E> int length(ImmutableList<E> list)
Compute the length of an arbitrary ImmutableList<E>.
SumProductCountdownFactorial
class: | SumProductCountdownFactorial.java | ![]() |
methods: | sum product countdown factorial |
|
package: | immutable.list.apps.assignment | |
source folder: | src/main/java |
sum
public static int sum(ImmutableList<Integer> xs)
Compute the sum of all the items in an ImmutableList<Integer>.
product
public static int product(ImmutableList<Integer> xs)
Compute the produce all the items in an ImmutableList<Integer>.
countdown
public static ImmutableList<Integer> countdown(int n)
Produce a new list which contains the values from n down to 1. For example, countdown(5) would produce the list [5,4,3,2,1].
factorial
public static int factorial(int n)
Can you implement this method using the methods you have already created?
Concat
class: | Concat.java | ![]() |
methods: | concat | |
package: | immutable.list.apps.assignment | |
source folder: | src/main/java |
public static <E> ImmutableList<E> concat(ImmutableList<E> xs, ImmutableList<E> ys)
Concatenate one list on to another.
Test
class: | ImmutableListTestSuite.java | ![]() |
package: | immutable.list.assignment | |
source folder: | src/test/java |
Pledge, Acknowledgments, Citations
file: | studio-immutable-list-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge