bitemyapp / bitemyapp/revise

Name binding does not work properly with nested lambda's

Open
#14 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Clojure
Stars
146
Forks
7
PR merge metrics
No merged PRs in 30d

Description

When nesting lambda's, the name bindings of the outer lambda's get overwritten by the name bindings of the inner lambda's and cannot be accessed in the inner lambda's.

Nesting works as expected in the official javascript driver.

---

The `test` table looks like this:

``` clojure
{:id "40cd4261-aa72-4427-877b-da45abe4142b" :field [{:name "foo"}]}
```

The javascript query looks like this:

``` javascript
r.table('test').get('40cd4261-aa72-4427-877b-da45abe4142b')('field')
.map(function(x){
return {a: x('name'),
b: r.expr(["x", "y", "z"]).map(function(y){
return x
})
}
})
```

And its output:

``` javascript
[
{
"a": "foo" ,
"b": [
{
"id": "afc9cb64-af3d-4cae-8993-fc5d35ebd6ee" ,
"name": "foo" ,
},
{
"id": "afc9cb64-af3d-4cae-8993-fc5d35ebd6ee" ,
"name": "foo" ,
},
{
"id": "afc9cb64-af3d-4cae-8993-fc5d35ebd6ee" ,
"name": "foo" ,
}
]
}
]
```

However, when I try to run this in Revise, referncing `x` inside the nested lambda does not work.

The Clojure query:

``` clojure
(-> (r/table :test)
(r/get "40cd4261-aa72-4427-877b-da45abe4142b")
(r/get-field :field)
(r/map
(r/lambda [x]
{:a (r/get-field x :name)
:b (-> ["x" "y" "z"]
(r/map
(r/lambda [y]
x))})))
```

And its output:

``` clojure
[{:a "foo"
:b ["x" "y" "z"]}]
```

That is, the inner lambda seems to clobber the binding of the outer one.

Presumably [this](https://github.com/bitemyapp/revise/blob/master/src/bitemyapp/revise/query.clj#L114) doesn't know about nested bindings and instead blindly replaces variables with the inner lambda's argument? I guess the comment is correct: not the best model of scope.. :(

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at src/bitemyapp/revise/query.clj around line 114, then reproduce the nested-lambda query from the issue. Trace how the inner lambda's argument is substituted and compare the result with the JavaScript driver's expected output. Done means the outer x remains usable inside the nested lambda and the query returns the shown records.

Written by the indexing model from the issue text.

Assessment

Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.