Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 ( )

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;
}
// return true if there is an element in l that satisfies p
static boolean exists(Iterable l, Predicate p){
return false;
}
public static List filter(List l, Predicate p){
ArrayList l2= new ArrayList<>();
for (U x: l){
if (p.test(x)){
l2.add(x);
}
}
return null;
}
static record Person(String name, int age, boolean teach){}
public static void printFiltered2(ArrayList l, Predicate f){
List l2= filter(l,f);
map(l2,(Person p)->{return p.name(); });
}
// precondition: l is not empty
static U minVal(Iterable l, Comparator c){
// write using fold. No other loops or recursion permitted.
// use c.compare
//
return null;
}
/**
* Returns {@link Supplier} that always supply "Hello"
*
* @return a string supplier
*/
public static Supplier helloSupplier(){
throw new ExerciseNotCompletedException();
}
/**
* Returns a {@link Predicate} of string that checks if string is empty
*
* @return a string predicate
*/
public static Predicate isEmptyPredicate(){
throw new ExerciseNotCompletedException();
}
/**
* Return a {@link Function} that accepts {@link String} and returns that string repeate

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

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions