Saturday, February 20, 2016

FunctionalLambda

This blog is simply exploring lambda alone.

Follow the oracle link for explanation. Run and test the code to fully understand.
It is PSEUDO-functional...way...You can pass functions as parameters...but wait only as long as java can determine the target type , based on calling/called context.

https://github.com/maheshrajannan/java8Functional.git

Sunday, September 27, 2015

Java8-Nashorn

https://github.com/maheshrajannan/java8Nashorn2

Excitement aside. Please note down the below,

1. Javascript context has a single threaded event loop, Task Que and , Execution context.
2. Callback functions are put in to Java Scripts task queue by the Calling Execution context. In case of browser, it is the browser API.
3. All window (in browser) functions are not available in nashhorn.
4. As the calling context of Javascript is nashorn, there is not asynchronous call back execution after java completes.

Feel free to import this as a maven project and run the junit tests. It has the java8 nashorn concepts tested and explored in depth.

       

	/**
	 * Test method for
	 * {@link java8Group.java8Artifact.js.java.AsynchronousJs.
         * java.AsynchronousJsTest#main(java.lang.String[])}
	 * .
	 * 
	 * INFO: "console.log("I just timed out!");" never gets called from nashorn
	 * javascript engine because the engine exits as soon as it gets evaluated.
	 * In short, the execution context of nashorn javascript engine, is short
	 * lived just like your java main thread. It therefore behaves differently
	 * from browser(web) context, because browser context is long lived than the
	 * nashorn(java) context. So java script is a single threaded event
	 * loop.Browser, plugs in a callback function, to the task queue and
	 * javascript's event loop executes it. This("console.log("I just timed
	 * out!");") does NOT happen from nashorn context.
	 * 
	 * @throws ScriptException
	 * @throws FileNotFoundException
	 * @throws NoSuchMethodException
	 */
	@Test
	public void testAsyncCall() throws FileNotFoundException, ScriptException,
			NoSuchMethodException {
		nashornInterface.evaluateJsFile(
				"src/main/java/java8Group/java8Artifact/js/asynchronous.js");
		String result = (String) nashornInterface.getInvocable()
				.invokeFunction("asynccall");
		System.out.println("Result of async call : " + result);
		Assert.assertEquals("Result is unexpected ", result, "SUCCESS");
	}


       
 

Sunday, September 20, 2015

java8-lambda-review

This is a continuation of previous post.

Java 8 Lambda
1.Simpler to write for really simple expressions. (+)
2. Not re-usable expressions, for testing.(-)

Java Generic

1. Simpler to write for any expression (+)
2. More classes(predicates) to write. (-)
3. Maximum re-usability

Java 8 Lambda

Took the java8 case study posted from oracle and tried it out. It is a maven project with working code and unit test.

https://github.com/maheshrajannan/java8lamda

Please take a look at the java 8 lambda code below,

RoboContactLambda.java
       

 public List phoneContacts(List people, Predicate aTest) {
  List phoned = new ArrayList<>();
  for (Person person : people) {
   if (aTest.test(person)) {
    phoned.add(roboCall(person));
   }
  }
  return phoned;
 }

       
 

RoboContactLambdaTest.java
       

     //INFO: the age checks are not re-usable. They are repeated.
     driverPredicate = p -> p.getAge() >= 16;
     drafteePredicate = p -> p.getAge() >= 18
       && p.getAge() <= 25 && p.getGender() == Gender.MALE;
     pilotPredicate = p -> p.getAge() >= 23 && p.getAge() <= 65; 

       

RoboContactMyLambda.java
       

 public List<Person> phoneContacts(
           List<Person> people, MyPredicate<Person> aTest) {
  List<Person> phoned = new ArrayList<>();
  for (Person person : people) {
   if (aTest.test(person)) {
    phoned.add(roboCall(person));
   }
  }
  return phoned;
 }

       
 

RoboContactMyLambdaTest.java
       

     /**
      * Custom predicates...really Generic Classes 
             * much easier and more re-usable.
      */
     driverPredicate = DriverPredicate.getInstance();
     drafteePredicate = DrafteePredicate.getInstance();
     pilotPredicate = PilotPredicate.getInstance();