Runtime evaluation: Difference between revisions

Line 48:
 
=={{header|Groovy}}==
All these solutions are verified to give the same output:
{{incorrect|Groovy|It does not discuss passing in or returning values, or the environment the expression is evaluated in.}}
<pre>[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]</pre>
 
===Simple evaluation===
The '''GroovyShell''' class allows the evaluation of a string or of the text contents of a '''File''' or '''InputStream''' as a ''Groovy script''. A script is a either a set of statements to be executed in order, or a Groovy class with a '''main()''' method, or a Groovy '''Thread''' subclass or '''Runnable''' implementation. The return value is the value of the last statement executed, or the value of an explicit '''return''' statement (if any).
 
This program evaluates the Groovy program solution to the "[[Yuletide Holiday]]" task:
<lang Groovygroovy>def years1 = new GroovyShell().evaluate ('''
def result = (2008..2121).findAll {
def inFormat = new java.text.SimpleDateFormat("yyyy-MM-dd")
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
def checkFormat = new java.text.SimpleDateFormat("EEE")
}
''')
 
println years1</lang>
def result = (2008..2121).findAll {
 
Date date = inFormat.parse("${it}-12-25")
The last expression evaluated in the script, a list of years found, is the return value of the '''evaluate()''' method.
checkFormat.format(date) == "Sun"
 
===Evaluation with variables===
There are several approaches to evaluating a script with variables:
*'''GString''' embedded values
*'''Binding''' variables
*'''Eval''' shortcut
 
====GString embedded values====
This is a "natural" solution for Groovy programmers, but it does entail some problems if the script itself contains '''GString'''s.
<lang groovy>def startYear = 2008
def endYear = 2121
def years2 = new GroovyShell().evaluate("""
(${startYear}..${endYear}).findAll {
Date.parse("yyyy-MM-dd", "\${it}-12-25").format("EEE") == "Sun"
}
""")
 
println resultyears2</lang>
The variables "startYear" and "endYear" are dynamically pulled into the script '''GString''' as embedded values.
'''</lang>
 
Notice that in the script the embedded value "${it}" must be ''quoted'' with backslash (\) to prevent parsing as a part of the script '''GString'''. However, it is still correctly parsed when the script is run.
Output:
 
<pre>[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]</pre>
====Binding variables====
The '''Binding''' class can be used to pass variable values to a script. This is the only way to pass variables if the script comes from a '''File''' or '''InputStream''', but even if the script is a string '''Binding''' avoids the whole nested '''GString''' quoting issue.
<lang groovy>def context = new Binding()
context.startYear = 2008
context.endYear = 2121
def years3 = new GroovyShell(context).evaluate('''
(startYear..endYear).findAll {
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
}
''')</lang>
 
'''Binding''' can be instantiated with the variables as named parameters, allowing a more terse syntax:
<lang groovy>def years4 = new GroovyShell( new Binding(startYear: 2008, endYear: 2121) ).evaluate('''
(startYear..endYear).findAll {
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
}
''')
 
println years4</lang>
 
====Eval shortcut====
For simple evaluation of string-based scripts with only a few variables (like this one), the '''Eval''' class has static shortcut methods that do the '''Binding''' setup and '''GroovyShell''' evaluation under the surface. '''Eval.me(script)''' evaluates a script with no variables. '''Eval.x(x,script)''', '''Eval.xy(x,y,script)''', and '''Eval.xyz(x,y,z,script)''' evaluate scripts with 1, 2, and 3 variables, respectively. Here is an example with start and end years as script variables ''x'' and ''y''.
<lang groovy>def years5 = Eval.xy(2008, 2121, '''
(x..y).findAll {
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
}
''')
 
println years5</lang>
 
=={{header|Perl}}==
Anonymous user