Created
October 23, 2015 17:59
-
-
Save anonymous/84f3cfc68a17bce11f5e to your computer and use it in GitHub Desktop.
// source http://jsbin.com/cisiju
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.18.0/ramda.min.js"></script> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/******************************************* | |
Y O U R T U R N | |
********************************************/ | |
var articles = [ | |
{ | |
author: { | |
name: 'mike', | |
email: '[email protected]' | |
}, | |
books : [ | |
{ | |
title : 'java', | |
price : 14000 | |
}, | |
{ | |
title : 'cpp', | |
price : 16000 | |
} | |
] | |
}, | |
{ | |
author: { | |
name: 'peter', | |
email: '[email protected]' | |
}, | |
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)); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.18.0/ramda.min.js"><\/script> | |
</head> | |
<body> | |
</body> | |
</html> | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
/******************************************* | |
Y O U R T U R N | |
********************************************/ | |
var articles = [ | |
{ | |
author: { | |
name: 'mike', | |
email: '[email protected]' | |
}, | |
books : [ | |
{ | |
title : 'java', | |
price : 14000 | |
}, | |
{ | |
title : 'cpp', | |
price : 16000 | |
} | |
] | |
}, | |
{ | |
author: { | |
name: 'peter', | |
email: '[email protected]' | |
}, | |
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)); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************* | |
Y O U R T U R N | |
********************************************/ | |
var articles = [ | |
{ | |
author: { | |
name: 'mike', | |
email: '[email protected]' | |
}, | |
books : [ | |
{ | |
title : 'java', | |
price : 14000 | |
}, | |
{ | |
title : 'cpp', | |
price : 16000 | |
} | |
] | |
}, | |
{ | |
author: { | |
name: 'peter', | |
email: '[email protected]' | |
}, | |
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