This commit is contained in:
Yadunand Prem
2022-09-19 19:46:42 +08:00
parent f5a9db22d1
commit 367a4e9ad4
5 changed files with 114 additions and 21 deletions

View File

@@ -8,5 +8,9 @@
* CS2030S Lab 4
* AY22/23 Semester 1
*
* @author Put Your Name (Lab Group)
* @author Yadunand Prem (10B)
*/
interface Applicable<T> {
<R> Applicable<R> apply(Immutator<Probably<R>, Probably<T>> immutator);
}

View File

@@ -12,5 +12,5 @@
*/
interface Immutatorable<T> {
public <R> R transform(Immutator<R, ? super T> immutator);
public <R> Immutatorable<R> transform(Immutator<R, T> immutator);
}

View File

@@ -10,3 +10,19 @@
*
* @author Put Your Name (Lab Group)
*/
class IsModEq implements Immutator<Boolean, Integer> {
private int div;
private int check;
public IsModEq(int div, int check) {
this.div = div;
this.check = check;
}
@Override
public Boolean invoke(Integer val) {
return val % div == check;
}
}

View File

@@ -1,10 +1,60 @@
interface Function<T, R> {
R apply(T t);
}
interface Functor<T, F extends Functor<?, ?>> {
<R> F map(Function<T, R> f);
}
class Identity<T> implements Functor<T, Identity<?>> {
private final T value;
public Identity(T value) {
this.value = value;
}
@Override
public <R> Identity<R> map(Function<T, R> f) {
final R result = f.apply(this.value);
return new Identity<>(result);
}
}
class Incr implements Immutator<Integer, Integer> {
@Override
public Integer invoke(Integer param) {
return param + 1;
}
}
class Length implements Immutator<Integer, String> {
@Override
public Integer invoke(String param) {
return param.length();
}
}
class Print implements Action<Object> {
@Override
public void call(Object item) {
System.out.println(item);
}
}
class MyTest {
public static void main(String[] args) {
Probably<Integer> maybeInt = Probably.just(10);
System.out.println( maybeInt.transform(new Immutator<Integer,Integer>() {
public Integer invoke(Integer t1) {
return t1+1;
}
}));
maybeInt.act(new Print());
System.out.println(Probably.just(2030).check(new IsModEq(0, 2)));
Identity<String> idString = new Identity<>("abc");
Identity<Integer> idInt = idString.map(String::length);
}
}

View File

@@ -8,7 +8,7 @@
* @author Yadunand Prem (10B)
* @version CS2030S AY22/23 Semester 1
*/
class Probably<T> implements Actionable<T>, Immutatorable<T> {
class Probably<T> implements Actionable<T>, Immutatorable<T>, Applicable<T> {
private final T value;
private static final Probably<?> NONE = new Probably<>(null);
@@ -102,10 +102,33 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
action.call(this.value);
}
public Probably<T> check(IsModEq eq) {
if (this.value == null) {
return none();
}
if (this.value instanceof Integer) {
Integer val = (Integer) this.value;
if (eq.invoke(val)) {
return this;
}
}
return none();
}
@Override
public <R> Probably<R> transform(Immutator<Probably<R>, ? super T> immutator) {
return immutator.invoke(this.value);
public <R> Probably<R> transform(Immutator<R, T> immutator) {
if (this.value == null) {
return none();
}
return just(immutator.invoke(this.value));
}
@Override
public <R> Probably<R> apply(Immutator<Probably<R>, Probably<T>> immutator) {
if (this.value == null) {
return none();
}
return immutator.invoke(this);
}
}