Java 8 functional interfaces – random musings implementing a Scala type

In one of the assignments of the Functional programming with Scala course a type called Terrain is introduced – Terrain represents a region parts of which are accessible and parts which are not. So in a very clever way Terrain is defined the following way in the assignment:
 
 
 
 
 
 
 

case class Pos(x:Int, y: Int)

type Terrain = Pos => Boolean

Essentially Terrain is a function which takes a position and for that position returns a boolean based on whether the position is accessible or not!

Given this definition of Terrain, a way to define an “infinite” terrain where every position is accessible is done this way:

val infiniteTerrain = (pos: Pos) => true

or another terrain, where certain coordinates are accessible can be defined this way:

def terrainFunction(vector: Vector[Vector[Char]]) : Terrain = {
 (pos: Pos) => {
  if (pos.x > vector.size - 1 || pos.y > vector(0).size - 1 || pos.x < 0 || pos.y < 0) {
   false
  } else {
   val ch = vector(pos.x)(pos.y)
   ch == 'o';
  }
 }
}  

val terrain1 = terrainFunction(Vector(
     Vector('-','-','-'),
     Vector('-','o','-'),
     Vector('-','o','-'),
     Vector('-','o','-'),
     Vector('-','-','-')
     )
    )

All extremely clever.

Now, given that Java 8 release is imminent, an equally(almost :-) ) clever code can be attempted using Java 8 constructs:

whereas the Terrain could be defined as a function signature in Scala, it has to be defined as a functional interface with Java 8:

interface Terrain {
 public boolean isAccessible(Pos pos);
}

Given this interface, an infinite terrain looks like this using Lambdas in Java 8:

Terrain infiniteTerrain = (pos) -> true;

The terrainFunction equivalent in Java 8 can be defined along these lines:

public Terrain terrainFunction(char[][] arr) {
 return (pos) -> {
  if (pos.x > arr.length - 1 || pos.y > arr[0].length - 1 || pos.x < 0 || pos.y < 0) {
   return false;
  } else {
   char ch = arr[pos.x][pos.y];
   return ch == 'o';
  }
 };
}

char[][] arr = {
 {'-','-','-'},
 {'-','o','-'},
 {'-','o','-'},
 {'-','o','-'},
 {'-','-','-'}
};
Terrain terrain = terrainFunction(arr); 

assertTrue(terrain.isAccessible(new Pos(1, 1)));

Close enough!

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You'll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You'll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


4 − four =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books