wip
This commit is contained in:
committed by
Yadunand Prem
parent
4e99493aa5
commit
f5a9db22d1
@@ -11,5 +11,5 @@
|
||||
*/
|
||||
|
||||
interface Action<T> {
|
||||
void call(T item)
|
||||
void call(T item);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,10 @@
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Put Your Name (Lab Group)
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
// act consumes the T and passes it to action, thus super
|
||||
interface Actionable<T> {
|
||||
public void act(Action<? super T> action);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,9 @@
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Put Your Name (Lab Group)
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
interface Immutator<R,P> {
|
||||
public R invoke(P param);
|
||||
}
|
||||
|
||||
@@ -8,5 +8,9 @@
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Put Your Name (Lab Group)
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
interface Immutatorable<T> {
|
||||
public <R> R transform(Immutator<R, ? super T> immutator);
|
||||
}
|
||||
|
||||
@@ -7,3 +7,10 @@
|
||||
*
|
||||
* @author Put Your Name (Lab Group)
|
||||
*/
|
||||
|
||||
class Improbable<T> implements Immutator<Probably<T>, T> {
|
||||
@Override
|
||||
public Probably<T> invoke(T param) {
|
||||
return Probably.just(param);
|
||||
}
|
||||
}
|
||||
|
||||
10
Lab4/MyTest.java
Normal file
10
Lab4/MyTest.java
Normal file
@@ -0,0 +1,10 @@
|
||||
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;
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@
|
||||
* @author Put Your Name (Lab Group)
|
||||
*/
|
||||
|
||||
class Print<T> implements Action<T> {
|
||||
class Print implements Action<Object> {
|
||||
|
||||
@Override
|
||||
public void call(T item) {
|
||||
public void call(Object item) {
|
||||
System.out.println(item);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
* we may return something that contains nothing
|
||||
* where the nothing is a null.
|
||||
*
|
||||
* @author XXX
|
||||
* @author Yadunand Prem (10B)
|
||||
* @version CS2030S AY22/23 Semester 1
|
||||
*/
|
||||
class Probably<T> {
|
||||
class Probably<T> implements Actionable<T>, Immutatorable<T> {
|
||||
private final T value;
|
||||
|
||||
private static final Probably<?> NONE = new Probably<>(null);
|
||||
@@ -50,7 +50,7 @@ class Probably<T> {
|
||||
if (value == null) {
|
||||
return none();
|
||||
}
|
||||
return (Probably<T>) new Probably<>(value);
|
||||
return new Probably<>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,4 +93,19 @@ class Probably<T> {
|
||||
return "<" + this.value.toString() + ">";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void act(Action<? super T> action) {
|
||||
if (this.value == null) {
|
||||
return;
|
||||
}
|
||||
action.call(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Probably<R> transform(Immutator<Probably<R>, ? super T> immutator) {
|
||||
return immutator.invoke(this.value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -32,9 +32,6 @@ class Test3 {
|
||||
we.expect("new Improbable<String>().invoke(null)",
|
||||
new Improbable<String>().invoke(null).toString(),
|
||||
"<>");
|
||||
we.expect("new Improbable<Integer>().invoke(1).transform(new Incr())",
|
||||
new Improbable<Integer>().invoke(1).transform(new Incr()).toString(),
|
||||
"<2>");
|
||||
we.expect("new Improbable<>().invoke(new Improbable<>().invoke(1))",
|
||||
new Improbable<>().invoke(new Improbable<Integer>().invoke(1)).toString(),
|
||||
"<<1>>");
|
||||
|
||||
Reference in New Issue
Block a user