{"id":284291,"date":"2017-04-01T16:10:02","date_gmt":"2017-04-01T12:10:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=284291"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=284291","title":{"rendered":"\u0412\u044b \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b"},"content":{"rendered":"<p>\u0422\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u0431\u044b\u043b\u0438 \u0430\u043d\u043e\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0435\u0449\u0435 \u0432 \u0434\u0430\u043b\u0435\u043a\u043e\u043c 2014, \u0441 \u0442\u0435\u0445 \u043f\u043e\u0440 \u043f\u043e \u043d\u0438\u043c \u0431\u044b\u043b\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0435\u043c\u0430\u043b\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0430\u0442\u0435\u0439 (<a href=\"https:\/\/habrahabr.ru\/post\/237613\/\">\u0440\u0430\u0437<\/a>, <a href=\"https:\/\/habrahabr.ru\/post\/247889\/\">\u0434\u0432\u0430<\/a>), \u043d\u043e \u043d\u0438 \u043f\u043e\u0441\u043b\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u043d\u0435 \u043c\u043e\u0433 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u043a\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u043e \u044f\u0441\u043d\u043e. \u041f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0443 \u043c\u0435\u043d\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u043e \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u044f \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0447\u0442\u043e-\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0435, \u043d\u043e \u043e\u043d\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0430\u043b\u043e\u0441\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u043c. \u0410 \u043f\u043e\u0442\u043e\u043c \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0447\u0442\u043e-\u0442\u043e \u0449\u0435\u043b\u043a\u043d\u0443\u043b\u043e: &quot;\u042f \u0432\u0435\u0434\u044c \u0443\u0436\u0435 \u0432\u0438\u0434\u0435\u043b \u044d\u0442\u043e\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043d \u043d\u0430\u0437\u044b\u0432\u0430\u043b\u0441\u044f \u0438\u043d\u0430\u0447\u0435!&quot;<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h3 id=\"zadacha\">\u0417\u0430\u0434\u0430\u0447\u0430:<\/h3>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 scores, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u043c\u043d\u043e\u044e \u0438\u0433\u0440 \u0432 \u0444\u0443\u0442\u0431\u043e\u043b \u0432 \u0432\u0438\u0434\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441 \u043f\u043e\u043b\u044f\u043c\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li>gameID \u2014 \u043d\u043e\u043c\u0435\u0440 \u0438\u0433\u0440\u044b;<\/li>\n<li>my \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0447\u043a\u043e\u0432 \u0443 \u043c\u0435\u043d\u044f;<\/li>\n<li>others \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0447\u043a\u043e\u0432 \u0443 \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u0438\u043a\u0430.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u043e\u043c\u0435\u0440\u0430 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0432\u044b\u0438\u0433\u0440\u0430\u043d\u043d\u044b\u0445 \u043c\u043d\u043e\u044e \u0438\u0433\u0440.<\/p>\n<p>  <\/p>\n<p>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 (\u043e\u0442\u0432\u0435\u0442 \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 [1, 3]):<\/p>\n<p>  <\/p>\n<pre><code>const scores = [   { gameID: 0, my: 1, others: 2 },   { gameID: 1, my: 2, others: 1 },   { gameID: 2, my: 0, others: 3 },   { gameID: 3, my: 3, others: 2 },   { gameID: 4, my: 3, others: 1 },   { gameID: 5, my: 0, others: 0 },   { gameID: 6, my: 4, others: 1 },   ]<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"reshenie-1-imperativnyy-cikl\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u21161. \u0418\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0446\u0438\u043a\u043b<\/h3>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0438\u0437\u0434\u0430\u043b\u0435\u043a\u0430, \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u0432 \u0442\u0435 \u0434\u0430\u043b\u0435\u043a\u0438\u0435 \u0434\u043d\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b \u0438 \u043c\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c (\u0441\u043e\u0433\u043b\u0430\u0448\u0443\u0441\u044c, \u0447\u0442\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u043e \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c):<\/p>\n<p>  <\/p>\n<pre><code>const result = []  \/\/ \u041f\u0440\u043e\u0431\u0435\u0433\u0430\u0435\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u0438\u0433\u0440\u0430\u043c, \u043f\u043e\u043a\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \u0434\u0432\u0443\u0445. for (let i = 0; i &lt; scores.length, result.length &lt; 2; i++) {   const { gameID, my, others } = scores[i]    if (my &gt; others) {     result.push(gameID)   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0443\u0442 \u0432\u0441\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e, \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e, \u043d\u043e \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0438 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e. \u0412\u0430\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043b\u0438\u0448\u043d\u0438\u0435 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f, \u0446\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>  <\/p>\n<h3 id=\"reshenie-2-arraymap-i-arrayfilter\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u21162. Array#map \u0438 Array#filter<\/h3>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0443\u0436\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0435, \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435, &quot;\u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435&quot;.<\/p>\n<p>  <\/p>\n<pre><code>const result =   scores     .filter(({ my, others }) =&gt; my &gt; others) \/\/ \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u043d\u044b\u0435     .map(({ gameID }) =&gt; gameID)             \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0445 \u043d\u043e\u043c\u0435\u0440     .slice(0, 2)                             \/\/ \u0431\u0435\u0440\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u0440\u0435\u0448\u0438\u043b\u043e \u0431\u044b \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e js-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0432 \u043d\u0430\u0448\u0438 \u0434\u043d\u0438, \u043f\u043e\u043b\u0430\u0433\u0430\u044e. \u041d\u043e \u0443 \u044d\u0442\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u043e\u0434\u043d\u0430 \u0432\u0430\u0436\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043c\u044b \u0434\u0435\u043b\u0430\u043b\u0438 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434, \u0434\u0430 \u0438 \u0442\u043e\u0442 \u043d\u0435 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u0442\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0430\u0436 \u0434\u0432\u0430 \u043f\u043e\u043b\u043d\u044b\u0445 \u043f\u0440\u043e\u0445\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u0431\u044b \u0443 \u043d\u0430\u0441 scores \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0442\u043e \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442 \u0432 filter \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0441\u044f \u0431\u044b \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0440\u0430\u0437, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 map \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043b\u0430\u0441\u044c \u0431\u044b \u043c\u0435\u043d\u044c\u0448\u0435\u0435, \u043d\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437, \u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0434\u0432\u0430 \u043f\u0435\u0440\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0440\u0435\u0436\u0434\u0435\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u0437\u043b\u043e, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u043d\u0438 \u0432 \u043a\u0430\u043a\u0438\u0435 \u0432\u043e\u0440\u043e\u0442\u0430.<\/p>\n<p>  <\/p>\n<h3 id=\"reshenie-3-svertka\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u21163. \u0421\u0432\u0435\u0440\u0442\u043a\u0430<\/h3>\n<p>  <\/p>\n<p>\u0427\u0435\u0440\u0435\u0437 \u0441\u0432\u0435\u0440\u0442\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c [\u043f\u043e\u0447\u0442\u0438] \u043b\u044e\u0431\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043d\u0430\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438. \u0412 \u044d\u0442\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0443 \u043d\u0430\u0441 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code>const result = scores.reduce((result, { my, others, gameID }) =&gt; {   \/\/ \u0415\u0441\u043b\u0438 \u0438\u0433\u0440\u0430 \u0432\u044b\u0438\u0433\u0440\u0430\u043d\u0430 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043c\u0435\u043d\u044c\u0448\u0435 \u0434\u0432\u0443\u0445,   \/\/ \u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u043c\u0435\u0440 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.   if (my &gt; others &amp;&amp; result.length &lt; 2) {     return result.concat([gameID])   }    return result }, [])<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0447\u0435\u043c-\u0442\u043e \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0430\u043c\u0438, \u043d\u043e \u0442\u0443\u0442 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u044f\u0432\u043d\u043e \u0438 \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e. \u041d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u2014 \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0432\u0443\u0445 \u043f\u0440\u043e\u0445\u043e\u0434\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0434\u0438\u043d, \u043d\u043e \u043e\u043d \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043b\u043d\u044b\u0439, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u0438 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u044b \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0443, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438, \u0432\u0435\u0434\u044c \u0443 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e reduce \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 break. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 reduce, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0439\u0442\u0438, \u0435\u0441\u043b\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c reduced-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (\u0438\u0434\u0435\u044e \u043f\u043e\u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u0438\u0437 clojure).<\/p>\n<p>  <\/p>\n<pre><code>\/\/ \u041a\u043b\u0430\u0441\u0441-\u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u0434\u043b\u044f \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u044f\u0442\u043e\u0440\u0430. class Reduced {   constructor(value) {     this.value = value   } }  \/\/ \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0445\u044d\u043b\u043f\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c `new`. const reduced = value =&gt;   new Reduced(value)  \/\/ \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f reduce \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u044f\u0442\u043e\u0440\u0430 \u043d\u0430 reduced-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. const reduce = (reducer, state, array) =&gt; {   \/\/ \u0415\u0441\u043b\u0438 \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u044f\u0442\u043e\u0440 - reduced-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c,   \/\/ \u0442\u043e \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044f \u0446\u0438\u043a\u043b.   if (state instanceof Reduced) {     return state.value   }    if (array.length === 0) {     return state   }    \/\/ \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0435\u043c reduce \u0434\u043b\u044f \u0445\u0432\u043e\u0441\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430,   \/\/ \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.   const [x, ...xs] = array   return reduce(reducer, reducer(state, x), xs) }  const result = reduce((result, { my, others, gameID }) =&gt; {   if (my &gt; others) {     if (result.length &lt; 2) {       result = result.concat(gameID)     }      \/\/ \u0417\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u0446\u0438\u043a\u043b, \u0435\u0441\u043b\u0438 \u043d\u0430\u0431\u0440\u0430\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0447\u0438\u0441\u043b\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432.     if (result.length &gt;= 2) {       result = reduced(result)     }   }    return result }, [], scores)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0423\u0445, \u0442\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u0443 \u043d\u0430\u0441 \u0432\u0441\u0435 \u0431\u044b\u0441\u0442\u0440\u043e (\u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434 \u0438 \u0440\u043e\u0432\u043d\u043e \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043c\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432), \u043d\u043e \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043b\u0438? \u042f \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u043a\u043e\u0434 \u0432\u044b\u0448\u0435 \u2014 \u0441\u0442\u0440\u0430\u0448\u043d\u044b\u0439: \u0432 \u043d\u0435\u043c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0437\u0430\u0434\u0430\u0447\u0435.<\/p>\n<p>  <\/p>\n<h3 id=\"reshenie-4-dekompoziciya-redyusera-transdyusery\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u21164. \u0414\u0435\u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430, \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b<\/h3>\n<p>  <\/p>\n<p>\u0420\u0435\u0434\u044c\u044e\u0441\u0435\u0440 \u0432\u044b\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043d\u0430 4 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435, \u0433\u0434\u0435 \u043e\u0434\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e.<\/p>\n<p>  <\/p>\n<ol>\n<li><code>filterWins<\/code> \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u0442 \u0438\u0433\u0440\u044b \u043f\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u0443, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0438\u0433\u0440\u0430\u043d\u043d\u044b\u0435 \u0438\u0433\u0440\u044b.<\/li>\n<li><code>mapGameID<\/code> \u0434\u043e\u0441\u0442\u0430\u0435\u0442 \u0438\u0437 \u0438\u0433\u0440\u044b \u0435\u0435 \u043d\u043e\u043c\u0435\u0440.<\/li>\n<li><code>firstTwo<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u0415\u0441\u043b\u0438 \u0438\u0445 \u043c\u0435\u043d\u044c\u0448\u0435 \u0434\u0432\u0443\u0445, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u0430 \u043f\u043e\u0442\u043e\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0446\u0438\u043a\u043b, \u0435\u0441\u043b\u0438 \u043d\u0430\u0431\u0440\u0430\u043d\u043e \u043d\u0443\u0436\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e.<\/li>\n<li><code>appendToResult<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u043e\u043c\u0435\u0440 \u0438\u0433\u0440\u044b \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e.<\/li>\n<\/ol>\n<p>  <\/p>\n<pre><code>\/\/ next - \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 \u0446\u0435\u043f\u043e\u0447\u043a\u0435 const filterWins = next =&gt; (result, score) =&gt;   \/\/ \u0415\u0441\u043b\u0438 \u0438\u0433\u0440\u0430 \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u043d\u0430\u044f, \u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0435\u0435 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435   score.my &gt; score.others     ? next(result, score)     : result  const mapGameID = next =&gt; (result, { gameID }) =&gt;   \/\/ \u041f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435 \u043d\u043e\u043c\u0435\u0440 \u0438\u0433\u0440\u044b   next(result, gameID)  \/\/ \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0431\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. const firstTwo = next =&gt; {   \/\/ \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a \u043d\u0430\u0448\u0438\u043c \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430\u043c.   \/\/ n - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435 \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432   \/\/ \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432.   let n = 2    return (result, score) =&gt; {     \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u043c\u0435\u0441\u0442\u0430, \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438\u0433\u0440\u0443 \u0434\u0430\u043b\u044c\u0448\u0435 \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e.     if (n &gt; 0) {       result = next(result, score)       n -= 1     }      \/\/ \u0415\u0441\u043b\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u043c\u0435\u0441\u0442 \u043d\u0435\u0442, \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u0446\u0438\u043a\u043b.     if (n &lt;= 0) {       result = reduced(result)     }      return result   } }  const appendToResult = (result, score) =&gt;   result.concat([score])  const result = reduce(   \/\/ \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u043d\u0430\u0448\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a   \/\/ \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0443, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u043e\u0432.   filterWins(mapGameID(firstTwo(appendToResult))),   [],   scores, )<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0440\u043e\u0434\u0435 \u043a\u0430\u043a \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b\u0438, \u0440\u0430\u0437\u0431\u0438\u0432 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u0442\u0440\u0430\u0448\u043d\u043e\u0432\u0430\u0442\u043e. \u041f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0431\u044b \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c. \u041d\u043e \u043c\u044b \u044d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435, \u043a\u043e\u0433\u0434\u0430 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u041f\u0430\u0442\u0442\u0435\u0440\u043d \u0432\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c, \u0432\u0435\u0434\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 middleware. \u042d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c middleware. \u0418 \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c middleware. \u0422\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440 \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u0434\u0438\u043d \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 (\u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0434 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430).<\/p>\n<p>  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0443 \u043d\u0430\u0441 \u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u0430: <code>filterWins<\/code>, <code>mapGameID<\/code> \u0438 <code>firstTwo<\/code>, \u0438 \u043c\u044b \u0438\u0445 \u043f\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043a \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0443 <code>appendToResult<\/code>, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u0432\u0441\u0435 \u0431\u043e\u043b\u0435\u0435 \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440.<\/p>\n<p>  <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b:<\/p>\n<p>  <\/p>\n<pre><code>\/\/ pred - \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442 - \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 true \u0438\u043b\u0438 false. \/\/ \u0415\u0441\u043b\u0438 \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 true, \u0442\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u0434\u0430\u043b\u044c\u0448\u0435, \u0438\u043d\u0430\u0447\u0435 \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u0439 \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u044f\u0442\u043e\u0440. const filter = pred =&gt; next =&gt; (acc, x) =&gt;   pred(x) ? next(acc, x) : acc  \/\/ mapper - \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439. const map = mapper =&gt; next =&gt; (acc, x) =&gt;   next(acc, mapper(x))  \/\/ \u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e n \u0440\u0430\u0437. \/\/ \u041f\u043e\u0441\u043b\u0435 n-\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0446\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f. const take = n =&gt; next =&gt; (acc, x) =&gt; {   if (n &gt; 0) {     acc = next(acc, x)     n -= 1   }    if (n &lt;= 0) {     acc = reduced(acc)   }    return acc }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u043e\u0434\u043d\u043e \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u0443\u0435\u043c \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code>const append = (xs, x) =&gt;   xs.concat([x])<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0442\u0430\u043a \u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0445\u044d\u043b\u043f\u0435\u0440 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0443\u0431\u0440\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0435 \u0441\u043a\u043e\u0431\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code>const compose = (...fns) =&gt; x =&gt;   fns.reduceRight((x, fn) =&gt; fn(x), x)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0445\u044d\u043b\u043f\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442 \u0442\u0440\u0430\u043dc\u0434\u044c\u044e\u0441\u0435\u0440 \u043a \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0443 \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 reduce \u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u043c\u0441\u044f \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code>const transduce = (transducer, reducer, init, array) =&gt;   reduce(transducer(reducer), init, array)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0443 \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0440\u0435\u0448\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<p>  <\/p>\n<pre><code>const firstTwoWins = compose(   filter(({ my, others }) =&gt; my &gt; others),   map(({ gameID }) =&gt; gameID),   take(2), )  const result = transduce(firstTwoWins, append, [], scores)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0435, \u0431\u044b\u0441\u0442\u0440\u043e\u0435, \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435, \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0435, \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u043a \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0440\u0435\u0448\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<h3 id=\"vyvod\">\u0412\u044b\u0432\u043e\u0434:<\/h3>\n<p>  <\/p>\n<p>\u0422\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u2014 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u043d\u044b\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 middleware, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0448\u0438\u0440\u0435 (\u043e\u0442\u0441\u044e\u0434\u0430 \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438), \u043e\u0441\u043d\u043e\u0432\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u2014 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438. \u0410 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u0447\u0435\u043d\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b, \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439, \u0441\u0442\u0440\u0438\u043c\u043e\u0432, Redux.<\/p>\n<p>  <\/p>\n<p>\u041f\u0438\u0441\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u0430\u043c\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f, \u0432\u0435\u0434\u044c \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 <code>ramda.js<\/code> \u0438\u043b\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 clojure)<\/p>\n<p>  <\/p>\n<h4 id=\"ramda\">Ramda:<\/h4>\n<p>  <\/p>\n<pre><code>const scores = [   { gameID: 0, my: 1, others: 2 },   { gameID: 1, my: 2, others: 1 },   { gameID: 2, my: 0, others: 3 },   { gameID: 3, my: 3, others: 2 },   { gameID: 4, my: 3, others: 1 },   { gameID: 5, my: 0, others: 0 },   { gameID: 6, my: 4, others: 1 },   ]  {   const firstTwoWins = compose(     filter(({ my, others }) =&gt; my &gt; others),     pluck(&quot;gameID&quot;),     take(2),   )    const result = transduce(firstTwoWins, flip(append), [], scores) }  \/\/ \u0418\u043b\u0438 \u0435\u0449\u0435 \u043b\u0443\u0447\u0448\u0435 {   const firstTwoWins = into([], compose(     filter(({ my, others }) =&gt; my &gt; others),     pluck(&quot;gameID&quot;),     take(2),   ))    const result = firstTwoWins(scores) }<\/code><\/pre>\n<p>  <\/p>\n<h4 id=\"clojure\">Clojure:<\/h4>\n<p>  <\/p>\n<pre><code>(def scores [{:game-id 0, :my 1, :others 2}              {:game-id 1, :my 2, :others 1}              {:game-id 2, :my 0, :others 3}              {:game-id 3, :my 3, :others 2}              {:game-id 4, :my 3, :others 1}              {:game-id 5, :my 0, :others 0}              {:game-id 6, :my 4, :others 1}])  (defn win? [{:keys [my others]}]   (&gt; my others))  (def first-two-wins   (comp (filter win?)         (map :game-id)         (take 2)))  (def result (transduce first-two-wins conj [] scores)) ;; [1 3]<\/code><\/pre>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/325388\/\"> https:\/\/habrahabr.ru\/post\/325388\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0422\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u0431\u044b\u043b\u0438 \u0430\u043d\u043e\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0435\u0449\u0435 \u0432 \u0434\u0430\u043b\u0435\u043a\u043e\u043c 2014, \u0441 \u0442\u0435\u0445 \u043f\u043e\u0440 \u043f\u043e \u043d\u0438\u043c \u0431\u044b\u043b\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0435\u043c\u0430\u043b\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0430\u0442\u0435\u0439 (<a href=\"https:\/\/habrahabr.ru\/post\/237613\/\">\u0440\u0430\u0437<\/a>, <a href=\"https:\/\/habrahabr.ru\/post\/247889\/\">\u0434\u0432\u0430<\/a>), \u043d\u043e \u043d\u0438 \u043f\u043e\u0441\u043b\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u043d\u0435 \u043c\u043e\u0433 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0442\u0440\u0430\u043d\u0441\u0434\u044c\u044e\u0441\u0435\u0440\u044b \u043a\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u043e \u044f\u0441\u043d\u043e. \u041f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0443 \u043c\u0435\u043d\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u043e \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u044f \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0447\u0442\u043e-\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0435, \u043d\u043e \u043e\u043d\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0430\u043b\u043e\u0441\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u043c. \u0410 \u043f\u043e\u0442\u043e\u043c \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0447\u0442\u043e-\u0442\u043e \u0449\u0435\u043b\u043a\u043d\u0443\u043b\u043e: &quot;\u042f \u0432\u0435\u0434\u044c \u0443\u0436\u0435 \u0432\u0438\u0434\u0435\u043b \u044d\u0442\u043e\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043d \u043d\u0430\u0437\u044b\u0432\u0430\u043b\u0441\u044f \u0438\u043d\u0430\u0447\u0435!&quot;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-284291","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284291"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284291\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}