Skip to content

Instantly share code, notes, and snippets.

@kimyongin
Forked from anonymous/index.html
Last active October 23, 2015 18:00
Show Gist options
  • Save kimyongin/40bfb61a86aa93d23266 to your computer and use it in GitHub Desktop.
Save kimyongin/40bfb61a86aa93d23266 to your computer and use it in GitHub Desktop.
/*******************************************
Y O U R T U R N
********************************************/
var articles = [
{
author: {
name: 'mike',
email: 'mike@gmail.com'
},
books : [
{
title : 'java',
price : 14000
},
{
title : 'cpp',
price : 16000
}
]
},
{
author: {
name: 'peter',
email: 'peter@outlook.com'
},
books : [
{
title : '.net',
price : 20000
},
{
title : 'python',
price : 20000
}
]
},
];
// ----------------------------------------------------------
// 1. 저자의 이름만 가져오기
// ----------------------------------------------------------
// 1. for문 사용하기
var getAuthorName = function(array){
var result = [];
array.forEach(function(item1, index1, array1){
result.push(item1.author.name);
});
return result;
};
console.log(getAuthorName(articles));
// 2. array의 map 사용
var getAuthor = function(obj){ return obj.author; };
var getName = function(obj){ return obj.name; };
var getAuthorName = function(obj) { return obj.map(getAuthor).map(getName); };
console.log(getAuthorName(articles));
// 3. rambda.js의 curry, map, compose 사용
var get = R.curry(function(x, obj){ return obj[x]; });
var getAuthorName = R.compose(R.map(get('name')), R.map(get('author')));
console.log(getAuthorName(articles));
// ----------------------------------------------------------
// 2. 저자의 책값의 총합을 가져오기
// ----------------------------------------------------------
// 1. for문 사용하기
var getBookTotalPrice = function(array){
var result = [];
array.forEach(function(item1, index1, array1){
result[index1] = 0;
item1.books.forEach(function(item2, index2, array2){
result[index1] += item2.price;
});
});
return result;
};
console.log(getBookTotalPrice(articles));
// 2. array의 map, reduce 사용
var getBook = function(obj){ return obj.books; };
var getTotalPrice = function(obj){ return obj.reduce(
function(prev, now, index, array) {
return prev.price + now.price;
});
};
var getBookTotalPrice = function(obj) { return obj.map(getBook).map(getTotalPrice); };
console.log(getBookTotalPrice(articles));
// 3. ramba.js 사용
var get = R.curry(function(x, obj){ return obj[x]; });
var log = R.curry(console.log);
var getBookTotalPrice0 = R.compose(R.map(R.reduce(R.add, 0)), R.map(R.map(get('price'))), R.map(get('books')));
console.log(getBookTotalPrice0(articles));
var getBookTotalPrice1 = R.compose(R.map(R.sum), R.map(R.map(get('price'))), R.map(get('books')));
console.log(getBookTotalPrice1(articles));
var total = R.curry(function(x, obj){ return R.compose(R.sum, R.map(get(x)))(obj); });
var getBookTotalPrice2 = R.compose(R.map(total('price')), R.map(get('books')));
console.log(getBookTotalPrice2(articles));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment