Java Functional Programming (Part 1: The Beginning)
Description:
With the release of Java 8, Java has finally added support for "lambda functions", that is, variables that contain a function which can operate on data just as class methods can (well, not just as class methods can...)
Let's learn about lambdas. This is part 1 in a series.
- Part 2: http://www.codewars.com/kata/java-functional-programming-part-2-multiline-functions
- Part 3: http://www.codewars.com/kata/java-functional-programming-part-3-closured-for-business
- Part 4: http://www.codewars.com/kata/java-functional-programming-part-4-row-row-row-your-boat-gently-down-the-dot-dot-dot
Coming from most scripting languages (e.g., Javascript, Python, Ruby), the concept of functions that can be passed as variables should not be anything new. The syntax in Java should even look familiar:
Function<MyObject, String> f = p -> p.toString();
String myString = f.apply(myObject); //Stores whatever the toString() of myObject is in myString
The above is a simple mapper function: given an input of type MyObject, return a specific result of type String, in this case the toString of the object. They can, of course, become much more complicated.
A full listing of the default function types can be found at http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Given this POJO:
public class Student {
private final String firstName;
private final String lastName;
public final String studentNumber;
public String getFullName() {
return firstName + " " + lastName;
}
public String getCommaName() {
return lastName + ", " + firstName;
}
}
Write a Function (with the appropriate types) that returns true if a given student is "John Smith" with a student number of "js123" (otherwise return false).
Similar Kata:
Stats:
Created | Jan 2, 2015 |
Published | Jan 2, 2015 |
Warriors Trained | 8734 |
Total Skips | 966 |
Total Code Submissions | 6852 |
Total Times Completed | 2525 |
Java Completions | 2524 |
Total Stars | 140 |
% of votes with a positive feedback rating | 91% of 335 |
Total "Very Satisfied" Votes | 279 |
Total "Somewhat Satisfied" Votes | 49 |
Total "Not Satisfied" Votes | 7 |