Skip to content

Commit d29975f

Browse files
author
John Sanda
committed
completed exercises
1 parent 86f8298 commit d29975f

3 files changed

Lines changed: 134 additions & 73 deletions

File tree

src/main/java/learnrxjava/ComposableListExercises.java

Lines changed: 93 additions & 55 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

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

137137
/*
@@ -146,10 +146,10 @@ public static ComposableList<JSON> exercise5() {
146146
new Video(654356453, "Bad Boys", 5.0),
147147
new Video(65432445, "The Chamber", 4.0),
148148
new Video(675465, "Fracture", 5.0));
149-
150-
// complete this expression
151-
// return newReleases.map(video ->
152-
throw new UnsupportedOperationException("Not implemented yet.");
149+
150+
// complete this expression
151+
return newReleases.map(video -> json("id", video.id, "title", video.title));
152+
// throw new UnsupportedOperationException("Not implemented yet.");
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

195-
// return highRatedVideos;
196-
throw new UnsupportedOperationException("Not implemented yet.");
197+
return highRatedVideos;
198+
// throw new UnsupportedOperationException("Not implemented yet.");
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

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

231235
/*
@@ -266,7 +270,9 @@ 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(video -> video.rating == 5.0).map(video -> video.id);
274+
275+
// throw new UnsupportedOperationException("Not implemented yet.");
270276
}
271277

272278
/*
@@ -299,9 +305,12 @@ public static ComposableList<Integer> exercise9() {
299305
// ------------ INSERT CODE HERE! -----------------------------------
300306
// Use two nested forEach loops to flatten the movieLists into a list of
301307
// video ids.
308+
movieLists.forEach(list -> {
309+
list.videos.forEach(video -> allVideoIdsInMovieLists.add(video.id));
310+
});
302311
// ------------ INSERT CODE HERE! -----------------------------------
303-
//return allVideoIdsInMovieLists;
304-
throw new UnsupportedOperationException("Not implemented yet.");
312+
return allVideoIdsInMovieLists;
313+
// throw new UnsupportedOperationException("Not implemented yet.");
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(results::add);
342351
}
343352

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

348357
/*
@@ -371,12 +380,14 @@ public static ComposableList<Integer> exercise11() {
371380
// Use map and concatAll to flatten the movieLists in a list of video ids.
372381
// return movieLists // finish expression
373382
// ------------ INSERT CODE HERE! -----------------------------------
383+
return movieLists.concatMap(movieList -> movieList.videos.map(video -> video.id));
384+
374385
// **************ANSWER START***************//
375386
// return movieLists.
376387
// concatMap(movieList ->
377388
// movieList.videos.map(video -> video.id));
378389
// **************ANSWER END***************//
379-
throw new UnsupportedOperationException("Not implemented yet.");
390+
// throw new UnsupportedOperationException("Not implemented yet.");
380391
}
381392

382393
/*
@@ -449,8 +460,12 @@ public static ComposableList<JSON> exercise12() {
449460
// {"id": 675465,"title": "Fracture","boxart":"http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" },
450461
// };
451462

452-
// return movieLists // Complete this expression!
453-
throw new UnsupportedOperationException("Not implemented yet.");
463+
return movieLists.concatMap(movieList ->
464+
movieList.videos.concatMap(video ->
465+
video.boxarts.filter(boxArt -> boxArt.width == 150 && boxArt.height == 200)
466+
.map((BoxArt filtered) -> json("id", video.id, "title", video.title, "boxart", filtered.url))));
467+
468+
// throw new UnsupportedOperationException("Not implemented yet.");
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

492-
// return largestBoxart;
493-
throw new UnsupportedOperationException("Not implemented yet.");
509+
return largestBoxart;
510+
// throw new UnsupportedOperationException("Not implemented yet.");
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,14 @@ 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 ********
534-
552+
accumulatedValue = combiner.apply(accumulatedValue, this.get(counter));
535553
counter++;
536554
}
537555

538-
//return ComposableListExercises.of(accumulatedValue);
556+
return ComposableListExercises.of(accumulatedValue);
539557
}
540558

541-
throw new UnsupportedOperationException("Not implemented yet.");
559+
// throw new UnsupportedOperationException("Not implemented yet.");
542560
}
543561

544562
/*
@@ -568,7 +586,7 @@ public <R> ComposableList<R> reduce(R initialValue, BiFunction<R, T, R> combiner
568586

569587
// If the list is empty, do nothing
570588
if (this.size() == 0) {
571-
return new ComposableListExercises<R>();
589+
return new ComposableListExercises<>();
572590
} else {
573591
counter = 0;
574592
accumulatedValue = initialValue;
@@ -596,10 +614,12 @@ public static ComposableList<Integer> exercise16() {
596614
// returns a list with one item.
597615

598616
// complete the expression below
599-
//return ratings.reduce
617+
return ratings.reduce(ratings.get(0), (acc, curr) -> curr > acc ? curr : acc);
600618

601-
throw new UnsupportedOperationException("Not implemented yet.");
619+
// throw new UnsupportedOperationException("Not implemented yet.");
602620
}
621+
622+
static Function<BoxArt, Integer> size = boxArt -> boxArt.width * boxArt.height;
603623
/*
604624
Exercise 17: Retrieve url of the largest boxart
605625
@@ -616,8 +636,10 @@ public static ComposableList<String> exercise17() {
616636

617637
// You should return a list containing only the largest box art. Remember that reduce always
618638
// returns a list with one item.
619-
// return boxarts.reduce
620-
throw new UnsupportedOperationException("Not implemented yet.");
639+
return boxarts.reduce(boxarts.get(0), (acc, curr) -> size.apply(curr) > size.apply(acc) ? curr : acc)
640+
.map(boxArt -> boxArt.url);
641+
642+
// throw new UnsupportedOperationException("Not implemented yet.");
621643
}
622644

623645
/*
@@ -666,7 +688,7 @@ public static ComposableList<Map<Integer, String>> exercise18() {
666688
reduce(
667689
// Use an empty map as the initial value instead of the first item in
668690
// the list.
669-
new HashMap<Integer, String> (),
691+
new HashMap<>(),
670692
(accumulatedMap, video) -> {
671693
// ************ INSERT CODE HERE ************
672694
// Remember that you the functions passed to map, filter, concatMap, reduce, and zip can only
@@ -677,7 +699,11 @@ public static ComposableList<Map<Integer, String>> exercise18() {
677699
// exercise simply copy the accumulatedMap into a new map, add the video information to the copy,
678700
// and return the copy.
679701
// ************ INSERT CODE HERE ************
680-
throw new UnsupportedOperationException("Not implemented yet.");
702+
HashMap<Integer, String> newMap = new HashMap<>();
703+
newMap.putAll(accumulatedMap);
704+
newMap.put(video.id, video.title);
705+
return newMap;
706+
// throw new UnsupportedOperationException("Not implemented yet.");
681707
});
682708
}
683709

@@ -750,15 +776,15 @@ public static ComposableList<JSON> exercise19() {
750776
// ];
751777

752778
// Uncomment the code below and finish the expression.
753-
/*
754-
return movieLists.
755-
concatMap(movieList -> {
756-
757-
})
758-
*/
759-
throw new UnsupportedOperationException("Not implemented yet.");
779+
return movieLists.concatMap(movieList ->
780+
movieList.videos.concatMap(video ->
781+
video.boxarts.reduce(video.boxarts.get(0), minSize).map(boxArt ->
782+
json("id", video.id, "title", video.title, "boxart", boxArt.url))));
760783
}
761784

785+
static BiFunction<BoxArt, BoxArt, BoxArt> minSize = (left, right) ->
786+
size.apply(left) < size.apply(right) ? left : right;
787+
762788
/*
763789
Exercise 20: Zipping Lists
764790
@@ -805,10 +831,12 @@ public static ComposableList<JSON> exercise20() {
805831
for (int counter = 0; counter < Math.min(videos.size(), bookmarks.size()); counter++) {
806832
// Insert code here to create a {"videoId" : videoId, "bookmarkId" : bookmarkId} JSON
807833
// using json() and add it to the videoIdAndBookmarkIdPairs list.
834+
videoIdAndBookmarkIdPairs.add(
835+
json("videoId", videos.get(counter).id, "bookmarkId", bookmarks.get(counter).id));
808836
}
809837

810-
// return videoIdAndBookmarkIdPairs;
811-
throw new UnsupportedOperationException("Not implemented yet.");
838+
return videoIdAndBookmarkIdPairs;
839+
// throw new UnsupportedOperationException("Not implemented yet.");
812840
}
813841

814842
/*
@@ -825,16 +853,18 @@ public static ComposableList<JSON> exercise20() {
825853
ComposableList.of(4,5,6),
826854
(x,y) -> x + y) is structurally equivalent to ComposableList.of(5,7,9)
827855
*/
828-
public static <T0,T1,R> ComposableList<R> zip(ComposableList<T0> left, ComposableList<T1> right, BiFunction<T0,T1, R> combinerFunction) {
856+
public static <T0,T1,R> ComposableList<R> zip(ComposableList<T0> left, ComposableList<T1> right, BiFunction<T0, T1,
857+
R> combiner) {
829858
ComposableListExercises<R> results = new ComposableListExercises<R>();
830859

831860
for (int counter = 0; counter < Math.min(left.size(), right.size()); counter++) {
832861
// Add code here to apply the combinerFunction to the left and right-hand items in the
833862
// respective lists, and add the result to the results List
863+
results.add(combiner.apply(left.get(counter), right.get(counter)));
834864
}
835865

836-
// return results;
837-
throw new UnsupportedOperationException("Not implemented yet.");
866+
return results;
867+
// throw new UnsupportedOperationException("Not implemented yet.");
838868
}
839869

840870
/*
@@ -875,8 +905,9 @@ public static ComposableList<JSON> exercise22() {
875905
);
876906

877907
//... finish this expression
878-
// return ComposableListExercises.zip(
879-
throw new UnsupportedOperationException("Not implemented yet.");
908+
return ComposableListExercises.zip(videos, bookmarks, ((video, bookmark) ->
909+
json("videoId", video.id, "bookmarkId", bookmark.id)));
910+
// throw new UnsupportedOperationException("Not implemented yet.");
880911
}
881912

882913
/*
@@ -962,15 +993,22 @@ public static ComposableList<JSON> exercise23() {
962993
)
963994
);
964995

965-
//------------ COMPLETE THIS EXPRESSION --------------
966-
/*
967-
return movieLists.
968-
concatMap(movieList -> {
996+
Function<ComposableList<BoxArt>, ComposableList<BoxArt>> reduceSmallest = boxArts ->
997+
boxArts.reduce(boxArts.get(0), minSize);
969998

970-
});
971-
*/
999+
Predicate<InterestingMoment> isMiddle = interestingMoment -> interestingMoment.type.equals("Middle");
9721000

973-
throw new UnsupportedOperationException("Not implemented yet.");
1001+
//------------ COMPLETE THIS EXPRESSION --------------
1002+
return movieLists.
1003+
concatMap(movieList ->
1004+
movieList.videos.concatMap(video ->
1005+
zip(reduceSmallest.apply(video.boxarts), video.interestingMoments.filter(isMiddle),
1006+
(boxArt, moment) ->
1007+
json("id", video.id, "title", video.title, "time", moment.time, "url",
1008+
boxArt.url)
1009+
)));
1010+
1011+
// throw new UnsupportedOperationException("Not implemented yet.");
9741012
}
9751013

9761014

0 commit comments

Comments
 (0)