# What's the right way to handle lazy evaluation in a dynamic binding?

**URL:** https://clojureverse.org/t/whats-the-right-way-to-handle-lazy-evaluation-in-a-dynamic-binding/10279
**Category:** How to?
**Tags:** clojure
**Created:** [August 27, 2023, 4:16pm UTC](https://clojureverse.org/t/whats-the-right-way-to-handle-lazy-evaluation-in-a-dynamic-binding/10279 "2023-08-27T16:16:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sammy](https://clojureverse.org/letter_avatar_proxy/v4/letter/s/7ab992/32.png) [@sammy](https://clojureverse.org/u/sammy)
#### Post date: [August 27, 2023, 4:16pm UTC](https://clojureverse.org/t/whats-the-right-way-to-handle-lazy-evaluation-in-a-dynamic-binding/10279/1 "2023-08-27T16:16:52Z")

</div>

As an example, the following snipit returns (100 100 100) even though I’d like it to return (10 10 10)

```auto
(def ^:dynamic x 100)

(defn lazy-eval []
  (map identity [x]))

(take 3 (binding [x 10] (repeatedly lazy-eval)))

```

I know that this is _technically_ correct, because the function call to lazy-eval happens outside of the binding’s scope, but:

- it’s counterintuitive to what I actually want
- the premise of lazy evaluation-by-default is performance optimization, i.e., if you call a pure function it doesn’t matter when/if you look at the value, but I don’t want lazy eval to impact behavior
- I know that technically dynamic binding is implemented as a mutable operation (especially in cljs lol), but ideologically, I think of dynamic binding as a variable capture within the binding’s scope. i.e. within the scope of the binding lazy-eval is a function that returns (10 10 10)

Is there a correct way to get at this behavior without causing a bunch of weird other effects?

---

<div class="post-metadata">

### Author: ![p-himik](https://clojureverse.org/user_avatar/clojureverse.org/p-himik/32/4137_2.png) [@p-himik](https://clojureverse.org/u/p-himik)
#### Post date: [August 27, 2023, 4:30pm UTC](https://clojureverse.org/t/whats-the-right-way-to-handle-lazy-evaluation-in-a-dynamic-binding/10279/2 "2023-08-27T16:30:17Z")

</div>

> if you call a pure function it doesn’t matter when/if you look at the value

But `lazy-eval` is not pure.

> Is there a correct way to get at this behavior without causing a bunch of weird other effects?

Capture the dynamic value deliberately OR bind the value where you’re going to realize the seq.

For example:

```clj
(def ^:dynamic x 100)

(defn lazy-eval [x]
  (map identity [x]))

(defn bind-lazy-eval [x]
  #(lazy-eval x))

(take 3 (binding [x 10] (repeatedly (bind-lazy-eval x))))
;; OR
(binding [x 10] (doall (take 3 (repeatedly #(lazy-eval x)))))

```

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [February 26, 2024, 4:30am UTC](https://clojureverse.org/t/whats-the-right-way-to-handle-lazy-evaluation-in-a-dynamic-binding/10279/3 "2024-02-26T04:30:40Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
