SlideShare a Scribd company logo
1 of 29
Download to read offline
Beyond Lambdas, the aftermath
@DanielSawano
@DanielDeogun
Spotify 2016
About Us…
Daniel Deogun Daniel Sawano
Omegapoint
Stockholm - Gothenburg - Malmoe - Umea - New York
[insert code here]
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
[Questions]
Code examples can be found here:
https://github.com/sawano/beyond-lambdas-the-aftermath
Thank you!
@DanielSawano @DanielDeogun

More Related Content

What's hot

Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
SFilipp
 

What's hot (19)

Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fp
 
The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185
 
Java programs
Java programsJava programs
Java programs
 
Java file
Java fileJava file
Java file
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30
 
The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185
 
The Ring programming language version 1.5.1 book - Part 28 of 180
The Ring programming language version 1.5.1 book - Part 28 of 180The Ring programming language version 1.5.1 book - Part 28 of 180
The Ring programming language version 1.5.1 book - Part 28 of 180
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88
 
New Java Date/Time API
New Java Date/Time APINew Java Date/Time API
New Java Date/Time API
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
 
C#
C#C#
C#
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbook
 
Pattern printing programs
Pattern printing programsPattern printing programs
Pattern printing programs
 
The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.9 book - Part 42 of 210The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.9 book - Part 42 of 210
 

Similar to Spotify 2016 - Beyond Lambdas - the Aftermath

ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docxch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
cravennichole326
 
String in .net
String in .netString in .net
String in .net
Larry Nung
 

Similar to Spotify 2016 - Beyond Lambdas - the Aftermath (20)

JFokus 2016 - Beyond Lambdas - the Aftermath
JFokus 2016 - Beyond Lambdas - the AftermathJFokus 2016 - Beyond Lambdas - the Aftermath
JFokus 2016 - Beyond Lambdas - the Aftermath
 
The Ring programming language version 1.5.2 book - Part 34 of 181
The Ring programming language version 1.5.2 book - Part 34 of 181The Ring programming language version 1.5.2 book - Part 34 of 181
The Ring programming language version 1.5.2 book - Part 34 of 181
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
The Ring programming language version 1.8 book - Part 40 of 202
The Ring programming language version 1.8 book - Part 40 of 202The Ring programming language version 1.8 book - Part 40 of 202
The Ring programming language version 1.8 book - Part 40 of 202
 
The Ring programming language version 1.6 book - Part 37 of 189
The Ring programming language version 1.6 book - Part 37 of 189The Ring programming language version 1.6 book - Part 37 of 189
The Ring programming language version 1.6 book - Part 37 of 189
 
The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88
 
Java 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR BeneluxJava 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR Benelux
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
The Ring programming language version 1.3 book - Part 26 of 88
The Ring programming language version 1.3 book - Part 26 of 88The Ring programming language version 1.3 book - Part 26 of 88
The Ring programming language version 1.3 book - Part 26 of 88
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84
 
The Ring programming language version 1.5.4 book - Part 35 of 185
The Ring programming language version 1.5.4 book - Part 35 of 185The Ring programming language version 1.5.4 book - Part 35 of 185
The Ring programming language version 1.5.4 book - Part 35 of 185
 
The Ring programming language version 1.9 book - Part 43 of 210
The Ring programming language version 1.9 book - Part 43 of 210The Ring programming language version 1.9 book - Part 43 of 210
The Ring programming language version 1.9 book - Part 43 of 210
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docxch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
ch12.DS_Store__MACOSXch12._.DS_Storech12section_1.D.docx
 
String in .net
String in .netString in .net
String in .net
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversions
 
The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 

More from Daniel Sawano

More from Daniel Sawano (11)

GeeCon Prague 2017 - Cracking the Code to Secure Software
GeeCon Prague 2017 - Cracking the Code to Secure SoftwareGeeCon Prague 2017 - Cracking the Code to Secure Software
GeeCon Prague 2017 - Cracking the Code to Secure Software
 
Devoxx PL 2017 - Cracking the Code to Secure Software
Devoxx PL 2017 - Cracking the Code to Secure SoftwareDevoxx PL 2017 - Cracking the Code to Secure Software
Devoxx PL 2017 - Cracking the Code to Secure Software
 
DevDays LT 2017 - Secure by Design
DevDays LT 2017 - Secure by DesignDevDays LT 2017 - Secure by Design
DevDays LT 2017 - Secure by Design
 
Devoxx PL 2016 - Beyond Lambdas, the Aftermath
Devoxx PL 2016 - Beyond Lambdas, the AftermathDevoxx PL 2016 - Beyond Lambdas, the Aftermath
Devoxx PL 2016 - Beyond Lambdas, the Aftermath
 
Devoxx, MA, 2015, Failing Continuous Delivery
Devoxx, MA, 2015, Failing Continuous DeliveryDevoxx, MA, 2015, Failing Continuous Delivery
Devoxx, MA, 2015, Failing Continuous Delivery
 
Failing Continuous Delivery, Agile Prague 2015
Failing Continuous Delivery, Agile Prague 2015Failing Continuous Delivery, Agile Prague 2015
Failing Continuous Delivery, Agile Prague 2015
 
Failing Continuous Delivery, Devoxx Poland, 2015
Failing Continuous Delivery, Devoxx Poland, 2015Failing Continuous Delivery, Devoxx Poland, 2015
Failing Continuous Delivery, Devoxx Poland, 2015
 
Things Every Professional Programmer Should Know
Things Every Professional Programmer Should KnowThings Every Professional Programmer Should Know
Things Every Professional Programmer Should Know
 
Failing Continuous Delivery, JDays, 2015
Failing Continuous Delivery, JDays, 2015Failing Continuous Delivery, JDays, 2015
Failing Continuous Delivery, JDays, 2015
 
Akka Made Our Day
Akka Made Our DayAkka Made Our Day
Akka Made Our Day
 
Reactive Programming With Akka - Lessons Learned
Reactive Programming With Akka - Lessons LearnedReactive Programming With Akka - Lessons Learned
Reactive Programming With Akka - Lessons Learned
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

Spotify 2016 - Beyond Lambdas - the Aftermath

  • 1. Beyond Lambdas, the aftermath @DanielSawano @DanielDeogun Spotify 2016
  • 2. About Us… Daniel Deogun Daniel Sawano Omegapoint Stockholm - Gothenburg - Malmoe - Umea - New York
  • 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 }
  • 28. Code examples can be found here: https://github.com/sawano/beyond-lambdas-the-aftermath