{"id":416735,"date":"2024-06-30T01:08:15","date_gmt":"2024-06-30T01:08:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=416735"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=416735","title":{"rendered":"<span>\u0414\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0432 PHP<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0432 PHP. <a href=\"https:\/\/github.com\/doctrine\/collections\/blob\/1.6.x\/lib\/Doctrine\/Common\/Collections\/Collection.php\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0442\u0440\u0438\u043d\u043e\u0432\u0441\u043a\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/a> \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b \u0438 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b. <a href=\"https:\/\/github.com\/azjezz\/psl\" rel=\"noopener noreferrer nofollow\">PSL \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/a> \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b. \u041d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0432\u0438\u0434\u0435\u043b \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439. \u0412\u0435\u0437\u0434\u0435 \u043c\u0435\u043d\u044f \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043b\u043e \u0438 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <a href=\"https:\/\/github.com\/whsv26\/functional\" rel=\"noopener noreferrer nofollow\">\u0441\u0432\u043e\u044e open source \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e<\/a> \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0441 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u043c\u0438 \u0442\u0435\u043c\u043f\u043b\u0435\u0439\u0442-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0438 \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0435\u0439 \u043f\u0443\u0441\u0442\u044b\u0445 \u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 \u0431\u044b\u043b \u0432\u044b\u0431\u0440\u0430\u043d <a href=\"https:\/\/github.com\/vimeo\/psalm\" rel=\"noopener noreferrer nofollow\">Psalm<\/a>.<\/p>\n<hr\/>\n<h2>\u0418\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439<\/h2>\n<p>\u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430 <code>Collection<\/code> \u0438 <code>NonEmptyCollection<\/code>.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0435 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439: \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Collection) \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b <code>Seq<\/code>, <code>Map<\/code> \u0438 <code>Set<\/code>.<\/p>\n<ul>\n<li>\n<p>Seq &#8212; \u044d\u0442\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 Sequence. \u0423\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c Seq &#8212; \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a LinkedList.<\/p>\n<\/li>\n<li>\n<p>Map \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c HashMap.<\/p>\n<\/li>\n<li>\n<p>Set &#8212; \u044d\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c HashSet.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 NonEmptyCollection) \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u043f\u043e \u0442\u0430\u043a\u043e\u043c\u0443 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u043d\u043e \u0443 \u043d\u0438\u0445 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0444\u0438\u043a\u0441 NonEmpty \u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432.<\/p>\n<h4>\u041e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/h4>\n<pre><code class=\"php\">Collection&lt;TV> -> Seq&lt;TV>     -> LinearSeq&lt;TV>   -> LinkedList&lt;TV> Collection&lt;TV> -> Seq&lt;TV>     -> IndexedSeq&lt;TV>  -> ArrayList&lt;TV> Collection&lt;TV> -> Set&lt;TV>     -> HashSet&lt;TV> Collection&lt;TV> -> Map&lt;TK, TV> -> HashMap&lt;TK, TV><\/code><\/pre>\n<h4>\u041d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/h4>\n<pre><code class=\"php\">NonEmptyCollection&lt;TV> -> NonEmptySeq&lt;TV> -> NonEmptyLinearSeq&lt;TV>  -> NonEmptyLinkedList&lt;TV> NonEmptyCollection&lt;TV> -> NonEmptySeq&lt;TV> -> NonEmptyIndexedSeq&lt;TV> -> NonEmptyArrayList&lt;TV> NonEmptyCollection&lt;TV> -> NonEmptySet&lt;TV> -> NonEmptyHashSet&lt;TV><\/code><\/pre>\n<h2>\u0422\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043b\u0430\u0433\u0438\u043d\u044b \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 Psalm, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0444\u0430\u0439\u043d\u0438\u043d\u0433 \u0442\u0438\u043f\u043e\u0432 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043a\u043b\u044e\u0447\u0435\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u0430\u043a\u00a0<code>filter<\/code>\u00a0\u0438\u00a0<code>filterNotNull<\/code>.<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f NonEmptyLinkedList&lt;1|2|3>  *\/ $collection = NonEmptyLinkedList::collectNonEmpty([1, 2, 3]);  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f NonEmptyLinkedList&lt;int>  *   * \u041b\u0438\u0442\u0435\u0440\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043f\u0440\u043e\u043f\u0430\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438   * \u041d\u043e NonEmpty \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0441\u044f,   * \u0422.\u043a. \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c  *\/ $mappedCollection = $collection->map(fn($elem) => $elem - 1);  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f LinkedList&lt;positive-int>  *   * NonEmpty \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043f\u0440\u043e\u043f\u0430\u043b \u043f\u043e\u0441\u043b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438  * \u0422.\u043a. \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u043e\u0433\u043b\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c\u0441\u044f   * \u0418 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0441\u0442\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439   *\/ $filteredCollection = $mappedCollection->filter(fn(int $elem) => $elem > 0);<\/code><\/pre>\n<hr\/>\n<pre><code class=\"php\">&lt;?php  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f non-empty-list&lt;Foo|null|Bar>  *\/ $source = [new Foo(1), null, new Bar(2)];  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f ArrayList&lt;Foo|Bar>  *   * Null \u0431\u044b\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d \u0438\u0437 \u0442\u0438\u043f\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  *   * NonEmpty \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u043f\u0430\u043b \u043f\u043e\u0441\u043b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438  * \u0422.\u043a. \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u043e\u0433\u043b\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c\u0441\u044f   * \u0418 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0441\u0442\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439   *\/ $withoutNulls = NonEmptyArrayList::collectNonEmpty($source)->filterNotNull();  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f ArrayList&lt;Foo>  *   * Bar \u0431\u044b\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d \u0438\u0437 \u0442\u0438\u043f\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  *\/ $onlyFoos = $withoutNulls->filter(fn($elem) => $elem instanceof Foo);<\/code><\/pre>\n<h2>\u041a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0422\u0430\u0439\u043f-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b, \u0432\u00a0\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u00a0\u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0434\u043e\u043a\u0442\u0440\u0438\u043d\u044b \u0438\u00a0PHP Standart Library (PSL).<\/p>\n<pre><code class=\"php\">&lt;?php  class User {} class Admin extends User {}  \/** * @param NonEmptyCollection&lt;User> $collection *\/ function acceptUsers(NonEmptyCollection $collection): void {}  \/**   * @var NonEmptyLinkedList&lt;Admin> $collection   *\/ $collection = NonEmptyLinkedList::collectNonEmpty([new Admin()]);  \/**  * \u041c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0430\u0434\u043c\u0438\u043d\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439  * \u0418\u0437-\u0437\u0430 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0442\u0430\u0439\u043f-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  *\/ acceptUsers($collection);<\/code><\/pre>\n<h2>\u0418\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435\u00a0\u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f. \u0412\u0441\u0435 \u043c\u0443\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438.<\/p>\n<pre><code class=\"php\">&lt;?php  $originalCollection = LinkedList::collect([1, 2, 3]);  \/**  * $originalCollection \u043d\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f  * \u0418 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439  *\/ $prependedCollection = $originalCollection->prepended(0);  \/**  * $prependedCollection \u043d\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f  * \u0418 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439  *\/ $mappedCollection = $prependedCollection->map(fn(int $elem) => $elem + 1);<\/code><\/pre>\n<h2>Null-\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u043d\u0430\u0434\u044b\u00a0<code>Option<\/code>\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c null pointer \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439. \u0412\u00a0\u043b\u0438\u0431\u0435 \u0432\u043c\u0435\u0441\u0442\u043e\u00a0<code>null<\/code>\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043b\u0438\u0431\u043e\u00a0<code>Some,<\/code>\u00a0\u043b\u0438\u0431\u043e\u00a0<code>None<\/code>\u00a0.<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * @var Collection&lt;int> $emptyCollection   *\/ $emptyCollection = getEmptyCollection();  \/**  * \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f reduce \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043d\u0435\u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439  *   * \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u0438\u0434\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c null,   * \u0435\u0441\u043b\u0438 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432,   * \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0432 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f   * \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043c\u043e\u043d\u0430\u0434\u044b Option&lt;T> (Some&lt;T>|None)  *  * \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435   * \u0438\u043b\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u043d\u0430 null    *\/ $resultWithDefaultValue = $emptyCollection     ->reduce(fn(int $accumulator, int $element) => $accumulator + $element)     ->getOrElse(0);<\/code><\/pre>\n<hr\/>\n<pre><code class=\"php\">&lt;?php  class Order {     public function __construct(public int $id, public int $price) {} }  \/**  * In-memory \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u0440\u043e\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e ID \u0437\u0430\u043a\u0430\u0437\u043e\u0432  *\/ $ordersInMemoryStorage = HashMap::collect([     [$orderId1 = rand(), new Order(id: $orderId1, price: 0)],     [$orderId2 = rand(), new Order(id: $orderId2, price: 999)], ]);  \/**  * \u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0435 \u0434\u0435\u043b\u0435\u043d\u0438\u0435  *   * \u042d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430   * \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 Option::flatMap  *   * @return Option&lt;float>  *\/ function div(int $dividend, int $divisor): Option {     return 0 === $divisor         ? Option::none()         : Option::some($dividend \/ $divisor); }  \/**  * \u041f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u0432 Map-\u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u043e\u043d\u0430\u0434\u0443 Option  *   * \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439   * \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e map, flatMap \u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 Option.  *   * \u0426\u0435\u043f\u043e\u0447\u043a\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0441\u044f,   * \u0435\u0441\u043b\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435  * \u0438\u043b\u0438 \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u043d\u043e\u043b\u044c  *   * \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043d\u0430 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u044d\u0442\u0430\u043f\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430,  * \u0442\u043e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 getOrElse(0) \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f 0,   * \u0432\u043c\u0435\u0441\u0442\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f  *\/ $ordersInMemoryStorage     ->get($orderId1)     ->map(fn(Order $order): int => $order->price)     ->flatMap(fn(int $price): Option => div(1, $price))     ->getOrElse(0);<\/code><\/pre>\n<h2>\u0421\u0432\u044f\u0437\u044c HashSet, HashMap \u0438\u00a0HashContract<\/h2>\n<p><code>HashSet<\/code>\u00a0\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u00a0<code>HashMap<\/code>.<\/p>\n<p>\u0427\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f\u00a0<code>HashMap<\/code>\u00a0\u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 PHP? \u041a\u043b\u044e\u0447\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u00a0PHP \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438. \u0422\u0438\u043f \u043a\u043b\u044e\u0447\u0435\u0439 \u0432\u00a0<code>HashMap<\/code>\u00a0\u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d. \u042d\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432. Numeric-string \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 PHP \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432\u00a0\u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438. \u0412\u00a0<code>HashMap<\/code>\u00a0\u0442\u0430\u043a\u043e\u0433\u043e \u043d\u0435\u00a0\u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u00a0\u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u00a0\u043a\u043b\u044e\u0447\u0435\u043c\u00a0<code>\"1\"<\/code>\u00a0\u043d\u0435\u043b\u044c\u0437\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u043f\u043e\u00a0\u043a\u043b\u044e\u0447\u0443\u00a0<code>1<\/code>.<\/p>\n<p><code>HashMap<\/code>\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043d\u0435\u00a0\u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u00a0\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u00a0\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u00a0<code>HashContract<\/code>. \u0415\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442, \u0442\u043e\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c\u044b\u0435 \u0432\u00a0\u043a\u043b\u0430\u0441\u0441\u0435 \u043a\u043b\u044e\u0447\u0430-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>hashCode<\/code>\u00a0\u0438\u00a0<code>equals<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u00a0\u043a\u043b\u044e\u0447\u0430\u043c.<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * \u0415\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c HashContract (\u043c\u0435\u0442\u043e\u0434\u044b hashCode \u0438 equals),  * \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043b\u0430\u0441\u0441\u0430  * \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0435\u0439 \u0432 HashMap   * \u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 HashSet  *\/ class Foo implements HashContract {     public function __construct(public int $a, public bool $b = true)     {     }      \/**      * \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u043d\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442       * \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c      * \u043f\u043e \u0442\u0438\u043f\u0443 \u0438 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u043c \u043f\u043e\u043b\u044f\u043c \u043a\u043b\u0430\u0441\u0441\u0430      *      * \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0431\u0430\u043a\u0435\u0442\u0435       * \u043f\u043e \u043a\u043b\u044e\u0447\u0443-\u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430      *\/     public function equals(mixed $rhs): bool     {         return $rhs instanceof self             &amp;&amp; $this->a === $rhs->a             &amp;&amp; $this->b === $rhs->b;     }      \/**      * \u0425\u044d\u0448 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u0430      *       * \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u0431\u0430\u043a\u0435\u0442       * \u0432 HashMap'\u0435      *\/      public function hashCode(): string     {         return md5(implode(',', [$this->a, $this->b]));     } }  \/**  * \u0421\u0431\u043e\u0440\u043a\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435,  * \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u0438\u0434\u0430 array{TKey, TValue}  *\/ $hashMap = HashMap::collect([     [new Foo(1), 1], [new Foo(2), 2],     [new Foo(3), 3], [new Foo(4), 4] ]);  \/**  * \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 HashMap  * \u0438 \u0447\u0435\u0439\u043d\u0438\u043d\u0433\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439  *\/ $hashMap     ->map(fn($elem) => $elem + 1)     ->filter(fn($elem) => $elem > 2)     ->reindex(fn($elem, Foo $key) => $key->a)     ->fold(0, fn(int $acc, array $pair) => $acc + $pair[1]); \/\/ 3+4+5=12  \/**  * \u041f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  * \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u044b  *   * \u041e\u0441\u0442\u0430\u043d\u0443\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e Foo(1), Foo(2), Foo(3), Foo(4)  *\/ $hashSet = HashSet::collect([     new Foo(1), new Foo(2), new Foo(2),      new Foo(3), new Foo(3), new Foo(4), ]);  \/**  * \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 HashSet  * \u0438 \u0447\u0435\u0439\u043d\u0438\u043d\u0433\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439  *\/ $hashSet     ->map(fn(Foo $elem) => $elem->a)     ->filter(fn(int $elem) => $elem > 1)     ->reduce(fn($acc, $elem) => $acc + $elem)     ->getOrElse(0); \/\/ 9 <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/574782\/\"> https:\/\/habr.com\/ru\/articles\/574782\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0432 PHP. <a href=\"https:\/\/github.com\/doctrine\/collections\/blob\/1.6.x\/lib\/Doctrine\/Common\/Collections\/Collection.php\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0442\u0440\u0438\u043d\u043e\u0432\u0441\u043a\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/a> \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b \u0438 \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b. <a href=\"https:\/\/github.com\/azjezz\/psl\" rel=\"noopener noreferrer nofollow\">PSL \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/a> \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b. \u041d\u0438\u0433\u0434\u0435 \u043d\u0435 \u0432\u0438\u0434\u0435\u043b \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439. \u0412\u0435\u0437\u0434\u0435 \u043c\u0435\u043d\u044f \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043b\u043e \u0438 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <a href=\"https:\/\/github.com\/whsv26\/functional\" rel=\"noopener noreferrer nofollow\">\u0441\u0432\u043e\u044e open source \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e<\/a> \u0438\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0441 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u043c\u0438 \u0442\u0435\u043c\u043f\u043b\u0435\u0439\u0442-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0438 \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0435\u0439 \u043f\u0443\u0441\u0442\u044b\u0445 \u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 \u0431\u044b\u043b \u0432\u044b\u0431\u0440\u0430\u043d <a href=\"https:\/\/github.com\/vimeo\/psalm\" rel=\"noopener noreferrer nofollow\">Psalm<\/a>.<\/p>\n<hr\/>\n<h2>\u0418\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439<\/h2>\n<p>\u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430 <code>Collection<\/code> \u0438 <code>NonEmptyCollection<\/code>.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0435 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439: \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Collection) \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b <code>Seq<\/code>, <code>Map<\/code> \u0438 <code>Set<\/code>.<\/p>\n<ul>\n<li>\n<p>Seq &#8212; \u044d\u0442\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 Sequence. \u0423\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c Seq &#8212; \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a LinkedList.<\/p>\n<\/li>\n<li>\n<p>Map \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c HashMap.<\/p>\n<\/li>\n<li>\n<p>Set &#8212; \u044d\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c HashSet.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 NonEmptyCollection) \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u043f\u043e \u0442\u0430\u043a\u043e\u043c\u0443 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u043d\u043e \u0443 \u043d\u0438\u0445 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0444\u0438\u043a\u0441 NonEmpty \u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432.<\/p>\n<h4>\u041e\u0431\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/h4>\n<pre><code class=\"php\">Collection&lt;TV> -> Seq&lt;TV>     -> LinearSeq&lt;TV>   -> LinkedList&lt;TV> Collection&lt;TV> -> Seq&lt;TV>     -> IndexedSeq&lt;TV>  -> ArrayList&lt;TV> Collection&lt;TV> -> Set&lt;TV>     -> HashSet&lt;TV> Collection&lt;TV> -> Map&lt;TK, TV> -> HashMap&lt;TK, TV><\/code><\/pre>\n<h4>\u041d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/h4>\n<pre><code class=\"php\">NonEmptyCollection&lt;TV> -> NonEmptySeq&lt;TV> -> NonEmptyLinearSeq&lt;TV>  -> NonEmptyLinkedList&lt;TV> NonEmptyCollection&lt;TV> -> NonEmptySeq&lt;TV> -> NonEmptyIndexedSeq&lt;TV> -> NonEmptyArrayList&lt;TV> NonEmptyCollection&lt;TV> -> NonEmptySet&lt;TV> -> NonEmptyHashSet&lt;TV><\/code><\/pre>\n<h2>\u0422\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043b\u0430\u0433\u0438\u043d\u044b \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 Psalm, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0444\u0430\u0439\u043d\u0438\u043d\u0433 \u0442\u0438\u043f\u043e\u0432 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043a\u043b\u044e\u0447\u0435\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u0430\u043a\u00a0<code>filter<\/code>\u00a0\u0438\u00a0<code>filterNotNull<\/code>.<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f NonEmptyLinkedList&lt;1|2|3>  *\/ $collection = NonEmptyLinkedList::collectNonEmpty([1, 2, 3]);  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f NonEmptyLinkedList&lt;int>  *   * \u041b\u0438\u0442\u0435\u0440\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043f\u0440\u043e\u043f\u0430\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438   * \u041d\u043e NonEmpty \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0441\u044f,   * \u0422.\u043a. \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c  *\/ $mappedCollection = $collection->map(fn($elem) => $elem - 1);  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f LinkedList&lt;positive-int>  *   * NonEmpty \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043f\u0440\u043e\u043f\u0430\u043b \u043f\u043e\u0441\u043b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438  * \u0422.\u043a. \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u043e\u0433\u043b\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c\u0441\u044f   * \u0418 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0441\u0442\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439   *\/ $filteredCollection = $mappedCollection->filter(fn(int $elem) => $elem > 0);<\/code><\/pre>\n<hr\/>\n<pre><code class=\"php\">&lt;?php  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f non-empty-list&lt;Foo|null|Bar>  *\/ $source = [new Foo(1), null, new Bar(2)];  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f ArrayList&lt;Foo|Bar>  *   * Null \u0431\u044b\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d \u0438\u0437 \u0442\u0438\u043f\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  *   * NonEmpty \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u043f\u0430\u043b \u043f\u043e\u0441\u043b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438  * \u0422.\u043a. \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u043e\u0433\u043b\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c\u0441\u044f   * \u0418 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0441\u0442\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439   *\/ $withoutNulls = NonEmptyArrayList::collectNonEmpty($source)->filterNotNull();  \/**  * \u0412\u044b\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f ArrayList&lt;Foo>  *   * Bar \u0431\u044b\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d \u0438\u0437 \u0442\u0438\u043f\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  *\/ $onlyFoos = $withoutNulls->filter(fn($elem) => $elem instanceof Foo);<\/code><\/pre>\n<h2>\u041a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0422\u0430\u0439\u043f-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b, \u0432\u00a0\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u00a0\u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0434\u043e\u043a\u0442\u0440\u0438\u043d\u044b \u0438\u00a0PHP Standart Library (PSL).<\/p>\n<pre><code class=\"php\">&lt;?php  class User {} class Admin extends User {}  \/** * @param NonEmptyCollection&lt;User> $collection *\/ function acceptUsers(NonEmptyCollection $collection): void {}  \/**   * @var NonEmptyLinkedList&lt;Admin> $collection   *\/ $collection = NonEmptyLinkedList::collectNonEmpty([new Admin()]);  \/**  * \u041c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0430\u0434\u043c\u0438\u043d\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439  * \u0418\u0437-\u0437\u0430 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0442\u0430\u0439\u043f-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  *\/ acceptUsers($collection);<\/code><\/pre>\n<h2>\u0418\u043c\u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435\u00a0\u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f. \u0412\u0441\u0435 \u043c\u0443\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u043d\u043e\u0432\u044b\u0435 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438.<\/p>\n<pre><code class=\"php\">&lt;?php  $originalCollection = LinkedList::collect([1, 2, 3]);  \/**  * $originalCollection \u043d\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f  * \u0418 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439  *\/ $prependedCollection = $originalCollection->prepended(0);  \/**  * $prependedCollection \u043d\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f  * \u0418 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439  *\/ $mappedCollection = $prependedCollection->map(fn(int $elem) => $elem + 1);<\/code><\/pre>\n<h2>Null-\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u043d\u0430\u0434\u044b\u00a0<code>Option<\/code>\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c null pointer \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439. \u0412\u00a0\u043b\u0438\u0431\u0435 \u0432\u043c\u0435\u0441\u0442\u043e\u00a0<code>null<\/code>\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043b\u0438\u0431\u043e\u00a0<code>Some,<\/code>\u00a0\u043b\u0438\u0431\u043e\u00a0<code>None<\/code>\u00a0.<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * @var Collection&lt;int> $emptyCollection   *\/ $emptyCollection = getEmptyCollection();  \/**  * \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f reduce \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043d\u0435\u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439  *   * \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u0438\u0434\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c null,   * \u0435\u0441\u043b\u0438 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432,   * \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0432 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f   * \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043c\u043e\u043d\u0430\u0434\u044b Option&lt;T> (Some&lt;T>|None)  *  * \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435   * \u0438\u043b\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u043d\u0430 null    *\/ $resultWithDefaultValue = $emptyCollection     ->reduce(fn(int $accumulator, int $element) => $accumulator + $element)     ->getOrElse(0);<\/code><\/pre>\n<hr\/>\n<pre><code class=\"php\">&lt;?php  class Order {     public function __construct(public int $id, public int $price) {} }  \/**  * In-memory \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u0440\u043e\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e ID \u0437\u0430\u043a\u0430\u0437\u043e\u0432  *\/ $ordersInMemoryStorage = HashMap::collect([     [$orderId1 = rand(), new Order(id: $orderId1, price: 0)],     [$orderId2 = rand(), new Order(id: $orderId2, price: 999)], ]);  \/**  * \u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0435 \u0434\u0435\u043b\u0435\u043d\u0438\u0435  *   * \u042d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430   * \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 Option::flatMap  *   * @return Option&lt;float>  *\/ function div(int $dividend, int $divisor): Option {     return 0 === $divisor         ? Option::none()         : Option::some($dividend \/ $divisor); }  \/**  * \u041f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u0432 Map-\u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u043e\u043d\u0430\u0434\u0443 Option  *   * \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439   * \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e map, flatMap \u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 Option.  *   * \u0426\u0435\u043f\u043e\u0447\u043a\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0441\u044f,   * \u0435\u0441\u043b\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435  * \u0438\u043b\u0438 \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u043d\u043e\u043b\u044c  *   * \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043d\u0430 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u044d\u0442\u0430\u043f\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430,  * \u0442\u043e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 getOrElse(0) \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f 0,   * \u0432\u043c\u0435\u0441\u0442\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f  *\/ $ordersInMemoryStorage     ->get($orderId1)     ->map(fn(Order $order): int => $order->price)     ->flatMap(fn(int $price): Option => div(1, $price))     ->getOrElse(0);<\/code><\/pre>\n<h2>\u0421\u0432\u044f\u0437\u044c HashSet, HashMap \u0438\u00a0HashContract<\/h2>\n<p><code>HashSet<\/code>\u00a0\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u00a0<code>HashMap<\/code>.<\/p>\n<p>\u0427\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f\u00a0<code>HashMap<\/code>\u00a0\u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 PHP? \u041a\u043b\u044e\u0447\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u00a0PHP \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438. \u0422\u0438\u043f \u043a\u043b\u044e\u0447\u0435\u0439 \u0432\u00a0<code>HashMap<\/code>\u00a0\u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d. \u042d\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432. Numeric-string \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 PHP \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432\u00a0\u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438. \u0412\u00a0<code>HashMap<\/code>\u00a0\u0442\u0430\u043a\u043e\u0433\u043e \u043d\u0435\u00a0\u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u00a0\u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u00a0\u043a\u043b\u044e\u0447\u0435\u043c\u00a0<code>\"1\"<\/code>\u00a0\u043d\u0435\u043b\u044c\u0437\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u043f\u043e\u00a0\u043a\u043b\u044e\u0447\u0443\u00a0<code>1<\/code>.<\/p>\n<p><code>HashMap<\/code>\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043d\u0435\u00a0\u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u00a0\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u00a0\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u00a0<code>HashContract<\/code>. \u0415\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442, \u0442\u043e\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c\u044b\u0435 \u0432\u00a0\u043a\u043b\u0430\u0441\u0441\u0435 \u043a\u043b\u044e\u0447\u0430-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>hashCode<\/code>\u00a0\u0438\u00a0<code>equals<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u00a0\u043a\u043b\u044e\u0447\u0430\u043c.<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * \u0415\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c HashContract (\u043c\u0435\u0442\u043e\u0434\u044b hashCode \u0438 equals),  * \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043b\u0430\u0441\u0441\u0430  * \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0435\u0439 \u0432 HashMap   * \u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 HashSet  *\/ class Foo implements HashContract {     public function __construct(public int $a, public bool $b = true)     {     }      \/**      * \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u043d\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442       * \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c      * \u043f\u043e \u0442\u0438\u043f\u0443 \u0438 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u043c \u043f\u043e\u043b\u044f\u043c \u043a\u043b\u0430\u0441\u0441\u0430      *      * \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0431\u0430\u043a\u0435\u0442\u0435       * \u043f\u043e \u043a\u043b\u044e\u0447\u0443-\u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430      *\/     public function equals(mixed $rhs): bool     {         return $rhs instanceof self             &amp;&amp; $this->a === $rhs->a             &amp;&amp; $this->b === $rhs->b;     }      \/**      * \u0425\u044d\u0448 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u0430      *       * \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u0431\u0430\u043a\u0435\u0442       * \u0432 HashMap'\u0435      *\/      public function hashCode(): string     {         return md5(implode(',', [$this->a, $this->b]));     } }  \/**  * \u0421\u0431\u043e\u0440\u043a\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435,  * \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u0438\u0434\u0430 array{TKey, TValue}  *\/ $hashMap = HashMap::collect([     [new Foo(1), 1], [new Foo(2), 2],     [new Foo(3), 3], [new Foo(4), 4] ]);  \/**  * \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 HashMap  * \u0438 \u0447\u0435\u0439\u043d\u0438\u043d\u0433\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439  *\/ $hashMap     ->map(fn($elem) => $elem + 1)     ->filter(fn($elem) => $elem > 2)     ->reindex(fn($elem, Foo $key) => $key->a)     ->fold(0, fn(int $acc, array $pair) => $acc + $pair[1]); \/\/ 3+4+5=12  \/**  * \u041f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438  * \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u044b  *   * \u041e\u0441\u0442\u0430\u043d\u0443\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e Foo(1), Foo(2), Foo(3), Foo(4)  *\/ $hashSet = HashSet::collect([     new Foo(1), new Foo(2), new Foo(2),      new Foo(3), new Foo(3), new Foo(4), ]);  \/**  * \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 HashSet  * \u0438 \u0447\u0435\u0439\u043d\u0438\u043d\u0433\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439  *\/ $hashSet     ->map(fn(Foo $elem) => $elem->a)     ->filter(fn(int $elem) => $elem > 1)     ->reduce(fn($acc, $elem) => $acc + $elem)     ->getOrElse(0); \/\/ 9 <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/574782\/\"> https:\/\/habr.com\/ru\/articles\/574782\/<\/a><br \/><\/br><\/br><\/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-416735","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/416735","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=416735"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/416735\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=416735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=416735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}