<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Naveen</title>
    <description>The latest articles on DEV Community by Naveen (@oneroguebishop).</description>
    <link>https://dev.to/oneroguebishop</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F924674%2F7d114de4-1dbb-4c81-92f4-aab3dbbc413b.png</url>
      <title>DEV Community: Naveen</title>
      <link>https://dev.to/oneroguebishop</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oneroguebishop"/>
    <language>en</language>
    <item>
      <title>Silence the makefile recipes</title>
      <dc:creator>Naveen</dc:creator>
      <pubDate>Sat, 28 Mar 2026 15:21:41 +0000</pubDate>
      <link>https://dev.to/oneroguebishop/silence-the-makefile-recipes-4igl</link>
      <guid>https://dev.to/oneroguebishop/silence-the-makefile-recipes-4igl</guid>
      <description>&lt;p&gt;Makefile is made up of recipes. Running a recipe in make, prints the recipe and also the output of every command in the recipe. Recipe can be implemented by re-directing the output to &lt;code&gt;/dev/null&lt;/code&gt; device.  But this approach has a problem. Sometimes, I wanted to read the output of the command and not be re-directed to &lt;code&gt;/dev/null&lt;/code&gt;, especially during troubleshooting. In this situation, there is no other way but to change implementation of recipe to remove the redirection. &lt;/p&gt;

&lt;p&gt;Makefile has a switch -s to silence the printing of the recipe during execution. I wanted to use this switch to decide for redirection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="k"&gt;ifneq&lt;/span&gt; &lt;span class="nv"&gt;($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)&lt;/span&gt;
  &lt;span class="nv"&gt;.REDIRECT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null 2&amp;gt;&amp;amp;1
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nv"&gt;.REDIRECT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="k"&gt;endif&lt;/span&gt;

&lt;span class="nl"&gt;test&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    which scala &lt;span class="p"&gt;$(&lt;/span&gt;.REDIRECT&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s2"&gt;"scala is not present."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, make test prints the recipe and output of the which command. But &lt;code&gt;make test -s&lt;/code&gt; completely silences the recipe execution.&lt;/p&gt;

&lt;p&gt;Makefile provides a way to silence the echoing of recipes but the implementation of recipe, as in this case, uses conditional redirection to silence the output of commands in the recipe. &lt;/p&gt;

&lt;p&gt;This approach, now, becomes less noisy until recipe requires to be debugged.&lt;/p&gt;

</description>
      <category>makefile</category>
    </item>
    <item>
      <title>Curl on FTP</title>
      <dc:creator>Naveen</dc:creator>
      <pubDate>Wed, 03 Apr 2024 12:32:37 +0000</pubDate>
      <link>https://dev.to/oneroguebishop/curl-on-ftp-4pl3</link>
      <guid>https://dev.to/oneroguebishop/curl-on-ftp-4pl3</guid>
      <description>&lt;h2&gt;
  
  
  cURL for FTP
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;cUrl&lt;/code&gt; is a terminal based client for data exchange over URL syntax. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I knew this definition about curl and I was usually using curl against &lt;code&gt;http&lt;/code&gt; endpoints with a several combination of options. There was a time at work where I had to troubleshoot an issue in production and logs were shared over an ftp server path. Little did I know that this little tool would just work with &lt;code&gt;ftp&lt;/code&gt; endpoints as well. So, here are some of the combinations I used curl to work on ftp endpoints. These are just starting points to give quick start and there are many more scenarios as well. &lt;/p&gt;

&lt;h2&gt;
  
  
  Directory listing
&lt;/h2&gt;

&lt;p&gt;Straight forward. Just remember that the directory to be listed would be the home directory of the authenticated user. Output would be list of all files &amp;amp; directories in the home directory of the authenticated user. While trying out listing, try using &lt;code&gt;--list-only&lt;/code&gt; to only return the name of remote objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="s2"&gt;"ftp://&amp;lt;ftp-host&amp;gt;:21"&lt;/span&gt; &lt;span class="nt"&gt;--user&lt;/span&gt; &amp;lt;ftp-user-id&amp;gt;:&amp;lt;ftp-user-password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Upload a local file to remote
&lt;/h2&gt;

&lt;p&gt;Option &lt;code&gt;--upload-file&lt;/code&gt; or &lt;code&gt;-T&lt;/code&gt;, in short, is used to upload files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--upload-file&lt;/span&gt; ./&amp;lt;local-filename&amp;gt; &lt;span class="s2"&gt;"ftp://&amp;lt;ftp-host&amp;gt;:21/&amp;lt;remote-filename&amp;gt;"&lt;/span&gt; &lt;span class="nt"&gt;--user&lt;/span&gt; &amp;lt;ftp-user-id&amp;gt;:&amp;lt;ftp-user-password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Download a remote file to local
&lt;/h2&gt;

&lt;p&gt;Option &lt;code&gt;--output&lt;/code&gt; or &lt;code&gt;-o (lowercase)&lt;/code&gt; is used to collect the content from the remote stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="s2"&gt;"ftp://&amp;lt;ftp-host&amp;gt;:21/&amp;lt;remote-name&amp;gt;"&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; ./&amp;lt;local-filename&amp;gt;  &lt;span class="nt"&gt;--user&lt;/span&gt; &amp;lt;ftp-user-id&amp;gt;:&amp;lt;ftp-user-password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additional point to remember is to consider &lt;code&gt;-O (uppercase)&lt;/code&gt; when the file has to be downloaded with the same name as in remote.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting a remote file
&lt;/h2&gt;

&lt;p&gt;Recollect that use of &lt;code&gt;-X&lt;/code&gt; option used to set the method name like &lt;code&gt;GET, PUT, POST, DELETE&lt;/code&gt; against http endpoints. It is because http server had commands named like &lt;code&gt;DELETE&lt;/code&gt;. In similar lines, FTP server has a command &lt;code&gt;DELE &amp;lt;file-to-delete&amp;gt;&lt;/code&gt; to purge the given file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="s2"&gt;"ftp://&amp;lt;ftp-server&amp;gt;:21"&lt;/span&gt; &lt;span class="nt"&gt;-X&lt;/span&gt; &lt;span class="s2"&gt;"DELE remote-file"&lt;/span&gt;  &lt;span class="nt"&gt;--user&lt;/span&gt; &amp;lt;ftp-user-id&amp;gt;:&amp;lt;ftp-user-password&amp;gt; &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Suggestions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use the option &lt;code&gt;-v&lt;/code&gt; for debugging. There is a lot to understand out of the request and response once this debugging switch is enabled.&lt;/li&gt;
&lt;li&gt;FTP server I used was not over ssl. In case, the remote FTP server is over ssl, use the switch --ftp-ssl.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it ! &lt;/p&gt;

&lt;p&gt;But yes, there are a lot more scenarios like traversing directory tree, purging a directory, handling binary content. But then the idea is that curl works well on ftp protocols. That makes it easy to script. Then there is no need of a dedicated application for working on FTP requests/responses. &lt;/p&gt;

</description>
      <category>development</category>
      <category>coding</category>
      <category>curl</category>
      <category>ftp</category>
    </item>
    <item>
      <title>Self signed certificate</title>
      <dc:creator>Naveen</dc:creator>
      <pubDate>Wed, 03 Apr 2024 12:30:28 +0000</pubDate>
      <link>https://dev.to/oneroguebishop/self-signed-certificate-1555</link>
      <guid>https://dev.to/oneroguebishop/self-signed-certificate-1555</guid>
      <description>&lt;h2&gt;
  
  
  What is a certificate?
&lt;/h2&gt;

&lt;p&gt;Certificate represents an identity of any entity.  An entity could be a person, host, application. In real world, I relate passport to a certificate. A passport is valid only when a &lt;em&gt;passport issuing authority&lt;/em&gt; approves a passport request. This is a valid passport because &lt;em&gt;passport issuing authority&lt;/em&gt; is trusted. With a trusted authority issuing a passport, this passport becomes an identity of the requesting entity, a certificate. Of course, authority requires a fees to be paid to validate the passport request before issuing one. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a self signed certificate?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In cryptography and computer security, self-signed certificates are public key certificates that their users issue on their own behalf, as opposed to a certificate authority (CA) issuing them.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;[ do not try this at home :-) ]&lt;/em&gt; Issuing your passport by your own self is a typical example I relate self signed certificate with. No authority is going to accept a self issued passport. Consider yourself spared if you even tried and not face legal issues. Issuing your own passport has no valid use case in real world. But a self signed certificate has a few use cases like it can be used in application development phase, no signing authority (CA) is involved - so no wait time on the authority to certificate approved and no cost involved.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never use a self signed certificate in a production system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to create self signed certificate?
&lt;/h2&gt;

&lt;p&gt;There are tools available to create self signed certificate - &lt;code&gt;keytool&lt;/code&gt; and &lt;code&gt;openssl&lt;/code&gt; are popular ones. In this blog, I will use &lt;code&gt;keytool&lt;/code&gt; which is provided with jdk. &lt;code&gt;openssl&lt;/code&gt; can also be used but the steps would be different. I will assume that I have a web application hosted in my local machine. This application should be serving HTTP requests over ssl - &lt;em&gt;HTTPS&lt;/em&gt;.  Let's create a self signed certificate by running the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;keytool &lt;span class="nt"&gt;-storetype&lt;/span&gt; pkcs12 &lt;span class="nt"&gt;-keystore&lt;/span&gt; sks.p12 &lt;span class="nt"&gt;-storepass&lt;/span&gt; manage &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-alias&lt;/span&gt; server_key_rsa &lt;span class="nt"&gt;-keyalg&lt;/span&gt; rsa &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-genkeypair&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-dname&lt;/span&gt; &lt;span class="s2"&gt;"CN=is, OU=rnd, O=sag, L=ba, ST=ka, C=in"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-ext&lt;/span&gt; &lt;span class="nv"&gt;ku&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;digitalsignature,keyencipherment,dataencipherment,keycertsign &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-ext&lt;/span&gt; &lt;span class="nv"&gt;bc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ca:true &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-ext&lt;/span&gt; &lt;span class="nv"&gt;san&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dns:localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation of the options used.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;-storetype&lt;/strong&gt; &lt;em&gt;pkcs12&lt;/em&gt;
&lt;/td&gt;
&lt;td&gt;Defines type of the keystore. Either pkcs12 or jks.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;- keystore&lt;/strong&gt; &lt;em&gt;sks.p12&lt;/em&gt;
&lt;/td&gt;
&lt;td&gt;Defines name of keystore file. Ex: &lt;em&gt;sks.p12&lt;/em&gt;.  Keystore file could be any name. A new file is created if one mentioned is not present.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;-storepass&lt;/strong&gt; &lt;em&gt;manage&lt;/em&gt;
&lt;/td&gt;
&lt;td&gt;Defines the password for the store.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;-keyalg&lt;/strong&gt; rsa&lt;/td&gt;
&lt;td&gt;Defines the algorithm to create the keypair.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;-alias&lt;/strong&gt; &lt;em&gt;server_key_rsa&lt;/em&gt;
&lt;/td&gt;
&lt;td&gt;Defines an alias for the keypair in a keystore. A keystore can have many key pairs, each of them uniquely identified by an alias. Ex: &lt;em&gt;server_key_rsa&lt;/em&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;-genkeypair&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Command to create keypair.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;-dname&lt;/strong&gt; &lt;em&gt;"CN=is, OU=rnd, O=gas, L=ba, ST=ka, C=in"&lt;/em&gt;
&lt;/td&gt;
&lt;td&gt;Defines distinguished name for the entity.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;-ext ku=digitalsignature,dataencipherment,keycertsign&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;For a self signed certificate, key usage (&lt;strong&gt;&lt;em&gt;ku&lt;/em&gt;&lt;/strong&gt;) should have &lt;strong&gt;keycertsign&lt;/strong&gt; and &lt;strong&gt;digitalsignature&lt;/strong&gt; as the primary generated by the entity will be used to sign its own public certificate.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;-ext bc=ca:true&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Defines certificate entity as ca.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;-ext san=dns:localhost&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Defines server name as &lt;em&gt;localhost&lt;/em&gt;. It is also possible to &lt;em&gt;localhost&lt;/em&gt; in CN and clients could accept but it is more clear to define the dns as localhost to be explicit while the name of application can be used in &lt;em&gt;CN&lt;/em&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, we have a keystore (&lt;em&gt;sks.p12&lt;/em&gt;) with a keypair identified by alias &lt;em&gt;'server_key_rsa'&lt;/em&gt;.  This keystore should be configured in java server application to server HTTP over ssl. I wont be getting into how to configure keystore in java application to server HTTPS. &lt;/p&gt;

&lt;h2&gt;
  
  
  Extracting self signed cert
&lt;/h2&gt;

&lt;p&gt;Let's assume application is serving HTTPS on port 5443. I use &lt;code&gt;curl&lt;/code&gt; and &lt;code&gt;httpie&lt;/code&gt;.  On firing &lt;code&gt;curl https://localhost:5443&lt;/code&gt;, there is an ssl error. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1652131255613%2FV6M9AUFtb.png%2520align%3D" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1652131255613%2FV6M9AUFtb.png%2520align%3D" alt="bash.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Curl&lt;/code&gt; connected to https port. Server sent the certificate, a self signed one which curl does not trust simply because there is no issuing authority involved.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing the client to accommodate self signed certificate
&lt;/h2&gt;

&lt;p&gt;I know that the server is using a self signed certificate and this is not a production system. So, I want the client to work with it. That leaves two options. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ignore ssl verification&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;curl&lt;/th&gt;
&lt;th&gt;httpie&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;curl https://localhost:5443 --insecure&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 |&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;http https://localhost:5443 --verify no&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the server certificate which was prepared with &lt;code&gt;-ext bc=ca:true&lt;/code&gt; to be used as a mocked ca cert.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Refresh the keystore command we used to create the keypair. The alias we use is &lt;em&gt;server_key&lt;/em&gt; and we need a server certificate. Use the below command to extract the server certificate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;keytool &lt;span class="nt"&gt;-export&lt;/span&gt; &lt;span class="nt"&gt;-rfc&lt;/span&gt; &lt;span class="nt"&gt;-keystore&lt;/span&gt; sks.p12 &lt;span class="nt"&gt;-alias&lt;/span&gt; server_key_rsa &lt;span class="nt"&gt;--storepass&lt;/span&gt; manage &lt;span class="nt"&gt;-file&lt;/span&gt; server_key_rsa_ca.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the command options, by now, are already defined are familiar. Option &lt;code&gt;-rfc&lt;/code&gt; defines the format of the certificate to be rfc format. Now, server certificate is exported to a file &lt;em&gt;server_key_rsa_ca.pem&lt;/em&gt;. Feel free to try out &lt;code&gt;keytool -printcert -file server_key_rsa_ca.pem&lt;/code&gt; to view the content of certificate. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;curl&lt;/th&gt;
&lt;th&gt;httpie&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;curl https://localhost:5443 --cacert  server_key_rsa_ca.pem&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 |&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;http https://localhost:5443 --verify  server_key_rsa_ca.pem&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, the self signed certificate sent by the server is validated against the server certificate and request passes through. &lt;/p&gt;

&lt;p&gt;This is how I got self signed certificate created, configured and used it in the client to test. &lt;/p&gt;

&lt;p&gt;I put this blog up as I went through a lot of articles to get the self signed working with server and client by accepting the certificate. While this is a reference to the future me, I hope this article helped you as well. &lt;/p&gt;

&lt;p&gt;Now, leave your experience with self signed certificate for me to learn !! &lt;/p&gt;

</description>
      <category>ssl</category>
      <category>development</category>
      <category>coding</category>
    </item>
    <item>
      <title>Type Class</title>
      <dc:creator>Naveen</dc:creator>
      <pubDate>Wed, 03 Apr 2024 12:26:15 +0000</pubDate>
      <link>https://dev.to/oneroguebishop/type-class-3ikd</link>
      <guid>https://dev.to/oneroguebishop/type-class-3ikd</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Type class&lt;/em&gt;&lt;/strong&gt; is an abstract, parameterized type used to extend the behavior of a closed type without sub-typing it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Suppose, we want to represent an instance of a particular type in its string format. The built-in &lt;code&gt;toString&lt;/code&gt; is not always useful and is a typical example to understand type class. There are 2 steps to creating a type class&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Implement a type class&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement type class instances of concrete types.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the instance is created, there are different approaches to using the instance with slight variations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement a type class
&lt;/h2&gt;

&lt;p&gt;Let's define a type class to define the method to return a string of the type instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A type class is a &lt;em&gt;trait&lt;/em&gt;. It is parameterized on a type and defines a behavior. The behavior in &lt;em&gt;Show&lt;/em&gt; is the method &lt;em&gt;show(t: T)&lt;/em&gt;. This becomes the template to apply to those types that require.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement concrete type class
&lt;/h2&gt;

&lt;p&gt;Let us assume, &lt;em&gt;Domain&lt;/em&gt; type must be &lt;em&gt;Showable&lt;/em&gt;. We implement &lt;em&gt;Show[Domain]&lt;/em&gt; and create a type class instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShowDepartment&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;name&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showDepartmentInstance&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ShowDepartment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type class instance &lt;code&gt;showDomain&lt;/code&gt; need not be implicit to be a type class instance! This is covered in the usage of the instance. That's it. We have implemented a concrete type class and created a type class instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Different use cases and convenience required in usage lead to multiple approaches that the type class and its implementation evolve with additional methods. There are two types of using the implementation of concrete type class. They are classified as &lt;strong&gt;instance type&lt;/strong&gt; and &lt;strong&gt;syntax type&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Instance type
&lt;/h3&gt;

&lt;p&gt;We have an instance of type class created and it could be explicitly used by direct reference. This makes the call site tightly coupled to the type which is probably never a requirement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Domain&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;showDepartmentInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's make the call site polymorphic on the type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;ii&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;ii&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the call site is polymorphic on type. The type class instance to be used depends on the type. Hence, the instance is marked implicit. This means a type of &lt;code&gt;Show[A]&lt;/code&gt; must be present in the implicit scope. This is the reason the type class instance &lt;code&gt;showDomain&lt;/code&gt; is marked implicit for the call site to work with &lt;code&gt;Department&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sugared usage
&lt;/h4&gt;

&lt;p&gt;Call site still uses an implicit reference to type class instance and can it be eliminated? Being so, the call site becomes concise. The conciseness is in the idea that there is no reference to type class instance in the snippet. Call site is completely expressed as the name of type class and the behavior required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The semantic &lt;code&gt;show[A: Show]&lt;/code&gt; is referred to as the &lt;strong&gt;context-bound&lt;/strong&gt; type. So, it is read as &lt;em&gt;the call site method&lt;/em&gt; &lt;code&gt;show&lt;/code&gt; is polymorphic on A provided the Show is bound to A.&lt;/p&gt;

&lt;p&gt;In a context-bound call site of a type class instance, there is no direct reference to a type class instance. So, we use &lt;code&gt;implicitly[_]&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;implicitly&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]].&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The design of the type class has forced the call site to implicitly resolve the type class instance whether it is sugared or the de-sugared form. This can be improved by a companion for the type class which summons the implicit instance of the type class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// companion for the type class `Show`&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// call site&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Companion object only summons an implicit instance. It does not create one. The complete implementation looks this way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// type class&lt;/span&gt;
&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// summons an implicit instance&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// creation of implicit instance&lt;/span&gt;
  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showDepartmentInstance&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;name&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// call site #1 with implicit parameters defined&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_v1&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;ii&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;ii&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// call site #2 with context bound implicit instance&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_v2&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// test&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Engineering"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;show_v1&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// String: Engineering&lt;/span&gt;
&lt;span class="nf"&gt;show_v2&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// String: Engineering&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem statement could further be extended but the solution pattern is already simplified. In the current domain model, let's suppose there is a &lt;code&gt;Student&lt;/code&gt; belonging &lt;code&gt;Department&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// create implicit instance&lt;/span&gt;
  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showStudentInstance&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; 
      &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"${s.name}, ${Show[Department].show(s.department)}"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// test&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;s&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Martin"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Domain&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Engineering"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;show_v1&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// String: "Martin, Engineering"&lt;/span&gt;
&lt;span class="nf"&gt;show_v2&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// String: "Martin, Engineering"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Student&lt;/code&gt; has an instance of &lt;code&gt;Department&lt;/code&gt;. So, the &lt;code&gt;show()&lt;/code&gt; of the Student requires the &lt;code&gt;show()&lt;/code&gt; of the Department. Since there is one in the implicit scope already, it only needs to be summoned. Hence, &lt;code&gt;Show[Department]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, we notice a redundancy in the way type class instances are getting created. The core difference lies only in the function by which a given instance of &lt;code&gt;Department&lt;/code&gt; or &lt;code&gt;Student&lt;/code&gt; would be transformed to String. The rest of it is all boilerplate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// from Department&lt;/span&gt;
  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showDepartment&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;name&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// from Student&lt;/span&gt;
  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showStudentInstance&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; 
      &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"${s.name}, ${Show[Department].show(s.department)}"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be solved by enhancing the companion object of &lt;code&gt;Show&lt;/code&gt; with a helper method to create instances given a required function. It is just a helper function to create instances but the instances get created when this helper function is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// summons the instance&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;inst&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inst&lt;/span&gt;

  &lt;span class="c1"&gt;// helper: to create type class instance.&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// instance creation&lt;/span&gt;
  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showDepartmentInstance&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;showStudentInstance&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"${s.name]} from ${Show[Department].show(s.department)}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the boilerplate code to create an instance is now replaced by a function.&lt;/p&gt;

&lt;p&gt;It appears to be a lengthy topic about type class with instance type of usage but the definition of type class and its concrete implementation define the type class behavior. The rest of it is the evolution of the companion object &lt;code&gt;Show&lt;/code&gt; from not existing in the bare-bone implementation of type class to a flavor with helpers to create and summon the type class instance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax type
&lt;/h3&gt;

&lt;p&gt;With the &lt;em&gt;syntax type&lt;/em&gt;, we can improve the type class usage with the possibility of calling the &lt;code&gt;show()&lt;/code&gt; on the given object as if it were defined on the object itself. This is called the &lt;em&gt;extension method&lt;/em&gt;. This is achieved by having an implicit class wrapping over the instance of type T.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShowOps&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;enggDep&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Engineering"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;enggDep&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt; &lt;span class="c1"&gt;// String: "Engineering"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no &lt;code&gt;show()&lt;/code&gt; on &lt;code&gt;Department&lt;/code&gt; yet, with the syntactic usage of a type class, the method appears to be on the &lt;code&gt;Deparment&lt;/code&gt;. The method &lt;code&gt;show()&lt;/code&gt; could have been given any other name as well. It is an arbitrary method name but &lt;code&gt;show()&lt;/code&gt; makes sense in this case. This is re-written as &lt;code&gt;(new ShowOps[Department](enggDep)).show(showDomainInstance)&lt;/code&gt; by the compiler. In this implementation of &lt;code&gt;ShowOps&lt;/code&gt;, it is only the method &lt;code&gt;show&lt;/code&gt; constraint by the type. An alternate implementation is to enforce the constraint at the class level.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShowOps&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;enggDep&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Engineering"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;enggDep&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I prefer enforcing the constraint at the lowest unit of work which, in this case, is the method&lt;code&gt;show&lt;/code&gt; leaving the class generic.&lt;/p&gt;

&lt;h4&gt;
  
  
  Where to place Ops implementation?
&lt;/h4&gt;

&lt;p&gt;Class &lt;code&gt;ShowOps&lt;/code&gt; is generic and can be placed in the companion of &lt;code&gt;Show&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// instantiator&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// summoner&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;

  &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;ops&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShowOps&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Show&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Show._&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Show.ops._&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Finance"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;show&lt;/span&gt; &lt;span class="c1"&gt;// String: Finance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type class is an interesting concept and I have been reading about it from different sources. So, I decided to pen down my learning from different sources like &lt;a href="https://docs.scala-lang.org" rel="noopener noreferrer"&gt;scala-lang.org&lt;/a&gt;, &lt;a href="https://scalac.io" rel="noopener noreferrer"&gt;scalac.io&lt;/a&gt;, with examples that I could relate to better.&lt;/p&gt;

</description>
      <category>scala</category>
      <category>coding</category>
    </item>
    <item>
      <title>Function in bash</title>
      <dc:creator>Naveen</dc:creator>
      <pubDate>Sat, 08 Oct 2022 07:45:43 +0000</pubDate>
      <link>https://dev.to/oneroguebishop/function-in-bash-502b</link>
      <guid>https://dev.to/oneroguebishop/function-in-bash-502b</guid>
      <description>&lt;h2&gt;
  
  
  Function
&lt;/h2&gt;

&lt;p&gt;Function is a familiar topic from mathematics to many programming languages. Function has a defined operation or computation, &lt;em&gt;example: f(x)=x ^ 2&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Function, generally, takes one or more input, operates on the input and returns output. As in the example, given &lt;em&gt;x&lt;/em&gt;, the function returns &lt;em&gt;x ^ 2&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function in bash
&lt;/h2&gt;

&lt;p&gt;Bash scripts can have functions too - tricky part is to return the result of a computation. There are a few options to return the result of the computation in function back to the caller of the function. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;em&gt;return&lt;/em&gt; keyword
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;return&lt;/em&gt;, in bash, is used to status of execution. Status of execution is represented as integers ranging from &lt;em&gt;0 - 2 ^ 7&lt;/em&gt; with &lt;em&gt;0&lt;/em&gt; indicating and the rest indicating failure. The caller of such a function should use &lt;em&gt;$?&lt;/em&gt; to read the returned value. This approach is restrictive that the result of computation may not always be returned with loss. Range 0-255 is too small range of integer probably my niece from kindergarten could use !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;add&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;expr &lt;/span&gt;200 + 200&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# main&lt;/span&gt;
add
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try this one out ! Expect 400 but result is going to be different (&lt;em&gt;144&lt;/em&gt;). Clearly &lt;em&gt;return&lt;/em&gt; is not meant return result of computation. It is meant to return the state of computation. 0 return code means and any other integer just means something went wrong in the computation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using global variable
&lt;/h2&gt;

&lt;p&gt;Using global variable is a naive approach that leads mutation of state. Relying on the global variable based approach would nearly make the script stateful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;em&gt;echo&lt;/em&gt; keyword
&lt;/h2&gt;

&lt;p&gt;Using &lt;em&gt;echo&lt;/em&gt; is open ended. Any arbitrary type can be returned, example Int, String. Caller of such a function should use evaluation to consume the output of computation. Unlike &lt;em&gt;return&lt;/em&gt; statement, there is no limitation in the range, datatype and there is no mutation state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;add&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;expr &lt;/span&gt;200 + 200&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# main&lt;/span&gt;
&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;add 200 + 200&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;em&gt;echo&lt;/em&gt; approach is closer to how the commands are used within the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="nv"&gt;today&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Command &lt;code&gt;date&lt;/code&gt; is a builtin tool and the example shows the usage. Now, I can relate to using &lt;code&gt;echo&lt;/code&gt; more. I can build shell with all such library functions and import them into any necessary shell scripts. &lt;/p&gt;

&lt;h3&gt;
  
  
  Gotcha with echo
&lt;/h3&gt;

&lt;p&gt;As the purpose of &lt;em&gt;echo&lt;/em&gt; is only to return values, echo should not be used to perform console logging.  However, logging to file is still fine with &lt;code&gt;echo "My log message" &amp;gt; logfile&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There is no one specific recommended approach. It depends on the scenario and the principles followed by the team. When the principles of referential transparency, purity has to be strictly followed, the preferred approach is to use &lt;strong&gt;echo&lt;/strong&gt; approach.&lt;/p&gt;

</description>
      <category>bash</category>
    </item>
    <item>
      <title>Tail recursion</title>
      <dc:creator>Naveen</dc:creator>
      <pubDate>Sat, 08 Oct 2022 07:29:19 +0000</pubDate>
      <link>https://dev.to/oneroguebishop/tail-recursion-4l93</link>
      <guid>https://dev.to/oneroguebishop/tail-recursion-4l93</guid>
      <description>&lt;p&gt;Before I get into tail recursion, I will take a step back to refresh how I reached the topic of &lt;strong&gt;Tail Recursion&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Coming from the foundation of object oriented paradigms, I wanted to understand the principles of functional programming.  Of the many principles, one took my attention that I read multiple times to confirm what I just read. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Looping is evil&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Functional programming paradigm is about all about expressions. Looping as imperative action and not an expression. If “looping is evil” then how to “loop”? How to iterate over data-structure? &lt;/p&gt;

&lt;h2&gt;
  
  
  Enter RECURSION
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Recursion&lt;/strong&gt;. Seriously ! StackOverFlowError ! That’s what struck my head immediately. But then I guessed there could be something special in it and I was thrilled to be ‘technically’ surprised 😉.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Recursion – process of a function calling itself&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Application of recursion – the infamous example – factorial, fibonacci series are typical applications. Let us look at an implementation of factorial.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ref: 1 - recursive implementation&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A happy little implementation but only until ‘n’ tends to be a large number – just as large as 5000 is sufficient to brought out the evil – StackOverflowError. StackOverflowError occurred because the implementation used stack – an implicit one, called as ‘call stack’ and it was attempted to fill beyond its limit. Stacks are fixed size and they can run out of space. But what is filling the implicit stack? Take a look at the code. When is the expression evaluated? Only when the termination condition &lt;code&gt;n == 1&lt;/code&gt; is met. Until then the expression is preserved in the stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# ref: 2
For f (3) = 
stack[0] -&amp;gt; 3 * f(2)
stack[1] -&amp;gt; 3 * 2 * f(1)
stack[2] -&amp;gt; 3 * 2 * 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When ‘n’ tends to be a large number, the stack is bound to be filled. Now that the problem is understood, how to use recursion without running into StackOverflowError?&lt;/p&gt;

&lt;p&gt;The iterative approach to calculate the recursion does not fail even for a large number that failed with recursion. Below snippet is an imperative implementation of factorial.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ref: 3 - imperative implementation&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;factIt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="nf"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why did the recursive function run into an error?
&lt;/h3&gt;

&lt;p&gt;In the recursive implementation ref:1, the implicit call stack was preserved because the last expression had to be evaluated out of the return of the recursive call – referring to &lt;code&gt;n * fact (n-1)&lt;/code&gt;. The stack element has to be preserved to evaluate the product of fact(n-1) and n. This led to preservation of the stack, there by overflowing the capacity of the stack itself for large values of ‘n’.&lt;/p&gt;

&lt;h2&gt;
  
  
  From recursion to tail recursion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A type of recursion
-- Other types are direction and indirect recursion&lt;/li&gt;
&lt;li&gt;Recursive function calls itself as the last step in the body.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tail recursion solves the problem of running into &lt;code&gt;StackOverflowError&lt;/code&gt; by not having the expression to be evaluated after the recursive call. Instead, the expression is passed as ‘another’ parameter. Let us go through the tail recursive approach of factorial function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ref: 4 - tail recursion&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" factorial of 5000"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Appears more lines of implementation – but effective enough to handle the large input value with recursion. The key piece of code is &lt;code&gt;else helper(n-1, a * n)&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;function ‘helper’ invokes itself and that is the final step.&lt;/li&gt;
&lt;li&gt;function ‘helper’ evaluates expressions (n-1, a * n) before invoking itself.&lt;/li&gt;
&lt;li&gt;Expression &lt;code&gt;a * n&lt;/code&gt; is the state that gets carried over. It is called &lt;em&gt;accumulation&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now with the refactored recursive function, is the stack actually used? Or is the stack still used? Answer: the implicit call stack is still used in tail recursive implementation but the problem of StackOverflowError no longer occurs for large values of ‘n’. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reason =&amp;gt; tail call optimization by compilers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Modern day compilers are optimized for tail calls. When the compiler identifies a function to be tail recursive, it internally optimizes the tail recursive functions to use an iterative approach internally – at least for Scala compiler.&lt;/p&gt;

&lt;p&gt;Now, do we replace every recursion by tail recursion? Not necessarily. But being aware of such a technique given that the compiler can perform tail call optimizations is important.&lt;/p&gt;

&lt;p&gt;I am glad that this article kept you interested this far.&lt;/p&gt;

</description>
      <category>recursion</category>
      <category>scala</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
