/** * Returns a new collection containing all elements * in the passed (old) collection where the passed * select function outputs true when invoked */ select(selectFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { if (selectFct(nextElem)) newElements.add(nextElem); } return QueryArray.from(newElements); } /** * returns a new collection containing all elements in * the passed collection by rejecting those that yield * "true" using the passed reject function. */ reject(rejectFct) { let inverseFct = (elem) => !rejectFct(elem); return this.select(inverseFct); } /** Returns the index of the first match, or -1 if no match */ findFirst(matchFct) { let elements = this.elements; for (let i = 0; i < elements.length; i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } /** Returns the index of the last match, or -1 if no match */ findLast(matchFct) { let elements = this.elements; for (let i = elements.length - 1; i >= 0; i--) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } /** * Returns a new QueryArray containing the elements of this list * between the given index start (inclusive) and the given * index stop (exclusive). */ subList(start, stop) { let resultSubList = Array.newDynamic(); for (let i = start; i < stop; i++) resultSubList.add(this.get(i)); return QueryArray.from(resultSubList); } /** Returns the element at the passed zero-based index */ get(index) { if (index < 0 || index >= this.elements.length) throw "(get) index is out of bounds: " + index; return this.elements[index]; } /** * Returns new QueryArray where mapFct is used to generate * new elements from existing elements in "this" object */ map(mapFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return QueryArray.from(newElements); }
class QueryArray { // ----------------------------------------------------- //Instance Variables (Ivars) //this.elements -- a fixed array holding the elements // ----------------------------------------------------- //Constructors /** Static factory methods that returns new QueryArray using the passed fixed array "array" */ static from(array) { //Call traditional constructor return new this(array); } /** Traditional constructor -- set ivar elements to passed array */ constructor(array) { this.elements = array; } // ----------------------------------------------------- //Instance Methods - Core, Convenience /** Returns nice display string */ toString() { return this.elements.toString(); } forEach(actionFct) { for (let nextElem of this.elements) actionFct(nextElem); } /** Prints all elements to console (with newline after each */ printAll(listLabel) { println(listLabel); this.forEach(nextElem => println(nextElem.toString())); } // ----------------------------------------------------- //Specified Methods /** Returns a new collection containing all elements * in the passed (old) collection where the passed * select function outputs true when invoked */ select(selectFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { if (selectFct(nextElem)) newElements.add(nextElem); } return QueryArray.from(newElements); } /** returns a new collection containing all elements in * the passed collection by rejecting those that yield * "true" using the passed reject function. */ reject(rejectFct) { let inverseFct = (elem) => !rejectFct(elem); return this.select(inverseFct); } /** Returns the index of the first match, or -1 if no match */ findFirst(matchFct) { let elements = this.elements; for (let i = 0; i < elements.length; i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } /** Returns the index of the last match, or -1 if no match */ findLast(matchFct) { let elements = this.elements; for (let i = elements.length - 1; i >= 0; i--) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } /** Returns a new QueryArray containing the elements of this list * between the given index start (inclusive) and the given * index stop (exclusive). */ subList(start, stop) { let resultSubList = Array.newDynamic(); for (let i = start; i < stop; i++) resultSubList.add(this.get(i)); return QueryArray.from(resultSubList); } /** Returns the element at the passed zero-based index */ get(index) { if (index < 0 || index >= this.elements.length) throw "(get) index is out of bounds: " + index; return this.elements[index]; } /** Returns new QueryArray where mapFct is used to generate * new elements from existing elements in "this" object */ map(mapFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return QueryArray.from(newElements); } /** Accumulate a value by iterating over the collection * and accumulating the next value using the fct */ accumulate(accumulatorFct, initialValue) { let accumulation = initialValue; for (let each of this.elements) accumulation = accumulatorFct(accumulation, each); return accumulation; } } _br_ _br_//Try It var fixed, qarray, isGhanaFct, selected, selectedByReject, mapped, getCountryFct; fixed = Mathematician.newSampleSet1(); qarray = QueryArray.from(fixed); //var "m" is a mathemetician isGhanaFct = (m) => m.getCountry().equals("Ghana"); selected = qarray.select(isGhanaFct); selected.printAll("-- Select Ghana --"); selectedByReject = qarray.reject(isGhanaFct); selectedByReject.printAll("-- Reject Ghana --"); getCountryFct = (m) => m.getCountry(); mapped = qarray.map(getCountryFct); mapped.printAll("-- All Countries --"); //CODE HERE -- try the other specified methods
class QueryArray { // ----------------------------------------------------- //Instance Variables (Ivars) //this.elements -- a fixed array holding the elements // ----------------------------------------------------- //Constructors /** Static factory methods that returns new QueryArray using the passed fixed array "array" */ static from(array) { //Call traditional constructor return new this(array); } /** Traditional constructor -- set ivar elements to passed array */ constructor(array) { this.elements = array; } // ----------------------------------------------------- //Instance Methods - Core, Convenience /** Returns nice display string */ toString() { return this.elements.toString(); } forEach(actionFct) { for (let nextElem of this.elements) actionFct(nextElem); } /** Prints all elements to console (with newline after each */ printAll(listLabel) { println(listLabel); this.forEach(nextElem => println(nextElem.toString())); } // ----------------------------------------------------- //Specified Methods /** Returns a new collection containing all elements * in the passed (old) collection where the passed * select function outputs true when invoked */ select(selectFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { if (selectFct(nextElem)) newElements.add(nextElem); } return QueryArray.from(newElements); } /** returns a new collection containing all elements in * the passed collection by rejecting those that yield * "true" using the passed reject function. */ reject(rejectFct) { let inverseFct = (elem) => !rejectFct(elem); return this.select(inverseFct); } /** Returns the index of the first match, or -1 if no match */ findFirst(matchFct) { let elements = this.elements; for (let i = 0; i < elements.length; i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } /** Returns the index of the last match, or -1 if no match */ findLast(matchFct) { let elements = this.elements; for (let i = elements.length - 1; i >= 0; i--) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } /** Returns a new QueryArray containing the elements of this list * between the given index start (inclusive) and the given * index stop (exclusive). */ subList(start, stop) { let resultSubList = Array.newDynamic(); for (let i = start; i < stop; i++) resultSubList.add(this.get(i)); return QueryArray.from(resultSubList); } /** Returns the element at the passed zero-based index */ get(index) { if (index < 0 || index >= this.elements.length) throw "(get) index is out of bounds: " + index; return this.elements[index]; } /** Returns new QueryArray where mapFct is used to generate * new elements from existing elements in "this" object */ map(mapFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return QueryArray.from(newElements); } /** Accumulate a value by iterating over the collection * and accumulating the next value using the fct */ accumulate(accumulatorFct, initialValue) { let accumulation = initialValue; for (let each of this.elements) accumulation = accumulatorFct(accumulation, each); return accumulation; } } _br_ _br_//Try It var fixed, rectangles, sumOfWidths; fixed = Rectangle.newSampleSet1(); rectangles = QueryArray.from(fixed); rectangles.printAll("-- All --"); //sum sumOfWidths = rectangles.accumulate((sum, each) => sum + each.getWidth(), 0) println("sumOfWidths: " + sumOfWidths);