-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mario Benov
committed
Nov 21, 2023
1 parent
5c8c7f5
commit 982c279
Showing
8 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
materials/2023-2024/11b/2023-11-17-interfaces/untitled/src/Foo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface Foo { | ||
void bar(); | ||
} |
58 changes: 58 additions & 0 deletions
58
materials/2023-2024/11b/2023-11-17-interfaces/untitled/src/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
public class Main { | ||
public static class Baz implements Foo { | ||
@Override | ||
public void bar() { | ||
System.out.println("Baz::bar()"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Foo f1 = new Baz(); | ||
f1.bar(); | ||
|
||
f1 = new Foo() { | ||
@Override | ||
public void bar() { | ||
System.out.println("Foo::bar()"); | ||
} | ||
}; | ||
f1.bar(); | ||
|
||
MyClass mc = MyClass.foo(); | ||
|
||
|
||
|
||
|
||
|
||
SingleArgWithResult myVar = new SingleArgWithResult() { | ||
@Override | ||
public int process(int arg) { | ||
return arg * 2; | ||
} | ||
}; | ||
Map<Integer, Integer[]> res = NumberProcessor | ||
.fromArray(arr) | ||
// .map(myVar) | ||
// .map(new MyClass()) | ||
.map(new SingleArgWithResult() { | ||
... | ||
}) | ||
.filter(new SingleArgWithResult() { | ||
@Override | ||
public int process(int arg) { | ||
return arg > 2 ? 1 : 0; | ||
// if(arg > 2) { | ||
// return 1; | ||
// } else { | ||
// return 0; | ||
// } | ||
} | ||
}) | ||
.groupBy(new SingleArgWithResult() { | ||
@Override | ||
public int process(int arg) { | ||
return arg % 5; | ||
} | ||
}); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
materials/2023-2024/11b/2023-11-17-interfaces/untitled/src/MyClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class MyClass { | ||
private MyClass() {} | ||
|
||
static MyClass foo(int arg) { | ||
return new MyClass(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
materials/2023-2024/11b/2023-11-17-interfaces/untitled/untitled.iml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
11 changes: 11 additions & 0 deletions
11
materials/2023-2024/11b/2023-11-20-lambdas/lambdas/lambdas.iml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
52 changes: 52 additions & 0 deletions
52
materials/2023-2024/11b/2023-11-20-lambdas/lambdas/src/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.ArrayList; | ||
import java.util.Map; | ||
import java.util.Vector; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
int[] arr = new int[] {2, 4, 5, 2, 3, 1}; | ||
|
||
// Map<Integer, Vector> res = NumberProcessor | ||
// .fromArray(arr) | ||
// .map(new SingleArgWithResult() { | ||
// @Override | ||
// public int process(int arg) { | ||
// return arg * 2; | ||
// } | ||
// }) | ||
// .map(new SingleArgWithResult() { | ||
// @Override | ||
// public int process(int arg) { | ||
// return arg + 2; | ||
// } | ||
// }) | ||
// .filter(new SingleArgWithResult() { | ||
// @Override | ||
// public int process(int arg) { | ||
// return arg % 3 == 0 ? 1 : 0; | ||
// } | ||
// }) | ||
// .groupBy(new SingleArgWithResult() { | ||
// @Override | ||
// public int process(int arg) { | ||
// return arg % 5; | ||
// } | ||
// }); | ||
|
||
Map<Integer, Vector> res = NumberProcessor | ||
.fromArray(arr) | ||
.map(arg -> (int) (arg * Math.pow(2, 3))) | ||
.map(arg -> arg + 2) | ||
.filter(arg -> arg % 3 == 0 ? 1 : 0) | ||
.map(arg -> { | ||
int c = arg; | ||
return c * 5; | ||
}) | ||
.groupBy(arg -> arg % 5); | ||
|
||
System.out.println(res); | ||
|
||
SingleArgWithResult foo = (arg) -> arg * 4; | ||
System.out.println(foo.process(123)); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
materials/2023-2024/11b/2023-11-20-lambdas/lambdas/src/NumberProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import java.util.*; | ||
|
||
public class NumberProcessor { | ||
private List<Integer> numbers; | ||
|
||
private NumberProcessor() {} | ||
|
||
public static NumberProcessor fromArray(int[] arr) { | ||
NumberProcessor np = new NumberProcessor(); | ||
|
||
np.numbers = new ArrayList<>(); | ||
for(int i : arr) { | ||
np.numbers.add(i); | ||
} | ||
|
||
return np; | ||
} | ||
|
||
public NumberProcessor map(SingleArgWithResult mapper) { | ||
// int r = mapper.process(23); | ||
|
||
// List<Integer> res = new ArrayList<>(); | ||
// for(int i : numbers) { | ||
// res.add(mapper.process(i)); | ||
// } | ||
// numbers = res; | ||
|
||
|
||
for(int i = 0; i < numbers.size(); i++ ) { | ||
// arr[i] = mapper.process(arr[i]) | ||
numbers.set( | ||
i, | ||
mapper.process( | ||
numbers.get(i) | ||
) | ||
); | ||
} | ||
|
||
// numbers.replaceAll(mapper::process); | ||
|
||
return this; | ||
} | ||
|
||
public NumberProcessor filter(SingleArgWithResult fn) { | ||
List<Integer> res = new ArrayList<>(); | ||
for(int i : numbers) { | ||
if(fn.process(i) == 1) | ||
res.add(i); | ||
} | ||
numbers = res; | ||
|
||
return this; | ||
} | ||
|
||
public Map<Integer, Vector> groupBy(SingleArgWithResult fn) { | ||
Map<Integer, Vector> res = new HashMap<>(); | ||
|
||
for(int i : numbers) { | ||
Integer key = fn.process(i); | ||
if(!res.containsKey(key)) { | ||
res.put(key, new Vector()); | ||
} | ||
|
||
res.get(key).add(i); | ||
} | ||
|
||
return res; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
materials/2023-2024/11b/2023-11-20-lambdas/lambdas/src/SingleArgWithResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@FunctionalInterface | ||
public interface SingleArgWithResult { | ||
int process(int arg); | ||
|
||
// int process(int arg, int arg2); | ||
} |