4. Higher-order Function
... is a function that does at least one of the following:
• Takes one or more functions as arguments
• Returns a function as its result
Cache<String, String> cache = CacheBuilder.newBuilder().build();
String sql = "SQL_1";
String result = cache.get(sql, new Callable<String>() {
@Override
public String call() throws Exception {
return "I am the syntax tree of " + sql;
}
}));
5. What is Lambda Expression?
Lambda is just a syntactic sugar, but it will change how you programing.
Cache<String, String> cache = CacheBuilder.newBuilder().build();
String sql = "SQL_1";
String result = cache.get(sql, new Callable<String>() {
@Override
public String call() throws Exception {
return "I am the syntax tree of " + sql;
}
}));
String result = cache.get(sql, () -> "I am the syntax tree of " + sql);
6.
7. Closures
• sql is passed to this Callable. How?
Cache<String, String> cache = CacheBuilder.newBuilder().build();
String sql = "SQL_1";
String result = cache.get(sql, new Callable<String>() {
@Override
public String call() throws Exception {
return "I am the syntax tree of " + sql;
}
}));
String result = cache.get(sql, () -> "I am the syntax tree of " + sql);
8. • How to pass a function along with its context as an argument?
• (extend) How to implement a linked list with Closures?
Objects are poor man's closures.
Closures are poor man's objects.
Closures <=> Objects
9. Functional Interfaces
• Interfaces with only one abstract method.
• @FunctionalInterface
public interface ActionListener {
public void actionPerformed(ActionEvent event);
}
10. Logger + Lambda
Logger logger = new Logger();
if (logger.isDebugEnabled()) {
logger.debug("Look at this: " + expensiveOperation());
}
Logger logger = new Logger();
logger.debug(() -> "Look at this: " + expensiveOperation());
function is first class object in Functional Programing
12. Example
• Given List<Artist>
• Filter artists living in China
public final class Album {
private String name;
private List<Track> tracks;
private List<Artist> musicians;
}
public final class Artist {
private String name;
private List<Artist> members;
private String nationality;
}
public final class Track {
private final String name;
private final int length;
}
13. Without streams...
public List<Artist> artistsLivingInLondon() {
List<Artist> results = new ArrayList<>();
for (Artist artist : artists) {
if ("China".equals(artist.getNationality())) {
results.add(artist);
}
}
return results;
}
14. Example
• Given List<Artist>, filter artists living in China
• Given List<Artist>, return names of all artists
public List<Artist> artistsLivingInChina() {
return artists.stream()
.filter(artist -> "China".equals(artist.getNationality()))
.collect(Collectors.toList());
}
public List<String> getNamesOfArtists_MethodReference() {
return artists.stream()
.map(Artist::getName)
.collect(Collectors.toList());
}
38. A Kind Reminder
• How to make everything functional?
• Pure Function
• Object
• Loop
• What is Curry?
• What is Y-combinator?
.....
Functional
Programming in Java