groovysh
?
help
(\h)
?
(\?)
exit
(\x)
quit
(\q)
import
(\i)
display
(\d)
clear
(\c)
show
(\S)
inspect
(\n)
purge
(\p)
edit
(\e)
load
(\l)
.
(\.)
save
(\s)
record
(\r)
history
(\H)
alias
(\a)
set
(\=)
generaciondecodigos@nereida:~/src/groovy/separatedcomp/groovyscriptsfromgroovy$ cat -n Script2.groovy 1 println "In Script2" 2 shell = new GroovyShell() 3 shell.evaluate(new File('Script1.groovy'))
shell.evaluate
?
run
y evaluate
?
parse
?
binding.getVariable
?
binding.setVariable
?
~/Lgroovy/separatedcomp/groovyscriptsfromgroovy$ cat -n calc.groovy 1 def stdin = new BufferedReader(new InputStreamReader(System.in)) 2 Binding binding = new Binding() 3 def shell = new GroovyShell(binding) 4 5 int c = 1; 6 print "> " 7 stdin.eachLine{ input -> 8 def script = shell.parse(input) 9 def a = script.run() 10 println a 11 print "> " 12 }Indique cual es la salida para cada una de las siguientes entradas:
a =2*3
a+2
b = a-3
Math.sqrt(b)
^D
a = (1..10).toList() b = a.collect { 2*it } r = (0..9).collect { a[it]+b[it] } println r
it
?
collect
?
1..10
?
generaciondecodigos@nereida:~/src/groovy/simpledatatypes$ cat -n Frequency.java 1 import java.util.*; 2 3 // Prints a frequency table of the words on the command line 4 public class Frequency { 5 public static void main(String[] args) { 6 Map<String, Integer> m = new TreeMap<String, Integer>(); 7 for (String word : args) { 8 Integer freq = m.get(word); 9 m.put(word, (freq == null ? 1 : freq + 1)); 10 } 11 System.out.println(m); 12 } 13 }
args
?
public
?
static
?
m
?
for
como el del ejemplo?
b
?
Vector<String> x = new Vector<String>(); Vector<Integer> y = new Vector<Integer>(); boolean b = x.getClass() == y.getClass();
groovysh
?
groovy:000> String a = 1; a = 4; a = a+1 groovy:000> String a = 1; a = 4; a = a*2 groovy:000> String a = 1; a = 4; a = a*3 groovy:000> String a = 2; b = a.toInteger(); b = b*2
groovysh
?
groovy:000> b = "432" groovy:000> b.class groovy:000> c = b as Integer groovy:000> c.class groovy:000> d = "432b" groovy:000> e = d as Integer
groovysh
?
groovy:000> a = 1+1.0 groovy:000> a.class
groovysh
?
groovy:000> b = 1 as BigDecimal groovy:000> b.class
groovysh
?
groovy:000> d = 65 groovy:000> d.class groovy:000> c = d as String groovy:000> c.class groovy:000> c groovy:000> c[1] groovy:000> e = 8.5 groovy:000> f = e as String groovy:000> f.class groovy:000> f[1] groovy:000> f[2]
a + b a - b a * b a ** b a / b a % b a | b a & b a ^ b a++ or ++a a-- or --a a[b] a[b] = c a << b a >> b switch(a) { case(b) : } ~a -a +a
groovysh
?
groovy:000> a = null groovy:000> b = "foo" groovy:000> a != b groovy:000> b != a groovy:000> a == null
~/src/groovy/overloading$ cat -n Numberscohercioncomparison.groovy 1 #!/usr/bin/env groovy 2 Byte a = 12 3 Double b = 256 4 5 println "a($a) instanceof Byte: ${a instanceof Byte}" 6 println "b($b) instanceof Double: ${b instanceof Double}" 7 8 if (b > a) { 9 println "(b > a): Cohercion working. $b is greater than $a" 10 println "($b > $a) instanceof Boolean: ${(b > a) instanceof Boolean}" 11 } 12 13 if (a > (Byte) b) { 14 println "Byte $a is greater than (byte) Double $b" 15 println "($a > (Byte) $b) instanceof Boolean: ${(a > (Byte) b) instanceof Boolean}" 16 }
generaciondecodigos@nereida:~/Lgroovy/overloading$ cat -n isCase1.groovy 1 class A{ 2 boolean isCase(Object o){ 3 if(o == 'A') return true 4 else return false 5 } 6 } 7 8 def a= new A() 9 10 println "a.isCase('A') = "+a.isCase('A') 11 println "('A' in a) = "+('A' in a) 12 13 println "(a.isCase('Z') = "+ (a.isCase('Z')) 14 println "('Z' in a) = "+ ('Z' in a)
generaciondecodigos@nereida:~/Lgroovy/overloading$ cat -n isCase2.groovy 1 class MyList extends ArrayList { 2 boolean isCase(Object val) { 3 return val == 66 4 } 5 } 6 7 def myList = new MyList() 8 myList << 55 9 println (55 in myList) 10 println (66 in myList)
generaciondecodigos@nereida:~/Lgroovy/overloading$ cat -n Switch1.groovy 1 def values= [ 2 'abc': 'abc', 3 'xyz': 'list', 4 18: 'range', 5 31: BigInteger, 6 'dream': 'something beginning with dr', 7 1.23: 'none', 8 ] 9 values.each{ 10 def result 11 switch( it.key ){ 12 case 'abc': //if switched expression matches case-expression, execute all 13 //statements until 'break' 14 result= 'abc' 15 break 16 case [4, 5, 6, 'xyz']: 17 result= 'list' 18 break 19 case 'xyz': //this case is never chosen because 'xyz' is matched by 20 //previous case, then 'break' executed 21 result= 'xyz' 22 break 23 case 12..30: 24 result= 'range' 25 break 26 case Integer: 27 result= Integer //because this case doesn't have a 'break', result 28 //overwritten by BigInteger in next line 29 case BigInteger: 30 result= BigInteger 31 break 32 case ~/dr.*/: 33 result= 'something beginning with dr' 34 break 35 case {it instanceof Integer && it>30}: //use Closure 36 result= 'result is > 30' 37 break 38 default: 39 result= 'none' 40 } 41 println "it = (${it.key}, ${it.value}) => result == $result" 42 }
switch
si la clase de la expresión de un case
no dispone de un método isCase
?
Casiano Rodríguez León