{"id":365434,"date":"2024-05-21T02:38:08","date_gmt":"2024-05-21T02:38:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=365434"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=365434","title":{"rendered":"<span>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0441\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430. \u041a\u0430\u0441\u0442\u043e\u043c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f. (Custom implementation of higher-order functions.)<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e23\/fc1\/31b\/e23fc131b1be5c50d9ba8d7cba90948b.png\" width=\"1200\" height=\"675\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e23\/fc1\/31b\/e23fc131b1be5c50d9ba8d7cba90948b.png\"\/><\/figure>\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442!<\/p>\n<p>\u041d\u0430 \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0441\u044f\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432\u044b\u0441\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430. \u0420\u0435\u0448\u0438\u043b \u043f\u043e\u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0438\u0445 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043c\u043e\u0438\u0445 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u043a =) <\/p>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u0431\u044b\u0432\u0430\u0435\u0442 \u043b\u0435\u0433\u0447\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0451 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0439 (Generic). \u0422\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u044f \u0438 \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u043d\u0438\u0436\u0435.<\/p>\n<p>\u0412\u0441\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u00a0extension Array)<\/p>\n<h4>Reduce<\/h4>\n<pre><code class=\"swift\">extension Array {     func customIntReduce(res: Int, handler:(Int, Int) -> Int) -> Int {         var result = res         for elem in self {             result = handler(result, elem as! Int)         }         return result     }          func customReduce&lt;T, U>(res: T, handler:(T, U) -> T) -> T {         var result = res         for elem in self {             result = handler(result, elem as! U)         }         return result     } }  print([1, 2, 3].customIntReduce(res: 0) { $0 + $1 }) print([1, 2, 3].customReduce(res: 0) { $0 + $1 })<\/code><\/pre>\n<h4>Filter<\/h4>\n<pre><code class=\"swift\">  func customIntFilter(handler: (Int) -> Bool) -> [Int] {         var filteredArray: [Int] = []                  for element in self {             if handler(element as! Int) {                 filteredArray.append(element as! Int)             }         }         return filteredArray     }          func customFilter(handler: (Element) -> Bool) -> [Element] {         var filteredArray: [Element] = []         for element in self {             if handler(element) {                 filteredArray.append(element)             }         }         return filteredArray     }      print([1, 2, 3, 4, 5].customIntFilter{ $0 > 2})     print([1, 2, 3, 4, 5].customFilter{ $0 > 2})<\/code><\/pre>\n<h4>Map<\/h4>\n<pre><code class=\"swift\"> func customIntMap(handler: (Int) -> Any) -> [Any] {         var mapedArray:[Any] = []         for elem in self {             let mapedValue = handler(elem as! Int)             mapedArray.append(mapedValue)         }         return mapedArray     }          func customMap&lt;T>(handler: (Element) -> T) -> [T] {         var mapedArray: [T] = []         for elem in self {             let mapedValue = handler(elem)             mapedArray.append(mapedValue)         }         return mapedArray     }      print([1, 2, 3].customIntMap{ \"\\($0 * $0)\" })     print([1, 2, 3].customMap{ \"\\($0 * $0)\" })<\/code><\/pre>\n<h4>Compact Map<\/h4>\n<pre><code class=\"swift\">func customStringCompactMap(_ handler: (String) -> Any?) -> [Any] {         var mapedArray: [Any] = []         for elem in self {             if let mapedValue = handler(elem as! String) {                 mapedArray.append(mapedValue)             }         }         return mapedArray     }          func customCompactMap&lt;T>(handler: (Element) -> T?) -> [T] {         var mapedArray: [T] = []         for elem in self {             if let mapedValue = handler(elem) {                 mapedArray.append(mapedValue)             }         }         return mapedArray     }      print([\"1\", \"two\", \"3\"].customStringCompactMap{ Int($0) })     print([\"1\", \"two\", \"3\"].customCompactMap{ Int($0) })<\/code><\/pre>\n<h4>Flat Map<\/h4>\n<pre><code class=\"swift\"> func customIntFlatMap(handler: ([Int]) -> Any) -> [Any] {         var mappedArray: [Any] = []         forEach {             mappedArray += handler($0 as! [Int]) as! Array&lt;Any>         }         return mappedArray     }       func customFlatMap&lt;T>(handler: (Element) -> [T]) -> [T] {         var mappedArray: [T] = []         forEach {             mappedArray += handler($0)         }         return mappedArray     }       let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]              let mapped = arr.customIntFlatMap{ $0 }     print(mapped)              let mapped2 = arr.customFlatMap{ $0 }     print(mapped2)<\/code><\/pre>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043a \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044e, \u0430 \u043a\u043e\u043c\u0443-\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445.<\/p>\n<\/p>\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\/786746\/\"> https:\/\/habr.com\/ru\/articles\/786746\/<\/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<figure class=\"full-width\"><\/figure>\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442!<\/p>\n<p>\u041d\u0430 \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0441\u044f\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432\u044b\u0441\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430. \u0420\u0435\u0448\u0438\u043b \u043f\u043e\u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0438\u0445 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043c\u043e\u0438\u0445 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u043a =) <\/p>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u0431\u044b\u0432\u0430\u0435\u0442 \u043b\u0435\u0433\u0447\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0451 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0439 (Generic). \u0422\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u044f \u0438 \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u043d\u0438\u0436\u0435.<\/p>\n<p>\u0412\u0441\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u00a0extension Array)<\/p>\n<h4>Reduce<\/h4>\n<pre><code class=\"swift\">extension Array {     func customIntReduce(res: Int, handler:(Int, Int) -> Int) -> Int {         var result = res         for elem in self {             result = handler(result, elem as! Int)         }         return result     }          func customReduce&lt;T, U>(res: T, handler:(T, U) -> T) -> T {         var result = res         for elem in self {             result = handler(result, elem as! U)         }         return result     } }  print([1, 2, 3].customIntReduce(res: 0) { $0 + $1 }) print([1, 2, 3].customReduce(res: 0) { $0 + $1 })<\/code><\/pre>\n<h4>Filter<\/h4>\n<pre><code class=\"swift\">  func customIntFilter(handler: (Int) -> Bool) -> [Int] {         var filteredArray: [Int] = []                  for element in self {             if handler(element as! Int) {                 filteredArray.append(element as! Int)             }         }         return filteredArray     }          func customFilter(handler: (Element) -> Bool) -> [Element] {         var filteredArray: [Element] = []         for element in self {             if handler(element) {                 filteredArray.append(element)             }         }         return filteredArray     }      print([1, 2, 3, 4, 5].customIntFilter{ $0 > 2})     print([1, 2, 3, 4, 5].customFilter{ $0 > 2})<\/code><\/pre>\n<h4>Map<\/h4>\n<pre><code class=\"swift\"> func customIntMap(handler: (Int) -> Any) -> [Any] {         var mapedArray:[Any] = []         for elem in self {             let mapedValue = handler(elem as! Int)             mapedArray.append(mapedValue)         }         return mapedArray     }          func customMap&lt;T>(handler: (Element) -> T) -> [T] {         var mapedArray: [T] = []         for elem in self {             let mapedValue = handler(elem)             mapedArray.append(mapedValue)         }         return mapedArray     }      print([1, 2, 3].customIntMap{ \"\\($0 * $0)\" })     print([1, 2, 3].customMap{ \"\\($0 * $0)\" })<\/code><\/pre>\n<h4>Compact Map<\/h4>\n<pre><code class=\"swift\">func customStringCompactMap(_ handler: (String) -> Any?) -> [Any] {         var mapedArray: [Any] = []         for elem in self {             if let mapedValue = handler(elem as! String) {                 mapedArray.append(mapedValue)             }         }         return mapedArray     }          func customCompactMap&lt;T>(handler: (Element) -> T?) -> [T] {         var mapedArray: [T] = []         for elem in self {             if let mapedValue = handler(elem) {                 mapedArray.append(mapedValue)             }         }         return mapedArray     }      print([\"1\", \"two\", \"3\"].customStringCompactMap{ Int($0) })     print([\"1\", \"two\", \"3\"].customCompactMap{ Int($0) })<\/code><\/pre>\n<h4>Flat Map<\/h4>\n<pre><code class=\"swift\"> func customIntFlatMap(handler: ([Int]) -> Any) -> [Any] {         var mappedArray: [Any] = []         forEach {             mappedArray += handler($0 as! [Int]) as! Array&lt;Any>         }         return mappedArray     }       func customFlatMap&lt;T>(handler: (Element) -> [T]) -> [T] {         var mappedArray: [T] = []         forEach {             mappedArray += handler($0)         }         return mappedArray     }       let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]              let mapped = arr.customIntFlatMap{ $0 }     print(mapped)              let mapped2 = arr.customFlatMap{ $0 }     print(mapped2)<\/code><\/pre>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043a \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044e, \u0430 \u043a\u043e\u043c\u0443-\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445.<\/p>\n<\/p>\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\/786746\/\"> https:\/\/habr.com\/ru\/articles\/786746\/<\/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-365434","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/365434","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=365434"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/365434\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}