Posts

Functional Programming

Image
Functional programming is a software development paradigm out of various available software development methodology. It is based on function which is nothing but a computation unit and it's result depends on the argument and variable defined in the scope. In computer program, a functional programming treats every computation as an evaluation of mathematical functions and never use mutable data to change it's state. The output value of a pure function depends on the arguments which are input to the function and will produce the same result every time for the same set of inputs.  It means f(x) will always produce the same result y for same value of x. A function is full of functions or declarations instead of statements. Languages: Scala, Python, Erlang, Haskell etc. Various concepts of functional programming : Higher Order Functions First Class Function  Pure Function Immutability     

Null check of Object and Object Chain in Java

Image
JDK 7 and earlier: Avoid using null check in if statement. Use ternary operator for null/exist check and assign a default value before using. This will make the code clear and more understandable as comparable to using multiple if, else   statements. JDK 8 and above : Java-8 provides improved API to deal with object null check. Use isPresent() function available in Java.util.Optional interface for null check of single object. Ex – Employee employee = new Employee(); employee .setName( "Pramoda" ); // Getting optional value Optional<Employee> optionalEmployee = Optional. ofNullable ( employee ) .map(Root::getEmployee); System. out .println( "Is Employee present : + optionalemployee .isPresent()); Use Java.util.Optional functional interface for object null check and assign default value to avoid null pointer exception. Ex – Employee employee = new Employee(); employee .setNa

Scala Features

Image
Scala features The Scala features enables to write efficient programs using both object and functional way but the real power of Scala lies in functional. The functions applies a lots of magic to achieve programming goal. These are following features of Scala:  Object Class Constructor Data Type Access specifiers Operators Operator Overloading Abstract class and Sealed Abstract class Trait and Sealed Trait Inheritance Case class Companion Object Variable declaration Lambda Expression ( X => Y ) Function,  Tail Recursion Scala Function with Value Scala Function with Name Scala Function with Named Arguments Scala function with Variable Arguments Scala function with Default Parameter Scala function with Recursion Higher-order functions Nested Functions Anonymous Functions Scala Partially Applied Functions Currying Functions A bstract function  Closure Option, Some  and  None Either, Left  and  Right Try, Success  and  Failure type, yi

Scala

Image
Scala is a multi paradigm programming language used for building scalable application. Applications build on Scala are - Secure, Faster,  Reliable, Resistance to Run time exception About Scala Scala is scalable, functional, immutable and most importantly uses less piece of code to achieve a task. The Scala compiler generates byte code which is identical to that generated by the Java compiler. Scala code can be de-compiled to Java code (With the exception of certain constructor operations). To the JVM, both java code and Scala code looks similar with the only difference is a run time scala-library.jar library. Getting Started with Scala Installation And Setup Download the Java Run time Environment 1.6 or later (1.8 recommended) from - http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html Add the  \bin  directory to the  PATH  system environment variable. Download Scala IDE for Eclipse ( Download Scala IDE ) Create SB

Scala Functions

Function : A function is computational unit in a program. Function : To define a function specify - name, parameter, body and return type.   Functions are implicitly declared as abstract if leave off the equal sign and method body. Remove = sign if not to return any thing. A function can have named and optional parameters. Use Unit if the function does not return anything. ( Unit is equivalent to Void in java) Function definition :   def functionName ([list of parameters]) : [ return type ]             Abstract Function :   Ex - def calculation (a:Int, b:String)         Non Abstract Function :    def functionName ([list of parameters]) : [ return type ] = { }              Example -       def calculation ( a : Int, b : Int): Int = {     var sum : Int = 0     sum = a + b     return sum    } Function Recursion: Head  Recursion Tail  Recursion Head  Recursion : It stores result values in stack which may cause stack