6 ms·
First steps with Scala, say goodbye to bash scripts…
Playframework 2.0 is right around the corner, and it’s core is programmed in Scala, so it’s a wonderful opportunity to give this object-oriented / functional hybrid beast a try…
Like many others, I will pick a very simple script to give my first steps…
- jcdavis 15y agoA nice article, but its worth mentioning that scala for bash script usage is a non-starter for most stuff you might use bash for because JVM startup overhead, even for the client VM, is so substantial
- alexatkeplar 15y agoTrue, but: 1. There are a ton of long-running/cron-style tasks where JVM startup overhead just isn't relevant (and Scala's type safety, access to Java libraries and logging tools rinse Bash), and: 2. If you are using Scala scripting in "live operator" mode, here's a good SO answer on how to speed that up: http://stackoverflow.com/questions/4237863/improving-scala-script-startup-time-client-mode http://stackoverflow.com/questions/4237863/improving-scala-s...
- gaius 15y agoSee http://www.scribd.com/doc/36045849/Practical-Haskell-Programming-scripting-with-types http://www.scribd.com/doc/36045849/Practical-Haskell-Program... Type safety, much better type inference than Scala, and no JVM overhead.
- opensas 15y agoI've found out that with the -savecompiled option the delay is not so pitiful... see http://playlatam.wordpress.com/2012/01/13/first-steps-with-scala-revisited-bash-and-python-strikes-back/#performance http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...
- ww520 15y agoI don't get how the bash scripts are not used. It's still used at #!/bin/sh exec scala "$0" "$@"
- switch007 15y ago/bin/sh is probably a symlink to something other than bash though. It's not using any features/functions of the shell other than to execute scala. It's a stretch calling that a script.
- alexatkeplar 15y agoThat's a one-line Bash wrapper to invoke Scala (line 2) with the original arguments sent to the Scala script, compiling the Scala code which follows below. You can read more at the bottom of the man page here: http://www.scala-lang.org/docu/files/tools/scala.html http://www.scala-lang.org/docu/files/tools/scala.html
- slashclee 15y agoI wonder why they don't use #!/usr/bin/env scala as the first line instead. If there's some hack to support #!/bin/sh in the scala interpreter... ew.
- moonboots 15y agoI still prefer a simple shell script for these use cases. Here's a snippet for the translated file size calculation: translated_files=$(grep "todavida no ha sido traducida" *) total_size=$(du --summarize . | cut -f 1) translated_file_size=$(du --summarize --total $(echo translated_files) | tail -n 1 | cut -f 1) echo "translated size: ${translated_file_size}KB/${total_size}KB $(($translated_file_size/$total_size))%"
- archangel_one 15y agoI find that much more elegant; four lines of bash script which would run on nearly any Unix-like system, versus 49 (or a bit less if you drop the whitespace) of Scala, which won't. Even the shebang is three lines instead of one! I guess this example is nice as an introduction to Scala, but it doesn't seem like Scala is a particularly appropriate replacement for any of the things one would normally use a shell script for.
- viraptor 15y ago> which would run on nearly any Unix-like system Did you mean on nearly any Unix-like system which has the output of du in the same format as the one on your machine? I don't believe those can be treated as standard if you want any kind of portability.
- archangel_one 15y agoOkay, I'm not an expert on variations between different Unices, that may well be the case. The point I was trying to make was more that it would run without having to install a great wodge of Java/Scala dependencies which aren't on most systems by default.
- opensas 15y agoWhy wouldn't it run on any unix, linux, or windows, or wherever a JVM can run? you should run it with "scala status.scala" apart from that, I came to a similar conclusion: http://playlatam.wordpress.com/2012/01/13/first-steps-with-scala-revisited-bash-and-python-strikes-back/#conclusion http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...
- ronreiter 15y agoWhat's wrong with Python?
- opensas 15y agonothing - http://playlatam.wordpress.com/2012/01/13/first-steps-with-scala-revisited-bash-and-python-strikes-back/#conclusion http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...
- jezclaremurugan 15y agonice article, but they could have removed the last part of the title. Its a nice way to learn scala, but a poor way to say goodbye to bash.
- opensas 15y agoyou're right jez, I guess I couldn't resist the temptation of a catchy title, just changed it to: First steps with Scala, a functional alternative to bash scripts… I think it's more appropriate: http://playlatam.wordpress.com/2011/12/05/first-steps-with-scala-say-goodbye-to-bash-scripts/ http://playlatam.wordpress.com/2011/12/05/first-steps-with-s...
- jezclaremurugan 15y agosounds great now, i loved it anyway!
- opensas 15y agoThanks to everybody's feedback on this one... You encouraged me to write another article, mainly because I felt a little guilty for bashing (pun intented) bash and python ;-) Hope you like it http://playlatam.wordpress.com/2012/01/13/first-steps-with-scala-revisited-bash-and-python-strikes-back/ http://playlatam.wordpress.com/2012/01/13/first-steps-with-s...