KPI driven JAVA(functional) development
Gradual journey from imperative to functional development in JAVA
Anirban Bhattacharjee – Architect @ Walmart Labs
About my
functional
exposure
Writing java for
living … since
JDK 4 (mostly
OO way in the
last decade)
Came across
clojure/scala
around
2010…and used
in production
Learning and
using F# since
last year
@work
Occasional
tinkering with
OCaml and
Haskell
Problem Statement
• We have tons of code written in JAVA
• Where will I find developer?
• One company one tech stack…
• Performance, Monitoring, Support?
• Interop, library support.
CAN I STILL WRITE FUNCTIONAL CODE, IMPROVE KPI, MAKE MY LEADERSHIP HAPPY
Today’s JAVA
OOO -> FP
Functional programming is mainstream
since JDK 8 (GA – 2014)
We could use the power of FP ever since
then
Powerful Lambdas/Streams
Collection Enhancements
Function, Supplier, Consumer
Growing eco-system
• Many promising and production ready libraries
• Clean APIs
• Reduction of verbosity, testable and concise code
• Great dev tools
• Rapid learning from other functional programming languages
KPI improvements (target areas)
using FP in java
SONAR
DEVELOPER
PRODUCTIVITY
Time to market
GC, PERF
IMPROVEMENT
Journey to FP
• Birds eye view of functional java
• Additional powerful frameworks
 vavr
 functional java
 streamex
 Atlassian fugue
 misc…
Functional Java : Stream basics
Loop
List<String> countries = new
ArrayList<>();
for (String s : allCountries) {
if (s.startsWith(“I")) {
countries.add(s);
}
}
Stream
List<String> countries
= allCountries.stream()
.filter(a -> a.startsWith(“I"))
.collect(toList())
Functional Java : Stream illustrated
Origin EndOp 1 Op 2 Op n
Data Pipeline Start Data Pipeline End
Intermediate
Operations
Functional Java : Map, reduce, collect
List<String> list = Stream.of("India", "Iceland",“Peru“,"Italy")
.filter(s -> s.startsWith("I"))
.map(String::toUpperCase)
.sorted()
.collect(toList())
India Iceland ItalyPeru
India Iceland Italy
INDIA ICELAND ITALY
ICELAND INDIA ITALY
ICELAND INDIA ITALY
1
2
3
4
5
1
2
3
4
5
Intermediate Operation : Filter
Ind It UKJP
Ind It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK")
.filter(s -> s.startsWith("I"));
1
2
Functional Java : Intermediate Operation
Ind It UKJP
Ind It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK")
.limit(3);
1
2
JP
Functional Java : Intermediate Operation
Ind It UKJP
Ind It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK", "UK")
.distinct();
1
2
JP
UK
UK
Functional Java : Intermediate Operation
Ind It UKUS
Ind It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", “US", "UK")
.sorted();
1
2
UK US
Functional Java : Intermediate Operation
Ind It UKUS
Ind It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", “US", "UK")
.sorted(Comparator.comparing(String::length));
1
2
UK US
Functional Java : Terminal Operation
Ind It UKJP
Ind
It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK")
.forEach(System.out.println());
1
2
JP
UK
Functional Java : Downstream Collectors
Ind It UKJP
Ind It
1
2
Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK”, "UK” )
.collect(Collectors.toSet());
1
2
JP UK
UK
Functional Java : Custom Downstream collector
return Collector.of(
() -> new ArrayList<T>(), // supplier
(a, e) -> { a.add(e); }, // accumulator
(a, b) -> { a.addAll(b); return a; }, // combiner
a -> { // finisher
a.sort(comp);
DA da = downstream.supplier().get();
for (T e : a)
downstream.accumulator().accept(da, e);
return downstream.finisher().apply(da);
});
Stream<Locale> locales =
Stream.of(Locale.getAvailableLocales());
var result = locales.collect(
groupingBy(Locale::getCountry,
mapping(Locale::getDisplayName,
sorting(String::compareTo,
joining(",")))));
Functional Java : Collection enhancemetns
map.forEach((k, v) -> System.out.println(k + v));for (Map.Entry<String,String> entry : map.entrySet())
System.out.println(entry.getKey() + entry.getValue());
for (Map.Entry<String,String> entry : map.entrySet())
entry.setValue(entry.getValue().toLowerCase());
map.replaceAll((k, v) -> v.toLowerCase());
map.computeIfAbsent(str, x -> HashsSet::new).add(i);Set<Integer> set = multimap.get(str);
if (set == null) {
set = new HashSet<>();
multimap.put(str, set);
}
set.add(i);
Functional Java : More Collection Examples
Contents
Functional Java : Consumer, Supplier, Functions
Contents
Functional Java : Byte Code
• Is Functional java only syntactic sugar?
• Byte code improvements
• Example of Byte code of BEFORE and AFTER
Functional Java : Byte Code
• Is Functional java only syntactic sugar?
• Byte code improvements
• Example of Byte code
Vavr
Vavr : basics
• New functional data-structures (persistent) with superior
performance
• Pure Functions, monads, superior exception management
• Less verbosity
• Great Functional semantics
Vavr : Examples
• LIST, TREE, SET and the cool APIs
•
Vavr : Examples
• CHECKED FUNCTION, CONSUMER, PREDICATE
•
Vavr : Examples
• CHECKED FUNCTION, CONSUMER, PREDICATE
•
Other frameworks: Streamex, Fugue etc..
• CHECKED FUNCTION, CONSUMER, PREDICATE
•
Million Dollar Question: KPI improvement
• SONAR (detailed examples)
Million Dollar Question: KPI improvement
• Developer Productivity -REPL
• Developer Productivity - IDE support
Million Dollar Question: KPI improvement
• All about GC
Million Dollar Question: KPI improvement
• Ease of testing and performance

Kpi driven-java-development

  • 1.
    KPI driven JAVA(functional)development Gradual journey from imperative to functional development in JAVA Anirban Bhattacharjee – Architect @ Walmart Labs
  • 2.
    About my functional exposure Writing javafor living … since JDK 4 (mostly OO way in the last decade) Came across clojure/scala around 2010…and used in production Learning and using F# since last year @work Occasional tinkering with OCaml and Haskell
  • 3.
    Problem Statement • Wehave tons of code written in JAVA • Where will I find developer? • One company one tech stack… • Performance, Monitoring, Support? • Interop, library support. CAN I STILL WRITE FUNCTIONAL CODE, IMPROVE KPI, MAKE MY LEADERSHIP HAPPY
  • 4.
    Today’s JAVA OOO ->FP Functional programming is mainstream since JDK 8 (GA – 2014) We could use the power of FP ever since then Powerful Lambdas/Streams Collection Enhancements Function, Supplier, Consumer
  • 5.
    Growing eco-system • Manypromising and production ready libraries • Clean APIs • Reduction of verbosity, testable and concise code • Great dev tools • Rapid learning from other functional programming languages
  • 6.
    KPI improvements (targetareas) using FP in java SONAR DEVELOPER PRODUCTIVITY Time to market GC, PERF IMPROVEMENT
  • 7.
    Journey to FP •Birds eye view of functional java • Additional powerful frameworks  vavr  functional java  streamex  Atlassian fugue  misc…
  • 8.
    Functional Java :Stream basics Loop List<String> countries = new ArrayList<>(); for (String s : allCountries) { if (s.startsWith(“I")) { countries.add(s); } } Stream List<String> countries = allCountries.stream() .filter(a -> a.startsWith(“I")) .collect(toList())
  • 9.
    Functional Java :Stream illustrated Origin EndOp 1 Op 2 Op n Data Pipeline Start Data Pipeline End Intermediate Operations
  • 10.
    Functional Java :Map, reduce, collect List<String> list = Stream.of("India", "Iceland",“Peru“,"Italy") .filter(s -> s.startsWith("I")) .map(String::toUpperCase) .sorted() .collect(toList()) India Iceland ItalyPeru India Iceland Italy INDIA ICELAND ITALY ICELAND INDIA ITALY ICELAND INDIA ITALY 1 2 3 4 5 1 2 3 4 5
  • 11.
    Intermediate Operation :Filter Ind It UKJP Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK") .filter(s -> s.startsWith("I")); 1 2
  • 12.
    Functional Java :Intermediate Operation Ind It UKJP Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK") .limit(3); 1 2 JP
  • 13.
    Functional Java :Intermediate Operation Ind It UKJP Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK", "UK") .distinct(); 1 2 JP UK UK
  • 14.
    Functional Java :Intermediate Operation Ind It UKUS Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", “US", "UK") .sorted(); 1 2 UK US
  • 15.
    Functional Java :Intermediate Operation Ind It UKUS Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", “US", "UK") .sorted(Comparator.comparing(String::length)); 1 2 UK US
  • 16.
    Functional Java :Terminal Operation Ind It UKJP Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK") .forEach(System.out.println()); 1 2 JP UK
  • 17.
    Functional Java :Downstream Collectors Ind It UKJP Ind It 1 2 Stream<String> startsWithI = Stream.of("Ind", "It", "JP", "UK”, "UK” ) .collect(Collectors.toSet()); 1 2 JP UK UK
  • 18.
    Functional Java :Custom Downstream collector return Collector.of( () -> new ArrayList<T>(), // supplier (a, e) -> { a.add(e); }, // accumulator (a, b) -> { a.addAll(b); return a; }, // combiner a -> { // finisher a.sort(comp); DA da = downstream.supplier().get(); for (T e : a) downstream.accumulator().accept(da, e); return downstream.finisher().apply(da); }); Stream<Locale> locales = Stream.of(Locale.getAvailableLocales()); var result = locales.collect( groupingBy(Locale::getCountry, mapping(Locale::getDisplayName, sorting(String::compareTo, joining(",")))));
  • 19.
    Functional Java :Collection enhancemetns map.forEach((k, v) -> System.out.println(k + v));for (Map.Entry<String,String> entry : map.entrySet()) System.out.println(entry.getKey() + entry.getValue()); for (Map.Entry<String,String> entry : map.entrySet()) entry.setValue(entry.getValue().toLowerCase()); map.replaceAll((k, v) -> v.toLowerCase()); map.computeIfAbsent(str, x -> HashsSet::new).add(i);Set<Integer> set = multimap.get(str); if (set == null) { set = new HashSet<>(); multimap.put(str, set); } set.add(i);
  • 20.
    Functional Java :More Collection Examples Contents
  • 21.
    Functional Java :Consumer, Supplier, Functions Contents
  • 22.
    Functional Java :Byte Code • Is Functional java only syntactic sugar? • Byte code improvements • Example of Byte code of BEFORE and AFTER
  • 23.
    Functional Java :Byte Code • Is Functional java only syntactic sugar? • Byte code improvements • Example of Byte code
  • 24.
  • 25.
    Vavr : basics •New functional data-structures (persistent) with superior performance • Pure Functions, monads, superior exception management • Less verbosity • Great Functional semantics
  • 26.
    Vavr : Examples •LIST, TREE, SET and the cool APIs •
  • 27.
    Vavr : Examples •CHECKED FUNCTION, CONSUMER, PREDICATE •
  • 28.
    Vavr : Examples •CHECKED FUNCTION, CONSUMER, PREDICATE •
  • 29.
    Other frameworks: Streamex,Fugue etc.. • CHECKED FUNCTION, CONSUMER, PREDICATE •
  • 30.
    Million Dollar Question:KPI improvement • SONAR (detailed examples)
  • 31.
    Million Dollar Question:KPI improvement • Developer Productivity -REPL • Developer Productivity - IDE support
  • 32.
    Million Dollar Question:KPI improvement • All about GC
  • 33.
    Million Dollar Question:KPI improvement • Ease of testing and performance