Skip to content

Commit

Permalink
11b - interfaces, lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Benov committed Nov 21, 2023
1 parent 5c8c7f5 commit 982c279
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface Foo {
void bar();
}
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;
}
});
}
}
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();
}
}
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 materials/2023-2024/11b/2023-11-20-lambdas/lambdas/lambdas.iml
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 materials/2023-2024/11b/2023-11-20-lambdas/lambdas/src/Main.java
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));
}
}
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;
}
}
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);
}

0 comments on commit 982c279

Please sign in to comment.