<?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: Muthukumaran</title>
    <description>The latest articles on DEV Community by Muthukumaran (@muthuishere).</description>
    <link>https://dev.to/muthuishere</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%2F440720%2Fc799c445-f0ed-4a20-b941-621a86b2a484.jpeg</url>
      <title>DEV Community: Muthukumaran</title>
      <link>https://dev.to/muthuishere</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muthuishere"/>
    <language>en</language>
    <item>
      <title>Declarative Optional.Js</title>
      <dc:creator>Muthukumaran</dc:creator>
      <pubDate>Mon, 03 Jan 2022 07:30:12 +0000</pubDate>
      <link>https://dev.to/muthuishere/declarative-optionaljs-eek</link>
      <guid>https://dev.to/muthuishere/declarative-optionaljs-eek</guid>
      <description>&lt;p&gt;I have created a small Functional programming library in javascript , which supports developing code by chaining asynchronous and synchronous operations.&lt;/p&gt;

&lt;p&gt;Consider this example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getFromUserService({username, password}) {
    return new Promise((function (resolve) {
        resolve({name: "user", isAdmin: true})
    }))
}
async function login(input) {

 if (null == input) {
        throw new Error("Input Cannot be Null")
    }

const {username, password} = input
    if (null == username || null == password) {
        throw new Error("Input Cannot be Null")
    }
    const result = await getFromUserService(username, password)
    if (result.isAdmin) {
        redirectTo("adminPage")
    } else {
        redirectTo("userPage")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same can be rewritten as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const page = await Optional.of(input)
       .filter(({username, password}) =&amp;gt; (null != username &amp;amp;&amp;amp; null != password))
        .map(getFromUserService)
        .map(result =&amp;gt; result.isAdmin ? "adminPage" : "userPage")
        .toAsync();
    page.ifPresentOrElse(redirectTo, () =&amp;gt; {
        throw new Error("Input Cannot be Null")
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using with Fetch&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rawResults = await fetch('https://jsonplaceholder.typicode.com/todos/' + item);
const response = await rawResults.json();
if (response.completed) {
    return response.title
} else {
    return null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above can be rewritten as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return await Optional.of('https://jsonplaceholder.typicode.com/todos/' + item)
    .map(fetch)
    .map(response =&amp;gt; response.json())
    .filter(response =&amp;gt; response.completed == true)
    .map(response =&amp;gt; response.title)
    .getAsync();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Chaining of expressions enables the code concise and help to reasonate better, can easily be composed , combined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation &amp;amp; Usage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npm install declarative-optional   

//ES6
 import Optional from "declarative-optional";

//Common Js
  const Optional = require( "declarative-optional");


//Increment a Number , which may be null
 Optional.of(input)
    .map(val=&amp;gt;val+1)
    .get()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Links&lt;br&gt;
 &lt;a href="https://github.com/muthuishere/declarative-optional"&gt;npm&lt;/a&gt;&lt;br&gt;
 &lt;a href="https://github.com/muthuishere/declarative-optional"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>functional</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>DeclarativeX — A Composable approach to exception &amp; conditions in Java</title>
      <dc:creator>Muthukumaran</dc:creator>
      <pubDate>Fri, 03 Sep 2021 17:24:52 +0000</pubDate>
      <link>https://dev.to/muthuishere/declarativex-a-composable-approach-to-exception-conditions-in-java-1lf</link>
      <guid>https://dev.to/muthuishere/declarativex-a-composable-approach-to-exception-conditions-in-java-1lf</guid>
      <description>&lt;p&gt;The idea of utilizing functional programming is to enable declarative programming along with supporting concurrency &amp;amp; state issues.&lt;/p&gt;

&lt;p&gt;Most of the items are already better with Java 8 in terms of declarative programming, with the help of Optional Monad, Streams API.&lt;/p&gt;

&lt;p&gt;I could sense a need, especially in exception handling and conditions. We might need a composable api to effectively handle exceptions and multi conditions.&lt;/p&gt;

&lt;p&gt;The Declarativex library attempts to solve the issue through some functional and fluent interface way.&lt;/p&gt;

&lt;p&gt;Chaining Exceptions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsu7tsoh14kool8omp831.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsu7tsoh14kool8omp831.png" alt="Chaining Exceptions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If all your functions has the same parameter within chaining, You can use this alternative&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovu82lz3anxm0ldly525.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovu82lz3anxm0ldly525.png" alt="Chaining Exceptions same parameters"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Catching Exceptions based on Type and Performing appropriate actions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftk2z557ondky3z70blr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftk2z557ondky3z70blr3.png" alt="Catching Exceptions based on Type and Performing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Logging / peeking&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjuaatjnp9utlkbtysu2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjuaatjnp9utlkbtysu2.png" alt="Logging"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Composing Conditions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqt7kim5exflilzwj4lwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqt7kim5exflilzwj4lwg.png" alt="Composing Conditions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Usage&lt;br&gt;
Add Dependency&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;io.github.muthuishere&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;declarativex&amp;lt;/artifactId&amp;gt;
&amp;lt;/dependency&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import declarativex.Try;
import declarativex.Filter;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/muthuishere/declarativex" rel="noopener noreferrer"&gt;Github URL&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>functional</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Reactive Programming Basics - adding of two numbers and beyond </title>
      <dc:creator>Muthukumaran</dc:creator>
      <pubDate>Sat, 17 Oct 2020 11:30:29 +0000</pubDate>
      <link>https://dev.to/muthuishere/reactive-programming-with-simple-addition-2oe2</link>
      <guid>https://dev.to/muthuishere/reactive-programming-with-simple-addition-2oe2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt; : &lt;em&gt;I am not going to explain observables, Observer,Subscriptions,Subscriber,preventing memory leaks… Just simple reactive programming.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a=74
b=18
c= a +b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of c =&amp;gt; &lt;strong&gt;92&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If our program is completed here , we don’t have to go into reactive programming.&lt;/p&gt;

&lt;p&gt;Now consider&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a=a + 1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Stop..&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It appears lame to use variables like a,b,c  usage and incrementing one of it.&lt;/p&gt;

&lt;p&gt;We will consider practical situation&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Donald Trump born on 14 June 1946 &amp;amp; Cheguara on same day (14 June) of 1928 , the age difference between these two is 18 years&lt;br&gt;
Now we can write these as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let ageOfTrump = 74
let ageOfCheGuara = ageOfTrump + 18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so ageOfCheGuara would be 92&lt;br&gt;
And the long kept secret about Age is , it always kept incrementing on birthday 😊&lt;/p&gt;

&lt;p&gt;So if there is a method happyBirthdayTrump which increase age of trump&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function happyBirthDayToTrump(){
    ageOfTrump=ageOfTrump + 1   
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What should be the value of ageOfCheGuara after this method has been called?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Still  92, 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The contract (ageOfCheGuara = ageOfTrump + 18) denotes &lt;br&gt;
 ageOfCheGuara should always be sum of ageOfTrump &amp;amp; 18 , But unfortunately it was not always.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to fix it&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So to fix it , Whenever Trump got a birthday and his age gets incremented&lt;/p&gt;

&lt;p&gt;We should also be updating ageOfCheGuara ,something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function happyBirthDayToTrump(){
    ageOfTrump=ageOfTrump + 1
    ageOfCheGuara =  ageOfTrump + 18

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now things are fine, Because i keep on insisting that relationship again.&lt;/p&gt;

&lt;p&gt;Is there a way , I could specify only once the relationship and use it across the application&lt;/p&gt;

&lt;p&gt;if I could update ageOfTrump, ageOfCheGuara should automatically be updated.&lt;/p&gt;

&lt;p&gt;This is a common problem, if you are from C or C++ background you could have used pointer to look into address than its value, which will help us to solve these kindda issues. &lt;/p&gt;

&lt;p&gt;But This is Javascript, we don’t do that. How can we do ?&lt;/p&gt;

&lt;p&gt;By making every relationship as sacred and final and no unwanted updates for sake of programming, you have already been making your system reactive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Making Reactive with RxJS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are many ways to achieve it , We use Rx Libraries to achieve it here&lt;/p&gt;

&lt;p&gt;So the &lt;br&gt;
ageOfTrump = 74&lt;br&gt;
ageOfCheGuara = ageOfTrump + 18 &lt;/p&gt;

&lt;p&gt;instead of using as variables we will have something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ageOfTrump = new BehaviorSubject(74)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BehaviourSubject helps us to set a default value 74&lt;br&gt;
    We can retrieve its current value by using getValue method&lt;/p&gt;

&lt;p&gt;Don’t worry about these syntax, There are more than 100’s of operators. Just focus on the concept.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ageOfCheGuara = ageOfTrump.pipe(
                        map(val=&amp;gt;val + 18)
                        )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by using pipe operator with ageOfTrump , we are specifying ageOfCheGuara depends on it.map operator accepts a function , which will receive the value from trump and transform the value&lt;/p&gt;

&lt;p&gt;The happyBirthDayToTrump method will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function happyBirthDayToTrump(){
    const incrementedAge =ageOfTrump.getValue() + 1
    ageOfTrump.next(incrementedAge)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The currentValue to be retrieved with getValue method&lt;/p&gt;

&lt;p&gt;The value should be set with next method&lt;/p&gt;

&lt;p&gt;Now we have made these variables reactive. &lt;/p&gt;

&lt;p&gt;The entire source code is available on &lt;a href="https://github.com/muthuishere/reactive-basics-addition"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same has been demonstrated in video and available on&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/EgWwEakQWjk"&gt;Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rxjs</category>
      <category>reactiveprogramming</category>
    </item>
  </channel>
</rss>
