Skip to content

Instantly share code, notes, and snippets.

@bsitruk
Last active November 20, 2015 14:18
Show Gist options
  • Save bsitruk/c429cdf4c546675afda6 to your computer and use it in GitHub Desktop.
Save bsitruk/c429cdf4c546675afda6 to your computer and use it in GitHub Desktop.
Functional programming: Combine map with zip
function() {
var movieLists = [
{
name: "New Releases",
videos: [
{
"id": 70111470,
"title": "Die Hard",
"boxarts": [
{ width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" },
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"interestingMoments": [
{ type: "End", time:213432 },
{ type: "Start", time: 64534 },
{ type: "Middle", time: 323133}
]
},
{
"id": 654356453,
"title": "Bad Boys",
"boxarts": [
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" },
{ width: 140, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"interestingMoments": [
{ type: "End", time:54654754 },
{ type: "Start", time: 43524243 },
{ type: "Middle", time: 6575665}
]
}
]
},
{
name: "Instant Queue",
videos: [
{
"id": 65432445,
"title": "The Chamber",
"boxarts": [
{ width: 130, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" },
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"interestingMoments": [
{ type: "End", time:132423 },
{ type: "Start", time: 54637425 },
{ type: "Middle", time: 3452343}
]
},
{
"id": 675465,
"title": "Fracture",
"boxarts": [
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" },
{ width: 120, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" },
{ width: 300, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"interestingMoments": [
{ type: "End", time:45632456 },
{ type: "Start", time: 234534 },
{ type: "Middle", time: 3453434}
]
}
]
}
];
//------------ COMPLETE THIS EXPRESSION --------------
return movieLists.
concatMap(function(movieList) {
return movieList.videos.concatMap(function(video) {
return Array.zip(
video.boxarts.reduce(function(acc, curr) {
if (curr.width * curr.height < acc.width * acc.height) {
return curr;
} else {
return acc;
}
}), // [{smallestBoxart}]
video.interestingMoments.filter(function(moment) {
return moment.type === "Middle";
}), // [{middleMoment}]
function(boxart, moment) {
return {
id: video.id,
title: video.title,
time: moment.time,
url: boxart.url
}
}
); // [{id, title, time, url}]
}); // [{id, title, time, url} * video.length]
}); // [{id, title, time, url} * video.length * movieList.length]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment