What is the "proper" of writing the standard read-while loop in Scala? By proper I mean written in a Scala-like way as opposed to a Java-like way.
Here is the code I have in Java:
MessageDigest md = MessageDigest.getInstance( "MD5" ); InputStream input = new FileInputStream( "file" ); byte[] buffer = new byte[1024]; int readLen; while( ( readLen = input.read( buffer ) ) != -1 ) md.update( buffer, 0, readLen ); return md.digest();
Here is the code I have in Scala:
val md = MessageDigest.getInstance( hashInfo.algorithm ) val input = new FileInputStream( "file" ) val buffer = new Array[ Byte ]( 1024 ) var readLen = 0 while( readLen != -1 ) { readLen = input.read( buffer ) if( readLen != -1 ) md.update( buffer, 0, readLen ) } md.digest
The Scala code is correct and works, but feels very un-Scala-ish. For one it is a literal translation of the Java code, taking advantage of none of the advantages of Scala. Further it is actually longer than the Java code! I really feel like I'm missing something, but I can't figure out what.
I'm fairly new to Scala, and so I'm asking the question to avoid falling into the pitfall of writing Java-style code in Scala. I'm more interested in the Scala way to solve this kind of problem than in any specific helper method that might be provided by the Scala API to hash a file.
(I apologize in advance for my ad hoc Scala adjectives throughout this question.)
Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler.
In Scala, we use a break statement to break the execution of the loop in the program. Scala programing language does not contain any concept of break statement(in above 2.8 versions), instead of break statement, it provides a break method, which is used to break the execution of a program or a loop.
Using for-loop with Filters In Scala, for-loop allows you to filter some elements from the given collection using one or more if statements in for-loop. Syntax: for(i<- List if condition1; if condition2; if condition3; ...) { // code.. }
Overview. The while construct consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is true, the code within all of their following in the block is executed. This repeats until the condition/expression becomes false.
Based on Rex's post that he mentioned:
Stream.continually(input.read(buffer)).takeWhile(_ != -1).foreach(md.update(buffer, 0, _))
You should replace the var readLen + while {...} lines with it, it produces the same result.
As Rex mentioned, it works with scala 2.8.
What Rex Kerr suggests in his comment is the following:
val md = MessageDigest.getInstance("MD5") val input = new FileInputStream("foo.txt") val buffer = new Array[ Byte ]( 1024 ) Stream.continually(input.read(buffer)) .takeWhile(_ != -1) .foreach(md.update(buffer, 0, _)) md.digest
The key is the Stream.continually
. It gets an expression which is evaluated continually, creating an infinite Stream
of the evaluated expression. The takeWhile
is the translation from the while
-condition. The foreach
is the body of the while
-loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With