Skip to content

Commit 01c5965

Browse files
committed
Day 1 Final
1 parent 812e617 commit 01c5965

4 files changed

Lines changed: 89 additions & 9 deletions

File tree

Capture 8.png

3.38 MB
Loading

src/main/java/students2/School.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
import java.util.ArrayList;
77
import java.util.Arrays;
88
import java.util.List;
9+
import java.util.function.Consumer;
910

11+
@FunctionalInterface
1012
interface Criterion<E> {
1113
boolean test(E s);
14+
// void doStuff();
1215
}
16+
//
17+
//interface Something<E> {
18+
// void doSomething(E e);
19+
//}
1320

14-
interface Something<E> {
15-
void doSomething(E e);
16-
}
17-
18-
class PrintSomething implements Something<Student> {
21+
//class PrintSomething implements Something<Student> {
22+
class PrintSomething implements Consumer<Student> {
1923
@Override
20-
public void doSomething(Student student) {
24+
// public void doSomething(Student student) {
25+
public void accept(Student student) {
2126
System.out.println("> " + student);
2227
}
2328
}
@@ -29,9 +34,11 @@ public class School {
2934
// }
3035
// System.out.println("---------------------------");
3136
// }
32-
public static <E> void withEvery(Iterable<E> ls, Something<E> someObject) {
37+
// public static <E> void withEvery(Iterable<E> ls, Something<E> someObject) {
38+
public static <E> void withEvery(Iterable<E> ls, Consumer<E> someObject) {
3339
for (E s : ls) {
34-
someObject.doSomething(s);
40+
// someObject.doSomething(s);
41+
someObject.accept(s);
3542
}
3643
}
3744

@@ -58,7 +65,7 @@ public static void main(String[] args) {
5865
// });
5966

6067
// show smart students
61-
// showAll(getByCriterion(roster, ???));
68+
withEvery(getByCriterion(roster, s -> s.getGpa() > 3.0), s -> System.out.println(s));
6269

6370
// show unenthusiastic students
6471
// showAll(getByCriterion(roster, ???));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package superiterable;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.function.Function;
7+
import java.util.function.Predicate;
8+
9+
public class SuperIterable<E> implements Iterable<E> {
10+
private Iterable<E> self;
11+
12+
public SuperIterable(Iterable<E> self) {
13+
this.self = self;
14+
}
15+
16+
// public void withEvery(SuperIterable<E> this, Consumer<E> someObject) {
17+
// This is "forEach"
18+
// public void withEvery(Consumer<E> someObject) {
19+
// for (E s : this.self) {
20+
// someObject.accept(s);
21+
// }
22+
// }
23+
24+
public SuperIterable<E> filter(SuperIterable<E>this, Predicate<E> crit) {
25+
List<E> res = new ArrayList<>();
26+
for (E s : this.self) {
27+
if (crit.test(s)) {
28+
res.add(s);
29+
}
30+
}
31+
return new SuperIterable<>(res);
32+
}
33+
34+
35+
// This type of wrapper (if it conforms to some math rules)
36+
// this is called (in functional programming) a "functor"
37+
public <F> SuperIterable<F> map(Function<E, F> op) {
38+
List<F> res = new ArrayList<>();
39+
for (E s : this.self) {
40+
F f = op.apply(s);
41+
res.add(f);
42+
}
43+
return new SuperIterable<>(res);
44+
}
45+
46+
@Override
47+
public Iterator<E> iterator() {
48+
return self.iterator();
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package superiterable;
2+
3+
import java.util.List;
4+
5+
public class UseSuperIterable {
6+
public static void main(String[] args) {
7+
SuperIterable<String> sis = new SuperIterable<>(List.of("Fred", "Jim", "Sheila"));
8+
for (String s : sis) {
9+
System.out.println("> " + s);
10+
}
11+
12+
// sis.withEvery(s -> System.out.println("I found the name: " + s));
13+
sis.forEach(s -> System.out.println("I found the name: " + s));
14+
15+
// SuperIterable<String> longs = sis.getByCriterion(s -> s.length() > 3);
16+
// longs.withEvery(s -> System.out.println("Long: " + s));
17+
18+
sis
19+
.filter(s -> s.length() > 3)
20+
.map(s -> s.toUpperCase())
21+
.forEach(s -> System.out.println("Long: " + s));
22+
}
23+
}

0 commit comments

Comments
 (0)