subject

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 {
// apply(p) runs some computation on param of type T and returns a value of type R
public R apply(T param);
}
a) Write a class implementing the Functor interface, called LengthFun.
Its apply() function returns the length (Integer) of a String parameter.
Write a LengthFun. main() function that:
1. illustrates how the class is used to print the length of a string.
2. instantiates a lambda expression of type Functor that does the same
thing as LengthFun. apply(), and uses it to print the length of a string.
b) Define a subclass of LinkedList, called MyList, that has an additional
generic function that "maps" the elements from the list to a new MyList object through a functor object.
The signature of the MyList. map() method is:
public MyList map(Functor fo) {
// write here the code for the map() function.
}
For an object mylist the mylist. map(fo) creates a new MyList object,
iterates over the elements of mylist and appends values fo. apply(current list element)
to the new list. At the end map() returns the new list.
For instance, consider a TimesTwoFun functor whose
Integer apply(Integer param)
function returns 2*param. intValue(). Define a variable tt = new TimesTwoFun();
Now, suppose the elements of a MyList object lst are (-2,1,0,4). Then ,
the new MyList object returned by the lst. map(tt) will have elements (-4,2,0,8).
That is, the lst. map(fo) function creates a new MyList object with elements equal to fo. apply(x),
for each element x from the lst list.
Write the TimesTwoFun class.
Write the MyList class that extends LinkedList, with the only custom method being map()
(all others are inherited from LinkedList).
Write a main() function in MyList. java that:
1. implements the example defined above with the TimesTwoFun class.
2. repeats the same thing using a lambda expression insted of the TimesTwoFun object.

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 20:20
Sometimes writers elaborate on the truth when recalling past events so they can enhance their narrative essay with more interesting descriptions. do you feel that published writers should or should not embellish real life events just to make their stories more interesting?
Answers: 2
question
Computers and Technology, 22.06.2019 17:00
Which of the following is not contained on the slide show toolbar? a. next button b. slide button c. close button d. pen tool
Answers: 1
question
Computers and Technology, 22.06.2019 17:00
Aisha has finished working on a word processing document that contains 15 pages. she has added some special elements in the first three pages, page 9 and 10, and page 15 from the document. she wants to print only these pages to see how they look. which option is the correct way to represent (in the print dialog box) the pages that aisha wants to print
Answers: 3
question
Computers and Technology, 23.06.2019 06:30
You have a small company and want to keep your costs low, but it is important your employees share data. which network would provide you with the most economical solution?
Answers: 1
You know the right answer?
A functor is an object that encapsulates a function. Its apply() method takes one parameter, applie...
Questions
question
Mathematics, 30.09.2020 01:01
question
Mathematics, 30.09.2020 01:01
Questions on the website: 13722363