viernes, 5 de julio de 2019

Groovy Language


https://groovy-lang.org/index.html
http://docs.groovy-lang.org/latest/html/groovy-jdk/

Desde la consola de Groovy

> 1 + 1
Result: 2


> (0..<10).size()
Result: 10

> (0..10).class.name
Result: groovy.lang.IntRange

> (0..10).collect()
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> (0..10).collect().class.name
Result: java.util.ArrayList

> ('a'..'z').collect()
Result: [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]

> ('a'..'z').collect().class.name
Result: java.util.ArrayList

> (0..<10).collect{ it }
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

> (0..<10).collect{ it }.class.name
Result: java.util.ArrayList

> (0..<10).collect{it ** 2}
Result: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

> (0..<10).stream()
Result: java.util.stream.ReferencePipeline$Head@9635fa

> (0..<10).stream().class.name
Result: java.util.stream.ReferencePipeline$Head

> (0..<10).stream().map{ it }.forEach{ println it}
0
1
2
3
4
5
6
7
8
9

> (1..<10).stream().map{ 2 ** it}.forEach{println it}
2
4
8
16
32
64
128
256
512

> (1..<10).stream().map{ int i -> 2 ** i}.forEach{println it}
2
4
8
16
32
64
128
256
512

Creamos un archivo groovy textAnalyzer.groovy

import groovy.xml.MarkupBuilder

if (!(args.size() in 1..2)) {
    println "numero incorrecto de parametros"    println()
    println "USO: textAnalyzer TEXTFILE [STRING]"    System.exit 1}

def content = new File(args[0]).text
def charCount = content.size()
def wordCount = content.split(/\W+/).size()

def stringCount
if (args.size() ==  2) {
    stringCount = content.count(args[1])
}

println "charCount:".padRight(10) + charCount
println "wordCount:".padRight(10)+ wordCount
println "strinCount:".padRight(10)+ stringCount

ejemplo de ejecución

C:\Users\Bext\IdeaProjects\groovy01\src>groovy textAnalyzer.groovy
numero incorrecto de parametros

USO: textAnalyzer TEXTFILE [STRING]

C:\Users\Bext\IdeaProjects\groovy01\src>groovy textAnalyzer.groovy textAnalyzer.groovy
charCount:550
wordCount:67
strinCount:null

C:\Users\Bext\IdeaProjects\groovy01\src>groovy textAnalyzer.groovy textAnalyzer.groovy word
charCount:550
wordCount:67
strinCount:3


Hacemos una pequeña modificación al programa textAnalizer.groovy para que genere un archivo de reporte con los resultados en formato xml

import groovy.xml.MarkupBuilder

if (!(args.size() in 1..2)) {
    println "numero incorrecto de parametros"    println()
    println "USO: markupGenerator TEXTFILE [STRING]"    System.exit 1}

def content = new File(args[0]).text
def charCount = content.size()
def wordCount = content.split(/\W+/).size()
def stringCount
if (args.size() ==  2) {
    stringCount = content.count(args[1])
}

new File("text-report.xml").withWriter { Writer w ->
    def builder = new MarkupBuilder(w)
    builder.report {
        'char-count'(charCount)
        'word-count'(wordCount)
        if (stringCount) {
            'string-search' {
                string(args[1])
                occurrences(stringCount)
            }
        }
    }
}

lo ejecutamos
C:\Users\Bext\IdeaProjects\groovy01\src>groovy markupGenerator.groovy markupGenerator.groovy word

el resultado es el archivo  text-report.xml
<report>
  <char-count>761</char-count>
  <word-count>82</word-count>
  <string-search>
    <string>word</string>
    <occurrences>3</occurrences>
  </string-search>
</report>


eot

No hay comentarios:

Publicar un comentario