Skip to content

Instantly share code, notes, and snippets.

@kavitshah8
Last active May 24, 2018 05:59
Show Gist options
  • Save kavitshah8/b8190f6b7bfda4fc9ff3 to your computer and use it in GitHub Desktop.
Save kavitshah8/b8190f6b7bfda4fc9ff3 to your computer and use it in GitHub Desktop.
Functional Programming in Javascript
'use strict';
function chain() {
var newReleases = [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}, {
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}];
var result = [];
return newReleases.
filter(video => video.rating === 5.0).
map(video => video.id);
}
console.log(chain());
/*
[654356453, 675465]
*/
'use strict';
function declarativeSquare(arr) {
/* You have to specify the followings :
1. Logic
*/
return arr.map(function(val) {
return val*val;
});
}
var a = [1, 2, 3, 4, 5];
console.log(declarativeSquare(a)); // [1, 4, 9, 16, 25]
'use strict';
function flattenThreeLevelNestedTree() {
var movieLists = [{
name: "Instant Queue",
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"
}]
}, {
"id": 654356453,
"title": "Bad Boys",
"boxarts": [{
width: 200,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg"
}, {
width: 150,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg"
}]
}]
}, {
name: "New Releases",
videos: [{
"id": 65432445,
"title": "The Chamber",
"boxarts": [{
width: 150,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/TheChamber150.jpg"
}, {
width: 200,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg"
}]
}, {
"id": 675465,
"title": "Fracture",
"boxarts": [{
width: 200,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg"
}, {
width: 150,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg"
}, {
width: 300,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg"
}]
}]
}];
// Use one or more map, concatAll, and filter calls to create an array with the following items
// [
// {"id": 675465,"title": "Fracture","boxart":"http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" },
// {"id": 65432445,"title": "The Chamber","boxart":"http://cdn-0.nflximg.com/images/2891/TheChamber150.jpg" },
// {"id": 654356453,"title": "Bad Boys","boxart":"http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg" },
// {"id": 70111470,"title": "Die Hard","boxart":"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }
// ];
return movieLists.map((movie) => {
return movie.videos.map((video) => {
return video.boxarts.filter((boxart) => {
return boxart.width === 150;
}).map((item) => {
return {
"id": video.id,
"title": video.title,
"boxart": item.url
};
})
}).concatAll();
}).concatAll();
}
console.log(flattenThreeLevelNestedTree());
'use strict';
function imperativeSquare(arr) {
/* You have to specify the followings :
1. How to loop throgh the array
2. The order of looping
3. Logic
*/
for (var i = 0; i < arr.length; i++) {
arr[i] *= arr[i];
}
return arr;
}
var a = [1, 2, 3, 4, 5];
console.log(imperativeSquare(a)); // [1, 4, 9, 16, 25]
'use strict';
function predicate() {
var newReleases = [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}, {
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}];
var result = [];
newReleases.forEach(function(video) {
return video.rating === 5.0 ? result.push(video) : null;
});
return result;
}
console.log(predicate());
/*
[
{id: 654356453, title: "Bad Boys"},
{id: 675465, title: "Fracture"}
]
*/
'use strict';
Array.prototype.filter = function(Predicate) {
var result = [];
this.forEach(function(itemInArray) {
Predicate(itemInArray) ? result.push(itemInArray) : null;
});
return result;
}
console.log([1, 2, 3, 4, 5].filter(i => i > 3)); // [4, 5]
'use strict';
function predicate() {
var newReleases = [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}, {
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}];
return newReleases.filter(video => video.rating === 5.0);
}
console.log(predicate());
/*
[
{id: 654356453, title: "Bad Boys"},
{id: 675465, title: "Fracture"}
]
*/
'use strict';
function projection() {
var newReleases = [{
"id": 70111470,
"title": "Die Hard",
"rating": [4.0]
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": [5.0]
}, {
"id": 65432445,
"title": "The Chamber",
"rating": [4.0]
}, {
"id": 675465,
"title": "Fracture",
"rating": [5.0]
}],
videoAndTitlePairs = [];
newReleases.forEach(function(video) {
videoAndTitlePairs.push({
id: video.id,
title: video.title
});
});
return videoAndTitlePairs;
}
console.log(projection());
/*
[{id: 10111470, title: "Die Hard"},
{id: 654356453, title: "Bad Boys"},
{id: 65432445, title: "The Chamber"},
{id: 675465, title: "Fracture"}
]
*/
'use strict';
Array.prototype.map = function(projectionFunction) {
var results = [];
this.forEach(function(itemInArray) {
results.push(projectionFunction(itemInArray));
});
return results;
};
console.log([1, 2, 3].map(i => i * i)); // [1, 4, 9]
'use strict';
function projection() {
var newReleases = [{
"id": 70111470,
"title": "Die Hard",
"rating": [4.0]
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": [5.0]
}, {
"id": 65432445,
"title": "The Chamber",
"rating": [4.0]
}, {
"id": 675465,
"title": "Fracture",
"rating": [5.0]
}];
return newReleases.map( release => ({
id: release['id'],
title: release['title']
})
);
}
console.log(projection());
/*
[{id: 10111470, title: "Die Hard"},
{id: 654356453, title: "Bad Boys"},
{id: 65432445, title: "The Chamber"},
{id: 675465, title: "Fracture"}
]
*/
'use strict';
function queryNestedArray() {
var movieLists = [{
name: "New Releases",
videos: [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}]
}, {
name: "Dramas",
videos: [{
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}]
}],
allVideoIdsInMovieLists = [];
movieLists.forEach(function(movieList) {
return movieList.videos.forEach(function(video) {
return allVideoIdsInMovieLists.push(video.id);
});
});
return allVideoIdsInMovieLists;
}
console.log(queryNestedArray()); // [70111470, 654356453, 65432445, 675465]
'use strict';
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
if (Array.isArray(subArray))
subArray.forEach((item) => results.push(item))
else
throw new Error('Its not two dimensional array;');
});
return results;
};
function queryNestedArray() {
var movieLists = [{
name: "New Releases",
videos: [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}]
}, {
name: "Dramas",
videos: [{
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}]
}],
allVideoIdsInMovieLists = [];
return movieLists.map(movieList =>
movieList.videos.map(video =>
video.id
) // [id, id]
) // [[id, id], [id, id]]
.concatAll(); // [id, id, id, id]
}
console.log(queryNestedArray()); // [70111470, 654356453, 65432445, 675465]
'use strict';
function reduce() {
var boxarts = [{
width: 200,
height: 200
}, {
width: 150,
height: 200
}, {
width: 300,
height: 200
}, {
width: 425,
height: 150
}],
currentSize,
maxSize = -1,
largestBoxart;
boxarts.forEach(function(boxart) {
currentSize = boxart.width * boxart.height;
if (currentSize > maxSize) {
largestBoxart = boxart;
maxSize = currentSize;
}
});
return largestBoxart;
}
console.log(reduce()); // {width: 425, height: 150}
'use strict';
Array.prototype.reduce = function(callback, initialValue) {
let counter;
let accumulatedValue;
if (this.length === 0) {
return this;
} else {
if (arguments.length === 1) {
counter = 1;
accumulatedValue = this[0];
} else if (arguments.length == 2) {
counter = 0;
accumulatedValue = initialValue;
} else {
throw 'Invalid number of arguments were passed.'
}
}
while (counter < this.length) {
accumulatedValue = callback(accumulatedValue, this[counter]);
counter++;
}
return [accumulatedValue];
};
console.log([1, 2, 3].reduce(function(p, c) {
return p + c;
})); // [6]
console.log([1, 2, 3, 4, 5].reduce(function(p, c) {
return p > c ? p : c;
})); // [5]
var boxarts = [
{ width: 200, height:200 },
{ width: 150, height:200 },
{ width: 300, height:200 },
{ width: 425, height:150 }
];
console.log(boxarts.reduce(function(p, c) {
return p.width * p.height > c.width * c.height ? p : c;
})); // [{height: 150, width: 425}]
'use strict';
function traverse() {
let names = ["Apple", "Microsoft", "Facebook", "Linkedin"];
names.forEach(function(name) {
console.log(name);
});
};
traverse(); // Apple, Microsoft, Facebook, Linkedin
'use strict';
function traverse() {
let names = ["Apple", "Microsoft", "Facebook", "Linkedin"],
counter;
for (counter = 0; counter < names.length; counter++) {
console.log(names[counter]);
}
};
traverse(); // Apple, Microsoft, Facebook, Linkedin
'use strict';
function zip() {
var videos = [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0,
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0,
}, {
"id": 65432445,
"title": "The Chamber",
"rating": 4.0,
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0,
}],
bookmarks = [{
id: 470,
time: 23432
}, {
id: 453,
time: 234324
}, {
id: 445,
time: 987834
}],
counter,
videoIdAndBookmarkIdPairs = [];
for (counter = 0; counter < Math.min(videos.length, bookmarks.length); counter++) {
videoIdAndBookmarkIdPairs.push({
videoId: videos[counter].id,
bookmarkId: bookmarks[counter].id
});
}
return videoIdAndBookmarkIdPairs;
}
console.log(zip());
/*
[
{bookmarkId: 470, videoId: 70111470},
{bookmarkId: 453, videoId: 654356453},
{bookmarkId: 445, videoId: 65432445}
]
*/
'use strict';
Array.prototype.zip = function(left, right, combinerFunction) {
let results, length;
length = Math.min(left.length, right.length);
results = [];
for (let i = 0; i < length; i++) {
results.push(combinerFunction(left[i], right[i]));
}
return results;
}
console.log([].zip([1, 2], [1, 2, 3], function(l, r) {
return l + r;
})); // [2, 4]
'use strict';
Array.prototype.zip = function(left, right, combinerFunction) {
let results, length;
length = Math.min(left.length, right.length);
results = [];
for (let i = 0; i < length; i++) {
results.push(combinerFunction(left[i], right[i]));
}
return results;
}
function zip() {
var videos = [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}, {
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}],
bookmarks = [{
id: 470,
time: 23432
}, {
id: 453,
time: 234324
}, {
id: 445,
time: 987834
}];
return Array.prototype.zip(videos, bookmarks, function(l, r) {
return {
videoId: l.id,
bookmarkId: r.id
};
});
}
console.log(zip());
/*
[
{bookmarkId: 470, videoId: 70111470},
{bookmarkId: 453, videoId: 654356453},
{bookmarkId: 445, videoId: 65432445}
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment