{"id":3662,"date":"2014-08-05T10:18:11","date_gmt":"2014-08-05T01:18:11","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=3662"},"modified":"2019-04-10T11:46:42","modified_gmt":"2019-04-10T02:46:42","slug":"post-3662","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/javascript\/3662","title":{"rendered":"\u3081\u3061\u3083\u304f\u3061\u3083\u308f\u304b\u308a\u3084\u3059\u3044Functional JavaScript\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb"},"content":{"rendered":"<div class=\"amazlet-box\" style=\"margin-bottom:0px;\">\n<div class=\"amazlet-image\" style=\"float:left;margin:0px 12px 1px 0px;\"><a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/4873116600\/toriaezu04b-22\/ref=nosim\/\" name=\"amazletlink\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"http:\/\/ecx.images-amazon.com\/images\/I\/414qGlNpcHL._SL160_.jpg\" alt=\"JavaScript\u3067\u5b66\u3076\u95a2\u6570\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\" style=\"border: none;\" \/><\/a><\/div>\n<div class=\"amazlet-info\" style=\"line-height:120%; margin-bottom: 10px\">\n<div class=\"amazlet-name\" style=\"margin-bottom:10px;line-height:120%\"><a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/4873116600\/toriaezu04b-22\/ref=nosim\/\" name=\"amazletlink\" target=\"_blank\" rel=\"noopener noreferrer\">JavaScript\u3067\u5b66\u3076\u95a2\u6570\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/a><\/p>\n<div class=\"amazlet-powered-date\" style=\"font-size:80%;margin-top:5px;line-height:120%\">posted with <a href=\"http:\/\/www.amazlet.com\/\" title=\"amazlet\" target=\"_blank\" rel=\"noopener noreferrer\">amazlet<\/a> at 14.08.05<\/div>\n<\/div>\n<div class=\"amazlet-detail\">Michael Fogus <br \/>\u30aa\u30e9\u30a4\u30ea\u30fc\u30b8\u30e3\u30d1\u30f3 <br \/>\u58f2\u308a\u4e0a\u3052\u30e9\u30f3\u30ad\u30f3\u30b0: 209,036<\/div>\n<div class=\"amazlet-sub-info\" style=\"float: left;\">\n<div class=\"amazlet-link\" style=\"margin-top: 5px\"><a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/4873116600\/toriaezu04b-22\/ref=nosim\/\" name=\"amazletlink\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon.co.jp\u3067\u8a73\u7d30\u3092\u898b\u308b<\/a><\/div>\n<\/div>\n<\/div>\n<div class=\"amazlet-footer\" style=\"clear: left\"><\/div>\n<\/div>\n<p>\u590f\u3067\u3059\u306d\u3002\u6691\u3044\u3067\u3059\u306d\u3002<br \/>\n\u79c1\u306e\u90e8\u5c4b\u306e\u30af\u30fc\u30e9\u30fc\u306f\u6570\u5e74\u524d\u304b\u3089\u6545\u969c\u3057\u305f\u307e\u307e\u306a\u306e\u3067\u3001\u5bb6\u306b\u5e30\u3063\u3066\u304b\u3089\u4f55\u3082\u3084\u308a\u305f\u304f\u3042\u308a\u307e\u305b\u3093\u3002\u3068\u3044\u3046\u304b\u30d1\u30bd\u30b3\u30f3\u4f7f\u3046\u3068\u30b5\u30a6\u30ca\u307f\u305f\u3044\u306b\u306a\u308b\u3057\u3076\u3061\u58ca\u308c\u305d\u3046\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u672c\u65e5\u3082\u5c0f\u30cd\u30bf\u3067\u3059\u3002<\/p>\n<p>\u4f55\u3068\u306a\u304f\u30cd\u30c3\u30c8\u3092\u3055\u307e\u3088\u3063\u3066\u5c45\u305f\u3089JavaScript\u3092\u95a2\u6570\u578b\u306e\u30d1\u30e9\u30c0\u30a4\u30e0\u3067\u8a18\u8ff0\u3059\u308b\u3001\u3072\u3058\u3087\u3046\u306b\u308f\u304b\u308a\u6613\u3044\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u8a18\u4e8b\u3092\u767a\u898b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"https:\/\/nicolas.perriault.net\/code\/2013\/functional-javascript-for-crawling-the-web\/\">Functional JavaScript for crawling the Web | Code | Nicolas Perriault<\/a><\/p>\n<p>\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u4e00\u9023\u306e\u51e6\u7406\u3092\u3001\u4e00\u6bb5\u968e\u3065\u3064Fanctional\u306a\u30b3\u30fc\u30c9\u3078\u3068\u66f8\u304d\u66ff\u3048\u3066\u3044\u304f\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u3059\u3002<br \/>\n\u30b3\u30fc\u30c9\u3092\u66f8\u304d\u66ff\u3048\u3066\u3044\u304f\u904e\u7a0b\u304c\u5206\u304b\u308a\u6613\u304f\u63d0\u793a\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u3076\u3063\u3061\u3083\u3051\u82f1\u8a9e\u304c\u8aad\u3081\u306a\u304f\u3066\u3082\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u8ffd\u3063\u3066\u3044\u304f\u3060\u3051\u3067\u7406\u89e3\u3067\u304d\u308b\u7d20\u6674\u3089\u3057\u3044\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001ES6\u306e\u6587\u6cd5\u3067\u66f8\u304b\u308c\u3066\u3044\u308b\u306e\u3067(\u30a2\u30ed\u30fc\u95a2\u6570\u3068\u304b)\u305d\u3053\u3060\u3051\u3001\u3061\u3087\u3063\u3068\u306a\u308c\u306a\u3044\u3068\u8aad\u307f\u8f9b\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\nFunctional JavaScripting\u306b\u8208\u5473\u3042\u308b\u65b9\u306b\u304a\u52e7\u3081\u3067\u3059\u3002<\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<p>ES5\u3067\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<div data-height=\"604\" data-theme-id=\"467\" data-slug-hash=\"idmuK\" data-default-tab=\"js\" class='codepen'>\n<pre><code>\/*\n *Functional JavaScript for crawling the Web\n *https:\/\/nicolas.perriault.net\/code\/2013\/functional-javascript-for-crawling-the-web\/\n *\n *\/\n\n\n\n\/*************************************************** *\n * Helper Fantion\n *\n *************************************************\/\n\n\/\/\u30c7\u30d0\u30c3\u30b0\u7528 \nfunction logs(){\n    console.log(arguments);\n    return arguments;\n}\n\n    \n\/\/dom\u304b\u3089\u8981\u7d20\u3092\u53d6\u5f97\nfunction nodes(sel, root){\n    return (root || document).querySelectorAll(sel);\n}\n\n\/\/nodes\u306e\u4e2d\u304b\u3089\u7279\u5b9a\u306e\u8981\u7d20\u3092\u898b\u3064\u3051\u51fa\u3059\nfunction finder(selector) {\n  return function(root) {\n    return nodes(selector, root);\n  }\n}\n\n\/\/\u4e0e\u3048\u3089\u308c\u305f\u95a2\u6570\u3092\u914d\u5217\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u306b\u5bfe\u3057\u3066\u9069\u7528\nfunction map(fn, iterable){\n    return [].map.call(iterable, fn)\n}\n\n\/\/\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u6307\u5b9a\u3057\u3066\u53d6\u5f97\nfunction prop(name){\n    return function(object){\n        return object[name];\n    }\n}\n\n\/\/function\u3092\u9806\u756a\u306b\u5b9f\u884c\nfunction sequence() {\n    var fns = Array.prototype.slice.call(arguments);\n    return fns.reduce(function(comp, fn) {\n        return function(){\n            var args =  Array.prototype.slice.call(arguments);\n            return comp(fn.apply(null, args))\n        };\n    });\n};\n\n\n\/\/\u96a3\u308a\u5408\u3046 2 \u3064\u306e\u914d\u5217\u8981\u7d20\u306b\u5bfe\u3057\u3066\uff08\u5de6\u304b\u3089\u53f3\u3078\uff09\u540c\u6642\u306b\u95a2\u6570\u3092\u9069\u7528\nfunction reduce(fn, init, iterable){\n    return [].reduce.call(iterable, fn, init)\n}\n\n\/\/\u95a2\u6570\u5408\u6210\nfunction compose(){\n    var fns = Array.prototype.slice.call(arguments);\n    return sequence.apply(null, fns.reverse());\n}\n\n\/\/\u30ab\u30ea\u30fc\u5316\nfunction curry( orig_func ) {\n    var ap = Array.prototype;\n    var args = arguments;\n    \n    function fn() {\n        ap.push.apply( fn.args, arguments );\n    \n        return fn.args.length &lt; orig_func.length\n          ? fn\n          : orig_func.apply( this, fn.args );\n    };\n    \n    return function() {\n        fn.args = ap.slice.call( args, 1 );\n        return fn.apply( this, arguments );\n    };\n};\n\n\/\/\u90e8\u5206\u9069\u7528\nfunction partial( orig_func ) {\n    var aps = Array.prototype.slice,\n    args = aps.call( arguments, 1 );\n    \n    return function() {\n        return orig_func.apply( this, args.concat( aps.call( arguments ) ) );\n    };\n};\n\n\n\/**************************************************\n *\n * \u4ee5\u4e0b\u3001main\n *\n *************************************************\/\n\n\/\/\u30c6\u30fc\u30d6\u30eb\u304b\u3089row\u8981\u7d20\u3092\u53d6\u5f97\nvar  rows = nodes(&quot;tbody tr&quot;);\n\/\/rows\u304b\u3089td\u3092\u53d6\u5f97\nvar findCells = finder(&quot;td&quot;);\n\/\/\u30bb\u30eb\u306e\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\nvar getText = prop(&quot;textContent&quot;);\n\nvar getFirst = prop(0);\nvar getSecond = prop(1);\nvar getThird = prop(2);\n\n\/*******************************\n * \u30c6\u30fc\u30d6\u30eb\u306e1\u884c\u76ee(\u56fd\u540d)\u3092\u53d6\u5f97\n *******************************\/\nconsole.log( &quot;first-ColumnList&quot;, map(compose(findCells, getFirst, getText), rows) )\n\n\/*******************************\n * \u9014\u4e2d\u306e\u51e6\u7406\u306blogs\u3092\u631f\u3093\u3067\u30c7\u30d0\u30c3\u30b0\n *******************************\/\nconsole.log( &quot;debug&quot;, map(compose(findCells, getFirst, logs, getText), rows) )\n\n\n\/*******************************\n * \u4eba\u53e3\u306e\u5408\u8a08\u3092\u53d6\u5f97\n *******************************\/\nvar sum = function(a, b) {\n    return a + b;\n}\nvar populations  =  map(compose(findCells, getSecond, getText, parseFloat), rows);\nconsole.log(&quot;sum-populations&quot;, reduce(sum, 0, populations));\n\n\n\n\/*******************************\n * \u30c7\u30fc\u30bf\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\n *******************************\/\nvar asObject = function(data){\n    return {\n        name:getFirst(data),\n        population: parseFloat(getSecond(data)),\n        gnp:parseFloat(getThird(data))\n    }\n};\nvar getTexts = partial(map, getText); \/\/getText\u95a2\u6570\u3092map\u95a2\u6570\u306b\u90e8\u5206\u9069\u7528\u3057\u3066\u3001array\u306bgetText\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u3059\u308b\nconsole.log(&quot;toObject&quot;,  map(compose(findCells, getTexts, asObject), rows ))\n\n\n\n<\/code><\/pre>\n<p>See the Pen <a href='http:\/\/codepen.io\/shimizu\/pen\/idmuK\/'>Functional JavaScript for crawling the Web<\/a> by shimizu masayuki (<a href='http:\/\/codepen.io\/shimizu'>@shimizu<\/a>) on <a href=\"https:\/\/codepen.io\/\">CodePen<\/a>.\n<\/div>\n<p><script async src=\"\/\/codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript\u3067\u5b66\u3076\u95a2\u6570\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 p&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3663,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,43],"tags":[],"class_list":["post-3662","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-book","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=3662"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3662\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/3663"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=3662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=3662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=3662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}