Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -10,8 +10,8 @@ operation(record delegator)
|
|||
text s;
|
||||
|
||||
if (r_key(delegator, "delegate")) {
|
||||
if (r_key(r_query(delegator, "delegate"), "thing")) {
|
||||
s = __text(call(r_query(r_query(delegator, "delegate"), "thing")));
|
||||
if (r_key(delegator["delegate"], "thing")) {
|
||||
s = call(r_query(delegator["delegate"], "thing"));
|
||||
} else {
|
||||
s = "default implementation";
|
||||
}
|
||||
|
|
|
|||
6
Task/Delegates/Java/delegates-2.java
Normal file
6
Task/Delegates/Java/delegates-2.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package delegate;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Thingable {
|
||||
public String thing();
|
||||
}
|
||||
19
Task/Delegates/Java/delegates-3.java
Normal file
19
Task/Delegates/Java/delegates-3.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package delegate;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface Delegator {
|
||||
public Thingable delegate();
|
||||
public Delegator delegate(Thingable thingable);
|
||||
|
||||
public static Delegator new_() {
|
||||
return $Delegator.new_();
|
||||
}
|
||||
|
||||
public default String operation() {
|
||||
return Optional.ofNullable(delegate())
|
||||
.map(Thingable::thing)
|
||||
.orElse("default implementation")
|
||||
;
|
||||
}
|
||||
}
|
||||
17
Task/Delegates/Java/delegates-4.java
Normal file
17
Task/Delegates/Java/delegates-4.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package delegate;
|
||||
|
||||
@FunctionalInterface
|
||||
/* package */ interface $Delegator extends Delegator {
|
||||
@Override
|
||||
public default Delegator delegate(Thingable thingable) {
|
||||
return new_(thingable);
|
||||
}
|
||||
|
||||
public static $Delegator new_() {
|
||||
return new_(() -> null);
|
||||
}
|
||||
|
||||
public static $Delegator new_(Thingable thingable) {
|
||||
return () -> thingable;
|
||||
}
|
||||
}
|
||||
8
Task/Delegates/Java/delegates-5.java
Normal file
8
Task/Delegates/Java/delegates-5.java
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package delegate;
|
||||
|
||||
public final class Delegate implements Thingable {
|
||||
@Override
|
||||
public String thing() {
|
||||
return "delegate implementation";
|
||||
}
|
||||
}
|
||||
36
Task/Delegates/Java/delegates-6.java
Normal file
36
Task/Delegates/Java/delegates-6.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package delegate;
|
||||
|
||||
// Example usage
|
||||
// Memory management ignored for simplification
|
||||
public interface DelegateTest {
|
||||
public static String thingable() {
|
||||
return "method reference implementation";
|
||||
}
|
||||
|
||||
public static void main(String... arguments) {
|
||||
// Without a delegate:
|
||||
Delegator d1 = Delegator.new_();
|
||||
assert d1.operation().equals("default implementation");
|
||||
|
||||
// With a delegate:
|
||||
Delegator d2 = d1.delegate(new Delegate());
|
||||
assert d2.operation().equals("delegate implementation");
|
||||
|
||||
// Same as the above, but with an anonymous class:
|
||||
Delegator d3 = d2.delegate(new Thingable() {
|
||||
@Override
|
||||
public String thing() {
|
||||
return "anonymous delegate implementation";
|
||||
}
|
||||
});
|
||||
assert d3.operation().equals("anonymous delegate implementation");
|
||||
|
||||
// Same as the above, but with a method reference:
|
||||
Delegator d4 = d3.delegate(DelegateTest::thingable);
|
||||
assert d4.operation().equals("method reference implementation");
|
||||
|
||||
// Same as the above, but with a lambda expression:
|
||||
Delegator d5 = d4.delegate(() -> "lambda expression implementation");
|
||||
assert d5.operation().equals("lambda expression implementation");
|
||||
}
|
||||
}
|
||||
34
Task/Delegates/Racket/delegates.rkt
Normal file
34
Task/Delegates/Racket/delegates.rkt
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#lang racket
|
||||
;; Delegates. Tim Brown 2014-10-16
|
||||
|
||||
(define delegator%
|
||||
(class object%
|
||||
(init-field [delegate #f])
|
||||
(define/public (operation)
|
||||
(cond [(and (object? delegate) (object-method-arity-includes? delegate 'thing 0))
|
||||
(send delegate thing)]
|
||||
[else "default implementation"]))
|
||||
(super-new)))
|
||||
|
||||
(define non-thinging-delegate% (class object% (super-new)))
|
||||
|
||||
(define thinging-delegate%
|
||||
(class object%
|
||||
(define/public (thing) "delegate implementation")
|
||||
(super-new)))
|
||||
|
||||
(module+ test
|
||||
(require tests/eli-tester)
|
||||
(define delegator-1 (new delegator%))
|
||||
(define delegator-2 (new delegator%))
|
||||
(define non-thinging-delegate (new non-thinging-delegate%))
|
||||
(define thinging-delegate (new thinging-delegate%))
|
||||
|
||||
(test
|
||||
(send delegator-1 operation) => "default implementation"
|
||||
(send delegator-2 operation) => "default implementation"
|
||||
(set-field! delegate delegator-1 non-thinging-delegate) => (void)
|
||||
(set-field! delegate delegator-2 thinging-delegate) => (void)
|
||||
(send delegator-1 operation) => "default implementation"
|
||||
(send delegator-2 operation) => "delegate implementation"
|
||||
(send (new delegator% [delegate thinging-delegate]) operation) => "delegate implementation"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue