{"id":313819,"date":"2020-11-26T09:01:32","date_gmt":"2020-11-26T09:01:32","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=313819"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=313819","title":{"rendered":"Kotlin FP: \u043c\u043e\u043d\u043e\u0438\u0434\u044b \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043c\u043e\u043d\u043e\u0438\u0434 \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u043c \u043f\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u0430 Kotlin \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e \u0437\u0430\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 <a href=\"https:\/\/www.youtube.com\/channel\/UCj4XH0Un0-v78VcMgeUGqkg\" rel=\"nofollow\">\u043c\u043e\u0439 youtube-\u043a\u0430\u043d\u0430\u043b<\/a>, \u0433\u0434\u0435 \u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044e \u0440\u0430\u0437\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0438\u0437 Kotlin FP.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tl\/i5\/7m\/tli57mfggcgrgnmloubm1txpjwe.jpeg\"><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u0422\u0435\u043e\u0440\u0438\u044f<\/h3>\n<p>  \u041c\u043e\u043d\u043e\u0438\u0434 \u2014 \u043f\u0440\u043e\u0441\u0442\u0430\u044f, \u043d\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0449\u043d\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439. \u0412\u0441\u0451, \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u2014 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. <\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0441 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u201c\u043d\u043e\u043b\u044c\u201d \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043c\u043e\u043d\u043e\u0438\u0434.<\/p>\n<p>  \u0410\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u2014 \u044d\u0442\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043a\u043e\u0431\u043e\u043a:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/9c4\/0e2\/c35\/9c40e2c358b9c86379bc282242f1f2df.svg\" alt=\"$a + b + c == a + (b + c) == (a + b) + c$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043f\u0440\u0438 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0447\u0438\u0441\u0435\u043b \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u043a\u043e\u0431\u043a\u0438.<\/p>\n<p>  \u041d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u2014 \u043d\u043e\u043b\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/f9b\/6c2\/b78\/f9b6c2b78a53f2cd0363699c8f2742ac.svg\" alt=\"$0 + a == a == a + 0$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u041c\u043d\u043e\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c.<\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <\/p>\n<p>  \u041d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0441 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u201c1\u201d  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/2e8\/07c\/379\/2e807c379cdd662907591435606e68e4.svg\" alt=\"$a * b * c == a * (b * c) == (a * b) * c$\" data-tex=\"display\"><\/math><\/p>\n<p>  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/7f8\/46f\/f87\/7f846ff872a04634d38c4d0609370824.svg\" alt=\"$1 * a == a == a * 1 $\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0421\u0442\u0440\u043e\u043a\u0438 \u0441 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u0438 (\u0441\u043a\u043b\u0435\u0438\u0432\u0430\u043d\u0438\u044f) \u0438 \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u201c\u201d (\u043f\u0443\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439)  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/9c4\/0e2\/c35\/9c40e2c358b9c86379bc282242f1f2df.svg\" alt=\"$a + b + c == a + (b + c) == (a + b) + c$\" data-tex=\"display\"><\/math><\/p>\n<p>  <\/p>\n<p><math>$$display$$ &quot;&quot; + a == a == a + &quot;&quot;$$display$$<\/math><\/p>\n<p>  <\/p>\n<h4>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c<\/h4>\n<p>  \u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0440\u0430\u0432\u0435\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u043c\u0443:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/baf\/4b6\/43c\/baf4b643c6c92b1f192783370098051f.svg\" alt=\"$(A) -&gt; A$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c.<\/p>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043c\u043e\u043d\u043e\u0438\u0434, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439.<br \/>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<br \/>  \u041f\u0435\u0440\u0432\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 A \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u0430 B:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/608\/08f\/761\/60808f761776891fcf53d601ef90b8be.svg\" alt=\"$(A) -&gt; B$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0412\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 B \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u0430 \u0421:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/a2b\/48f\/c8f\/a2b48fc8f56e7741fbb58c890fd59ffb.svg\" alt=\"$(B) -&gt; C$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0418\u0437 \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0440\u0435\u0442\u044c\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 A<br \/>  \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u0430 \u0421:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/a06\/7f0\/327\/a067f0327623d90b46bbd4b5bf1f1cab.svg\" alt=\"$(A) -&gt; C$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u042d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439.<\/p>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">infix fun &lt;A, B, C&gt; ((A) -&gt; B).andThen(g: (B) -&gt; C): (A) -&gt; C = { a: A -&gt;     g(this(a)) } <\/code><\/pre>\n<p>  \u0410\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430:<\/p>\n<pre><code class=\"kotlin\">val f: (A) -&gt; B val g: (B) -&gt; C val h: (C) -&gt; D  f andThen g andThen h == f andThen (g andThen h) == (f andThen g) andThen h <\/code><\/pre>\n<p>  \u041d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f identity:<\/p>\n<pre><code class=\"kotlin\">fun &lt;T&gt; identity(x: T): T = x<\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u0440\u0438\u043d\u044f\u0442\u043e\u0435 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435.<\/p>\n<p>  \u0414\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e identity \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0442\u0430\u043a \u0436\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">f andThen ::identity = f = ::identity andThen f <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u043d\u043e\u0432\u044c \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438   <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/baf\/4b6\/43c\/baf4b643c6c92b1f192783370098051f.svg\" alt=\"$(A) -&gt; A$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0422\u0430\u043a \u043a\u0430\u043a \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439,<br \/>  \u0430 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u0430 \u0438 \u0438\u043c\u0435\u0435\u0442 \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f<br \/>  (A) -&gt; A \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c.<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/608\/08f\/761\/60808f761776891fcf53d601ef90b8be.svg\" alt=\"$(A) -&gt; B$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0433\u0434\u0435 B \u2014 \u044d\u0442\u043e \u043c\u043e\u043d\u043e\u0438\u0434 (\u0442.\u0435. \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c). <\/p>\n<p>  \u0422\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/a1d\/d76\/758\/a1dd767584d15d356b9aa68fa0683872.svg\" alt=\"$((A) -&gt; B), (A) -&gt; B)) -&gt; (A) -&gt; B$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c.<\/p>\n<p>  \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/ec7\/7e9\/b75\/ec77e9b75186b392e91dd1ea8aaba271.svg\" alt=\"$(A) -&gt; Int$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0420\u0430\u043d\u0435\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438, \u0447\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0442\u0438\u043f\u0430 Int \u0441 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u2014 \u044d\u0442\u043e \u043c\u043e\u043d\u043e\u0438\u0434.<\/p>\n<p>  \u0410 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (A) -&gt; Int \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0431\u0438\u043d\u0430\u0440\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"kotlin\">operator fun &lt;A&gt; ((A) -&gt; Int).plus(g: (A) -&gt; Int) = { a: A -&gt; this(a) + g(a) }<\/code><\/pre>\n<p>  \u0410\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u043e\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 Int \u0438 \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043b\u044f Int, \u0442\u043e \u0435\u0441\u0442\u044c:<\/p>\n<pre><code class=\"kotlin\">val f: (A) -&gt; Int = { 0 }<\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u043d\u043e\u0438\u0434\u044b \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c.<\/p>\n<h3>\u041f\u0440\u0430\u043a\u0442\u0438\u043a\u0430<\/h3>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043c\u043e\u043d\u043e\u0438\u0434\u0430:<\/p>\n<pre><code class=\"kotlin\">interface Monoid&lt;A&gt; {    operator fun plus(rh: A): A     fun empty(): A } <\/code><\/pre>\n<p>  \u041e\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: <\/p>\n<ul>\n<li>plus \u2014 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f<\/li>\n<li>empty \u2014 \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 plus<\/li>\n<\/ul>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c enum-\u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0442\u0438\u043f\u0443 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 (\u0431\u043e\u043b\u044c\u0448\u0435, \u043c\u0435\u043d\u044c\u0448\u0435, \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u043d\u043e):<\/p>\n<pre><code class=\"kotlin\">enum class Order(val compareValue: Int) {    LT(-1),    EQ(0),    GT(1) } <\/code><\/pre>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f User \u0441 \u043f\u043e\u043b\u044f\u043c\u0438 name \u0438 age.<br \/>  \u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0434\u0432\u0443\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 user1 \u0438 user2.<br \/>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0438\u043c\u0435\u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Order, \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0442\u0438\u043f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438.<br \/>  \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0432\u043e\u0437\u0440\u0430\u0441\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Order.<\/p>\n<p>  \u0412 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0438\u043c\u0435\u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<br \/>  \u0415\u0441\u043b\u0438 \u043e\u043d\u0438 \u043d\u0435 \u0440\u0430\u0432\u043d\u044b, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0438\u043c\u0435\u043d.<br \/>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u0438\u043c\u0435\u043d\u0430 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u043e\u0432:<\/p>\n<pre><code class=\"kotlin\">fun plus(nameOrder: Order, ageOrder: Order) = when(nameOrder) {        LT -&gt; LT        EQ -&gt; ageOrder        GT -&gt; GT    } <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0434\u0430\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439. \u0418\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Order, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043f\u0440\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0438\u043c\u0435\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0445 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u043e\u0432, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Order, \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 User.<br \/>  Order \u0441 \u0442\u0430\u043a\u043e\u0439 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c.<\/p>\n<pre><code class=\"kotlin\">enum class Order(val compareValue: Int) : Monoid&lt;Order&gt; {   LT(-1),   EQ(0),   GT(1);     override fun plus(rh: Order) = when (this) {        LT -&gt; LT        EQ -&gt; rh        GT -&gt; GT    }     override fun empty() = EQ } <\/code><\/pre>\n<p>  \u0410\u0441\u0441\u043e\u0446\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u0440\u043e\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/10f\/934\/95e\/10f93495e139fee64f2cfeb3fc9d7062.svg\" alt=\"$order1 + order2 + order3 == order1 + (order2 + order3) == (order1 + order2) + order3$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0422\u0430\u043a \u043a\u0430\u043a \u043d\u0435 \u0432\u0430\u0436\u0435\u043d \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043a\u043e\u0431\u043e\u043a, \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c order, \u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043b\u0435\u0432\u0435\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445. <br \/>  \u0418 \u043d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 EQ, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u0443:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/07b\/e6c\/a40\/07be6ca4097a95b0da591e4983f1b97f.svg\" alt=\"$EQ + order == order + EQ == order$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:  <\/p>\n<p><math><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/cd3\/c60\/ece\/cd3c60ecef328bd6ed6584484e785197.svg\" alt=\"$(A, A) -&gt; Order$\" data-tex=\"display\"><\/math><\/p>\n<p>  \u041e\u043d\u0430 \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u2014 \u043c\u043e\u043d\u043e\u0438\u0434.<\/p>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"kotlin\">fun interface ComparatorMonoid&lt;A&gt; : Monoid&lt;ComparatorMonoid&lt;A&gt;&gt; {    fun compare(a: A, other: A): Order     override fun plus(rh: ComparatorMonoid&lt;A&gt;) =        ComparatorMonoid&lt;A&gt; { a, other -&gt; compare(a, other) + rh.compare(a, other) }     override fun empty() = ComparatorMonoid&lt;A&gt; { _, _ -&gt; Order.EQ } } <\/code><\/pre>\n<p>  \u0412 Kotlin \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (String, Int, Float, &#8230;), \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 Comparable. \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f Order \u0434\u043b\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 Kotlin:<\/p>\n<pre><code class=\"kotlin\">fun &lt;A : Comparable&lt;A&gt;&gt; A.compare(other: A) = when {    this &gt; other -&gt; Order.GT    this == other -&gt; Order.EQ    else -&gt; Order.LT } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u0430\u0448 ComparatorMonoid \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0435\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 Comparable:<\/p>\n<pre><code class=\"kotlin\">val &lt;A, B : Comparable&lt;B&gt;&gt; ((A) -&gt; B).comparator     get():ComparatorMonoid&lt;A&gt; = ComparatorMonoid&lt;A&gt; { a, b -&gt;         invoke(a).compare(invoke(b))     } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0443\u0447\u0438\u043c \u0441\u043f\u0438\u0441\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 ComparatorMonoid:<\/p>\n<pre><code class=\"kotlin\">fun &lt;A&gt; Iterable&lt;A&gt;.sort(comparatorMonoid: ComparatorMonoid&lt;A&gt;) =    sortedWith { a, b -&gt; comparatorMonoid.compare(a, b).compareValue } <\/code><\/pre>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<ul>\n<li>\u0421\u043f\u0438\u0441\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043c\u0435\u044e\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 ComparatorMonoid;<\/li>\n<li>ComparatorMonoid, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u0435\u0442 \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u043e\u0438\u0434\u043e\u043c;<\/li>\n<li>ComparatorMonoid \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 Kotlin Comparable.<\/li>\n<\/ul>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u044d\u0442\u0438\u043c.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 User \u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441 Address:<\/p>\n<pre><code class=\"kotlin\">data class User(val name: String, val age: Int, val gender: String, val address: Address) data class Address(val city: String, val number: Int) <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435: <\/p>\n<pre><code class=\"kotlin\">val users: List&lt;User&gt; <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e \u043b\u044e\u0431\u044b\u043c \u043f\u043e\u043b\u044f\u043c \u0438 \u0432 \u043b\u044e\u0431\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e. <\/p>\n<p>  \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0443 \u043d\u0430\u0441 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0443, \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u0438\u043c\u0435\u043d\u0438. \u0420\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"kotlin\">users.sort(User::age.comparator + User::name.comparator) <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u0433\u043e\u0440\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 \u043f\u043e\u043b\u0435 city \u043a\u043b\u0430\u0441\u0441\u0430 Address, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u043f\u043e\u043b\u0443 (gender), \u0430 \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0433\u043e \u0435\u0449\u0451 \u0438 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 (name), \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"kotlin\">users.sort(    User::address.andThen(Address::city).comparator +    User::gender.comparator +    User::name.comparator ) <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u043d\u043e\u0438\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0431\u0430\u0437\u0435 \u0442\u0435\u043e\u0440\u0438\u0438 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Lens, \u0438 \u0442\u043e\u0433\u0434\u0430 \u043e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: <\/p>\n<pre><code class=\"kotlin\">users.sort(    User.address.city.comparator +    User.gender.comparator +    User.name.comparator ) <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/gj\/rg\/1h\/gjrg1htocqwtvk-eqb0xpqs-_1m.gif\"><\/p>\n<p>  Lens \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u043d\u0430 <a href=\"https:\/\/www.youtube.com\/channel\/UCj4XH0Un0-v78VcMgeUGqkg\" rel=\"nofollow\">\u043c\u043e\u0451\u043c youtube-\u043a\u0430\u043d\u0430\u043b\u0435<\/a>.<\/div>\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:\/\/habr.com\/ru\/post\/529798\/\"> https:\/\/habr.com\/ru\/post\/529798\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043c\u043e\u043d\u043e\u0438\u0434 \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u043c \u043f\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u0430 Kotlin \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e \u0437\u0430\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 <a href=\"https:\/\/www.youtube.com\/channel\/UCj4XH0Un0-v78VcMgeUGqkg\" rel=\"nofollow\">\u043c\u043e\u0439 youtube-\u043a\u0430\u043d\u0430\u043b<\/a>, \u0433\u0434\u0435 \u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044e \u0440\u0430\u0437\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0438\u0437 Kotlin FP.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tl\/i5\/7m\/tli57mfggcgrgnmloubm1txpjwe.jpeg\">  <\/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-313819","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313819","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=313819"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313819\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}