1

I need to evaluate a string with nested function calls. Is there an easy way to do this with groovy?

Edit: Code made more realistic. The context is nonacademic; my function needs to combine and evaluate a bunch of arbitrary strings and values from json files.

JSON file 1 will have strings like:

"biggerThan(isList,0)"

"smallerThan(isList,3)"

"biggerThan(isList,1)"

JSON file 2 will have values like

[4,1]

[1,2,1]

[1,5,6,2,98]

[]

  def biggerThan= {func, val->
        {v->return func(v) && (v.size() > val)}
}


def isList ={n->
        return n instanceof List
}

def a=biggerThan(isList,1)

a([4,1])

// -> returns true in groovy console because [4,1] is a list with size>1

Roko
  • 35
  • 1
  • 7
  • Don't use reflection unless you have to. In your case - I don't think you have to. Please provide a larger context! – Nir Alfasi Feb 09 '15 at 03:28
  • 1
    it might be possible with either `Eval` or parsing `str` depending on what forms this all might take. it would help if you could provide a bigger picture and whether all of this is just academic excercise or if you have to handle e.g. a list of such calls from a remote source. – cfrick Feb 09 '15 at 07:21
  • But your example doesn't work even if it is not in Eval. Prepare your example to be working as simple Groovy code in `groovyConsole` and then call it from `Eval`. Look at this: http://groovy.codehaus.org/api/groovy/util/Eval.html – Bozydar Sobczak Feb 10 '15 at 15:10
  • Just clarified example and context. Thanks for everyone's input – Roko Feb 11 '15 at 23:48

0 Answers0