Question: //implement method only for to do section. import java.util.function.BiFunction; import java.util.function.Function; class EmptyListE extends Exception {} //------------------------------------------------------------- /** * This is persistent implementation. * After

//implement method only for to do section. import java.util.function.BiFunction; import java.util.function.Function;

class EmptyListE extends Exception {}

//-------------------------------------------------------------

/** * This is persistent implementation. * After a list is created, it is never updated * * See the test cases for examples */ abstract class List { static List countdown (int n) { if (n == 0) return new NodeL<>(0, new EmptyL<>()); else return new NodeL<>(n, countdown(n-1)); }

/** * Computes the length of the list */ abstract int length();

/** * Checks if the given elem occurs in the list * (Use .equals() to check for equality) */ abstract boolean inList(E elem);

/** * Inserts newElem after every occurrence of elem */ abstract List insertAfter (E elem, E newElem);

/** * Removes the first occurrence of elem (if * there is one) */ abstract List removeFirst (E elem);

/** * Returns the 0-based index of elem in the list * If the element is not in the list, throw * an exception */ abstract int indexOf (E elem) throws EmptyListE;

/** * Returns a new list that only contains the * elements satisfying the given predicate */ abstract List filter (Function pred);

/** * Returns a new list in which every element * is transformed using the given function */ abstract List map (Function f);

/** * Starting with base as the current result, * repeatedly applies the bifunction f to the * current result and one element from the list */ abstract F reduce(F base, BiFunction f);

/** * Appends the given list at the end of the * current list */ abstract List append (List other);

/** * Use to accumulate the reverse of the given list */ abstract List reverseHelper (List result);

/** * Returns a big list containing all the sublists of * the current list */ abstract List> powerSet ();

List reverse () { return reverseHelper(new EmptyL<>()); } }

//-------------------------------------------------------------

class EmptyL extends List {

int length() { return 0; // TODO }

boolean inList(E elem) { return false; // TODO }

List insertAfter(E elem, E newElem) { return null; // TODO }

List removeFirst(E elem) { return null; // TODO }

int indexOf(E elem) throws EmptyListE { return 0; // TODO }

List filter(Function pred) { return null; // TODO }

List map(Function f) { return null; // TODO }

F reduce(F base, BiFunction f) { return null; // TODO }

List append(List other) { return null; // TODO }

List reverseHelper(List result) { return null; // TODO }

List> powerSet() { return null; // TODO }

public boolean equals (Object o) { return o instanceof EmptyL; } }

//-------------------------------------------------------------

class NodeL extends List { private final E first; private final List rest;

NodeL (E first, List rest) { this.first = first; this.rest = rest; }

int length() { return 0; // TODO }

boolean inList(E elem) { return false; // TODO }

List insertAfter(E elem, E newElem) { return null; // TODO }

List removeFirst(E elem) { return null; // TODO }

int indexOf(E elem) throws EmptyListE { return 0; // TODO }

List filter(Function pred) { return null; // TODO }

List map(Function f) { return null; // TODO }

F reduce(F base, BiFunction f) { return null; // TODO }

List append(List other) { return null; // TODO }

List reverseHelper(List result) { return null; // TODO }

List> powerSet() { return null; // TODO }

public boolean equals (Object o) { if (o instanceof NodeL) { NodeL other = (NodeL) o; return first.equals(other.first) && rest.equals(other.rest); } else return false; } }

// //

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!