Skip to content

Commit 7e9bd96

Browse files
committed
lambdas
1 parent 655ae66 commit 7e9bd96

20 files changed

+863
-42
lines changed

src/SUMMARY.md

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
- [w3schools](./examples/w3schools.md)
1616
1717
Project ideas:
18-
calorie tracker
1918
tic tac toe
2019
chess
2120
go
22-
CSV
23-
image stuff with the PPM format
2421
2522
swing game - maybe have a simplified game engine
2623
@@ -37,7 +34,6 @@ airplane physics program
3734
ball throw physics program
3835
chemical stuff
3936
40-
(after bytes) Make an audio file. Play hot cross buns.
4137
4238
snake
4339
2048
@@ -66,7 +62,6 @@ HTML
6662
* Cursor Objects (sql)
6763
* Exception Hierarchies
6864
* Interior Iteration
69-
* Annotations
7065
7166
Make your own iterator
7267
@@ -88,13 +83,22 @@ Have a project that is just klarna but the your goal is "never write this progra
8883
- [Journal Entries]()
8984
9085
sealed interfaces
86+
lambdas
9187
9288
easy vs. simple
9389
9490
enshittification state machine example (Acquire -> Make good for business bad for users -> Bad for everyone)
9591
9692
Zombie Game
9793
94+
try to trick someone into making that.
95+
LegBeanProviderFactory
96+
97+
98+
stuff in mooc i do not have
99+
Streams
100+
Intro to testing
101+
Gui stuff
98102
-->
99103

100104
# Modern Java
@@ -438,6 +442,12 @@ Zombie Game
438442
- [Inferred Types](./global_fields/inferred_types.md)
439443
- [Challenges](./global_fields/challenges.md)
440444

445+
# Projects
446+
447+
<!-- chicken nugget number type things come up when dispensing change -->
448+
- [Tic-Tac-Toe]()
449+
450+
441451
# Concepts
442452

443453
- [Code is Read more than Written](./code_is_read_more_than_written.md)
@@ -735,7 +745,7 @@ Make them do one. -->
735745
- [Inferred Types](./loops_iii/inferred_types.md)
736746
- [Challenges](./loops_iii/challenges.md)
737747

738-
# Concepts II
748+
# Concepts III
739749

740750
- [Encapsulation](./encapsulation.md)
741751
- [Implementation Details](./encapsulation/implementation_details.md)
@@ -820,6 +830,13 @@ Make them do one. -->
820830
- [Final Classes](./class_extension/final_classes.md)
821831
- [Challenges](./class_extension/challenges.md)
822832

833+
<!--
834+
# Concepts IV
835+
836+
- [Easy vs. Simple]()
837+
- [Read the Docs]()
838+
-->
839+
823840

824841
# Data Types IX
825842

@@ -849,6 +866,21 @@ Make them do one. -->
849866
- [Multi-Module Directory Layout](./modules/multi_module_directory_layout.md)
850867
- [Purpose](./modules/purpose.md)
851868
- [Challenges](./modules/challenges.md)
869+
870+
- [Lambdas](./lambdas.md)
871+
- [Functional Interfaces](./lambdas/functional_interfaces.md)
872+
- [@FunctionalInterface](./lambdas/functional_interface_annotation.md)
873+
- [Lambda Expressions](./lambdas/lambda_expressions.md)
874+
- [Arguments](./lambdas/arguments.md)
875+
- [Return](./lambdas/return.md)
876+
- [Method References](./lambdas/method_references.md)
877+
- [Inference](./lambdas/inference.md)
878+
- [Built-In Functional Interfaces](./lambdas/built_in_functional_interfaces.md)
879+
- [Runnable](./lambdas/runnable.md)
880+
- [Function](./lambdas/function.md)
881+
- [Checked Exceptions](./lambdas/checked_exceptions.md)
882+
- [Challenges](./lambdas/challenges.md)
883+
852884
<!--
853885
- [Lambdas](./lambdas.md)
854886
- [Functional Interfaces](./lambdas/functional_interfaces.md)
@@ -911,6 +943,51 @@ TODO: Wait for hermetic java
911943
- [javadoc](./documentation/javadoc.md)
912944
- [Challenges](./documentation/challenges.md)
913945

946+
# Data Types X
947+
948+
- [Streams 🚧](./streams.md)
949+
- [Purpose](./streams/purpose.md)
950+
- [stream](./streams/stream.md)
951+
- [map](./streams/map.md)
952+
- [filter](./streams/filter.md)
953+
- [Collectors](./streams/collectors.md)
954+
- [toList](./streams/toList.md)
955+
- [Challenges](./streams/challenges.md)
956+
<!-- - [Regular Expressions 🚧]()
957+
- [Strings III 🚧]()
958+
959+
960+
# Code Structure IX
961+
962+
- [Sealed Interfaces 🚧]()
963+
- [permits]()
964+
- [non-sealed]()
965+
- [Reflection]()
966+
- [Generics II 🚧]()
967+
- [Bounds]()
968+
- [Wildcards]()
969+
- [Invariance]()
970+
971+
# Control Flow VI
972+
973+
- [Switch IV 🚧]()
974+
- [Patterns]()
975+
- [when]()
976+
977+
# Testing
978+
979+
- [Manual Testing 🚧]()
980+
- [Logging 🚧]()
981+
982+
# Concurrent Programs
983+
984+
- [Threads 🚧]()
985+
- [UncaughtExceptionHandler]()
986+
- [Races]()
987+
988+
# Concepts IV
989+
990+
- []() -->
914991

915992
<!---
916993
PROJECT IDEAS:
@@ -925,9 +1002,6 @@ PROJECT IDEAS:
9251002
9261003
9271004
928-
[HashMap](./hash_map.md)
929-
930-
9311005
9321006
# Code Structure V
9331007
- [Interface Extension](./interface_extension.md)
@@ -938,12 +1012,6 @@ PROJECT IDEAS:
9381012
9391013
# Building
9401014
941-
- [Compilation]()
942-
- [Compile Java Code]()
943-
- [Compile a List of Files]()
944-
- [Specify Where to Find Sources]()
945-
- [Enable Debug Info]()
946-
- [Package Java Code]()
9471015
- [Consume External Libraries]()
9481016
9491017
# Data Types VI
@@ -982,11 +1050,6 @@ PROJECT IDEAS:
9821050
9831051
9841052
985-
# Concepts
986-
987-
988-
# Concepts
989-
9901053
9911054
- [The Idiot Game](./the_idiot_game.md)
9921055
@@ -1009,20 +1072,14 @@ PROJECT IDEAS:
10091072
10101073
# Data Types VI
10111074
1012-
- [Integers II]()
10131075
- [Paths]()
10141076
- [Exceptions II]()
10151077
10161078
# Control Flow IV
10171079
10181080
# Algorithms II
10191081
1020-
# Data Types VII
10211082
1022-
- [Maps]()
1023-
- [HashMap]()
1024-
- [Sets]()
1025-
- [HashSet]()
10261083
10271084
<!--
10281085
@@ -1046,27 +1103,20 @@ Maven
10461103
XML
10471104
Pom
10481105
Multiversal Equality
1049-
Reflection
1050-
Cover Invoking *public* constructs
1051-
Javadoc and Documentation comments
10521106
-->
10531107

10541108
<!--
10551109
# Legacy Java
10561110
- [Legacy Java]()
10571111
- [Prelude]()
10581112
- [Subpar]()
1059-
- [Short]()
10601113
- [Scanner]()
10611114
- [File]()
10621115
- [Swing]()
10631116
- [Date]()
10641117
- [Crusty]()
10651118
- [Vector]()
10661119
- [Hashtable]()
1067-
- [Already Removed]()
1068-
- [Applets]()
1069-
- [Finalization]()
10701120
-->
10711121

10721122
<!--

src/interfaces/challenges.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,28 @@ interface Tarot {
8484
String standUser();
8585
}
8686
```
87+
88+
## Challenge 5.
89+
90+
Make a method named `promptGeneric` which can prompt the user for information
91+
but, based on if what they typed is properly interpretable, can reprompt them.
92+
93+
As part of this make a `Parser` interface and at least two implementations: `IntParser`
94+
and `DoubleParser`.
95+
96+
```java
97+
// CODE HERE
98+
99+
class Main {
100+
// CODE HERE
101+
102+
void main() {
103+
int x = promptGeneric(
104+
"Give me an x: ", new IntParser()
105+
);
106+
double y = promptGeneric(
107+
"Give me a floating point y: ", new DoubleParser()
108+
);
109+
}
110+
}
111+
```

src/lambdas.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
# Lambdas
22

3+
<img src="/lambdas/header.png" height="200px"/>
34

5+
6+
Making an implementation of `interface`s that
7+
only require one method turns out to be a common
8+
task for a Java developer.
9+
10+
```java,no_run
11+
interface Band {
12+
void playHitSong();
13+
}
14+
15+
class Starcadian implements Bank {
16+
@Override
17+
public void playHitSong() {
18+
IO.println("ultralove");
19+
}
20+
}
21+
```
22+
23+
As such there is a mechanism called "lambdas"
24+
which lowers the effort required to do so.
25+
26+
```java
27+
interface Band {
28+
void playHitSong();
29+
}
30+
31+
class Main {
32+
void main() {
33+
Band starcadian = () -> IO.println("ultralove");
34+
starcadian.playHitSong();
35+
}
36+
}
37+
```
42.2 KB
Loading

src/lambdas/arguments.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Arguments
2+
3+
If the method on the functional interface takes arguments
4+
you can include those in the parentheses before the `->`.
5+
6+
```java
7+
@FunctionalInterface
8+
interface Singer {
9+
void sing(String title, double volume);
10+
}
11+
12+
class Main {
13+
void main() {
14+
Singer woodkid = (String title, double volume) -> {
15+
IO.println(
16+
title
17+
+ " by woodkid is now "
18+
+ (volume > 10 ? "BLASTING" : "playing"
19+
)
20+
);
21+
};
22+
23+
// AC Revelations wasn't the best game
24+
// but that trailer was awesome.
25+
woodkid.sing("Iron", 11);
26+
}
27+
}
28+
```
29+
30+
If it won't lead to any ambiguity Java also allows you to omit the types
31+
from the argument list.
32+
33+
```java
34+
@FunctionalInterface
35+
interface Singer {
36+
void sing(String title, double volume);
37+
}
38+
39+
class Main {
40+
void main() {
41+
Singer twrp = (title, volume) -> {
42+
IO.println(
43+
title
44+
+ " by twrp is now "
45+
+ (volume > 10 ? "BLASTING" : "playing"
46+
)
47+
);
48+
};
49+
50+
woodkid.sing("Pets", 7);
51+
}
52+
}
53+
```
54+
55+
Further, if there is only one argument you may omit even the `()`.
56+
57+
```java
58+
@FunctionalInterface
59+
interface Conductor {
60+
void conduct(String tempo);
61+
}
62+
63+
class Main {
64+
void main() {
65+
Conductor jkSimmons = tempo -> IO.println("Not my tempo.")
66+
67+
jkSimmons.conduct("4/4");
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)