Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please look at the TODO part and help me with this following code package fp; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Comparator; import java.util.Map; import java.util.function.

Please look at the TODO part and help me with this following code
package fp;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.function.*;
import java.util.List;
class ExerciseNotCompletedException extends RuntimeException {
public ExerciseNotCompletedException(){
super("Exercise is not completed yet");
}
}
/**
*{@link HW} is an exercise class. Each method returns a functional interface and it should be implemented
* using either lambda or a method reference. Every method that is not implemented yet throws
*{@link ExerciseNotCompletedException}.
*
* TODO: remove exception and implement each method of this class using lambda or method reference
*
* TODO: to get the most out of your learning, visit our website
*
*
*/
record pair (U u, V v){}
public class HW {
// precondition: l1 and l2 have the same length
static List> zip(List l1, List l2){
// walk through the U's and V's
// construct list of pairs
ArrayList> l = new ArrayList<>();
for (int i =0; i < l1.size(); i++){
l.add(new pair(l1.get(i),l2.get(i)));
}
return l;
// ArrayList l = new ArrayList<>();
// for (int i =0; i < l1.size(); i++){ l.add(i); }
// return map(l, i -> new pair<>(l1.get(i), l2.get(i)));
}
// postcondition: result has the same length as l
static List map(List l, Function f){
// walk through the U's
// use f at every stage f.apply
// construct list of V's
ArrayList l2= new ArrayList<>();
for (U x: l){
l2.add(f.apply(x));
}
return l2;
}
static List flatmap(List l, Function> f){
// walk through the U's
// use f at every stage f.apply
// construct list of V's
ArrayList res = new ArrayList<>();
for (U x: l){
List l2= f.apply(x);
for (V y: l2){
res.add(y);
}
}
return res;
}
// foldleft(0,[1,2,3,4],(x,y)-> x+y)=10
// U = V= Integer
// fpldleft(1,[1,2,3,4],(x,y)-> x*y)=24
static V foldLeft(V e, Iterablel, BiFunction f){
// walk through the U's [u1,u2,..,un]
// e
// use f at every stage v1= f.apply(e,u1)
// v2= f.apply(v1,u2)
// v3= f.apply(v2,u3)..
// return the last v
V v = e;
for (U x: l){
v = f.apply(v,x);
}
return v;
}
// similar to above
// but from the right
// vn= f(un,e)
// vn-1= f(un-1,vn)
//..
// return the first v
static V foldRight(V e, Listl, BiFunction f){
// walk through the U's [u1,u2,..,un]
// e
// use f at every stage v1= f.apply(u1,e)
// v2= f.apply(u2,v1)
// v3= f.apply(u3,v2)..
// return the last v
V v = e;
for (int i = l.size()-1; i >=0; i--){
v = f.apply(l.get(i),v);
}
return v;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is an agile organization?

Answered: 1 week ago

Question

5. Prepare for the role of interviewee

Answered: 1 week ago

Question

6. Secure job interviews and manage them with confidence

Answered: 1 week ago