Why i like Groovy

This is why i like groovy. I follow an brazilian java list, and last week, a guy posted some logic exercices in java. One of them was:

Given an array of numbers, create a function that searches all instances of a given number an print the number of occurences of this number.
Another guy posted the answer in java:

public class Modulo{
public static  int EncontraNumero(double[]  vetor, double  x, int count){
for (int i = 0; i < vetor.length; i++) {
if (vetor[i]== x)
count++;//calcula quantas vezes apareceu no vetor

}
return count;
}
public static void main(String[] args) {

double[] vet={2.3,5.7,9.65,3.33,4.10,5.23, 12.5,  12.5};
System.out.println(EncontraNumero(vet, 12.5, 0));
}
}

I created the same thing with groovy:


numbers = [2.3,5.7,9.65,3.33,4.10,5.23, 12.5, 12.5]
println numbers.findAll {it == 12.5 }.size

15 lines in 2. Wonderful

About these ads
This entry was posted in groovy, technology and tagged , , . Bookmark the permalink.

3 Responses to Why i like Groovy

  1. euaostrinta says:

    Olá, tudo bem?
    Estou visitando os vizinhos, sabe como é, né? Mesmo sem ser programadora, gostei do groovy… rsrsrs (que mania as pessoas têm de sempre terem opinião pra tudo, mesmo que elas não entendam nada sobre o assunto, né?)
    Falando de tecnologia ou não, acho que vou voltar. Dá uma passada no meu blog, vai que eu falo sobre algo que te interessa, né? rsrsrs

  2. Lucas says:

    Implementaçãozinha porca também essa do cara.

    []s,

    Lucas

  3. André says:

    Em python:

    array = [2.3,5.7,9.65,3.33,4.10,5.23, 12.5, 12.5]
    print len([i for i in array if i == 12.5])
    :D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s