2

So, I have gotten a bit of an understanding of functional programming but for this I cannot use a loop and instead have to use the foldLeft or foldRight function that I wrote, on order to get the minVal.

static <U,V> V foldLeft(V e, Iterable<U>l, BiFunction<V,U,V> f){
for(U u:l) {
    e = f.apply(e, u);
}
return e;
}

static <U,V> V foldRight(V e, Iterable<U>l, BiFunction<U,V,V> f){
   for(U u:l) {
      e = f.apply(u, e);
   }
return e;

I now have to write a minVal:

//(5) Use minVal to calculate the minimum of a List of 
//       Integers
static <U> U minVal(Iterable<U> l, Comparator<U> c){
// write using fold.  No other loops permitted. 
List<U> temp = new ArrayList<U>();
l.forEach(temp::add);
return temp.stream().min(c).get(); //Not sure if this actually works yet
}

I have attempted to write this and test it, but I am now stuck also on ow I would test the minVal:

List<Integer> numList = new ArrayList<>();

    numList.add(5);
    numList.add(10);
    numList.add(15);
    numList.add(20);
    numList.add(22);
    numList.add(1);


    System.out.println(minVal(numList, 0)); //What would I place as the 
                                            //comparable argument 

The above, of course, is giving me an error. I have read up on Comparators in Lambda but do not understand how to implement this in the test(or the print statement).

Any help/explanation is appreciated! P.S. Please let me know if I am missing any information, I tried to be as thorough as possible.

QWERTY
  • 25
  • 5

1 Answers1

3

You can define your minVal using foldLeft as :

static <U> U minVal(Iterable<U> l, Comparator<U> c) {
    // write using fold.  No other loops permitted.
    return foldLeft(l.iterator().next(), l, (u, u2) -> c.compare(u, u2) < 0 ? u : u2);
    // check for l.iterator().hasNext() or else define the behaviour
}

and then invoke it using the Comparator<Integer defined as:

List<Integer> numList = List.of(5, 10, 15, 20, 22, 1);
System.out.println(minVal(numList, Integer::compare));
Naman
  • 27,789
  • 26
  • 218
  • 353
  • Thank you! I was able to test my method by just adding the "Integer::compare". Then your foldLeft allowed me to understand this a lot more! I just have one more question, how would I be able to flip this if I wanted to get the Max value? – QWERTY Jan 27 '20 at 04:04
  • 1
    @QWERTY Changing the sign in the ternary operator (i.e. `(u, u2) -> c.compare(u, u2) > 0 ? u : u2`) should be sufficient enough to get the max instead of min. – Naman Jan 27 '20 at 04:33
  • I get that, but I was wondering if there was a way to calculate the min and max with just the one line somehow? The next test have to do is `// (6) Use minVal to calculate the maximum of a List of Integers` . Again thank you for the asssitance! – QWERTY Jan 27 '20 at 12:18
  • 1
    @QWERTY Oh that way, yes you can reverse the `Comparator` being passed to `minVal`, such that `System.out.println(minVal(numList, Comparator.comparingInt(x -> x).reversed()));` would print `22` with existing data. – Naman Jan 27 '20 at 16:44
  • dude your awesome thank you so much. I started reading more on the Comparator and your solution really helped with this. I really do appreciate all the help! – QWERTY Jan 27 '20 at 22:32