Created
June 8, 2011 01:06
-
-
Save nick-orton/1013584 to your computer and use it in GitHub Desktop.
Lazy Evaluation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Operation | |
Result apply(Object element) | |
void stack(Operation operation) | |
Result | |
void apply(Collection c) | |
Chain.evaluate() | |
Collection c = Create.newCollection() | |
for e : elements | |
Result r = operation.apply(e) | |
r.apply(c) | |
return c | |
Chain.transform(Function f) | |
this.operation.stack(new TransformOperation(f)) | |
return this | |
TransformOperation implements Operation | |
Operation stack; | |
Result apply(Object element) | |
Object v = this.function.apply(element) | |
if(stack == null) | |
return new AddResult(v) | |
else return stack.apply(v) | |
AddResult implements Result | |
final Object v | |
void apply(Collection c) | |
c.add(v) | |
Chain.filter(Predicate p) | |
this.operation.stack(new FilterOperation(p)) | |
return this | |
FilterOperation | |
final Predicate p | |
Result apply(Object e) | |
b = this.p.apply(e) | |
if(!b) return new NoOpResult() | |
else if(stack == null) return new AddResult(e); | |
return stack.apply(e) | |
Chain.flatten(Function f) | |
this.operation.stack(new FlatternOperation(f) | |
return this | |
FlattenOperation | |
final Function f | |
Result apply(Object e) | |
Collection c = f.apply(e) | |
Collection t = Create.newList() | |
if(stack == null) return new AddAllResult(c) | |
for o : c | |
r = stack.apply(o) | |
r.apply(t) | |
return new AddAllResult(t) | |
Chain.detect(Predicate p) | |
this.operation.stack(new DetectOperation(p)) | |
try { | |
this.evaluate() | |
} catch(DetectionDetectedException e) { | |
return e.get() | |
} | |
return null; | |
DetectOperation | |
final Predicate p | |
Result apply(Object e) throws DetectionDetectedException | |
if(p.apply(e)) throw new DetectionDetectedException(e) | |
return new NoOpResult() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment