SlideShare a Scribd company logo
Beyond Lambdas, the
aftermath
@DanielSawano
@DanielDeogun
Deogun - Sawano, 11-13 May 2016
About us…
Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016
Daniel Deogun Daniel Sawano
Stockholm - Gothenburg - Malmoe - Umea - New York
[inert code here]
Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016
Optionals 1
7 final Oracle oracle = new Oracle();
8
9 String _() {
10 final Advise advise = currentAdvise();
11
12 if (advise != null) {
13 return advise.cheap();
14 }
15 else {
16 return oracle.advise().expensive();
17 }
18 }
Optionals 1
25 final Oracle oracle = new Oracle();
26
27 String _() {
28 final Advise advise = currentAdvise();
29
30 return Optional.ofNullable(advise)
31 .map(Advise::cheap)
32 .orElse(oracle.advise().expensive());
33 }
Optionals 1
25 final Oracle oracle = new Oracle();
26
27 String _() {
28 final Advise advise = currentAdvise();
29
30 return Optional.ofNullable(advise)
31 .map(Advise::cheap)
32 .orElseGet( () -> oracle.advise().expensive());
33 }
Optionals 2
26 String _(final Optional<String> optOfSomeValue) {
27
28 return optOfSomeValue.map(v -> calculate(v))
29 .filter(someCriteria())
30 .map(v -> transform(v))
31 .orElseGet(() -> completelyDifferentCalculation());
32
33 }
Optionals 2
26 String _(final Optional<String> optOfSomeValue) {
27
28 if (optOfSomeValue.isPresent()) {
29 final String calculatedValue = calculate(optOfSomeValue.get());
30 if (someCriteria().test(calculatedValue)) {
31 return transform(calculatedValue);
32 }
33 }
34
35 return completelyDifferentCalculation();
36
37 }
Optionals 2
26 String _() {
27 return value()
28 .flatMap(v -> firstCalculation(v))
29 .orElseGet(() -> completelyDifferentCalculation());
30 }
31
32 Optional<String> value() {
33 return Optional.of(someValue());
34 }
35
36 Optional<String> firstCalculation(final String v) {
37 return Optional.of(calculate(v))
38 .filter(someCriteria())
39 .map(value -> transform(value));
40 }
Optionals 3
27 <T> void _(final Optional<T> argument) {
28 argument.map(a -> doSomething(a));
29 }
Optionals 3
25 <T> void _(final T argument) {
26 if (argument != null) {
27 doSomething(argument);
28 }
29 }
Optionals 3
26 <T> void _(final T argument) {
27 doSomething(notNull(argument));
28 }
Streams 1
30 @Test
31 public void _() {
32
33 final Stream<String> stream = elements().stream()
34 .sorted();
35
36 final String result = stream.collect(joining(","));
37
38 assertEquals("A,B,C", result);
39
40 }
41
42 static List<String> elements() {
43 return asList("C", "B", null, "A");
44 }
Streams 1
31 @Test
32 public void _() {
33
34 final Stream<String> stream = elements().stream()
35 .filter(Objects::nonNull)
36 .sorted();
37
38 final String result = stream.collect(joining(","));
39
40 assertEquals("A,B,C", result);
41
42 }
43
44 static List<String> elements() {
45 return asList("C", "B", null, "A");
46 }
Streams 2
27 @Test
28 public void _() {
29
30 final long idToFind = 6;
31 final Predicate<Item> idFilter = item -> item.id().equals(idToFind);
32
33 service().itemsMatching(idFilter)
34 .findFirst()
35 .ifPresent(Support::doSomething);
36
37 }
Streams 2
28 @Test
29 public void _() {
30
31 final long idToFind = 6;
32 final Predicate<Item> idFilter = item -> item.id().equals(idToFind);
33
34 service().itemsMatching(idFilter)
35 .reduce(toOneItem())
36 .ifPresent(Support::doSomething);
37
38 }
39
40 BinaryOperator<Item> toOneItem() {
41 return (item, item2) -> {
42 throw new IllegalStateException("Found more than one item with the same id");
43 };
44 }
Streams 3
29 private final UserService userService = new UserService();
30 private final OrderService orderService = new OrderService();
31
32 @Test
33 public void _() {
34 givenALoggedInUser(userService);
35
36 itemsToBuy().stream()
37 .map(item -> new Order(item.id(), currentUser().id()))
38 .forEach(orderService::sendOrder);
39
40 System.out.println(format("Sent %d orders", orderService.sentOrders()));
41 }
42
43 User currentUser() {
44 final User user = userService.currentUser();
45 validState(user != null, "No current user found");
46 return user;
47 }
Streams 3
29 private final UserService userService = new UserService();
30 private final OrderService orderService = new OrderService();
31
32 @Test
33 public void _() {
34 givenALoggedInUser(userService);
35
36 final User user = currentUser();
37 itemsToBuy().parallelStream()
38 .map(item -> new Order(item.id(), user.id()))
39 .forEach(orderService::sendOrder);
40
41 System.out.println(format("Sent %d orders", orderService.sentOrders()));
42 }
43
44 User currentUser() {
45 final User user = userService.currentUser();
46 validState(user != null, "No current user found");
47 return user;
48 }
LAmbdas 1
28 static Integer numberOfFreeApples(final User user,
29 final Function<User, Integer> foodRatio) {
30 return 2 * foodRatio.apply(user);
31 }
32
33 @Test
34 public void _() {
35
36 final Function<User, Integer> foodRatioForVisitors = u -> u.age() > 12 ? 2 : 1;
37
38 final int numberOfFreeApples = numberOfFreeApples(someUser(), foodRatioForVisitors);
39
40 System.out.println(format("Number of free apples: %d", numberOfFreeApples));
41
42 }
LAmbdas 1
29 @Test
30 public void _() {
31
32 final Function<User, Integer> foodRatioForVisitors = u -> u.age() > 12 ? 2 : 1;
33 final Function<User, Integer> age = User::age;
34
35 final int numberOfFreeApples_1 = numberOfFreeApples(someUser(), foodRatioForVisitors);
36 final int numberOfFreeApples_2 = numberOfFreeApples(someUser(), age); // This is a bug!
37
38 System.out.println(format("Number of free apples (1): %d", numberOfFreeApples_1));
39 System.out.println(format("Number of free apples (2): %d", numberOfFreeApples_2));
40
41 }
LAmbdas 1
28 @FunctionalInterface
29 interface FoodRatioStrategy {
30
31 Integer ratioFor(User user);
32 }
33
34 static Integer numberOfFreeApples(final User user,
35 final FoodRatioStrategy ratioStrategy) {
36 return 2 * ratioStrategy.ratioFor(user);
37 }
38
39 @Test
40 public void _() {
41
42 final FoodRatioStrategy foodRatioForVisitors = user -> user.age() > 12 ? 2 : 1;
43 final Function<User, Integer> age = User::age;
44
45 final Integer numberOfFreeApples_1 = numberOfFreeApples(someUser(), foodRatioForVisitors);
46 //final Integer numberOfFreeApples_2 = numberOfFreeApples(someUser(), age);
47
48 System.out.println(format("Number of free apples (1): %d", numberOfFreeApples_1));
49 }
LAmbdas 2
25 @Test
26 public void should_build_tesla() {
27
28 assertEquals(1000, new TeslaFactory().createTesla().engine().horsepower());
29
30 }
31
32 @Test
33 public void should_build_volvo() {
34
35 assertEquals(250, new VolvoFactory().createVolvo().engine().horsepower());
36
37 }
LAmbdas 3
29 @Test
30 public void _() {
31
32 final List<Integer> values = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
33
34 allEvenNumbers(values);
35
36 System.out.println("Hello");
37
38 }
39
40 static List<Integer> allEvenNumbers(final List<Integer> values) {
41 return values.stream()
42 .filter(Support::isEven)
43 .collect(toList());
44 }
LAmbdas 3
31 @Test
32 public void _() {
33
34 final List<Integer> values = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
35
36 final Supplier<List<Integer>> integers = () -> allEvenNumbers(values);
37
38 System.out.println(integers.get());
39
40 }
41
42 static List<Integer> allEvenNumbers(final List<Integer> values) {
43 return values.stream()
44 .filter(Support::isEven)
45 .collect(toList());
46 }
LAmbdas 4
24 private final String pattern;
25
26 public _14(final String pattern) {
27 this.pattern = pattern;
28 }
29
30 public List<String> allMatchingElements(final List<String> elements) {
31 return elements.stream()
32 .filter(e -> e.contains(pattern))
33 .collect(toList());
34 }
LAmbdas 4
25 private final String pattern;
26
27 public _14(final String pattern) {
28 this.pattern = pattern;
29 }
30
31 public List<String> allMatchingElements(final List<String> elements) {
32 return elements.stream()
33 .filter(matches(pattern))
34 .collect(toList());
35 }
36
37 private Predicate<String> matches(final String pattern) {
38 return e -> e.contains(pattern);
39 }
Q&A
Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016
[Questions]
Code examples can be found here:
https://github.com/sawano/beyond-lambdas-the-aftermath
Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016
Thank you!
@DanielSawano @DanielDeogun
Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016

More Related Content

PDF
Spotify 2016 - Beyond Lambdas - the Aftermath
PDF
JDays 2016 - Beyond Lambdas - the Aftermath
PDF
Java practical(baca sem v)
PDF
Hierarchical free monads and software design in fp
DOC
Final JAVA Practical of BCA SEM-5.
DOCX
Java practical
DOCX
Java file
PDF
Spotify 2016 - Beyond Lambdas - the Aftermath
JDays 2016 - Beyond Lambdas - the Aftermath
Java practical(baca sem v)
Hierarchical free monads and software design in fp
Final JAVA Practical of BCA SEM-5.
Java practical
Java file

What's hot (18)

PDF
Important java programs(collection+file)
DOCX
C# labprograms
PPT
Oop lecture9 13
PDF
Java programs
PDF
TI1220 Lecture 6: First-class Functions
DOCX
C# console programms
PDF
JAVA 8 : Migration et enjeux stratégiques en entreprise
PDF
The Ring programming language version 1.5.3 book - Part 10 of 184
DOCX
.net progrmming part2
PDF
The Ring programming language version 1.5.4 book - Part 10 of 185
PDF
The Ring programming language version 1.2 book - Part 23 of 84
PDF
The Ring programming language version 1.2 book - Part 24 of 84
PDF
Java Performance Puzzlers
PDF
Java_practical_handbook
PDF
RMI Java Programming Lab Manual 2019
DOCX
Java PRACTICAL file
DOC
Dsprograms(2nd cse)
PDF
The Ring programming language version 1.3 book - Part 24 of 88
Important java programs(collection+file)
C# labprograms
Oop lecture9 13
Java programs
TI1220 Lecture 6: First-class Functions
C# console programms
JAVA 8 : Migration et enjeux stratégiques en entreprise
The Ring programming language version 1.5.3 book - Part 10 of 184
.net progrmming part2
The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.2 book - Part 23 of 84
The Ring programming language version 1.2 book - Part 24 of 84
Java Performance Puzzlers
Java_practical_handbook
RMI Java Programming Lab Manual 2019
Java PRACTICAL file
Dsprograms(2nd cse)
The Ring programming language version 1.3 book - Part 24 of 88
Ad

Viewers also liked (20)

PDF
Devoxx PL 2016 - Beyond Lambdas, the Aftermath
PPT
Evolucion De La Ocmunicaion
DOCX
Chuck Brooks; Cybersecurity & Homeland Security Leadership Profile
PPTX
Becoming current spring_14
PDF
Proyeksi vektor aji santoso ( 31 ) msp
PPT
Financial Management & Corporate Advisory For Your Business
PDF
Mobile Marketing for Health Clubs Webinar - June 2016
PPTX
Customer service careers
PDF
WordCamp Sydney 2016 - Day 2 Closing Remarks
PPT
Pourquoi les evenements sont importants
PDF
That's So Fake: Exploring Critical Literacy
PPTX
How to develop a mobile app for events and conferences with little to no reso...
PPTX
Люцко Н.М. Организационно-педагогические и правовые основы формирования незав...
PDF
Zinnov Confluence 2014: Edge of Tomorrow: Fundamental Shifts Shaping Our Future
PPTX
TENDANCES BRAND CONTENT 2015 : Just Dance présenté par Alban Dechelotte de Co...
PDF
Personal and Personalized Learning
PDF
Junho jardim
PDF
Yahoo! research - 'Appetite' - the hunger for mobile media
PDF
Hotspot
DOCX
Curriculum vitae sv
Devoxx PL 2016 - Beyond Lambdas, the Aftermath
Evolucion De La Ocmunicaion
Chuck Brooks; Cybersecurity & Homeland Security Leadership Profile
Becoming current spring_14
Proyeksi vektor aji santoso ( 31 ) msp
Financial Management & Corporate Advisory For Your Business
Mobile Marketing for Health Clubs Webinar - June 2016
Customer service careers
WordCamp Sydney 2016 - Day 2 Closing Remarks
Pourquoi les evenements sont importants
That's So Fake: Exploring Critical Literacy
How to develop a mobile app for events and conferences with little to no reso...
Люцко Н.М. Организационно-педагогические и правовые основы формирования незав...
Zinnov Confluence 2014: Edge of Tomorrow: Fundamental Shifts Shaping Our Future
TENDANCES BRAND CONTENT 2015 : Just Dance présenté par Alban Dechelotte de Co...
Personal and Personalized Learning
Junho jardim
Yahoo! research - 'Appetite' - the hunger for mobile media
Hotspot
Curriculum vitae sv
Ad

Similar to GeeCon 2016 - Beyond Lambdas, the Aftermath (20)

PDF
JFokus 2016 - Beyond Lambdas - the Aftermath
PDF
The Ring programming language version 1.6 book - Part 37 of 189
PDF
The Ring programming language version 1.5.2 book - Part 34 of 181
PDF
The Ring programming language version 1.8 book - Part 40 of 202
PDF
The Ring programming language version 1.9 book - Part 42 of 210
PDF
The Ring programming language version 1.3 book - Part 25 of 88
PDF
Java 8 lambda expressions
PDF
The Ring programming language version 1.3 book - Part 26 of 88
PDF
The Ring programming language version 1.7 book - Part 39 of 196
DOCX
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
PPT
Extractors & Implicit conversions
PDF
The Ring programming language version 1.5.4 book - Part 35 of 185
PPTX
String in .net
PDF
Java 8 - Nuts and Bold - SFEIR Benelux
PDF
The Ring programming language version 1.9 book - Part 43 of 210
PDF
Java, Up to Date Sources
PPS
Class method
PPT
String slide
PDF
Java VS Python
PPT
Learn Matlab
JFokus 2016 - Beyond Lambdas - the Aftermath
The Ring programming language version 1.6 book - Part 37 of 189
The Ring programming language version 1.5.2 book - Part 34 of 181
The Ring programming language version 1.8 book - Part 40 of 202
The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.3 book - Part 25 of 88
Java 8 lambda expressions
The Ring programming language version 1.3 book - Part 26 of 88
The Ring programming language version 1.7 book - Part 39 of 196
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
Extractors & Implicit conversions
The Ring programming language version 1.5.4 book - Part 35 of 185
String in .net
Java 8 - Nuts and Bold - SFEIR Benelux
The Ring programming language version 1.9 book - Part 43 of 210
Java, Up to Date Sources
Class method
String slide
Java VS Python
Learn Matlab

More from Daniel Sawano (10)

PDF
GeeCon Prague 2017 - Cracking the Code to Secure Software
PDF
Devoxx PL 2017 - Cracking the Code to Secure Software
PDF
DevDays LT 2017 - Secure by Design
PDF
Devoxx, MA, 2015, Failing Continuous Delivery
PDF
Failing Continuous Delivery, Agile Prague 2015
PDF
Failing Continuous Delivery, Devoxx Poland, 2015
PDF
Things Every Professional Programmer Should Know
PDF
Failing Continuous Delivery, JDays, 2015
PDF
Akka Made Our Day
PDF
Reactive Programming With Akka - Lessons Learned
GeeCon Prague 2017 - Cracking the Code to Secure Software
Devoxx PL 2017 - Cracking the Code to Secure Software
DevDays LT 2017 - Secure by Design
Devoxx, MA, 2015, Failing Continuous Delivery
Failing Continuous Delivery, Agile Prague 2015
Failing Continuous Delivery, Devoxx Poland, 2015
Things Every Professional Programmer Should Know
Failing Continuous Delivery, JDays, 2015
Akka Made Our Day
Reactive Programming With Akka - Lessons Learned

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
medical staffing services at VALiNTRY
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
history of c programming in notes for students .pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Essential Infomation Tech presentation.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Odoo Companies in India – Driving Business Transformation.pdf
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
wealthsignaloriginal-com-DS-text-... (1).pdf
medical staffing services at VALiNTRY
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
history of c programming in notes for students .pptx
Operating system designcfffgfgggggggvggggggggg
Essential Infomation Tech presentation.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Nekopoi APK 2025 free lastest update
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Migrate SBCGlobal Email to Yahoo Easily
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
CHAPTER 2 - PM Management and IT Context
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Odoo POS Development Services by CandidRoot Solutions
Odoo Companies in India – Driving Business Transformation.pdf

GeeCon 2016 - Beyond Lambdas, the Aftermath

  • 2. About us… Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016 Daniel Deogun Daniel Sawano Stockholm - Gothenburg - Malmoe - Umea - New York
  • 3. [inert code here] Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016
  • 4. Optionals 1 7 final Oracle oracle = new Oracle(); 8 9 String _() { 10 final Advise advise = currentAdvise(); 11 12 if (advise != null) { 13 return advise.cheap(); 14 } 15 else { 16 return oracle.advise().expensive(); 17 } 18 }
  • 5. Optionals 1 25 final Oracle oracle = new Oracle(); 26 27 String _() { 28 final Advise advise = currentAdvise(); 29 30 return Optional.ofNullable(advise) 31 .map(Advise::cheap) 32 .orElse(oracle.advise().expensive()); 33 }
  • 6. Optionals 1 25 final Oracle oracle = new Oracle(); 26 27 String _() { 28 final Advise advise = currentAdvise(); 29 30 return Optional.ofNullable(advise) 31 .map(Advise::cheap) 32 .orElseGet( () -> oracle.advise().expensive()); 33 }
  • 7. Optionals 2 26 String _(final Optional<String> optOfSomeValue) { 27 28 return optOfSomeValue.map(v -> calculate(v)) 29 .filter(someCriteria()) 30 .map(v -> transform(v)) 31 .orElseGet(() -> completelyDifferentCalculation()); 32 33 }
  • 8. Optionals 2 26 String _(final Optional<String> optOfSomeValue) { 27 28 if (optOfSomeValue.isPresent()) { 29 final String calculatedValue = calculate(optOfSomeValue.get()); 30 if (someCriteria().test(calculatedValue)) { 31 return transform(calculatedValue); 32 } 33 } 34 35 return completelyDifferentCalculation(); 36 37 }
  • 9. Optionals 2 26 String _() { 27 return value() 28 .flatMap(v -> firstCalculation(v)) 29 .orElseGet(() -> completelyDifferentCalculation()); 30 } 31 32 Optional<String> value() { 33 return Optional.of(someValue()); 34 } 35 36 Optional<String> firstCalculation(final String v) { 37 return Optional.of(calculate(v)) 38 .filter(someCriteria()) 39 .map(value -> transform(value)); 40 }
  • 10. Optionals 3 27 <T> void _(final Optional<T> argument) { 28 argument.map(a -> doSomething(a)); 29 }
  • 11. Optionals 3 25 <T> void _(final T argument) { 26 if (argument != null) { 27 doSomething(argument); 28 } 29 }
  • 12. Optionals 3 26 <T> void _(final T argument) { 27 doSomething(notNull(argument)); 28 }
  • 13. Streams 1 30 @Test 31 public void _() { 32 33 final Stream<String> stream = elements().stream() 34 .sorted(); 35 36 final String result = stream.collect(joining(",")); 37 38 assertEquals("A,B,C", result); 39 40 } 41 42 static List<String> elements() { 43 return asList("C", "B", null, "A"); 44 }
  • 14. Streams 1 31 @Test 32 public void _() { 33 34 final Stream<String> stream = elements().stream() 35 .filter(Objects::nonNull) 36 .sorted(); 37 38 final String result = stream.collect(joining(",")); 39 40 assertEquals("A,B,C", result); 41 42 } 43 44 static List<String> elements() { 45 return asList("C", "B", null, "A"); 46 }
  • 15. Streams 2 27 @Test 28 public void _() { 29 30 final long idToFind = 6; 31 final Predicate<Item> idFilter = item -> item.id().equals(idToFind); 32 33 service().itemsMatching(idFilter) 34 .findFirst() 35 .ifPresent(Support::doSomething); 36 37 }
  • 16. Streams 2 28 @Test 29 public void _() { 30 31 final long idToFind = 6; 32 final Predicate<Item> idFilter = item -> item.id().equals(idToFind); 33 34 service().itemsMatching(idFilter) 35 .reduce(toOneItem()) 36 .ifPresent(Support::doSomething); 37 38 } 39 40 BinaryOperator<Item> toOneItem() { 41 return (item, item2) -> { 42 throw new IllegalStateException("Found more than one item with the same id"); 43 }; 44 }
  • 17. Streams 3 29 private final UserService userService = new UserService(); 30 private final OrderService orderService = new OrderService(); 31 32 @Test 33 public void _() { 34 givenALoggedInUser(userService); 35 36 itemsToBuy().stream() 37 .map(item -> new Order(item.id(), currentUser().id())) 38 .forEach(orderService::sendOrder); 39 40 System.out.println(format("Sent %d orders", orderService.sentOrders())); 41 } 42 43 User currentUser() { 44 final User user = userService.currentUser(); 45 validState(user != null, "No current user found"); 46 return user; 47 }
  • 18. Streams 3 29 private final UserService userService = new UserService(); 30 private final OrderService orderService = new OrderService(); 31 32 @Test 33 public void _() { 34 givenALoggedInUser(userService); 35 36 final User user = currentUser(); 37 itemsToBuy().parallelStream() 38 .map(item -> new Order(item.id(), user.id())) 39 .forEach(orderService::sendOrder); 40 41 System.out.println(format("Sent %d orders", orderService.sentOrders())); 42 } 43 44 User currentUser() { 45 final User user = userService.currentUser(); 46 validState(user != null, "No current user found"); 47 return user; 48 }
  • 19. LAmbdas 1 28 static Integer numberOfFreeApples(final User user, 29 final Function<User, Integer> foodRatio) { 30 return 2 * foodRatio.apply(user); 31 } 32 33 @Test 34 public void _() { 35 36 final Function<User, Integer> foodRatioForVisitors = u -> u.age() > 12 ? 2 : 1; 37 38 final int numberOfFreeApples = numberOfFreeApples(someUser(), foodRatioForVisitors); 39 40 System.out.println(format("Number of free apples: %d", numberOfFreeApples)); 41 42 }
  • 20. LAmbdas 1 29 @Test 30 public void _() { 31 32 final Function<User, Integer> foodRatioForVisitors = u -> u.age() > 12 ? 2 : 1; 33 final Function<User, Integer> age = User::age; 34 35 final int numberOfFreeApples_1 = numberOfFreeApples(someUser(), foodRatioForVisitors); 36 final int numberOfFreeApples_2 = numberOfFreeApples(someUser(), age); // This is a bug! 37 38 System.out.println(format("Number of free apples (1): %d", numberOfFreeApples_1)); 39 System.out.println(format("Number of free apples (2): %d", numberOfFreeApples_2)); 40 41 }
  • 21. LAmbdas 1 28 @FunctionalInterface 29 interface FoodRatioStrategy { 30 31 Integer ratioFor(User user); 32 } 33 34 static Integer numberOfFreeApples(final User user, 35 final FoodRatioStrategy ratioStrategy) { 36 return 2 * ratioStrategy.ratioFor(user); 37 } 38 39 @Test 40 public void _() { 41 42 final FoodRatioStrategy foodRatioForVisitors = user -> user.age() > 12 ? 2 : 1; 43 final Function<User, Integer> age = User::age; 44 45 final Integer numberOfFreeApples_1 = numberOfFreeApples(someUser(), foodRatioForVisitors); 46 //final Integer numberOfFreeApples_2 = numberOfFreeApples(someUser(), age); 47 48 System.out.println(format("Number of free apples (1): %d", numberOfFreeApples_1)); 49 }
  • 22. LAmbdas 2 25 @Test 26 public void should_build_tesla() { 27 28 assertEquals(1000, new TeslaFactory().createTesla().engine().horsepower()); 29 30 } 31 32 @Test 33 public void should_build_volvo() { 34 35 assertEquals(250, new VolvoFactory().createVolvo().engine().horsepower()); 36 37 }
  • 23. LAmbdas 3 29 @Test 30 public void _() { 31 32 final List<Integer> values = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 33 34 allEvenNumbers(values); 35 36 System.out.println("Hello"); 37 38 } 39 40 static List<Integer> allEvenNumbers(final List<Integer> values) { 41 return values.stream() 42 .filter(Support::isEven) 43 .collect(toList()); 44 }
  • 24. LAmbdas 3 31 @Test 32 public void _() { 33 34 final List<Integer> values = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 35 36 final Supplier<List<Integer>> integers = () -> allEvenNumbers(values); 37 38 System.out.println(integers.get()); 39 40 } 41 42 static List<Integer> allEvenNumbers(final List<Integer> values) { 43 return values.stream() 44 .filter(Support::isEven) 45 .collect(toList()); 46 }
  • 25. LAmbdas 4 24 private final String pattern; 25 26 public _14(final String pattern) { 27 this.pattern = pattern; 28 } 29 30 public List<String> allMatchingElements(final List<String> elements) { 31 return elements.stream() 32 .filter(e -> e.contains(pattern)) 33 .collect(toList()); 34 }
  • 26. LAmbdas 4 25 private final String pattern; 26 27 public _14(final String pattern) { 28 this.pattern = pattern; 29 } 30 31 public List<String> allMatchingElements(final List<String> elements) { 32 return elements.stream() 33 .filter(matches(pattern)) 34 .collect(toList()); 35 } 36 37 private Predicate<String> matches(final String pattern) { 38 return e -> e.contains(pattern); 39 }
  • 27. Q&A Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016 [Questions]
  • 28. Code examples can be found here: https://github.com/sawano/beyond-lambdas-the-aftermath Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016
  • 29. Thank you! @DanielSawano @DanielDeogun Daniel Sawano, Daniel Deogun Kraków, 11-13 May 2016