{"id":17,"date":"2024-10-10T07:11:24","date_gmt":"2024-10-10T07:11:24","guid":{"rendered":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/?p=17"},"modified":"2024-10-10T07:28:11","modified_gmt":"2024-10-10T07:28:11","slug":"examples-of-map-function-in-swift","status":"publish","type":"post","link":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/2024\/10\/10\/examples-of-map-function-in-swift\/","title":{"rendered":"Examples of map function in Swift"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\" id=\"d504\"><strong>Map Function:<\/strong>&nbsp;Use map to loop over a collection and apply the same operation to each element in the collection. It returns an array always.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"0b87\">While learning \u2018map\u2019 function, I felt paucity of examples to clear the idea. Thought to document different examples I came through:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"b768\"><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let values = [2.0,4.0,5.0,7.0]<br>let squares = values.map {$0 * $0}<br>print(squares)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"ced7\"><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let arrayOfNumbers = [1, 2, 3, 4]<br>let arrayOfString = arrayOfNumbers.map { \"\\($0)\" }<br>print(arrayOfString) \/\/ Output: [\u201c1\u201d, \u201c2\u201d, \u201c3\u201d,\u201c4\u201d]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"7c92\"><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let scores = [0,28,124] <br>let words = scores.map { <br>NumberFormatter.localizedString(from: $0 as NSNumber, number: .spellOut) <br>}<br>print(words)\/\/ Output: [\u201czero\u201d, \u201ctwenty-eight\u201d, \u201cone hundred twenty-four\u201d]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"c559\"><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let celcius = [-5.0, 10.0, 21.0, 33.0, 50.0] <br>let fahrenheit = celcius.map { $0 * (9\/5) + 32 } <br>print(fahrenheit) \/\/ Output: [23.0, 50.0, 69.8, 91.4, 122.0]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"3e8b\"><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let lengthInMeters: Set = [4.0,6.2,8.9]<br>let lengthInFeet = lengthInMeters.map {meters in meters * 3.2808}<br>print(lengthInFeet)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"5ccf\">Here is a complete playground of the above code:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"b768\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import UIKit\n\n\/\/Example 1\nlet values = &#91;2.0,4.0,5.0,7.0]\nlet squares = values.map {$0 * $0}\nprint(squares)\n\n\/\/Example 2\nlet arrayOfNumbers = &#91;1, 2, 3, 4]\nlet arrayOfString = arrayOfNumbers.map {\"\\($0)\"}\nprint(arrayOfString)\n\/\/ Output: &#91;\u201c1\u201d, \u201c2\u201d, \u201c3\u201d,\u201c4\u201d]\n\n\/\/Example 3\nlet scores = &#91;0,28,124]\nlet words = scores.map {\n    NumberFormatter.localizedString(from: $0 as NSNumber, number: .spellOut)\n}\nprint(words)\n\/\/ Output: &#91;\u201czero\u201d, \u201ctwenty-eight\u201d, \u201cone hundred twenty-four\u201d]\n\n\/\/Example 4\nlet celcius = &#91;-5.0, 10.0, 21.0, 33.0, 50.0]\nlet fahrenheit = celcius.map { $0 * (9\/5) + 32 }\nprint(fahrenheit)\n\/\/ Output: &#91;23.0, 50.0, 69.8, 91.4, 122.0]\n\n\/\/Example 5\nlet lengthInMeters: Set = &#91;4.0,6.2,8.9]\nlet lengthInFeet = lengthInMeters.map {meters in meters * 3.2808}\nprint(lengthInFeet)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Map Function:&nbsp;Use map to loop over a collection and apply the same operation to each element in the collection. It returns an array always. While&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-basic-swift"],"menu_order":0,"_links":{"self":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":1,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}