Wednesday 12 April 2017

Doing Lazy Things in Java

Java is impatient, So it does things eagerly, Doing lazy things with Java is not straightforward!

Let's first understand What do we mean by Lazy invocation: The block of code (Function) is executed only when it needed. Some Programming languages built on this feature, i.e. Haskel support lazy evaluation.

Let's consider below example in Java:






package in.blogspot.imprashantsable;
import java.util.function.Supplier;
/** * Created by prashant on 4/12/17. */
public class LazyInJava {

    private static class BloodDoner{

        public BloodDoner(Integer age, String bloodGroup) {
            this.age = age;            
            this.bloodGroup = bloodGroup;        
       }

        private Integer age;        
        private String bloodGroup;
        
        public Integer getAge() {
              return age;        
        }

        public void setAge(Integer age) {
            this.age = age;        
        }

        public String getBloodGroup() {
            return bloodGroup;        
        }

        public void setBloodGroup(String bloodGroup) {
            this.bloodGroup = bloodGroup;        
        }

        private boolean isBPositive(String bloodGroup){
            System.out.println("Checking blood group");            
            return bloodGroup.equals("B+");        
        }

        private boolean canDonateBlood(){
            // For now Assume one can donate the blood if donar age > 16 and blood group = B+            
             boolean  isBPositive = isBPositive(bloodGroup);            
             return  (age > 16 && isBPositive);        
        }

    }


    public static void main(String[] args) {
        BloodDoner bloodDoner = new BloodDoner(14,"B+");        
        System.out.println(bloodDoner.canDonateBlood(bloodDoner));    
    }


}
Output is:
Checking blood group
false

In above example, Java invokes the isBPositive function eagerly. If you look condition closely, We could avoid checking blood group as age is Invalid. 

Let's make changes in Code to make it Lazy!

Change canDonateBlood function as Below:

private boolean canDonateBlood(BloodDoner bloodDoner){
    Supplier<Boolean>  isBPositive = () -> isBPositive(bloodGroup);    return  (bloodDoner.getAge() > 16 && isBPositive.get());
}


As you can see, we have used to Lambda to check blood group. Now lambda will execute only when it needed.

You run above program with the above changes. The output is:

false


As it does not invoke isBPostive() method. 

Doing Lazy evaluation is simple in Scala, you Just need to declare it as lazy.

The syntax is as below:
lazy val  isBPositive =  isBPositive(bloodGroup);


3 comments:

  1. Can you please write more clear.
    What is Supplier ?

    ReplyDelete
    Replies
    1. Sure Sumit! I will update the Same.

      Delete
    2. Supplier is functional interface, It does not take any argument but returns value, when we call a get() method.

      We should expect a boolean value from "Supplier isBPositive" .

      We have passed isBPositive() method to lambda, as it's lazy, when we call get() method, it execute the method and returns boolean value.

      Thank you.

      Delete