Scala Script vs Python Script

Comparison between scala script and python script

To print file’s contents line by line, you can use some programming languages to write scripts:
bash shell, python, scala, ruby …

I am familiar with bash, python, scala, java, so I try use these to write scripts.

  1. bash
    Bash is too complex to write this type script. So I give up to write by bash.

  2. python
    It’s good enough to do this thing.

  3. scala
    I love scala, so I will try to use scala to write. And scala is so smart.

  4. java
    No way.

1. Python Script

1
2
3
// test.py
for line in open("test.txt") :
print(len(line.split(",")))

execute it:

1
python3 test.py

2. Scala Script

1. General type

1
2
3
4
5
6
7
8
9
10
11
12
//Test.scala
object Test {
def main(args: Array[String]): Unit = {
execute()
}

def execute() = {
scala.io.Source.fromFile("./test.txt").getLines.foreach { line =>
println(line.split(",").size)
}
}
}

execute it:

1
2
scalac Test.scala
scala Test

2. Scala Shell Script

1
2
3
4
5
6
7
8
9
10
11
12
//Test.sh
#!/usr/bin/env scala
object Test extends App{
execute()

def execute() = {
scala.io.Source.fromFile("./test.text").getLines.foreach { line =>
println(line.split(",").size)
}
}
}
Test.main(args)

execute it:

1
2
chomod 770 Test.sh
./Test.sh

Summary

If you are familiar with python, I think you will glad to use python than scala to write this script.
It is so easy and powerful.
If not familiar with python, you can use scala too.
Of course, you can use ruby and other script languages.