Write a method named "select" that returns a new QueryArray containing all elements in the passed (old) collection where the passed select function outputs true when invoked.
Note the return type: QueryArray.
Let's say we have a collection of five Mathematician objects:
Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
And that two of them have a country of Ghana. We could select (keep) these objects like this:
let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let selectFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
selected = qarray.select(selectFct);
selected.printAll();
Write a function named reject that returns a new QueryArray containing all elements in the passed (old) collection. The new collection rejects elements in the old collection using the passed reject function.
The method "reject" is the inverse of the method "select."
Note the return type: QueryArray.
Let's say we have a collection of five Mathematician objects:
Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
And that two of them have a country of Ghana. We could reject these objects like this:
let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let rejectFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
newQarray = qarray.reject(rejectFct);
newQarray.printAll();
Expected results:
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
Write a method named findFirst that returns the index of the last match, or -1 if no match. by "last match" we mean the match closes to the end of the input array. Hint: start the search at the end and work backwards.
Method header: "findFirst(matchFct)"
The passed matchFct is invoked with input that is an old element (from the passed elements array) and it outputs true if the given element is a match.
Let's say we have a collection of five Mathematician objects:
Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
Let's say we want to find the last match for the country of Ghana. We could do it like this:
let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let matchFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
let matchIndex = qarray.findFirst(matchFct);
println("Match Index: " + matchIndex);
Write a method named findLast that returns the index of the last match, or -1 if no match. by "last match" we mean the match closes to the end of the input array. Hint: start the search at the end and work backwards.
Method header: "findLast(matchFct)"
The passed matchFct is invoked with input that is an old element (from the passed elements array) and it outputs true if the given element is a match.
Let's say we have a collection of five Mathematician objects:
Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
Let's say we want to find the last match for the country of Ghana. We could do it like this:
let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let matchFct = (aMathematician) => aMathematician.getCountry().equals("Ghana");
let matchIndex = qarray.findLast(matchFct);
println("Match Index: " + matchIndex);
Write a method subList(start, stop) that returns a new QueryArray containing the elements of this list between the given index start (inclusive) and the given index stop (exclusive).
Method header: "subList(start, stop)"
The passed parameters are integers.
Let's say we have a collection of five Mathematician objects:
Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
Let's say we want to get a sub list from index 2 to 3. We could do it like this:
let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
//Note the stop parameter is exclusive so we use index=4 (so we include index=3)let subList = qarray.subList(2, 4);
subList.printAll();
Write a method map that returns a new QueryArray which is the target (this) QueryArray mapped (converted) to new elements using the passed mapping function.
Note the return type: QueryArray.
Let's say we have a collection of five Mathematician objects:
Daniel Akyeampong (Ghana)
Francis Allotey (Ghana)
Isabel Hubard (Mexico)
Cecilia Mwathi (Kenya)
George Saitoti (Kenya)
Let's say we want to map the objects in country names.
let array = Mathematician.newSampleSet1();
let qarray = QueryArray.from(array);
let mapFct = (aMathematician) => aMathematician.getCountry();
mapped = qarray.map(mapFct);
mapped.printAll();