lambdas: myths and mistakes · lambdas: myths and mistakes by richard warburton. the critical...
TRANSCRIPT
![Page 1: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/1.jpg)
Lambdas:Myths andMistakesby Richard Warburton
![Page 2: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/2.jpg)
the critical design tool for softwaredevelopment is a mind well educated indesign principles. It is not ... technology.
Craig Larman
![Page 3: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/3.jpg)
Talk StructureWhy am I talking about this?
Intro to Lambda Expressions
Beyond the Myths
Functional Thinking
![Page 4: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/4.jpg)
Why am Italking about
this?
![Page 5: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/5.jpg)
Lambda Expressions arecoming in Java 8!
![Page 6: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/6.jpg)
lots of discussion/debate
![Page 7: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/7.jpg)
How can we help?Adopt-a-JSR
![Page 8: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/8.jpg)
Adopt-a-JSR?More community driven standards
Hackdays
Serve on Expert Groups
![Page 9: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/9.jpg)
Some discussionunimportant
Concrete Examples focus discussion
![Page 10: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/10.jpg)
Intro toLambda
Expressions
![Page 11: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/11.jpg)
Overview of LambdasGoal: Better Libraries
Example: Collections with Data Parallelism
Approach: Allow Code as Data
![Page 12: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/12.jpg)
Action Listenerbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); }});
![Page 13: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/13.jpg)
Code as Databutton.addActionListener( ?);
![Page 14: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/14.jpg)
Need a parameterbutton.addActionListener(event
);
![Page 15: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/15.jpg)
Lambda Examplebutton.addActionListener(event -> System.out.println("button clicked"));
![Page 16: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/16.jpg)
No parametersRunnable helloWorld = () -> System.out.println("Hello World");
![Page 17: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/17.jpg)
Variable CaptureString name = getUserName();button.addActionListener(event -> System.out.println("hi " + name));
![Page 18: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/18.jpg)
Functional InterfacesEverything in Java has a type
Problem: Need types to represent
Functions
Solution: Use interfaces
![Page 19: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/19.jpg)
Functional Interfacespublic interface ActionListener extends EventListener { public void actionPerformed(ActionEvent event);}
![Page 20: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/20.jpg)
StreamsSupport automated data parallelism
Build computation pipelines
Iterator with inversion of control
![Page 21: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/21.jpg)
External Iterationint count = 0;for (Artist artist : artists) { if (artist.isFrom("London")) { count++; }}
![Page 22: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/22.jpg)
Internal Iterationartists.stream() .filter(artist -> artist.isFrom("London")) .count();
![Page 23: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/23.jpg)
map
![Page 24: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/24.jpg)
mapList<String> collected = Stream.of("a", "b", "hello") .map(string -> string.toUpperCase()) .collect(toList());
assertEquals(asList("A", "B", "HELLO"), collected);
![Page 25: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/25.jpg)
reduce
![Page 26: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/26.jpg)
reduceint sum = Stream.of(1, 2, 3, 4) .reduce(0, (acc, x) -> acc + x);
assertEquals(10, sum);
![Page 27: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/27.jpg)
filter
![Page 28: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/28.jpg)
filterList<String> beginningWithNumbers = Stream.of("a", "1abc", "abc1") .filter(value -> isDigit(value.charAt(0))) .collect(toList());
assertEquals(asList("1abc"), beginningWithNumbers);
![Page 29: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/29.jpg)
Putting it all togetherfor a given an album, find the nationality of every band
playing on that album
![Page 30: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/30.jpg)
Putting it all together (2)1. transform an album into its artists2. figure out which artists are bands3. find the nationalities of each band
![Page 31: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/31.jpg)
Putting it all together (3)List<String> origins = album.getMusicians() .filter(artist -> artist.getName().startsWith("The")) .map(artist -> artist.getNationality()) .collect(toList());
![Page 32: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/32.jpg)
Method Referencesstr -> str.lengthString::length
x -> foo.bar(x)foo::bar
str -> new Name(str)Name::new
![Page 33: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/33.jpg)
Beyond theMyths
![Page 34: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/34.jpg)
Claim: Syntax is the mostimportant thing aboutLambda Expressions
![Page 35: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/35.jpg)
Yeah, I liked the # syntax proposal better,too. One less character to type! :)
![Page 36: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/36.jpg)
Have you considered 'default null'? It willsave a keyword
![Page 37: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/37.jpg)
How about a single punctuation mark,currently unused, as syntax sugar for "()->".
![Page 38: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/38.jpg)
(_, _) -> _ + _
This is starting to look like risque ASCII art :)
![Page 39: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/39.jpg)
Its a Myth!
![Page 40: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/40.jpg)
Claim: Syntax is irrelevant
![Page 41: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/41.jpg)
// Originally invalidStream.of(1, 2, 3) .forEach(x -> System.out.println(x));
// Required extra ;Stream.of(1, 2, 3) .forEach(x -> System.out.println(x););
![Page 42: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/42.jpg)
Difference betweenexpectations
Many language features stolen! adapted
Missing Features
Stronger Type System
Tuples
List construction syntax
![Page 43: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/43.jpg)
Framing EffectDifferent reactions depending on whether something is
presented as a loss or a gain.
![Page 44: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/44.jpg)
Recall our earlier exampleList<String> origins = album.getMusicians() .filter(artist -> artist.getName().startsWith("The")) .map(artist -> artist.getNationality()) .collect(toList());
![Page 45: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/45.jpg)
Eager vs Lazy (2) album.getMusicians() .filter(artist -> artist.getName().startsWith("The")) .map(artist -> artist.getNationality())
// What's happened? .collect(toList());
![Page 46: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/46.jpg)
Very little TestingMaybe ...
a reflection on popularity of TDD
spikes are good for learning
unfamiliarity with testing lambdas
![Page 47: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/47.jpg)
How do I test this?list.stream() .map(x -> 1.0 / Math.ceil(1 + Math.pow(x) + Math.atan2(y, x))) .collect(toList());
![Page 48: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/48.jpg)
Approach 1: Testsurrounding method
Don't test the lambda
Test the method its surrounded by
Works well for simple lambdas
![Page 49: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/49.jpg)
Approach 2: ExtractMethod
double complexFunction(double x) { return 1.0 / Math.ceil(1 + Math.pow(x) + Math.atan2(0, x));}
list.stream() .map(this::complexFunction) .collect(toList());
![Page 50: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/50.jpg)
Mistake: debugging// Streamslist.stream() .filter(filteringFunction) .map(mappingFunction) .collect(toList());
// Ye olde for loopList<Bar> bars = new ArrayList<>();for (Foo element : list) { if (filteringFunction(element) { Bar result = mappingFunction(element); bars.add(result); }}
![Page 51: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/51.jpg)
peeklist.stream() .filter(filteringFunction) .peek(e -> System.out.println("Filtered value: " + e)); .map(mappingFunction) .map(e -> e); .collect(toList());
![Page 52: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/52.jpg)
Compiler Error Messages
![Page 53: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/53.jpg)
ComparatorsComparator<String> comparator = comparing(String::length);
Comparator<String> comparator = comparing(str -> str.length);
![Page 54: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/54.jpg)
Compiler Errorjava: reference to comparing is ambiguous bothmethod<T>comparing(java.util.function.ToIntFunction< ? super T>)in java.util.Comparator and method<T,U>comparing(java.util.function.Function< ? super T,? extends U>)in java.util.Comparator match
![Page 55: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/55.jpg)
What happened?// Generic object variantpublic static <T, U extends Comparable< ? super U>> Comparator<T> comparing(Function< ? super T, ? extends U> keyExtractor)
// Specialised primitive variantpublic static <T> Comparator<T> comparing(ToIntFunction< ? super T> keyExtractor)
![Page 56: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/56.jpg)
SummarySyntax important, but not in the way
people think
New approaches for debugging and
testing
Take care of overloads and compiler error
messages
![Page 57: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/57.jpg)
FunctionalThinking
![Page 58: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/58.jpg)
FunctionalThinking?
Thinking in terms of the input to output relationship and nota sequence of steps
![Page 59: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/59.jpg)
First code that peoplewrite
List<Integer> numbers = Arrays.asList(1, 2, 3);numbers.forEach(x -> { System.out.println(x);});
![Page 60: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/60.jpg)
Non-idiomatic ProposalsEg: capture non-final local variables
![Page 61: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/61.jpg)
Example ProblemCount the number of instances of each word in a document.
![Page 62: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/62.jpg)
Ideal Solutionreader.lines() .flatMap(s -> s.splitAsStream(" ")) .collect(groupingBy(s -> s, counting()));
![Page 63: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/63.jpg)
Ideal Solution (then)reader.lines() .flatMap(s -> Stream.of(s.split(" "))) .collect(groupingBy(s -> s, reducing(s -> 1, Integer::sum)));
// Map entries for "dad"// [ "dad", "dad", "dad" ] => [1, 1, 1] => 3
![Page 64: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/64.jpg)
Bad Solution (Part 1)Map<String, List<String>> initial = br.lines() .flatMap(s -> Arrays.stream(s.split(" "))) .collect(groupingBy(s -> s)); Map<Map.Entry<String, Integer>, Integer> freq1 = initial .entrySet().stream() .map(entry -> new AbstractMap.SimpleImmutableEntry<String,Integer>(entry.getKey(), entry.getValue().size())) .collect(Collectors.toMap(entry -> entry.getValue()));
![Page 65: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/65.jpg)
Bad Solution (Part 2) Supplier<HashMap<String, Integer>> supplier = () -> newHashMap<String, Integer>(); BiConsumer<HashMap<String, Integer>, Map.Entry<String, Integer>> accum = (HashMap<String, Integer> result, Map.Entry<String, Integer>entry) -> result.put(entry.getKey(), entry.getValue()); BiConsumer<HashMap<String, Integer>, HashMap<String, Integer>>merger = HashMap::putAll;
Map<String, Integer> freq2 = initial.entrySet().stream() .map(entry -> new AbstractMap.SimpleImmutableEntry<String,Integer>(entry.getKey(), entry.getValue().size())) .collect(supplier, accum, merger);
![Page 66: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/66.jpg)
This takes thought
![Page 67: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/67.jpg)
SummaryIdioms are vital
Not Java specific at all
Requires Practise
![Page 68: Lambdas: Myths and Mistakes · Lambdas: Myths and Mistakes by Richard Warburton. the critical design tool for software ... Craig Larman. Talk Structure Why am I talking about this?](https://reader035.vdocuments.us/reader035/viewer/2022062602/5ed68328ff0e593c0b63fb66/html5/thumbnails/68.jpg)
ConclusionsGone through a bunch of examples of
specific issues
‘Functional Thinking’: Not necessary to
start learning.
Try before you buy