Too Many Languages!

groovy, Java, programming, Python, Scala

ARGH! I’ve been bounced between languages. It’s driving me NUTS.

This is a comparison of how to do something in each to help me map between them.

There is definitely more to each of these languages than what’s shown. But I have to start somewhere….

English:

Given a list of integers ( 1, 2, 3 ), cube each value, then filter out even numbers, then calculate the sum of the remaining numbers.

Java:

int answer = Stream.of( 1, 2, 3 )
 .map( i -> i * i * i )
 .filter( i -> i % 2 != 0 )
 .mapToInt( i -> i )
 .sum();

Groovy:

int answer = [ 1, 2, 3 ]
 .collect { it * it * it }
 .findAll { it % 2 != 0 }
 .sum()

Scala:

val answer = List( 1, 2, 3 )
 .map( i => i * i * i )
 .filter( _ % 2 != 0 )
 .sum

Python:

answer = sum([
  j for j in [
    i * i * i for i in [1, 2, 3]
  ] if j % 2 > 0
])