Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

While Java does not support higher-order functions, we recently had an introduct

ID: 3754361 • Letter: W

Question

While Java does not support higher-order functions, we recently had an introduction to emulat ing them using the edu psu.cmpsc2e1. Punction interface. Functional programming languages have a concept called map that takes a function and an array as parameters, and it returns the array produced by ap- plying the function to every element of the original array. Given that our recent introduction to the edu.psu.cmp sc221. Function interface used static members, our map function will also be static and will have the following signature: public static RC] map(Function function, Dá array) This assignment requires you to implement the map function specified; please implement it in a class named edu.psu.cmpsc221.Homework2. The syntax for this generic function is slightly different from the syntax of the other Java generics that we have seen in class (there is an additional pair of type variables that follows the static keyword). This is the syntax Java uses for a static generic method. Please also implement a PrintArray method that takes an array of reference types as a parameter and simply prints the elements of the array to the screen. Print Array shall be generic. To help clarify what map is doing, a few example uses are provided // Example 1 Function function neu CalculateSuccessor Integerl integerArray (1, 3, 4, 2, 5; PrintArray (nap (function, integerArray)); // map returns (2, 4, 5, 3, 6 // Example 2 Function another Function nes CalculateLength O; String[) stringArray "Java", "C++", "Smal1talk" PrintArray (map (anotherFunction, stringArray)); // map returns (4, 3, 9 // Example 3 Function tripleFunction = new Calcul ateTriple(); Double[] doubleArray { 2.0, 4.0, 5.0, 1.0 }; PrintArray (map (tripleFunction, doubleArray)); // map returns (6.0, 12.0, 15.0, 3.0 Please note that while the solution is actually quite short, this is a challenging problem because Java will likely get in your way and prevent you from implementing "obvious" solutions (recall that Java generics are known to not play nicely with arrays)

Explanation / Answer

Hello, the syntax of your map function is wrong. Every Function objects take first type as input and second type as output, for example, Function<String, Integer> takes String object as input and Integer object as output. But according to your map function signature, the input, output types appears to be swapped. Consider the syntax:

public static <R, D> R[] map(Function<R, D> function, D[] array )

The above map function takes a function (which takes an R type object as input, returns D type object), and an array of D type, returns an array of R type. But since the ‘function’ return an object of D type, we could only return an array of D type and not R type, or else we need to cast it to R, which is not correct and throw exceptions. The function will work with same input, output data types with no issues , but won’t work with different types. Therefore, you should use the below syntax instead.

public static <D, R> R[] map(Function<D, R> function, D[] array )

Please find the below code for the required function, and other required code for testing it. Also, I have included the function with original syntax, just in case if you need it. Thanks.

// HomeWork2.java

import java.util.function.Function;

public class HomeWork2 {

    //This is the right implementation of map function, corrected the syntax

    //this will take a Function (with input type - D and output type R), and an array

    //of type D, returns an array of type R after applying all elements with the

    //function

    public static <D, R> R[] map(Function<D, R> function, D[] array ){

        //creating an Object array of input array's capacity, casting it to R[]

        R[] result=(R[]) new Object[array.length];

        //iterating through all elements in input array

        for(int i=0;i<array.length;i++){

            //applying the function, adding to the result array

            result[i]=function.apply(array[i]);

        }

        //returning resultant array

        return result;

    }

   

    //this is the wrong implementation of the above function, according to the

    //question.

    public static <R, D> R[] map2(Function<R, D> function, D[] array ){

        R[] result=(R[]) new Object[array.length];

        for(int i=0;i<array.length;i++){

            result[i]=(R) function.apply((R) array[i]);

        }

        return result;

    }

   

    //method to print contents of an array of generic type R

    public static <R> void PrintArray(R[] array){

        for(int i=0;i<array.length;i++){

            System.out.print(array[i]+" ");

        }

        System.out.println();

    }

  

    public static void main(String[] args) {

        //creating a function to triple elements

        Function<Double, Double> function=new CalculateTriple();

        //creating a double array

        Double[] doubleArray={2.0,4.0,5.0,1.0};

        //mapping and printing the array after applying the function

        PrintArray(map(function, doubleArray));

       

        //creating a function to find length of input String

        Function<String, Integer> function2=new CalculateLength();

        //creating a String array for input

        String[] stringArray={"Java", "C++", "Smalltalk"};

        //mapping and printing the array after applying the function

        PrintArray(map(function2, stringArray));

    }

}

//CalculateTriple.java (tripling function class)

import java.util.function.Function;

/**

* Function class to triple an input Double value.

*/

class CalculateTriple implements Function<Double, Double> {

    @Override

    public Double apply(Double t) {

        return t*3;

    }

   

}

// CalculateLength.java (returns length of the String)

import java.util.function.Function;

/**

* Function class to find the length of an input String value.

*/

class CalculateLength implements Function<String, Integer> {

    @Override

    public Integer apply(String t) {

        return t.length();

   }

  

   

}

/*OUTPUT*/

6.0 12.0 15.0 3.0

4 3 9