Skip to content

Commit c6f43ee

Browse files
committed
Solution
1 parent 86f8298 commit c6f43ee

File tree

2 files changed

+129
-52
lines changed

2 files changed

+129
-52
lines changed

src/main/java/learnrxjava/ComposableListExercises.java

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public <R> ComposableList<R> map(Function<T, R> projectionFunction) {
127127
// Note that you can apply a projectionFunction to a value like this:
128128
// projectionFunction.apply(5)
129129
// ------------ INSERT CODE HERE! ----------------------------
130-
130+
results.add(projectionFunction.apply(itemInList));
131131
});
132132

133133
//return results;
134-
throw new UnsupportedOperationException("Not implemented yet.");
134+
return results;
135135
}
136136

137137
/*
@@ -149,7 +149,7 @@ public static ComposableList<JSON> exercise5() {
149149

150150
// complete this expression
151151
// return newReleases.map(video ->
152-
throw new UnsupportedOperationException("Not implemented yet.");
152+
return newReleases.map(v -> json("id", v.id, "title", v.title));
153153
}
154154

155155
/*
@@ -189,11 +189,13 @@ public static ComposableList<Video> exercise6() {
189189
newReleases.forEach(video -> {
190190
// Insert code here that adds a video to the highRatedVideos list
191191
// if it has a rating of 5.0.
192-
192+
if (video.rating == 5.0) {
193+
highRatedVideos.add(video);
194+
}
193195
});
194196

195197
// return highRatedVideos;
196-
throw new UnsupportedOperationException("Not implemented yet.");
198+
return highRatedVideos;
197199
}
198200

199201
/*
@@ -221,11 +223,13 @@ public ComposableList<T> filter(Predicate<T> predicateFunction) {
221223
// Note: you can apply the predicateFunction to a value like this:
222224
// predicateFunction.test(5)
223225
// ------------ INSERT CODE HERE! ----------------------------
224-
226+
if (predicateFunction.test(itemInList)) {
227+
results.add(itemInList);
228+
}
225229
});
226230

227231
// return results;
228-
throw new UnsupportedOperationException("Not implemented yet.");
232+
return results;
229233
}
230234

231235
/*
@@ -266,7 +270,7 @@ public static ComposableList<Integer> exercise8() {
266270
// ------------ INSERT CODE HERE! -----------------------------------
267271
//return newReleases // Complete this expression
268272

269-
throw new UnsupportedOperationException("Not implemented yet.");
273+
return newReleases.filter(x -> x.rating == 5.0).map(x -> x.id);
270274
}
271275

272276
/*
@@ -299,9 +303,14 @@ public static ComposableList<Integer> exercise9() {
299303
// ------------ INSERT CODE HERE! -----------------------------------
300304
// Use two nested forEach loops to flatten the movieLists into a list of
301305
// video ids.
306+
307+
movieLists.forEach(movie -> {
308+
movie.videos.forEach(video -> allVideoIdsInMovieLists.add(video.id));
309+
});
310+
302311
// ------------ INSERT CODE HERE! -----------------------------------
303312
//return allVideoIdsInMovieLists;
304-
throw new UnsupportedOperationException("Not implemented yet.");
313+
return allVideoIdsInMovieLists;
305314
}
306315

307316
/*
@@ -338,11 +347,11 @@ public <R> ComposableList<R> concatMap(
338347
// Note that you can apply a projectionFunction to a value like this:
339348
// projectionFunctionThatReturnsList.apply(5)
340349
// ------------ INSERT CODE HERE! ----------------------------
341-
350+
projectionFunctionThatReturnsList.apply(itemInList).forEach(item -> results.add(item));
342351
}
343352

344353
//return results;
345-
throw new UnsupportedOperationException("Not implemented yet.");
354+
return results;
346355
}
347356

348357
/*
@@ -376,7 +385,7 @@ public static ComposableList<Integer> exercise11() {
376385
// concatMap(movieList ->
377386
// movieList.videos.map(video -> video.id));
378387
// **************ANSWER END***************//
379-
throw new UnsupportedOperationException("Not implemented yet.");
388+
return movieLists.concatMap(movieList -> movieList.videos.map(video -> video.id));
380389
}
381390

382391
/*
@@ -450,7 +459,13 @@ public static ComposableList<JSON> exercise12() {
450459
// };
451460

452461
// return movieLists // Complete this expression!
453-
throw new UnsupportedOperationException("Not implemented yet.");
462+
return movieLists.concatMap(movieList ->
463+
movieList.videos.concatMap(v -> {
464+
ComposableList<BoxArt> boxArts = v.boxarts.filter(ba -> ba.width == 150 && ba.height == 200);
465+
return boxArts.map(ba ->
466+
json("id", v.id, "title", v.title, "boxart", ba.url)
467+
);
468+
}));
454469
}
455470

456471
/*
@@ -486,11 +501,13 @@ public static BoxArt exercise13() {
486501
// ****** INSERT CODE HERE ********
487502
// Assign the largestBoxart to the current boxart, and assign the maxSize to the currentSize.
488503
// ****** INSERT CODE HERE ********
504+
largestBoxart = boxart;
505+
maxSize = currentSize;
489506
}
490507
}
491508

492509
// return largestBoxart;
493-
throw new UnsupportedOperationException("Not implemented yet.");
510+
return largestBoxart;
494511
}
495512
/*
496513
Exercise 14: Implement reduce()
@@ -520,6 +537,7 @@ public ComposableList<T> reduce(BiFunction<T, T, T> combiner) {
520537
// ************ INSERT CODE HERE **************
521538
// if the list is empty, return this
522539
// ********************************************
540+
return this;
523541
} else {
524542
accumulatedValue = this.get(0);
525543

@@ -531,14 +549,13 @@ public ComposableList<T> reduce(BiFunction<T, T, T> combiner) {
531549
// Set accumulatedValue to the result of passing accumulatedValue and the list value at the
532550
// counter index to the combiner function.
533551
// ****** INSERT CODE HERE ********
552+
accumulatedValue = combiner.apply(this.get(counter), accumulatedValue);
534553

535554
counter++;
536555
}
537556

538-
//return ComposableListExercises.of(accumulatedValue);
557+
return ComposableListExercises.of(accumulatedValue);
539558
}
540-
541-
throw new UnsupportedOperationException("Not implemented yet.");
542559
}
543560

544561
/*
@@ -568,7 +585,7 @@ public <R> ComposableList<R> reduce(R initialValue, BiFunction<R, T, R> combiner
568585

569586
// If the list is empty, do nothing
570587
if (this.size() == 0) {
571-
return new ComposableListExercises<R>();
588+
return new ComposableListExercises<>();
572589
} else {
573590
counter = 0;
574591
accumulatedValue = initialValue;
@@ -598,7 +615,13 @@ public static ComposableList<Integer> exercise16() {
598615
// complete the expression below
599616
//return ratings.reduce
600617

601-
throw new UnsupportedOperationException("Not implemented yet.");
618+
return ratings.reduce((x, y) -> {
619+
if(x > y) {
620+
return x;
621+
} else {
622+
return y;
623+
}
624+
});
602625
}
603626
/*
604627
Exercise 17: Retrieve url of the largest boxart
@@ -617,7 +640,13 @@ public static ComposableList<String> exercise17() {
617640
// You should return a list containing only the largest box art. Remember that reduce always
618641
// returns a list with one item.
619642
// return boxarts.reduce
620-
throw new UnsupportedOperationException("Not implemented yet.");
643+
return boxarts.reduce((x, y) -> {
644+
if (x.height * x.width > y.height * y.width) {
645+
return x;
646+
} else {
647+
return y;
648+
}
649+
}).map(boxArt -> boxArt.url);
621650
}
622651

623652
/*
@@ -677,7 +706,9 @@ public static ComposableList<Map<Integer, String>> exercise18() {
677706
// exercise simply copy the accumulatedMap into a new map, add the video information to the copy,
678707
// and return the copy.
679708
// ************ INSERT CODE HERE ************
680-
throw new UnsupportedOperationException("Not implemented yet.");
709+
final HashMap<Integer, String> copy = new HashMap<>(accumulatedMap);
710+
copy.put(video.id, video.title);
711+
return copy;
681712
});
682713
}
683714

@@ -750,13 +781,22 @@ public static ComposableList<JSON> exercise19() {
750781
// ];
751782

752783
// Uncomment the code below and finish the expression.
753-
/*
754784
return movieLists.
755-
concatMap(movieList -> {
756-
757-
})
758-
*/
759-
throw new UnsupportedOperationException("Not implemented yet.");
785+
concatMap(ml -> {
786+
return ml.videos.concatMap(v -> {
787+
return v.boxarts.reduce((max, box) -> {
788+
int maxSize = max.height * max.width;
789+
int boxSize = box.height * box.width;
790+
if(boxSize < maxSize) {
791+
return box;
792+
} else {
793+
return max;
794+
}
795+
}).map(maxBoxart -> {
796+
return json("id", v.id, "title", v.title, "boxart", maxBoxart.url);
797+
});
798+
});
799+
});
760800
}
761801

762802
/*
@@ -805,10 +845,13 @@ public static ComposableList<JSON> exercise20() {
805845
for (int counter = 0; counter < Math.min(videos.size(), bookmarks.size()); counter++) {
806846
// Insert code here to create a {"videoId" : videoId, "bookmarkId" : bookmarkId} JSON
807847
// using json() and add it to the videoIdAndBookmarkIdPairs list.
848+
final Video video = videos.get(counter);
849+
final Bookmark bookmark = bookmarks.get(counter);
850+
videoIdAndBookmarkIdPairs.add(json("videoId", video.id, "bookmarkId", bookmark.id));
808851
}
809852

810853
// return videoIdAndBookmarkIdPairs;
811-
throw new UnsupportedOperationException("Not implemented yet.");
854+
return videoIdAndBookmarkIdPairs;
812855
}
813856

814857
/*
@@ -831,10 +874,11 @@ public static <T0,T1,R> ComposableList<R> zip(ComposableList<T0> left, Composabl
831874
for (int counter = 0; counter < Math.min(left.size(), right.size()); counter++) {
832875
// Add code here to apply the combinerFunction to the left and right-hand items in the
833876
// respective lists, and add the result to the results List
877+
results.add(
878+
combinerFunction.apply(left.get(counter), right.get(counter))
879+
);
834880
}
835-
836-
// return results;
837-
throw new UnsupportedOperationException("Not implemented yet.");
881+
return results;
838882
}
839883

840884
/*
@@ -875,8 +919,8 @@ public static ComposableList<JSON> exercise22() {
875919
);
876920

877921
//... finish this expression
878-
// return ComposableListExercises.zip(
879-
throw new UnsupportedOperationException("Not implemented yet.");
922+
return ComposableListExercises.zip(videos, bookmarks, (v, b) ->
923+
json("videoId", v.id, "bookmarkId", b.id));
880924
}
881925

882926
/*
@@ -963,14 +1007,25 @@ public static ComposableList<JSON> exercise23() {
9631007
);
9641008

9651009
//------------ COMPLETE THIS EXPRESSION --------------
966-
/*
9671010
return movieLists.
968-
concatMap(movieList -> {
969-
970-
});
971-
*/
972-
973-
throw new UnsupportedOperationException("Not implemented yet.");
1011+
concatMap(movieList -> {
1012+
return movieList.videos.concatMap(video -> {
1013+
ComposableList<BoxArt> smallestBoxArt = video.boxarts.reduce((smallest, box) -> {
1014+
int smallestSize = smallest.height * smallest.width;
1015+
int boxSize = box.height * box.width;
1016+
if(boxSize < smallestSize) {
1017+
return box;
1018+
} else {
1019+
return smallest;
1020+
}
1021+
});
1022+
1023+
ComposableList<InterestingMoment> moment = video.interestingMoments.filter(m -> m.type == "Middle");
1024+
return ComposableListSolutions.zip(smallestBoxArt, moment, (s, m) -> {
1025+
return json("id", video.id, "title", video.title, "time", m.time, "url", s.url);
1026+
});
1027+
});
1028+
});
9741029
}
9751030

9761031

0 commit comments

Comments
 (0)