{"id":2722,"date":"2022-03-06T09:37:54","date_gmt":"2022-03-06T04:07:54","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2722"},"modified":"2022-03-06T09:37:54","modified_gmt":"2022-03-06T04:07:54","slug":"lazy-parameter-evaluation-in-d-language","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/","title":{"rendered":"Lazy Parameter Evaluation in D Language"},"content":{"rendered":"<p>Languages such as <em><strong>C\/<\/strong><strong>C++<\/strong><\/em> and <em><strong>Java<\/strong><\/em> follow an\u00a0<em><strong>&#8220;eager&#8221;<\/strong><\/em> argument evaluation approach, where function arguments are evaluated before the function is entered. Of course, there are idioms and tricks that one can use to force <em><strong>&#8220;lazy&#8221;<\/strong><\/em> evaluation. The primary benefit of <em><strong>&#8220;lazy&#8221;<\/strong><\/em> parameter evaluation is that the parameter is evaluated only when it is used and this can improve efficiency. <em><strong>D<\/strong><\/em> language supports both <em><strong>&#8220;eager&#8221;<\/strong><\/em> and <em><strong>&#8220;lazy&#8221;<\/strong><\/em> parameter evaluations.<\/p>\n<p>Here is a simple <em><strong>D<\/strong><\/em> program to show <em><strong>&#8220;eager&#8221;<\/strong><\/em> parameter evaluation:<\/p>\n<figure id=\"attachment_2723\" aria-describedby=\"caption-attachment-2723\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2723\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/eagercode\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg\" data-orig-size=\"783,993\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646491500&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example Program\" data-image-description=\"&lt;p&gt;Example Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg\" class=\"wp-image-2723\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg?resize=500%2C634&#038;ssl=1\" alt=\"Example Program\" width=\"500\" height=\"634\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg?resize=237%2C300&amp;ssl=1 237w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg?resize=768%2C974&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode.jpg?w=783&amp;ssl=1 783w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2723\" class=\"wp-caption-text\"><strong>Example Program<\/strong><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>If you study the function <strong><em>&#8220;fooBar&#8221;<\/em><\/strong>, it takes a boolean first parameter along with two integer params. Based on the value of the first param, it uses one of the other two params (not both) inside the body for computing its result.<\/p>\n<p>Now look at the program output:<\/p>\n<figure id=\"attachment_2724\" aria-describedby=\"caption-attachment-2724\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2724\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/result1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result1.jpg\" data-orig-size=\"637,201\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646491462&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Eager Evaluation Result\" data-image-description=\"&lt;p&gt;Eager Evaluation Result&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Eager Evaluation Result&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result1.jpg\" class=\"wp-image-2724\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result1.jpg?resize=500%2C158&#038;ssl=1\" alt=\"Eager Evaluation Result\" width=\"500\" height=\"158\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result1.jpg?resize=300%2C95&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result1.jpg?w=637&amp;ssl=1 637w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2724\" class=\"wp-caption-text\"><strong>Eager Evaluation Result<\/strong><\/figcaption><\/figure>\n<p>We see that <em><strong>both<\/strong><\/em> the functions are evaluated in each case. This is not surprising. Because by default, <em><strong>D<\/strong><\/em> language (like <em><strong>C, C++, Java<\/strong><\/em>) uses <em><strong>&#8220;eager evaluation<\/strong><\/em>&#8220;, when the <em><strong>&#8220;fooBar&#8221;<\/strong><\/em> function is called, both the second and third parameters are evaluated <em><strong>apriori<\/strong><\/em>, even though only one is actually used inside the function. One might argue that it is <em><strong>&#8220;wasteful&#8221;<\/strong><\/em> to call the other function that is not used in the result computation, especially if the calculation is <em><strong>&#8220;costly&#8221;<\/strong><\/em>.<\/p>\n<p>The solution lies in <em><strong>&#8220;lazy&#8221;<\/strong><\/em> parameter evaluation. Here is the modified <em><strong>&#8220;fooBar&#8221;<\/strong><\/em> function:<\/p>\n<figure id=\"attachment_2725\" aria-describedby=\"caption-attachment-2725\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2725\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/lazy1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy1.jpg\" data-orig-size=\"678,287\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646492210&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Declaring Lazy Parameters\" data-image-description=\"&lt;p&gt;Declaring Lazy Parameters&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Declaring Lazy Parameters&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy1.jpg\" class=\"wp-image-2725\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy1.jpg?resize=500%2C212&#038;ssl=1\" alt=\"Declaring Lazy Parameters\" width=\"500\" height=\"212\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy1.jpg?resize=300%2C127&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy1.jpg?w=678&amp;ssl=1 678w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2725\" class=\"wp-caption-text\"><strong>Declaring Lazy Parameters<\/strong><\/figcaption><\/figure>\n<p>I have added the extra qualifier <em><strong>&#8220;lazy&#8221;<\/strong><\/em> to the second and third parameters of <em><strong>&#8220;fooBar&#8221;<\/strong><\/em>. This forces the <em><strong>D<\/strong><\/em> compiler to evaluate the argument(s) only at the <em><strong>point of use<\/strong><\/em>, not apriori. This is confirmed by the output from the program.<\/p>\n<figure id=\"attachment_2726\" aria-describedby=\"caption-attachment-2726\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2726\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/result2-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result2.jpg\" data-orig-size=\"671,151\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646492177&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result2.jpg\" class=\"wp-image-2726\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result2.jpg?resize=500%2C113&#038;ssl=1\" alt=\"Program Output\" width=\"500\" height=\"113\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result2.jpg?resize=300%2C68&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result2.jpg?w=671&amp;ssl=1 671w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2726\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Nice, isn&#8217;t it? There is a catch, though. While the positive aspect of declaring an argument as <em><strong>&#8220;lazy&#8221;<\/strong><\/em> is that the compiler evaluates it only when it is required, the negative side of it is that the compiler evaluates it <em><strong>&#8220;every&#8221;<\/strong><\/em> time it needs the parameter.<\/p>\n<p>Let us modify our <em><strong>&#8220;fooBar&#8221;<\/strong><\/em> function slightly:<\/p>\n<figure id=\"attachment_2727\" aria-describedby=\"caption-attachment-2727\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2727\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/lazy2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg\" data-orig-size=\"702,282\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646556478&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Multiple References to Lazy Param\" data-image-description=\"&lt;p&gt;Multiple References to Lazy Param&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Multiple References to Lazy Param&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg\" class=\"wp-image-2727\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg?resize=500%2C201&#038;ssl=1\" alt=\"Multiple References to Lazy Param\" width=\"500\" height=\"201\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg?resize=300%2C121&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg?resize=400%2C160&amp;ssl=1 400w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy2.jpg?w=702&amp;ssl=1 702w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2727\" class=\"wp-caption-text\"><strong>Multiple References to Lazy Param<\/strong><\/figcaption><\/figure>\n<p>The change, as you would have noticed, is that I am using the <em><strong>&#8220;<\/strong><strong>lazy&#8221;<\/strong><\/em> parameters twice in each expression just to prove a point. When we run the program now, this is what we get:<\/p>\n<figure id=\"attachment_2728\" aria-describedby=\"caption-attachment-2728\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2728\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/result3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result3.jpg\" data-orig-size=\"629,203\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646556432&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result3.jpg\" class=\"wp-image-2728\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result3.jpg?resize=500%2C161&#038;ssl=1\" alt=\"Program Output\" width=\"500\" height=\"161\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result3.jpg?resize=300%2C97&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result3.jpg?w=629&amp;ssl=1 629w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2728\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Clearly, this is not what we want! Our decision to declare the parameters as <em><strong>&#8220;lazy&#8221;<\/strong><\/em> in the first place was to avoid unnecessary computation, but now the objective is lost!<\/p>\n<p>So what is the way out? Simple really. We can evaluate the <em><strong>&#8220;lazy&#8221;<\/strong><\/em> parameter once and assign it to a different local variable and then use that local variable whenever we need the original parameter later in the function. This is shown below.<\/p>\n<figure id=\"attachment_2729\" aria-describedby=\"caption-attachment-2729\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2729\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/lazy3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy3.jpg\" data-orig-size=\"721,380\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646556895&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Caching the Parameter\" data-image-description=\"&lt;p&gt;Caching the Parameter&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Caching the Parameter&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy3.jpg\" class=\"wp-image-2729\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy3.jpg?resize=500%2C264&#038;ssl=1\" alt=\"Caching the Parameter\" width=\"500\" height=\"264\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy3.jpg?resize=300%2C158&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/lazy3.jpg?w=721&amp;ssl=1 721w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2729\" class=\"wp-caption-text\"><strong>Caching the Parameter<\/strong><\/figcaption><\/figure>\n<p>And here is the program output after the change:<\/p>\n<figure id=\"attachment_2730\" aria-describedby=\"caption-attachment-2730\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2730\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/result4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result4.jpg\" data-orig-size=\"648,175\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1646556856&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Output After Caching\" data-image-description=\"&lt;p&gt;Output After Caching&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output After Caching&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result4.jpg\" class=\"wp-image-2730\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result4.jpg?resize=500%2C135&#038;ssl=1\" alt=\"Output After Caching\" width=\"500\" height=\"135\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result4.jpg?resize=300%2C81&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/Result4.jpg?w=648&amp;ssl=1 648w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2730\" class=\"wp-caption-text\"><strong>Output After Caching<\/strong><\/figcaption><\/figure>\n<p>That solves our problem! We now have the benefit of <em><strong>&#8220;lazy&#8221;<\/strong><\/em> parameter evaluation, at the same time avoiding multiple evaluations of that parameter. Ideally, the compiler should cache the result of the first evaluation and use it subsequently, but we can live with that limitation.<\/p>\n<p>You can download the sample <em><strong>D<\/strong><\/em> program <a href=\"http:\/\/www.rangakrish.com\/downloads\/LazyParams.d\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Languages such as C\/C++ and Java follow an\u00a0&#8220;eager&#8221; argument evaluation approach, where function arguments are evaluated before the function is entered. Of course, there are idioms and tricks that one can use to force &#8220;lazy&#8221; evaluation. The primary benefit of &#8220;lazy&#8221; parameter evaluation is that the parameter is evaluated only when it is used and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[302,17],"tags":[304,311],"class_list":["post-2722","post","type-post","status-publish","format-standard","hentry","category-d-language","category-programming","tag-d-language","tag-lazy-parameter-evaluation"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-HU","jetpack-related-posts":[{"id":2668,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/","url_meta":{"origin":2722,"position":0},"title":"Template Mixins in D Programming Language","author":"admin","date":"January 23, 2022","format":false,"excerpt":"When I heard the name Mixin\u00a0for the first time in the context of Dlang, I imagined it would be something similar to the mixins\u00a0of Common Lisp, but I was completely wrong!\u00a0 Mixins are a very interesting feature of D language. They allow code to be \"injected\" into the source at\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Simple Template Mixin","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn-300x98.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1560,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/","url_meta":{"origin":2722,"position":1},"title":"Python Integration in Mathematica 12","author":"admin","date":"May 5, 2019","format":false,"excerpt":"Mathematica has had Python support since ver 11.2 through ExternalEvaluate[]. In ver 11.3 it was possible to input Python expression in a cell by beginning with \u201c>\u201d character. The good news is that Mathematica 12 has significantly enhanced this integration. Python Cells Let us start with the simplest way to\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Function References","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":2722,"position":2},"title":"Calling Lisp Functions from D Language","author":"admin","date":"December 25, 2021","format":false,"excerpt":"After exploring \"newLisp\" in the past few posts, I would like to start looking at the \"D Programming Language\"\u00a0(DLang). DLang\u00a0has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Lisp Function Called from D","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2975,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/","url_meta":{"origin":2722,"position":3},"title":"Std::tie","author":"admin","date":"December 25, 2022","format":false,"excerpt":"std::tuple is a widely used abstraction in C++ and has been around since C++11. It is a generalization of std::pair. std::tie is convenient when we want to create a tuple of lvalue references to existing variables. It is a function template commonly used to unpack a tuple into individual objects.\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::tie Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2540,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/09\/19\/c-20-concepts\/","url_meta":{"origin":2722,"position":4},"title":"C++ 20: Concepts","author":"admin","date":"September 19, 2021","format":false,"excerpt":"Concepts, introduced in C++20, are predicates that act as contraints on template parameters. As you would expect, the nice thing is that the constraint checking happens as part of template instantiation at compile time and not at run time! Since templates can have type as well as non-type parameters, Concepts\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Constraint on Type","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/09\/Example1-300x213.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2433,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/06\/getting-to-know-visual-prolog-10\/","url_meta":{"origin":2722,"position":5},"title":"Getting to Know Visual Prolog 10","author":"admin","date":"June 6, 2021","format":false,"excerpt":"Visual Prolog\u00a0has its roots in Turbo Prolog, which was popularized by Borland in the mid 1980s. It is developed and supported by PDC A\/S, Denmark. So how is Visual Prolog different from other Prolog implementations out there, for example, SWI-Prolog or Sicstus Prolog? Here are the key differences: 1) It\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Visual Prolog Project","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2722"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2722\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}