wip
This commit is contained in:
@@ -8,5 +8,9 @@
|
|||||||
* CS2030S Lab 4
|
* CS2030S Lab 4
|
||||||
* AY22/23 Semester 1
|
* 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> {
|
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)
|
* @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 {
|
interface Function<T, R> {
|
||||||
public static void main(String[] args) {
|
R apply(T t);
|
||||||
Probably<Integer> maybeInt = Probably.just(10);
|
}
|
||||||
System.out.println( maybeInt.transform(new Immutator<Integer,Integer>() {
|
|
||||||
public Integer invoke(Integer t1) {
|
interface Functor<T, F extends Functor<?, ?>> {
|
||||||
return t1+1;
|
<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)
|
* @author Yadunand Prem (10B)
|
||||||
* @version CS2030S AY22/23 Semester 1
|
* @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 final T value;
|
||||||
|
|
||||||
private static final Probably<?> NONE = new Probably<>(null);
|
private static final Probably<?> NONE = new Probably<>(null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor, can only be invoked inside.
|
* 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.
|
* create this using the two public static method.
|
||||||
*
|
*
|
||||||
* @return The shared NOTHING.
|
* @return The shared NOTHING.
|
||||||
@@ -23,7 +23,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
|
|||||||
private Probably(T value) {
|
private Probably(T value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It is probably nothing, no value inside.
|
* 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;
|
Probably<T> res = (Probably<T>) NONE;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It is probably just the given value.
|
* It is probably just the given value.
|
||||||
* Unless the value is null, then nothing is
|
* 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
|
* @param value Probably this is the value
|
||||||
* unless it is null then we say
|
* unless it is null then we say
|
||||||
* that there is no
|
* that there is no
|
||||||
* @return The given value or nothing but
|
* @return The given value or nothing but
|
||||||
* never null.
|
* never null.
|
||||||
*/
|
*/
|
||||||
@@ -52,7 +52,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
|
|||||||
}
|
}
|
||||||
return new Probably<>(value);
|
return new Probably<>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check for equality between something that
|
* Check for equality between something that
|
||||||
* is probably a value but maybe nothing.
|
* is probably a value but maybe nothing.
|
||||||
@@ -78,7 +78,7 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String representation of something that
|
* String representation of something that
|
||||||
* is probably a value but maybe nothing.
|
* is probably a value but maybe nothing.
|
||||||
@@ -102,10 +102,33 @@ class Probably<T> implements Actionable<T>, Immutatorable<T> {
|
|||||||
action.call(this.value);
|
action.call(this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public Probably<T> check(IsModEq eq) {
|
||||||
public <R> Probably<R> transform(Immutator<Probably<R>, ? super T> immutator) {
|
if (this.value == null) {
|
||||||
return immutator.invoke(this.value);
|
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