Question
L.1 A functor is an object that encapsulates a function. Its apply() method takes one parameter, applies an algorithm and returns a value. It is
L.1 A functor is an object that encapsulates a function. Its apply() method takes one parameter, applies an algorithm and returns a value. It is defined as a 'functional' interface:
interface Functor
a) Write a class implementing the Functor
Write a LengthFun.main() function that: o illustrates how the class is used to print the length of a string. o instantiates a lambda expression of type Functor
b) Define a subclass of LinkedList
The signature of the MyList
public
For an object mylist the mylist.map(fo) creates a new MyList
For instance, consider a TimesTwoFun
Write the TimesTwoFun
Write the MyList
Write a main() function in MyList.java that: o implements the example defined above with the TimesTwoFun class. o repeats the same thing using a lambda expression insted of the TimesTwoFun object.
c) A Functor2
interface Functor2
Add a reduce() function to the MyList
public
reduce(fo2, initVal) returns the sucessive "cummulative" application of the Functor2 apply() function to all elements of the list, beginning with the initial value and the first element of the list. For instance, assume the list elements are (a,b,c,d) and that a Functor2 object fo2 is defined. Then the reduce(fo2, v) call returns fo2.apply(fo2.apply(fo2.apply(fo2.apply(v, a), b), c), d) As a second example, a class called Summer implements the Functor2
To do: o Write the generic reduce() function in the MyList
d) The map and reduce functions are part of the functional programming paradigm and are widely used in non-functional languages as well. To understand the power of the map/reduce combination, use the code written in parts a)-c)) to implement the following in a the main() function of class MRExample:
- create a MyList
e) Write in the main() function of a new class MRExampleWithLambdas the same logic as in part d) ***using lambda expressions*** and chain the map() and reduce() calls in one expression: ls.map(...).reduce(....).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started