# Higher order function stage tests closures

**URL:** <https://forum.codecrafters.io/t/higher-order-function-stage-tests-closures/3069>\
**Category:** Challenges\
**Tags:** challenge:interpreter\
**Created:** [November 22, 2024, 7:06pm UTC](https://forum.codecrafters.io/t/higher-order-function-stage-tests-closures/3069 "2024-11-22T19:06:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellnix](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ellnix/32/3063_2.png) [@ellnix](https://forum.codecrafters.io/u/ellnix)\
**Post date:** [November 22, 2024, 7:06pm UTC](https://forum.codecrafters.io/t/higher-order-function-stage-tests-closures/3069/1 "2024-11-22T19:06:07Z")

</div>

In #EY3, the “Higher order function” stage all of the tests involve capturing the environment in a function.

See this test for instance:

```auto
fun makeGreeter(greeting) {
  fun greet(name) {
    print greeting + " " + name;
  }
  return greet;
}

var sayHello = makeGreeter("Hello");
sayHello("Bob");

```

The “greet” function uses the “greeting” variable which is not in the global scope. The tests are valid, they just happen to be testing behavior that ought to belong in “Closures” #GG6.

This isn’t a big deal, but it’s just kind of a pain to have to run tests twice for the same behavior. Maybe they should be merged into one stage or the Higher Order Function stage should simply return functions from a function without capturing the parent function’s arguments?

Relevant links (I don’t know if these are useful, this is my first time posting here):  
[Higher order functions #ey3](https://app.codecrafters.io/courses/interpreter/stages/ey3)  
[Closures #gg6](https://app.codecrafters.io/courses/interpreter/stages/gg6)

Like I said above, this is my first post in these forums, please let me know if I should have posted elsewhere or included something different in my post 🫤

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [November 22, 2024, 7:07pm UTC](https://forum.codecrafters.io/t/higher-order-function-stage-tests-closures/3069/2 "2024-11-22T19:07:22Z")

</div>

Thanks for highlighting this! cc-ing @ryan-gang, who worked on this extension and should have more context

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 25, 2024, 12:05pm UTC](https://forum.codecrafters.io/t/higher-order-function-stage-tests-closures/3069/4 "2024-11-25T12:05:29Z")

</div>

We’ve fixed the issue in [this PR](https://github.com/codecrafters-io/interpreter-tester/pull/56).

@ellnix thanks again for highlighting the issue!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 30, 2024, 12:06pm UTC](https://forum.codecrafters.io/t/higher-order-function-stage-tests-closures/3069/5 "2024-11-30T12:06:27Z")

</div>

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