wip
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user