<?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: Kuldeep Yadav</title>
    <description>The latest articles on DEV Community by Kuldeep Yadav (@novicedev7291).</description>
    <link>https://dev.to/novicedev7291</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%2F372054%2Fa05d0057-b187-4d5f-b000-dee8fa3b52ec.jpeg</url>
      <title>DEV Community: Kuldeep Yadav</title>
      <link>https://dev.to/novicedev7291</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/novicedev7291"/>
    <language>en</language>
    <item>
      <title>Python Extension Modules</title>
      <dc:creator>Kuldeep Yadav</dc:creator>
      <pubDate>Sun, 16 Jul 2023 05:12:29 +0000</pubDate>
      <link>https://dev.to/novicedev7291/python-extension-modules-1nhe</link>
      <guid>https://dev.to/novicedev7291/python-extension-modules-1nhe</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I am still exploring this space and might have&lt;br&gt;
interpreted few things wrongly here, hence I would advise readers to&lt;br&gt;
take it with a pinch of salt and explore themselves and don't use the&lt;br&gt;
examples in production :-)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently in my company, I was asked to analyse a python package which&lt;br&gt;
can be used to connect with kafka cluster as my team is moving away&lt;br&gt;
from tech stack which involves languages i.e. Java, C# to languages&lt;br&gt;
like python &amp;amp; node.I have some experience with python as once in my&lt;br&gt;
previous company, I used flask to write an API gateway (not&lt;br&gt;
completely, it was just a bad wrapper) and it was working quite well&lt;br&gt;
but then due to maintenance burden of a different tech (which I only&lt;br&gt;
knew at that time), we decided to ditch it completely.&lt;/p&gt;

&lt;p&gt;At first I thought, it should be easy as their must be some package on&lt;br&gt;
pypi, which can be used but there was a catch in my requirement.The&lt;br&gt;
cluster I had to connect was using &lt;code&gt;Kerberos&lt;/code&gt; authentication, which to&lt;br&gt;
be honest till date I have not understand completely due to its&lt;br&gt;
opaqueness in some regards and compairing it with other authN &amp;amp; authZ&lt;br&gt;
patterns i.e. OAuth2, token etc. Still, I thought there would be some&lt;br&gt;
packages available which can be leveraged, but to my surprise, I could&lt;br&gt;
only find 2 packages, &lt;code&gt;kafka-python&lt;/code&gt; &amp;amp; &lt;code&gt;confluent-kafka&lt;/code&gt;. It was fine,&lt;br&gt;
I just needed one to work, but here comes the challenge, both packages&lt;br&gt;
required SASL feature to be enabled which internally would use GSS&lt;br&gt;
API.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is little bit involved as GSS (Generic Security Services API)&lt;br&gt;
allows application to communicate securely using kerberos and requires&lt;br&gt;
native library libkrb5 to be present on client (and its dependencies).&lt;br&gt;
These native dependencies are available via most package managers i.e.&lt;br&gt;
brew, yum, apt etc to any linux or similar platform. In enterprises,&lt;br&gt;
where Microsoft ecosystem is ubiquitous, kerberos protocol is used&lt;br&gt;
heavily which provides SSO capabilities and employees only require&lt;br&gt;
their system credentials to access any service over internet. Now this&lt;br&gt;
post will become very long if describe the every piece of kerberos&lt;br&gt;
authentication and at the time of writing this I am not fully&lt;br&gt;
convinced that I undestand it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I first gave a try to &lt;code&gt;kafka-python&lt;/code&gt; as I found an example online&lt;br&gt;
demonstrating producer example using kerberos, but when I tried, it&lt;br&gt;
didn't work for me and error message were not clear enought what was&lt;br&gt;
happening, so I checked the package's github issues and I found&lt;br&gt;
similar issues posted and in one of the issue, the author himself&lt;br&gt;
confirmed that he doesn't have much clue about the SASL implementation&lt;br&gt;
as it was patched by community only and doesn't have idea around how&lt;br&gt;
to debug or test it.&lt;/p&gt;

&lt;p&gt;Finally, I decided to give up on this and turned towards&lt;br&gt;
&lt;code&gt;confluent-python-kafka&lt;/code&gt; package, which was also an official package&lt;br&gt;
from the &lt;code&gt;Confluent&lt;/code&gt; (company behind kafka now), this package had one&lt;br&gt;
big issue, it was not supporting SASL out of the box. On github, it's&lt;br&gt;
README file mentioned that this is a wrapper package on their existing&lt;br&gt;
C/C++ library &lt;code&gt;librdkafka&lt;/code&gt; and for SASL, simply installing it from&lt;br&gt;
pypi and using in our code, won't work. We would need to install using&lt;br&gt;
&lt;em&gt;source distribution&lt;/em&gt;, which then link the SASL feature.Normally, you&lt;br&gt;
would expect from any package manager to install the library and then&lt;br&gt;
you can start using it, but in above case we have to do some extra&lt;br&gt;
steps.&lt;/p&gt;
&lt;h4&gt;
  
  
  Packages
&lt;/h4&gt;

&lt;p&gt;Languages like java has a concept of bytecode, so every dependency you&lt;br&gt;
would include in your code would eventually have a jar file hosted on&lt;br&gt;
some repository, and JVM will take care of converting and executing it&lt;br&gt;
on any platform, hence abstracting away the complexity of creating&lt;br&gt;
platform dependent binaries. &lt;/p&gt;

&lt;p&gt;Javascript is also same in that regard as most modern js frameworks&lt;br&gt;
are using nodejs, which provides engine to run the js on any machine&lt;br&gt;
as well as abstracts away the complexity of running your js program on&lt;br&gt;
any platform. Similarly python also uses interpreter Cython to compile&lt;br&gt;
and execute the code on any platform. Since both node and Cython are&lt;br&gt;
written in C/C++, its natural to have the API available so that you&lt;br&gt;
can extend the capabilities of these languages.&lt;/p&gt;

&lt;p&gt;Python has two types of packages pure python or wrapper packages&lt;br&gt;
around native libraries written in c, c++, rust etc etc. As you might&lt;br&gt;
have got the idea that pure packages are written completely in python&lt;br&gt;
and you just install and use them. Wrapper packages requires more to&lt;br&gt;
setup and use because of inherited complexity of the platform on which&lt;br&gt;
it will run. These packages are mostly written in C/C++ and hence&lt;br&gt;
require platform specific tools to compile these.&lt;/p&gt;
&lt;h4&gt;
  
  
  My toy example
&lt;/h4&gt;

&lt;p&gt;Before we dive into how these packages are installed and setup, let's&lt;br&gt;
understand the python C/C++ extension API with a small example.&lt;/p&gt;

&lt;p&gt;Let's say you have your own module which offers a function sort, which&lt;br&gt;
will take a list and will return the sorted list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;wildsort&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sort&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I named the module &lt;code&gt;wildsort&lt;/code&gt; and it will expose the &lt;code&gt;sort&lt;/code&gt;&lt;br&gt;
function, which will do the sorting and return a new sorted list.&lt;/p&gt;

&lt;p&gt;Now this is very simple module with just a simple function but let's&lt;br&gt;
see how I develop this using C API.&lt;/p&gt;

&lt;p&gt;First, we need to create the sort function, so I created a&lt;br&gt;
&lt;code&gt;wildsort.c&lt;/code&gt; file and declare my function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define PY_SSIZE_T_CLEAN
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Python.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;wildsort_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PyObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PyObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For people who have not written a single line of C, this can be&lt;br&gt;
difficult to understand and I cannot help much here so I would suggest&lt;br&gt;
to first write a hello world in C to relate some of things written&lt;br&gt;
above.&lt;/p&gt;

&lt;p&gt;Now for those, who knows C, first two lines should be clear atleast&lt;br&gt;
from syntax point of view, since we will be using Python's C API, we&lt;br&gt;
need &lt;code&gt;Python.h&lt;/code&gt;, which define the contract for whole API. You can&lt;br&gt;
ignore the first macro for now. The function above just return NULL&lt;br&gt;
pointer as of now.&lt;/p&gt;

&lt;p&gt;After creating the function (this will be our sort function in python&lt;br&gt;
code), we need to create a defination of it to register with our&lt;br&gt;
module &lt;code&gt;wildsort&lt;/code&gt; later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;//...previous code above it will remain here&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;PyMethodDef&lt;/span&gt; &lt;span class="n"&gt;WildsortMethods&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"sort"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"wildsort_sort"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;METH_VARARGS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Sort a number of integers "&lt;/span&gt;
    &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"but not in performant way"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code we are using &lt;code&gt;PyMethodDef&lt;/code&gt; struct to create a method&lt;br&gt;
definiton in our module and here we are mapping the function name&lt;br&gt;
going to be in python &lt;code&gt;sort&lt;/code&gt; with &lt;code&gt;wildsort_sort&lt;/code&gt; declared above and&lt;br&gt;
defining the parameters type for the function &lt;code&gt;METH_VARARGS&lt;/code&gt;, meaning&lt;br&gt;
it will take single argument and no keyword arguments which python&lt;br&gt;
offers.&lt;/p&gt;

&lt;p&gt;Similarly, we need to create module definition in which we will map&lt;br&gt;
the method definition and then link it to the module creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;PyModuleDef&lt;/span&gt; &lt;span class="n"&gt;WildsortModuleDef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Py_ModuleDef_HEAD_INIT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"wildsort"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WildsortMethods&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read about the some of the params passed above in python C API&lt;br&gt;
docs. Now we will create the function, which will be called to create&lt;br&gt;
and initiaze our module when python interpreter starts executing our&lt;br&gt;
python code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;PyMODINT_FUNC&lt;/span&gt; &lt;span class="nf"&gt;PyInit_wildsort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;PyModule_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;WildsortModuleDef&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, one thing to note that the function name should always follow&lt;br&gt;
naming standard &lt;code&gt;PyInit_*&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; should match with your module name&lt;br&gt;
defined in module definition.&lt;/p&gt;

&lt;p&gt;Now, you can write the sort implementation in function &lt;code&gt;wildsort_sort&lt;/code&gt;&lt;br&gt;
method or can delegate it to any external shared library, which will&lt;br&gt;
involve some nitty gritties of converting python objects from and back&lt;br&gt;
to data structures in C.&lt;/p&gt;
&lt;h4&gt;
  
  
  Building the package
&lt;/h4&gt;

&lt;p&gt;Now, in main directory of your code, you need to add &lt;code&gt;setup.py&lt;/code&gt; with&lt;br&gt;
below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;distutils.core&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;

&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"pywildsort"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Wrapper package in C to be used as python package"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You must have some of the module installed in your python distribution&lt;br&gt;
to run above script i.e. setuptools etc. Most of these modules comes&lt;br&gt;
pre-installed with latest python distribution and I guess you won't&lt;br&gt;
have to install explicitly.&lt;/p&gt;

&lt;p&gt;Now, we need to run below steps to build a python package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python setup.py build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I would suggest you do these steps by creating a virtual env (check&lt;br&gt;
python docs) otherwise you may run into error using just &lt;code&gt;python&lt;/code&gt;&lt;br&gt;
because on some systems i.e. Mac OS it points to python 2.x version&lt;br&gt;
and command may fail. In that case you may have to explicitly use&lt;br&gt;
&lt;code&gt;python3.x&lt;/code&gt; variant of command and please check if its on path but I&lt;br&gt;
would recommend using virtual environment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will build the source code with available C compiler on your&lt;br&gt;
machine and will produce the object file(s) and other metadata&lt;br&gt;
information into &lt;code&gt;build&lt;/code&gt; directory in root directory of your source&lt;br&gt;
code.&lt;/p&gt;

&lt;p&gt;Now if you need to test the package, you can instead run the below,&lt;br&gt;
which will install the package into your python installation directory&lt;br&gt;
with your have run the setup.py file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python setup.py &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note if you are using virtual env, then it will install inside the&lt;br&gt;
virtual environment installation location.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can test the package with the code mentioned in the starting or&lt;br&gt;
with python REPL, keeping in mind that you installed with same python&lt;br&gt;
version or virtual environment.&lt;/p&gt;
&lt;h3&gt;
  
  
  Back to our confluent-kafka-python package
&lt;/h3&gt;

&lt;p&gt;Above was just to create the native wrapper package for python, in my&lt;br&gt;
case, I wanted to understand why I would need to install using  the&lt;br&gt;
source distribution for &lt;code&gt;confluent-kafka-python&lt;/code&gt; package and not just&lt;br&gt;
install and use it, irrespective of if it is written using C library&lt;br&gt;
or pure python.&lt;/p&gt;
&lt;h4&gt;
  
  
  Some python packaging history
&lt;/h4&gt;

&lt;p&gt;The answer lies in to understanding how python packages are build and&lt;br&gt;
uploaded to pypi and why there are multiple options to install the&lt;br&gt;
packages.&lt;/p&gt;

&lt;p&gt;In early days python offerred only one type of packaging format and&lt;br&gt;
that was source distribution, I guess this was easiest choice&lt;br&gt;
considering the complexity involving different platforms.&lt;/p&gt;

&lt;p&gt;In this distribution your package's source code will be deployed as&lt;br&gt;
zip file to pypi and basically when user will install it using &lt;code&gt;pip&lt;/code&gt;,&lt;br&gt;
it will be downloaded on user's machine and then bascially &lt;code&gt;python&lt;br&gt;
setup.py install&lt;/code&gt; will be executed like in our example. Package author&lt;br&gt;
would run the below command to create source distribution and upload&lt;br&gt;
it to pypi.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python setup.py sdist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since this process will be slow if there are projects which require&lt;br&gt;
many packages and then their dependent packages will all be compiled&lt;br&gt;
at user's machine, a binary distribution concept was introduced, in&lt;br&gt;
which the author will create platform dependent binary distribution&lt;br&gt;
for major platforms and deploy it on pypi. So when an user install, it&lt;br&gt;
will receive the precompiled files and &lt;code&gt;pip&lt;/code&gt; would just link it&lt;br&gt;
with current python interpreter.&lt;/p&gt;

&lt;p&gt;In python ecosystem, this binary distributon is called &lt;code&gt;wheel&lt;/code&gt; and&lt;br&gt;
this is the default mechanism for &lt;code&gt;pip&lt;/code&gt;. So pip checks for the binary&lt;br&gt;
distribution matching your machine OS and platform and if found it&lt;br&gt;
would just download it and unzip and just link the pre-compiled&lt;br&gt;
sources.&lt;/p&gt;

&lt;p&gt;But when it doesn't find any matching package to your machine&lt;br&gt;
requirement, it will fallback to source distribution and old ways of&lt;br&gt;
compiling and linking as per your machine arch and tools available. In&lt;br&gt;
case tools are not available on machine to compile and link the source&lt;br&gt;
then the package installation would fail and I guess this is another&lt;br&gt;
reason why binary distributon makes sense.&lt;/p&gt;

&lt;p&gt;You may check the naming convention of wheels available on pypi and&lt;br&gt;
python docs, you will understood why these are named that way.&lt;/p&gt;
&lt;h4&gt;
  
  
  Why confluent-kafka-python require source distribution for
&lt;/h4&gt;
&lt;h4&gt;
  
  
  kerberos?
&lt;/h4&gt;

&lt;p&gt;Now confluent kafka python package already had a binary distribution&lt;br&gt;
available for my machine but it was mentioned that to enable SASL&lt;br&gt;
feature, I would need to install package using below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--no-binary&lt;/span&gt; confluent_kafka confluent_kafka
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason is that the package authors decided to disable the SASL&lt;br&gt;
feature while creating binary distribution. I guess they might have&lt;br&gt;
thought that not everyone will be using kerberos to authenticate with&lt;br&gt;
kafka and they might have thought that generally the older languages&lt;br&gt;
like c/c++, java etc. only are used in that case to connect to kafka&lt;br&gt;
cluster and not python and it is required that the &lt;code&gt;librdkafka&lt;/code&gt; (their&lt;br&gt;
c/c++) lib and its dependencies should be present on the machine to&lt;br&gt;
utilise this feature. Hence the source distribution was necessity so&lt;br&gt;
that pip can link the &lt;code&gt;librdkafka&lt;/code&gt; dynamic library on OS while&lt;br&gt;
installing the package with SASL feature enabled and in your code you&lt;br&gt;
can easily use the kerberos authentication.&lt;/p&gt;

&lt;p&gt;If you want to learn more about how it all works, you may check the&lt;br&gt;
&lt;a href="https://github.com/confluentinc/confluent-kafka-python/"&gt;confluent-kafka-python&lt;/a&gt;&lt;br&gt;
and if you feel overwhelmed by the source code, then you can check &lt;a href="https://github.com/novicedev7291/pywildsort"&gt;my&lt;br&gt;
above example's source&lt;br&gt;
code&lt;/a&gt; in which I created&lt;br&gt;
a shared library &lt;code&gt;libwildsort&lt;/code&gt; and used it to complete the&lt;br&gt;
&lt;code&gt;wildsort_sort&lt;/code&gt; implementation in our example above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;It was exciting for me to touch base around how extension C API works&lt;br&gt;
and doing something in c/c++ after very long and I learn few things&lt;br&gt;
around python and its ecosystem. Also, I found that the most famous&lt;br&gt;
python libs are using C API and major chunk of the packages is written&lt;br&gt;
in c, c++, rust etc etc.&lt;/p&gt;

&lt;p&gt;I think there are some good use cases of doing this i.e. reusing&lt;br&gt;
component already written in other language stack and already famous,&lt;br&gt;
performance etc., but again it also makes the ecosystem complex for&lt;br&gt;
user who are not much aware of these things and generally work in&lt;br&gt;
python. Sometimes I have seen the error messages emitted by &lt;code&gt;pip&lt;/code&gt; are&lt;br&gt;
too cryptic to understand what has gone wrong if you don't know all&lt;br&gt;
this.&lt;/p&gt;

&lt;p&gt;I have mixed feeling about the whole ecosystem as of now and not sure&lt;br&gt;
if this is good thing but I guess this will be a trade off for easy&lt;br&gt;
    language syntax and ease of use for scripting....I don't know yet!&lt;/p&gt;

&lt;p&gt;You can also read this on my &lt;a href="https://coding-saga.com"&gt;blog&lt;/a&gt;, thanks!!&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/extending/extending.html#"&gt;Python extension API
docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/c-api/index.html"&gt;Python extension C API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>extensions</category>
      <category>package</category>
    </item>
    <item>
      <title>Chain Of Responsibility Design Pattern</title>
      <dc:creator>Kuldeep Yadav</dc:creator>
      <pubDate>Mon, 16 Aug 2021 12:41:51 +0000</pubDate>
      <link>https://dev.to/novicedev7291/chain-of-responsibility-design-pattern-35p1</link>
      <guid>https://dev.to/novicedev7291/chain-of-responsibility-design-pattern-35p1</guid>
      <description>&lt;p&gt;Chain of responsibility pattern is behavioural pattern from G.O.F book about design patterns, this pattern is very useful when you need to decouple the sender from its receivers and each receiver has some responsibility to fulfill.&lt;/p&gt;

&lt;p&gt;Let's understand this with an example, suppose you have a mail server, and you have already identified ways to identify spam, fan, business, complaining mails from normal mails, and you are asked to implement this logic into the mail server, so that it automatically filters these mails and perform some logic, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spam needs to be ignored.&lt;/li&gt;
&lt;li&gt;Fan mails needs to be forwarded directly to CEO of company.&lt;/li&gt;
&lt;li&gt;Business mails needs to be forwarded to business email in company.&lt;/li&gt;
&lt;li&gt;Complaint mails need to be directed to Consumer department.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we can simply put if and else-if checks in our logic and accomplish the requirement, but it will be problematic for&lt;br&gt;
few reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will be violating open-close principle, because whenever we need to add new rule we would change our function.&lt;/li&gt;
&lt;li&gt;We will also be breaking the single responsibility principle as the function would be doing a lot of things on its 
own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Better approach would be if we have different handlers for each responsibility i.e. filtering spam, fan and then some how combine them together, which will then handle the request object one by one. This is what COR helps us to do. Let's take a look at the implementation of this pattern to solve our problem at hand, given below is the UML diagram of the classes involved.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's refer the Chain of responsibility pattern with COR now on wards.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MZiQLg7y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hebrm3zotyfj2y8bbmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MZiQLg7y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hebrm3zotyfj2y8bbmv.png" alt="Class Diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;MailFilter&lt;/code&gt; is the functional interface with abstract &lt;code&gt;filter&lt;/code&gt; method, which will be then implemented by every concrete class i.e. &lt;code&gt;SpamFilter&lt;/code&gt;, &lt;code&gt;FanFilter&lt;/code&gt;, so each filter will provide its own implementation for filter logic.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MailServer&lt;/code&gt; class is the sender class which will send the mail object to handler assigned to it, in actual implementation it would not know if there are more than one handler and which handler it is interacting with.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Phr4dOh---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg7o3favubkzq37bx15m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Phr4dOh---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg7o3favubkzq37bx15m.png" alt="Sequence Diagram"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;For the implementation I have chosen the functional interface instead of abstract class for &lt;code&gt;MailHandler&lt;/code&gt; above since there is only one abstract method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;MailFilter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;MailFilter&lt;/span&gt; &lt;span class="nf"&gt;appendNext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MailFilter&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&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="n"&gt;mail&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mail&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Mail&lt;/span&gt; &lt;span class="n"&gt;mail&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;&lt;em&gt;Notice&lt;/em&gt; that &lt;code&gt;appendNext&lt;/code&gt; function is giving same interface(implementation) back to the caller here, also it is calling first the &lt;code&gt;filter&lt;/code&gt; method(which would be of concrete class) and then it calls the &lt;code&gt;next.filter(mail)&lt;/code&gt; method on next handler in chain.&lt;/p&gt;

&lt;p&gt;Now, lets look at one of the concrete implementation of this interface &lt;code&gt;SpamFilter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SpamFilter&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;MailFilter&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;allowedMails&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"abc@xyz.com"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"zyz@abc.com"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"kuldeep.yadav@xyz.com"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"johnoliver@xyz.com"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"vincent@rocketmail.com"&lt;/span&gt;
    &lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Mail&lt;/span&gt; &lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSpam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Ignoring spam mail"&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="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isSpam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Mail&lt;/span&gt; &lt;span class="n"&gt;mail&lt;/span&gt;&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;allowedMails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;from&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the &lt;code&gt;SpamFilter&lt;/code&gt; class implements the &lt;code&gt;MailFilter&lt;/code&gt; interface, so it provides the logic for &lt;code&gt;filter&lt;/code&gt; method which will then check if the &lt;code&gt;Mail&lt;/code&gt; object can be filtered and if true then it will process (In this case only printing on console).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The filtering logic in above example is just for demo purpose, please don't use it in production :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Similarly, there would be other filters in the chain which will process the &lt;code&gt;Mail&lt;/code&gt; object and work on it if the logic/condition passes. Let's see how it is being used by the sender/client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MailServer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;MailFilter&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MailServer&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;MailFilter&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&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="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SpamFilter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendNext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FanFilter&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;appendNext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BusinessFilter&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Mail&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mails&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;filter:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;filter&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here for the demo purpose I have chosen to define the chain in the same class but in real scenario, you may want to have&lt;br&gt;
some strategy or factory which will generate this chain and give you the &lt;code&gt;filter&lt;/code&gt; object to deal with in your code. Here, we need to check only the &lt;code&gt;process&lt;/code&gt; method which is receiving list of mails and process them one by one.&lt;/p&gt;

&lt;p&gt;You can see that this is much cleaner solution than if-else chain in your code and also decouples the sender from the &lt;br&gt;
handler/filter(s) in this case, for example if there was a separate class to initialise the chain, the &lt;code&gt;MailServer&lt;/code&gt;&lt;br&gt;
class would not even know how many filters the mail object would go through, hence we can directly add or remove filters&lt;br&gt;
from chain any time without touching the &lt;code&gt;MailServer&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Also, every filter logic resides in its own class, which follows the SRP(Single Responsibility principle) as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;p&gt;As with every thing in software world, this pattern is also not a magic bullet and comes with cons as well&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the chain is very long, it is hard to debug and verify where the problem lies.&lt;/li&gt;
&lt;li&gt;With very long chain of handlers, the call stack will grow as well, and it could lead to performance issues.&lt;/li&gt;
&lt;li&gt;It can lead to duplicate code among handlers, although duplication can be avoided.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Points To Consider
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use it when you don't need the guarantee that the request object must be executed by all the handlers, otherwise use
the decorator pattern.&lt;/li&gt;
&lt;li&gt;Use it when multiple handlers must be able to process a request object, and you don't want sender to have any idea
about multiple handlers.&lt;/li&gt;
&lt;li&gt;Don't use it when the client must know the handler which will execute the request.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;COR design pattern helps to decouple the sender from its receivers, the request sent by sender can be handled by&lt;br&gt;
multiple handlers or receivers without sender knowing it, which will then gives flexibility to remove or add the receivers without touching the code of sender.&lt;code&gt;javax.servlet.Filter&lt;/code&gt; API is nice example of this pattern implemented in java world, another good case of this pattern is logging, &lt;a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern"&gt;wikipedia&lt;/a&gt; mention this example with implementation. But COR pattern also have its downsides so as a developer we must be aware of these.&lt;/p&gt;

&lt;p&gt;You may read the same on my &lt;a href="https://coding-saga.com/2021/cor-design-pattern"&gt;personal blog&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern"&gt;Chain of Responsibility pattern wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.oreilly.com/library/view/head-first-design/0596007124/"&gt;Head First Design Pattern book&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/novicedev7291/design-patterns"&gt;Source code for above example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>design</category>
      <category>patterns</category>
      <category>java8</category>
    </item>
    <item>
      <title>Functional Programming in Java 8, Part 2</title>
      <dc:creator>Kuldeep Yadav</dc:creator>
      <pubDate>Sun, 14 Mar 2021 14:15:58 +0000</pubDate>
      <link>https://dev.to/novicedev7291/functional-programming-in-java-8-part-2-2hk7</link>
      <guid>https://dev.to/novicedev7291/functional-programming-in-java-8-part-2-2hk7</guid>
      <description>&lt;p&gt;This post is in continuation with my &lt;a href="https://dev.to/novicedev7291/functional-programming-in-java-8-with-example-s-32b4"&gt;last post&lt;/a&gt;, where we saw an example of builder pattern using Java 8 functional library, in this post I will share few more examples with you and discuss the pros and cons of functional programming.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's start with an example
&lt;/h3&gt;

&lt;p&gt;Suppose you have a list of persons with their name, age and salary, the person object would be like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;staticConstructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"of"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;salary&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;blockquote&gt;
&lt;p&gt;@value(staticConstructor = "of") is the lombok annotation to produce static factory method &lt;em&gt;of&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, you have a use case where you may want to find out all the persons who have either age more than 30 or have salary more than 100K but must not be both, how would you do that?&lt;/p&gt;

&lt;h3&gt;
  
  
  Simple traditional approach
&lt;/h3&gt;

&lt;p&gt;Simple solution could be the one which I have been doing since my college days (although I didn't know java then only C ;-) )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// solution list will have our persons with criteria&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;solution&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAge&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSalary&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;solution&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAge&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSalary&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;solution&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This approach is also called &lt;a href="https://en.wikipedia.org/wiki/Imperative_programming"&gt;imperative approach&lt;/a&gt;, where we are specifying &lt;em&gt;how&lt;/em&gt; our solution should arrive, you may hear this term a lot when reading about functional programming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Functional Programming approach
&lt;/h3&gt;

&lt;p&gt;Using Java 8 &lt;em&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html"&gt;predicate API&lt;/a&gt;&lt;/em&gt;  to filter with given criteria. &lt;/p&gt;

&lt;p&gt;As you can see in previous solution we have two &lt;code&gt;if&lt;/code&gt; condition, which in simple terms means that we have two mutually exclusive (set theory) conditions, hence we need to find &lt;code&gt;xOr&lt;/code&gt; of two conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Predicate API&lt;/em&gt;&lt;/strong&gt; does't not provide &lt;code&gt;xOr&lt;/code&gt; operation, though it has &lt;code&gt;or&lt;/code&gt; and &lt;code&gt;and&lt;/code&gt; default implementation. So we will extend the &lt;code&gt;Predicate&lt;/code&gt; API into our own functional interface &lt;code&gt;Specification&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;util&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;function&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Predicate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;xOR&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&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="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We can have default &lt;a href="https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html"&gt;function definition&lt;/a&gt; inside interfaces !!!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we can easily use this new interface to filter while using &lt;code&gt;stream&lt;/code&gt; over our persons list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ageGreaterThanThirty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAge&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;salaryMoreThanHundredThousand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSalary&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;xorAgeAndSalary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ageGreaterThanThirty&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;xOR&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salaryMoreThanHundredThousand&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;solution&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                                             &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xorAgeAndSalary&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                             &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Isn't it cleaner and readable than the traditional approach? we have no more those &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt; loop statements and also the criteria is now re-usable, if you need similar thing at some other place.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Why" Functional Programming
&lt;/h3&gt;

&lt;p&gt;This question often comes to my mind as well and as with everything in programming, there is no straight forward answer to this and due to our brain being used to of doing imperative programming for so many years, It's very difficult sometimes to see how functional programming helps.&lt;/p&gt;

&lt;p&gt;Here, I will share few pros of functional programming with you, which might help you to embrace this style of programming.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros of functional programming
&lt;/h3&gt;

&lt;p&gt;Functional programming revolves around the usage of pure functions and immutable objects, so many benefits of both applies to functional programming as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pure functions let you write deterministic functions.&lt;/li&gt;
&lt;li&gt;Pure functions are easier to test.&lt;/li&gt;
&lt;li&gt;Debugging is easy.&lt;/li&gt;
&lt;li&gt;Parallel and concurrent programming becomes easier.&lt;/li&gt;
&lt;li&gt;Function signature are meaningful and clear.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pure Functions
&lt;/h3&gt;

&lt;p&gt;Pure functions are those functions which takes some input and produces an output on the basis of encapsulated algorithm and input parameters, these do not perform any IO, Database Calls, UI interaction or any other side effects.&lt;/p&gt;

&lt;p&gt;Due to this definition of pure functions, they are deterministic in nature, that means if that function is called any number of times with same parameters, it would produce the same output every time.&lt;/p&gt;

&lt;p&gt;Let's look at some examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ele&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Comparator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cmp&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Some sorting algorithm omitted for brevity&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sortedList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findAllPersonUsing&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Specification&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;specification&lt;/span&gt;&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="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xorAgeAndSalary&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                              &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toList&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;Both above functions when passed with same arguments, will produce same output, no matter how many times these are called.&lt;/p&gt;

&lt;p&gt;What would be impure functions, then?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Function does not return anything, considered to have side effects&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;changePersonName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// doing some thing with person object here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Person output will depend on what personRepository will give back&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="nf"&gt;getPersonWithName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&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;return&lt;/span&gt; &lt;span class="n"&gt;personRepostitory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findPersonWithName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&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;// Function output will depend on the user input&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;readUserInput&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextLine&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;First function is interesting one, it does not return anything. In functional programming, these kind of functions are considered having side effects. &lt;/p&gt;

&lt;p&gt;Also as per comment, it is changing the state of person object, although this particular example is harmful in any programming style, one must not change parameter state inside function. &lt;/p&gt;

&lt;p&gt;This was little simple example to illustrate the point but when you are working with some entity framework i.e hibernate, you may find yourself doing this for some use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pure functions as expressions
&lt;/h3&gt;

&lt;p&gt;Pure functions are deterministic in nature, so they can be written as algebraic equation or composed together like an expression, for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;x&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;span class="n"&gt;any&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// f is pure function which returns integer&lt;/span&gt;
&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// g is also pure function which returns integer&lt;/span&gt;
&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because, &lt;code&gt;f(any)&lt;/code&gt; and &lt;code&gt;g(x)&lt;/code&gt; are deterministic functions, we can write above as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&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;span class="n"&gt;any&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;z&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;span class="n"&gt;any&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;z&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;span class="n"&gt;any&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;g&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;span class="n"&gt;any&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, &lt;code&gt;z&lt;/code&gt; can be replaced with the expression directly wherever it is used but if &lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; were not deterministic in nature it would not have been possible to replace in this manner.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing is easier
&lt;/h3&gt;

&lt;p&gt;With pure functions and immutable objects, testing is easier, because you don't have side effects, you won't be mocking objects and their behaviour to test your function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Debugging is easier
&lt;/h3&gt;

&lt;p&gt;Debugging is easy because you just need to follow the function values in stacktrace, you don't need to worry about other parts of applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parallel and Concurrent programming
&lt;/h3&gt;

&lt;p&gt;Since parallel and concurrent programming involves lot of threads, which might be executing your same function then if you have functions which are mutating states and you are not using immutable objects, then you would receive a panic call in the mid of your vacation, enjoying your time at beautiful beach, saying that everything is blown in production because of your function.&lt;/p&gt;

&lt;p&gt;Functional programming have pure functions and immutable objects, thats why you don't have to worrry about multithreaded environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function signatures are meaningful and clear
&lt;/h3&gt;

&lt;p&gt;Since, we write pure functions with functional programming, most of the time function signature tells you what that function might be doing or you wouldn't care what would be happening inside that function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cons of Functional Programming
&lt;/h3&gt;

&lt;p&gt;Functional programming is not a new concept though, it's roots are as old as the great depression (1930s), however, programming languages like C++, Java etc promoted OOPs and imperative programming since starting and atleast I leant programming from these 2 languages, I generally found functional programming concepts new and little hard to grasp in the begining.&lt;/p&gt;

&lt;p&gt;Following are the cons of functional programming&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's very hard to reason about initially for people, who have been writing programs using imperative approach for many years and often very intimidating.&lt;/li&gt;
&lt;li&gt;Due to its connotations with mathematical concepts i.e &lt;code&gt;monads&lt;/code&gt;, &lt;code&gt;functor&lt;/code&gt;, &lt;code&gt;combiner&lt;/code&gt; etc, it's very hard to comprehend sometimes.&lt;/li&gt;
&lt;li&gt;Real world software application involves IO, Database, UI, Reading/Writing To Files and since functional programming only uses pure functions, you cannot apply pure functional programming to the whole application, you would need to mix your approach.&lt;/li&gt;
&lt;li&gt;Also, fp uses immutable objects, therefore if written carelessly, it would be bad for performance  and may eat up lot of memory.&lt;/li&gt;
&lt;li&gt;With immutable objects, you would need to cover lot of update case, in that case you could use the pattern like "Update your copy" but again it would work for simple objects, with nested objects it becomes little hard to follow. Also, unlike Scala, its difficult to in Java, you would end up with lot of &lt;code&gt;copy&lt;/code&gt; methods inside your class.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I think, we can overcome some of these cons upto some extent&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Wrapping impure over pure
&lt;/h3&gt;

&lt;p&gt;We can limit the impact of impure part on pure part by wrapping the pure with a thin layer of wrapper around it to perform i.e IO, DB etc. This looks like we are doing the pure functions only. There are quite a few techiniques i.e &lt;code&gt;monads&lt;/code&gt; &lt;code&gt;HOF&lt;/code&gt; to do this in fp world, discussing them will be out of scope of this post.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning curve
&lt;/h3&gt;

&lt;p&gt;How much difficult it was to write in C++ or Java when you started the language and adopted the style, I am sure you must have written lot of programs to be confident about &lt;code&gt;loops&lt;/code&gt;, &lt;code&gt;statements&lt;/code&gt; &lt;code&gt;expressions&lt;/code&gt; etc . I think same applies to functional programming as well, the more you do, more you get comfortable with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mathematical Connotations
&lt;/h3&gt;

&lt;p&gt;I am telling you this from my personal experience, initially it intimidated me but as I kept doing it, I found none of those concepts are compulsory to get started with fp, yes it is good to learn those if you can understand but it doesn't mean you can't fp without learning those. (This is true for programming as general as well).&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory and Performance issues
&lt;/h3&gt;

&lt;p&gt;These issues are always debatable and I think these are mostly due to programmers' mistake not due to some programming style, unless you are processing a very huge number of data and you find that the immutable objects are problem, luckily I haven't encountered such case till now and I will appreciate, if someone can share their experiences about it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function programming decouple state and behaviours
&lt;/h3&gt;

&lt;p&gt;Functional programming prefers you decouple your data and behaviour that means you must have immutable classes with their behaviors(pure functions) in separate files or modules but this is not mandatory you can have it in same file as well but immutable classes/objects is important point. &lt;/p&gt;

&lt;p&gt;This is opposite to what OOPs suggest and might resembles &lt;a href="https://www.martinfowler.com/bliki/AnemicDomainModel.html"&gt;anemic model&lt;/a&gt; but it is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Functional programming let you write pure functions which will operate on immutable objects, which are easier to test, debug and maintain and are free from side effects. But you cannot write whole real world application which involves lot of moving part just using functional programming.&lt;/p&gt;

&lt;p&gt;It has some learning curve but helps to write clean APIs, I prefer to use it whenever I find the use case for it and mix with imperative style of programming to use best of both versions.&lt;/p&gt;

&lt;p&gt;So that's it, you can find the &lt;a href="https://github.com/novicedev7291/fp-patterns"&gt;source code&lt;/a&gt; used in both post on my github account.&lt;/p&gt;

&lt;p&gt;Now, you can also read the same post on my blog &lt;a href="https://coding-saga.com/2021/functional-programming-java-8-part-2"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.amazon.in/Functional-Programming-Simplified-Alvin-Alexander/dp/1979788782"&gt;Functional programming simplified, Scala Edition by Alvin Alaxender&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=K6BmGBzIqW0"&gt;From object oriented to functional domain modeling by Mario Fusco&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.martinfowler.com/bliki/AnemicDomainModel.html"&gt;Anemic model By Martin Fowler&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org"&gt;Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/8/docs"&gt;Oracle Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>functional</category>
      <category>java8</category>
    </item>
    <item>
      <title>Functional Programming in Java 8, with Example(s)</title>
      <dc:creator>Kuldeep Yadav</dc:creator>
      <pubDate>Thu, 18 Feb 2021 05:23:38 +0000</pubDate>
      <link>https://dev.to/novicedev7291/functional-programming-in-java-8-with-example-s-32b4</link>
      <guid>https://dev.to/novicedev7291/functional-programming-in-java-8-with-example-s-32b4</guid>
      <description>&lt;p&gt;It has been almost 7 years since Oracle released Java 8, with it, they also released lambda expressions and Functional interfaces, which lets you do functional programming in Java, but it's not very intuitive when compared to languages i.e javascript (functions are not the first-class citizen in java)&lt;/p&gt;

&lt;p&gt;In this post, I will give a brief introduction of functional interfaces and lambda expression in Java and try to explain the little strange syntax with one important example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional Interfaces
&lt;/h3&gt;

&lt;p&gt;Some of the important functional interfaces Java 8 has to offer, which we will discuss in a while.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function&lt;/li&gt;
&lt;li&gt;Consumer&lt;/li&gt;
&lt;li&gt;Supplier&lt;/li&gt;
&lt;li&gt;BiFunction&lt;/li&gt;
&lt;li&gt;Predicate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Function
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Represents a function which accepts one argument and produces a result.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is a very simple and understandable definition, right? But when you go and try to use it you would get confused at first, because as per definition, this represents something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&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="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Using it would be&lt;/span&gt;
&lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in your code, you would write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;convertIntToStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Some String from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;convertIntToStr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Got confused, right? &lt;/p&gt;

&lt;p&gt;This is because interfaces and classes are the first-class citizens in Java, that's why they have provided these &lt;strong&gt;&lt;em&gt;Functional Interfaces&lt;/em&gt;&lt;/strong&gt; and their usage is similar to creating an object and calling functions on that object.&lt;/p&gt;

&lt;p&gt;So, what are functional interfaces?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functional interface is the interface in Java 8 which has only one abstract method, any interface which qualifies this condition is a functional interface and you can also denote it with &lt;code&gt;@FunctionalInterface&lt;/code&gt; annotation to explicitly state that, so that compiler can generate an error if some try to add another abstract method to it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you checkout the implementation of &lt;strong&gt;Function,&lt;/strong&gt; you would see something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;
&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//only abstract method&lt;/span&gt;
    &lt;span class="c1"&gt;// default method omitted for brevity&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, our code example above initialises the object of this interface with &lt;em&gt;lambda expression which is in this case just a function&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;convertIntToStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Some String from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Some String from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; 

       &lt;span class="n"&gt;equivalent&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&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="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Some String from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&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;And since &lt;code&gt;convertIntToStr&lt;/code&gt; is an object of type Function, we call the apply method on this object to execute our &lt;code&gt;lambda expression&lt;/code&gt; or &lt;code&gt;someFunction&lt;/code&gt; and you must have got an idea by now that you provided the implementation for &lt;code&gt;apply&lt;/code&gt; function using a lambda expression, because the same statement can be written as &lt;strong&gt;anonymous object declaration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;convertIntToStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;@Override&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&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;return&lt;/span&gt; &lt;span class="s"&gt;"Some String from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, next time think the statement on left to be equivalent to the right&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&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="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;someFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All other, interfaces work in a similar manner, those represent some other useful functions, Let's see what they have to offer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consumer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Represents an operation that accepts a single input argument and returns no result.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can think of &lt;em&gt;Consumer&lt;/em&gt; as void function, which takes a parameter and it has an abstract method &lt;code&gt;void accept(T t)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Supplier
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Represent a supplier of results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can think of &lt;em&gt;Supplier&lt;/em&gt; as a function which doesn't take any argument and return a value and it has an abstract method &lt;code&gt;T get()&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  BiFunction
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Represents a function that accepts two arguments and produces a result.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can think of &lt;em&gt;BiFunction&lt;/em&gt; as a function which takes two arguments and return a value and has an abstract method &lt;code&gt;R apply(T t, U u)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Predicate
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Represents a predicate (boolean-valued function) of one argument.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can think of &lt;em&gt;Predicate&lt;/em&gt; as a function which takes an argument and returns a boolean value, it has an abstract method &lt;code&gt;boolean test(T t)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are more interfaces than mentioned, you can check &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html"&gt;docs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Why Functional Programming?
&lt;/h3&gt;

&lt;p&gt;Functional programming lets you compose your logic through pure functions in a declarative manner avoiding shared state, mutable data and side effects. Let's see this with a very simple example.&lt;/p&gt;

&lt;p&gt;Suppose we have an item and we need to calculate different taxes on the item's base price, we would write something like this&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I would be using lombok project to generate some boilerplate code in my examples, so please check its documentation for more info.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Value&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TaxCalculator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// I have passed the boolean flags for the taxes needs to be calculated&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="nf"&gt;calculateTaxablePrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;excise&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;vat&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;cess&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;gst&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;excise&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.02&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vat&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.04&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cess&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gst&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;basePrice&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="c1"&gt;// And we would call like this&lt;/span&gt;
&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;newPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;calculateTaxablePrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might have spotted the problems in the above code, let me point out a few below&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we need to add another tax tomorrow, we would change this method and so have to be the every calling method in client code.&lt;/li&gt;
&lt;li&gt;Client will have to dig into the implementation of a function to know, which flag is for which tax, so the client can make a mistake and this may not be captured by the unit test also.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, what can we do? An alternative approach can be to create a &lt;code&gt;TaxCalculatorBuilder&lt;/code&gt; which will create the &lt;code&gt;TaxCalculator&lt;/code&gt; object for us, which will have all the flags as state variables and &lt;code&gt;calculate&lt;/code&gt; function will take only &lt;code&gt;price&lt;/code&gt;, providing fluent API to the client&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;TaxCalculator&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TaxCalculatorBuilder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withExcise&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withVat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withCess&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withGst&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// And we would call like this, here calculator object knows which tax to be applied&lt;/span&gt;
&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;newPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;calculateTaxablePrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, there is an issue with the builder as well, you would have to change the builder class every time a new tax is added. So, how can we solve this problem?&lt;/p&gt;

&lt;p&gt;We can solve this issue using functional interface &lt;code&gt;Function&amp;lt;T, R&amp;gt;&lt;/code&gt; and we will see in a moment how?, but before that let's define each tax as a function which would take one argument and give back the result, in new class &lt;code&gt;TaxRules&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@NoArgsConstructor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;access&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AccessLevel&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PRIVATE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TaxRules&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="nf"&gt;excise&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&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="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.02&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="nf"&gt;vat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&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="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.04&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="nf"&gt;cess&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&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="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="nf"&gt;gst&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&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="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we would write our improved tax calculator as following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImprovedTaxCalculator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;taxRules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ImprovedTaxCalculator&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;taxRules&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="nf"&gt;calculateTaxablePrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;taxRules&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                      &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reduce&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstFn&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondFn&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;firstFn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondFn&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;price&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;em&gt;calculate&lt;/em&gt;&lt;/strong&gt; function here is little difficult to understand so let's apply divide and conquer to understand it&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;taxRules&lt;/code&gt; is a list, &lt;code&gt;.stream()&lt;/code&gt; method call gives us a stream of the object in the list.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reduce()&lt;/code&gt; method takes a stream of elements and produce a single result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take an example to understand it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&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;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reduce&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;em&gt;Integer value 0 is the identity value,&lt;/em&gt;&lt;/strong&gt; Its the initial value provided to the reduction process and if the value of the list is empty then it will be the default value which will be returned.&lt;/p&gt;

&lt;p&gt;The second argument is the &lt;strong&gt;&lt;em&gt;accumulator&lt;/em&gt;&lt;/strong&gt; which will add the previous sum to next number from the stream, initially &lt;strong&gt;&lt;em&gt;sum&lt;/em&gt;&lt;/strong&gt; would be 0 here&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="c1"&gt;// Lambda expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lambda expression can also be written as a method reference&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Integer&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                                            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's get back to our original example now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;compositeFn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;taxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;firstFn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;andThen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondFn&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;em&gt;first function in taxRules would be our identity&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;accumulator&lt;/em&gt;&lt;/strong&gt; is a lambda expression returning a new composed &lt;strong&gt;&lt;em&gt;Function,&lt;/em&gt;&lt;/strong&gt; that first applies its input to &lt;strong&gt;&lt;em&gt;firstFn&lt;/em&gt;&lt;/strong&gt; and then applies the result to &lt;strong&gt;&lt;em&gt;secondFn&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, Let's see how client would calculate tax using our improved calculator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ImprovedTaxCalculator&lt;/span&gt; &lt;span class="nx"&gt;calculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ImprovedTaxCalculator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TaxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;excise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TaxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TaxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;Double&lt;/span&gt; &lt;span class="nx"&gt;newPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calculateTaxablePrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If tomorrow, we got a new tax, then we just have to add the new function into &lt;code&gt;TaxRules&lt;/code&gt; class and the client can use it, we don't have to change our calculator.&lt;/p&gt;

&lt;p&gt;Also, if you have a case where only one client has to apply some extra tax which doesn't have to be defined in &lt;code&gt;TaxRules&lt;/code&gt;, then the client can very easily do that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ImprovedTaxCalculator&lt;/span&gt; &lt;span class="nx"&gt;calculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                       &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ImprovedTaxCalculator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TaxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;excise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TaxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TaxRules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;                                                                         
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.03&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Extra&lt;/span&gt;

&lt;span class="nx"&gt;Double&lt;/span&gt; &lt;span class="nx"&gt;newPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calculateTaxablePrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that how functional interface &lt;strong&gt;Function&lt;/strong&gt; can help us write better reusable code with clean fluent API, which is much better than the builder, we wrote earlier.&lt;br&gt;
For now, I will leave you all with this example, we will see some more examples and functional programming pros and cons in the coming posts. So till then &lt;strong&gt;&lt;em&gt;enjoy.....&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>functional</category>
      <category>programming</category>
      <category>java8</category>
    </item>
  </channel>
</rss>
