{"id":415,"date":"2020-02-15T11:40:19","date_gmt":"2020-02-15T11:40:19","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=415"},"modified":"2020-12-31T06:57:21","modified_gmt":"2020-12-31T06:57:21","slug":"package-string","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/02\/15\/package-string\/","title":{"rendered":"Package String"},"content":{"rendered":"\n<p>Just like Java or C++ there are standard libraries (packages) available with golang. The one that you might end up using the most often is <code>strings<\/code>. <\/p>\n\n\n\n<p>For this blog, I was writing another practice <a href=\"https:\/\/exercism.io\/my\/tracks\/go\">exercise<\/a> that is meant to count number of words in an input string.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Go string is a sequence of variable-width characters (each 1 to 4 bytes).<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Definition of Word<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>A <em>number<\/em> composed of one or more ASCII digits (ie &#8220;0&#8221; or &#8220;1234&#8221;) OR<\/li><li>A <em>simple word<\/em> composed of one or more ASCII letters (ie &#8220;a&#8221; or &#8220;they&#8221;) OR<\/li><li>A <em>contraction<\/em> of two <em>simple words<\/em> joined by a single apostrophe (ie &#8220;it&#8217;s&#8221; or &#8220;they&#8217;re&#8221;)<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Package : Strings<\/h2>\n\n\n\n<p>The package string exposes utility functions to manipulate UTF-8 encoded strings.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-image is-style-rounded\"><figure class=\"aligncenter size-thumbnail\"><img decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/12\/IMG_0846-150x150.jpg\" alt=\"\" class=\"wp-image-1205\"\/><\/figure><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center is-style-large is-layout-flow wp-block-quote-is-layout-flow\"><p>Strings are value types and immutable, which means that once created, you cannot modify the contents of the string. In other words, strings are immutable arrays of bytes.<\/p><cite>\u2014 Official GoDocs<\/cite><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>package wordcount\n\nimport (\n    \"strings\"\n    \"unicode\"\n)\n\n\/\/ Frequency of the words\ntype Frequency map&#91;string]int\n\n\/\/ WordCount Count the words occurrence.\nfunc WordCount(in string) Frequency {\n    var myMap Frequency = make(map&#91;string]int, 0)\n\n    in = strings.ToLower(in)\n    inStrings := strings.FieldsFunc(in, func(c rune) bool {\n        return c == ' ' || c == '\\n' || c == '\\t' || c == ','\n    })\n\n    for _, val := range inStrings {\n        val = strings.TrimFunc(val, func(r rune) bool {\n            return !unicode.IsLetter(r) &amp;&amp; !unicode.IsDigit(r)\n        })\n        myMap&#91;val]++\n    }\n    return myMap\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\"><div class=\"wp-block-group__inner-container\"><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Functions used<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>FieldsFunc<\/li><li>TrimFunc<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">FieldsFunc<\/h3>\n\n\n\n<p>More details <a href=\"https:\/\/golang.org\/pkg\/strings\/#FieldsFunc\">here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">func FieldsFunc(s <a href=\"https:\/\/golang.org\/pkg\/builtin\/#string\">string<\/a>, f func(<a href=\"https:\/\/golang.org\/pkg\/builtin\/#rune\">rune<\/a>) <a href=\"https:\/\/golang.org\/pkg\/builtin\/#bool\">bool<\/a>) []<a href=\"https:\/\/golang.org\/pkg\/builtin\/#string\">string<\/a><\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-style-large is-layout-flow wp-block-quote-is-layout-flow\"><p><em>FieldsFunc<\/em> splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned. <em>FieldsFunc<\/em> makes no guarantees about the order in which it calls f(c). If f does not return consistent results for a given c, FieldsFunc may crash.<\/p><cite>golang official documentation<\/cite><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>func(c rune) bool {\n        return c == ' ' || c == '\\n' || c == '\\t' || c == ','\n    })<\/code><\/pre>\n\n\n\n<p>The function <em><strong>FieldFunc<\/strong><\/em> is supplied with an input string <code>s<\/code> and a function that runs through the runes (<em>unicode point<\/em>) and returns the split if the condition evaluates to be true.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>in &gt; testing, 1, 2 testing<\/code><\/pre>\n\n\n\n<p>The function will split it as under<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"testing\", \"1\", \"2\", \"testing\"}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">TrimFunc<\/h3>\n\n\n\n<p>More details <a href=\"https:\/\/golang.org\/pkg\/strings\/#TrimFunc\">here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">func TrimFunc(s <a href=\"https:\/\/golang.org\/pkg\/builtin\/#string\">string<\/a>, f func(<a href=\"https:\/\/golang.org\/pkg\/builtin\/#rune\">rune<\/a>) <a href=\"https:\/\/golang.org\/pkg\/builtin\/#bool\">bool<\/a>) <a href=\"https:\/\/golang.org\/pkg\/builtin\/#string\">string<\/a><\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-style-large is-layout-flow wp-block-quote-is-layout-flow\"><p>TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.<\/p><cite>golang documentation<\/cite><\/blockquote>\n\n\n\n<p>This function trims down the unwanted characters in the string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func(r rune) bool {\n            return !unicode.IsLetter(r) &amp;&amp; !unicode.IsDigit(r)\n}<\/code><\/pre>\n\n\n\n<p>Essentially we remove everything that is not letter or digit form the string.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">in&gt; car: carpet as java: javascript!!&amp;@$%^&amp;<\/pre>\n\n\n\n<p>It will strip <code>car:<\/code> to <code>car<\/code> likewise <code>javascript<\/code> will be stripped of the unwanted characters in the end.<\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">&#8212; THE &#8211; END &#8212;<\/h3>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just like Java or C++ there are standard libraries (packages) available with golang. The one that you might end up using the most often is strings. For this blog, I was writing another practice exercise that is meant to count number of words in an input string. Go string is a sequence of variable-width characters [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":416,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[31,23,51,35],"class_list":["post-415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-exercism","tag-golang","tag-string","tag-strings"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/416"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}