Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy evaluation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Java==== In [[Java (programming language)|Java]], lazy evaluation can be done by using objects that have a method to evaluate them when the value is needed. The body of this method must contain the code required to perform this evaluation. Since the introduction of [[Anonymous function|lambda expressions]] in Java SE8, Java has supported a compact notation for this. The following example [[Generic classes in Java|generic]] interface provides a framework for lazy evaluation:<ref name="Piwowarek2018">Grzegorz Piwowarek, [https://4comprehension.com/leveraging-lambda-expressions-for-lazy-evaluation-in-java/ Leveraging Lambda Expressions for Lazy Evaluation in Java], [https://4comprehension.com/ 4Comprehension], July 25, 2018.</ref><ref name="Jones2020">Douglas W. Jones, [https://homepage.divms.uiowa.edu/~jones/object/fall20/notes/25.shtml CS:2820 Notes, Fall 2020, Lecture 25], retrieved Jan. 2021.</ref> <syntaxhighlight lang="java"> interface Lazy<T> { T eval(); } </syntaxhighlight> The <code>Lazy</code> interface with its <code>eval()</code> method is equivalent to the <code>Supplier</code> interface with its <code>get()</code> method in the <code>java.util.function</code> library.<ref>[https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html Interface Suppier<T>], retrieved Oct. 2020.</ref><ref name=Bloch>{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}</ref>{{rp|200}} Each class that implements the <code>Lazy</code> interface must provide an <code>eval</code> method, and instances of the class may carry whatever values the method needs to accomplish lazy evaluation. For example, consider the following code to lazily compute and print 2<sup>10</sup>: <syntaxhighlight lang="java"> Lazy<Integer> a = () -> 1; for (int i = 0; i < 10; i++) { Lazy<Integer> b = a; a = () -> b.eval() + b.eval(); } System.out.println("a = " + a.eval()); </syntaxhighlight> In the above, the variable {{mono|a}} initially refers to a lazy integer object created by the lambda expression <code>() -> 1</code>. Evaluating this lambda expression is similar{{efn|name=Java Lambda|Java lambda expressions are not exactly equivalent to anonymous classes, see [[Anonymous function#Differences compared to Anonymous Classes]]}} to constructing a new instance of an [[anonymous class]] that implements <code>Lazy<Integer></code> with an {{mono|eval}} method returning {{mono|1}}. Each iteration of the loop links {{mono|a}} to a new object created by evaluating the lambda expression inside the loop. Each of these objects holds a reference to another lazy object, {{mono|b}}, and has an {{mono|eval}} method that calls <code>b.eval()</code> twice and returns the sum. The variable {{mono|b}} is needed here to meet Java's requirement that variables referenced from within a lambda expression be effectively final. This is an inefficient program because this implementation of lazy integers does not [[memoize]] the result of previous calls to {{mono|eval}}. It also involves considerable [[Autoboxing|autoboxing and unboxing]]. What may not be obvious is that, at the end of the loop, the program has constructed a [[linked list]] of 11 objects and that all of the actual additions involved in computing the result are done in response to the call to <code>a.eval()</code> on the final line of code. This call [[Recursion (computer science)|recursively]] traverses the list to perform the necessary additions. We can build a Java class that memoizes a lazy object as follows:<ref name="Piwowarek2018" /><ref name="Jones2020" /> <syntaxhighlight lang="java"> class Memo<T> implements Lazy<T> { private Lazy<T> lazy; // a lazy expression, eval sets it to null private T memo; // the memorandum of the previous value public Memo(Lazy<T> lazy) { this.lazy = lazy; } public T eval() { if (lazy != null) { memo = lazy.eval(); lazy = null; } return memo; } } </syntaxhighlight> This allows the previous example to be rewritten to be far more efficient. Where the original ran in time exponential in the number of iterations, the memoized version runs in [[linear time]]: <syntaxhighlight lang="java"> Lazy<Integer> a = () -> 1; for (int i = 0; i < 10; i++) { Lazy<Integer> b = a; a = new Memo<Integer>(() -> b.eval() + b.eval()); } System.out.println("a = " + a.eval()); </syntaxhighlight> Java's lambda expressions are just [[syntactic sugar]]. Anything that can be written with a lambda expression can be rewritten as a call to construct an instance of an anonymous [[inner class]] implementing the interface,{{efn|name=Java Lambda}} and any use of an anonymous inner class can be rewritten using a named inner class, and any named inner class can be moved to the outermost nesting level.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)