tag:blogger.com,1999:blog-47827729986072822322020-02-28T03:19:03.604-08:00Innoviaz Corporate TrainingInnoviazhttp://www.blogger.com/profile/17870268715166344272noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4782772998607282232.post-65273706069114783752020-01-28T11:36:00.003-08:002020-02-09T09:34:14.908-08:00Nomad? No, Monad ! (And it has a home)<div dir="ltr" style="text-align: left;" trbidi="on">Without any prejudice, let me start talking about Monads right away. It's one of the least understood and most feared term among programmers (Unless one has spent considerable time in doing functional programming)<br /><div><br /></div><div><b>What's a Monad anyway?</b></div><div><b><br /></b></div><div>If I were to define it in one sentence, I'd borrow it :)</div><div><br /></div><div>"A monad is a monoid in the category of endofunctors"</div><div><br /></div><div>This is mathematically correct, and makes no sense to a non-starter. So I'll start with simple explanation.<br /><br />The term Monad comes from the "Category theory" (<a href="https://en.wikipedia.org/wiki/Category_theory">Wikipedia</a>)<br /><br />It's one of the most feared as a "complicated concept", but in reality, it's not. Let us start with the three rules which defines any programming construct to be a Monad and I'll explain them in terms of Java (As I come from a Java background and my target audience is people coming from OO background)<br /><br />1. A type constructor to build up a Monadic type (i.e., A way to construct a Monad, which must be of generic type)<br />2. Provides a way of wrapping any non-monadic type to a Monadic type<br />3. Provides a bind (or return) function, which can work on the embedded value and return the same monad with a transformed embedded value.<br /><br />Well, what do they mean?<br /><br />Let's take the example of Optional in Java. (Although I personally disagree it's a pure monadic type)<br />1. As per the first rule, it must be generic, so is Optional (i.e., <i>Optional<Integer></i>)<br />2. As per the second rule, it must provide a way of wrapping some non-monadic value into the monadic type (i.e., Optional.of(3))<br />3. As per the third rule, it must provide a bind function, which can work on the embedded value, transform it and then wrap it in the same monad. That function is map (flatMap to be more accurate).<br /><br />Example:<br /><i>Optional.of(3).map(value -> value.toString() + " is transformed")</i><br /><br />This example converts an Optional<Integer> into an Optional<String>. The map method works on the embedded value, than on the Optional itself.<br /><br /><b>How are they useful?</b><br />For one from OO background, the closest similarity of a Monad is to a design pattern. Like design patterns, it solves a commonly recurring issue - reduce boilerplate code.<br /><br />However, the difference from design patterns is that instead of solving one problem, like most of the design patterns do, it is more generic in nature. It can solve a lot of problems where there is boiler plate code. A few examples where Monads have been useful:<br /><br />1. Operations involving nullable values (where a function can return null or a valid value)<br />2. Operations that work on each element of a list<br />3. Operations involving error values (where a function can return an error or a result)<br />4. Operations that deal with a promise (i.e., async operations)<br /><br />As you can see, the operations we defined above are completely un-related to each other, and yet, all can be solved using the same theory, i.e., Monads.<br /><br />Let's look at each of them. As before, I will use examples of Java wherever possible. Unfortunately, Java doesn't have Monads to cater for 3 and 4 above.<br />Also, Java's list isn't a Monad. However, you can get the same monadic behavior by converting the list into a Stream.<br /><br />I will use a Java library called <a href="https://www.vavr.io/" target="_blank">vavr</a> to explain the operations where Java falls short.<br /><br /><b>1. Operations involving nullable values</b><br />How many times have we written code similar to the below to deal with nulls in Java?<br /><br />Consider a POJO named Person. <br /><i>class Person {</i><br /><i>private Name name;</i><i>...</i><br /><i>//Getters and Setters</i><br /><i>..</i><br /><i>..</i><br /><i>}</i><br /><br />And a POJO Name:<br /><i> </i><br /><i>class Name {</i><br /><i> private String fullName;</i><br /><i><br /></i><i> //Getters and Setters</i><br /><i>}</i><br /><br />Now, we pass an instance of the Person class to do some operations on it and return a result (And default to some value if any of the null checks fails).<br /><br /><i>public String getFullName(Person person) {</i><br /><i> if(person != null) {</i><br /><i> Name name = person.getName();</i><br /><i> if(name != null){</i><br /><i> return name.getFullName();</i><br /><i> }</i><br /><i> } </i><br /><i> return "";</i><br /><i>} </i><br /><br />Consider we are dealing with five levels of of class instances instead of two and the code, as you would imagine becomes more and more messy.<br /><br />The solution? Enter Optional in Java. Let's re-write the method using Optionals.<br /><br /><i>public String getFullName(Person person) {</i><br /><i> return Optional.ofNullable(person).map(Person::getName).map(Name::getFullName).orElse(""); </i><br /><i>}</i><br /><br />Neat, eh?<br /><i> </i><br />So what's going on here? The null check is built into the Optional Monad.<br /><br />Stay tuned for more:<br />(To be continued ... )<br /><br /></div></div>Innoviazhttp://www.blogger.com/profile/17870268715166344272noreply@blogger.com0tag:blogger.com,1999:blog-4782772998607282232.post-70701651247747861882020-01-27T11:08:00.001-08:002020-01-27T23:14:40.991-08:00What's Cooking?<div dir="ltr" style="text-align: left;" trbidi="on">One of my friends once told me, "If only we knew beforehand what programming language is going to rule the world next!", and that got me thinking. While everybody wants to be ahead of the curve, is it really possible to predict the next-gen programming language?<br /><br />To me, the answer is, kind of. Although one can't be sure of that one and only language, but we can surely do some research on the trends and come up with a handful of languages that are rapidly going up the curve.<br /><br />So I did, and here is my pick of the top five upcoming programming languages, that I think are going to shape the software industry next.<br /><br />My list is (In the order of my preference of course ðŸ˜‰ )<br /><br />1. Julia<br />2. Rust<br />3. Kotlin<br />4. Go Language<br />5. Elixir<br /><br />I'm just getting warmed up, Keep watching this space for a detailed description of each of these languages!</div>Innoviazhttp://www.blogger.com/profile/17870268715166344272noreply@blogger.com0