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 @@
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;
}
}));
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);
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,14 +8,14 @@
* @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);
/**
* Private constructor, can only be invoked inside.
* This is called a factory method. We can only
* This is called a factory method. We can only
* create this using the two public static method.
*
* @return The shared NOTHING.
@ -23,7 +23,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
private Probably(T value) {
this.value = value;
}
/**
* It is probably nothing, no value inside.
*
@ -34,7 +34,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
Probably<T> res = (Probably<T>) NONE;
return res;
}
/**
* It is probably just the given value.
* Unless the value is null, then nothing is
@ -42,7 +42,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
*
* @param value Probably this is the value
* unless it is null then we say
* that there is no
* that there is no
* @return The given value or nothing but
* never null.
*/
@ -52,7 +52,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
}
return new Probably<>(value);
}
/**
* Check for equality between something that
* is probably a value but maybe nothing.
@ -78,7 +78,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
}
return false;
}
/**
* String representation of something that
* is probably a value but maybe nothing.
@ -102,10 +102,33 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
action.call(this.value);
}
@Override
public <R> Probably<R> transform(Immutator<Probably<R>, ? super T> immutator) {
return immutator.invoke(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<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);
}
}