Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackOverflowError when nesting the maybe pattern #141

Closed
lucywang000 opened this issue Sep 29, 2020 · 5 comments
Closed

StackOverflowError when nesting the maybe pattern #141

lucywang000 opened this issue Sep 29, 2020 · 5 comments

Comments

@lucywang000
Copy link
Contributor

Say we have a list of maps, where there is an optional :b key, and inside :b there is an optional :b2 key.

(m/search [{:a 1}
           {:a 1 :b {:b1 1}}
           {:a 1 :b {:b1 1 :b2 2}}]
  (m/scan {:a ?a
           :b (m/or (m/and nil ?b1 ?b2)
                    {:b1 ?b1
                     :b2 (m/or (m/and nil ?b2)
                               ?b2)
                     })})
  {:a ?a :b1 ?b1 :b2 ?b2})

But this kind of nesting throwed a StackOverflowError.

btw: I wanted to ask in slack, but I think maybe github is a better place in case others would search for similar questions here.

@timothypratley
Copy link
Collaborator

could you please try the solution to #133 -- setting a larger stack size in JVM options (add :jvm-opts ["-Xss2m"] to your project.clj)

@jimmyhmiller
Copy link
Collaborator

jimmyhmiller commented Sep 29, 2020

Ignore this comment. Left a comment below. I thought there was a cata here when there was not. The m/somes I have here will still probably give you the results you really expected.

@timothypratley Not the problem in this case.

I can explain a bit more later, but here is the fix for this particular example

(m/search [{:a 1}
           {:a 1 :b {:b1 1}}
           {:a 1 :b {:b1 1 :b2 2}}]
  (m/scan {:a ?a
           :b (m/or (m/and nil ?b1 ?b2)
                    {:b1 (m/some ?b1)
                     :b2 (m/or (m/and nil ?b2)
                               (m/some ?b2))
                     })})
  {:a ?a :b1 ?b1 :b2 ?b2})

Basically, search goes down both paths if the pattern matches. So you need to guard against that or else you are infinitely recursing.

@jimmyhmiller
Copy link
Collaborator

Sorry, I shouldn't comment when busy. This doesn't stackoverflow for me. So maybe it is related to Tim's issue? How are you running this code?

@jimmyhmiller
Copy link
Collaborator

I am able to recreate the issue now. While we have a work around, this is something we need to fix and I will start trying to look into the issue.

@noprompt
Copy link
Owner

PR GH-141 is open for this ticket.

noprompt added a commit that referenced this issue Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants