Difference between revisions of "Lambda Demos"
Jump to navigation
Jump to search
(13 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
=Code To Investigate= | =Code To Investigate= | ||
− | == | + | ==fork join== |
− | <nowiki> | + | <nowiki>System.out.println("start"); |
− | |||
− | + | Future<Void> apples = void_fork(() -> { | |
− | + | sleepRandom(1_000); | |
− | + | System.out.println("-apples"); | |
− | + | }); | |
− | + | Future<Void> oranges = void_fork(() -> { | |
− | + | sleepRandom(1_000); | |
− | + | System.out.println("--oranges"); | |
− | + | }); | |
− | + | Future<Void> bananas = void_fork(() -> { | |
− | + | sleepRandom(1_000); | |
− | + | System.out.println("---bananas"); | |
− | + | }); | |
− | + | Future<Void> mangoes = void_fork(() -> { | |
− | + | sleepRandom(1_000); | |
− | + | System.out.println("----mangoes"); | |
− | + | }); | |
− | |||
− | |||
− | + | join(apples, oranges, bananas, mangoes); | |
− | |||
− | + | System.out.println("stop");</nowiki> | |
− | {{ | + | {{CodeToInvestigate|LambdasForkJoinExample|main|lambda.forkjoin.demo|demo}} |
− | {{ | + | {{CodeToInvestigate|AnonymousInnerClassesForkJoinExample|main|lambda.forkjoin.demo|demo}} |
+ | |||
+ | {{CodeToInvestigate|NamedClassesForkJoinExample|main|lambda.forkjoin.demo|demo}} | ||
+ | |||
+ | ==x10.finish== | ||
+ | ==fork join== | ||
+ | <nowiki>X10 x10 = new ForkJoinX10(); | ||
+ | System.out.println("start"); | ||
+ | |||
+ | x10.void_finish(() -> { | ||
+ | void_fork(() -> { | ||
+ | sleepRandom(1_000); | ||
+ | System.out.println("-apples"); | ||
+ | }); | ||
+ | void_fork(() -> { | ||
+ | sleepRandom(1_000); | ||
+ | System.out.println("--oranges"); | ||
+ | }); | ||
+ | void_fork(() -> { | ||
+ | sleepRandom(1_000); | ||
+ | System.out.println("---bananas"); | ||
+ | }); | ||
+ | void_fork(() -> { | ||
+ | sleepRandom(1_000); | ||
+ | System.out.println("----mangoes"); | ||
+ | }); | ||
+ | }); | ||
+ | |||
+ | System.out.println("stop");</nowiki> | ||
+ | {{CodeToInvestigate|LambdasFinishExample|main|lambda.x10.demo|demo}} | ||
+ | |||
+ | {{CodeToInvestigate|AnonymousInnerClassesFinishExample|main|lambda.x10.demo|demo}} | ||
+ | |||
+ | {{CodeToInvestigate|NamedClassesFinishExample|main|lambda.x10.demo|demo}} | ||
==compute== | ==compute== | ||
− | {{ | + | <nowiki>String text = "abracadabra"; |
+ | // NOTE: we use a TreeMap<K,V> since it is sorted producing consistent output | ||
+ | Map<Character, Integer> mapCharacterToCount = new TreeMap<>(); | ||
+ | for( char ch : text.toCharArray() ) { | ||
+ | // https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- | ||
+ | mapCharacterToCount.compute(ch, (Character character, Integer count)-> { | ||
+ | if( count != null ) { | ||
+ | //we have associated character before | ||
+ | return count+1; | ||
+ | } else { | ||
+ | //first encounter with character | ||
+ | return 1; | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | for( Entry<Character, Integer> entry : mapCharacterToCount.entrySet() ) { | ||
+ | System.out.println(entry); | ||
+ | }</nowiki> | ||
+ | |||
+ | {{CodeToInvestigate|LambdasMapComputeExample|demo|lambda.demo.compute}} | ||
− | {{ | + | {{CodeToInvestigate|AnonymousInnerClassesMapComputeExample|demo|lambda.demo.compute}} |
− | {{ | + | {{CodeToInvestigate|NamedClassesMapComputeExample|demo|lambda.demo.compute}} |
==user interface== | ==user interface== | ||
+ | [[File:LambdaViz.png]] | ||
− | {{Viz| | + | <nowiki>buttonI.addActionListener((ActionEvent e) -> { |
+ | System.out.println(">>> entering lambda"); | ||
+ | System.out.println(" " + accessible_within_lambda_i | ||
+ | + " (note: runtime stack with local variable i long since popped)"); | ||
+ | System.out.println("<<< leaving lambda"); | ||
+ | System.out.println(); | ||
+ | // Thread.dumpStack(); | ||
+ | });</nowiki> | ||
+ | {{Viz|LambdaDemoApp|lambda.demo.viz|demo}} |
Latest revision as of 06:20, 12 February 2022
Contents
Motivation
JDK 8 added lambdas to alleviate the bulkiness of anonymous inner classes. We use lambdas heavily in 231.
Background
Code To Investigate
fork join
System.out.println("start"); Future<Void> apples = void_fork(() -> { sleepRandom(1_000); System.out.println("-apples"); }); Future<Void> oranges = void_fork(() -> { sleepRandom(1_000); System.out.println("--oranges"); }); Future<Void> bananas = void_fork(() -> { sleepRandom(1_000); System.out.println("---bananas"); }); Future<Void> mangoes = void_fork(() -> { sleepRandom(1_000); System.out.println("----mangoes"); }); join(apples, oranges, bananas, mangoes); System.out.println("stop");
class: | LambdasForkJoinExample.java | DEMO: |
methods: | main | |
package: | lambda.forkjoin.demo | |
source folder: | src/demo/java |
class: | AnonymousInnerClassesForkJoinExample.java | DEMO: |
methods: | main | |
package: | lambda.forkjoin.demo | |
source folder: | src/demo/java |
class: | NamedClassesForkJoinExample.java | DEMO: |
methods: | main | |
package: | lambda.forkjoin.demo | |
source folder: | src/demo/java |
x10.finish
fork join
X10 x10 = new ForkJoinX10(); System.out.println("start"); x10.void_finish(() -> { void_fork(() -> { sleepRandom(1_000); System.out.println("-apples"); }); void_fork(() -> { sleepRandom(1_000); System.out.println("--oranges"); }); void_fork(() -> { sleepRandom(1_000); System.out.println("---bananas"); }); void_fork(() -> { sleepRandom(1_000); System.out.println("----mangoes"); }); }); System.out.println("stop");
class: | LambdasFinishExample.java | DEMO: |
methods: | main | |
package: | lambda.x10.demo | |
source folder: | src/demo/java |
class: | AnonymousInnerClassesFinishExample.java | DEMO: |
methods: | main | |
package: | lambda.x10.demo | |
source folder: | src/demo/java |
class: | NamedClassesFinishExample.java | DEMO: |
methods: | main | |
package: | lambda.x10.demo | |
source folder: | src/demo/java |
compute
String text = "abracadabra"; // NOTE: we use a TreeMap<K,V> since it is sorted producing consistent output Map<Character, Integer> mapCharacterToCount = new TreeMap<>(); for( char ch : text.toCharArray() ) { // https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- mapCharacterToCount.compute(ch, (Character character, Integer count)-> { if( count != null ) { //we have associated character before return count+1; } else { //first encounter with character return 1; } }); } for( Entry<Character, Integer> entry : mapCharacterToCount.entrySet() ) { System.out.println(entry); }
class: | LambdasMapComputeExample.java | DEMO: |
methods: | demo | |
package: | lambda.demo.compute | |
source folder: | src//java |
class: | AnonymousInnerClassesMapComputeExample.java | DEMO: |
methods: | demo | |
package: | lambda.demo.compute | |
source folder: | src//java |
class: | NamedClassesMapComputeExample.java | DEMO: |
methods: | demo | |
package: | lambda.demo.compute | |
source folder: | src//java |
user interface
buttonI.addActionListener((ActionEvent e) -> { System.out.println(">>> entering lambda"); System.out.println(" " + accessible_within_lambda_i + " (note: runtime stack with local variable i long since popped)"); System.out.println("<<< leaving lambda"); System.out.println(); // Thread.dumpStack(); });
class: | LambdaDemoApp.java | VIZ |
package: | lambda.demo.viz | |
source folder: | student/src/demo/java |