FOR, WHILE Is Too Easy, Let’s Go Looping
Caoyuan Blog - - October 22, 2008With several 10k code in Erlang, I’m familiar with functional style coding, and I found I can almost rewrite any functions in Erlang to Scala, in syntax meaning.
Now, I have some piece of code written in Java, which I need to translate them to Scala. Since “for”, “while”, or “do” statement is so easy in Java, I can find a lot of them in Java code. The problem is, should I keep them in the corresponding “for”, “while”, “do” in Scala, or, as what I do in Erlang, use recursive function call, or, “loop”?
I sure choose to loop, and since Scala supports recursive function call on functions defined in function body (Erlang doesn’t), I choose define these functions’ name as “loop”, and I tried to write code let “loop” looks like a replacement of “for”, “while” etc.
Here’s a piece of code that is used to read number string and convert to double, only piece of them.
The Java code:
public class ReadNum { private double readNumber(int fstChar, boolean isNeg) { StringBuilder out = new StringBuilder(22); out.append(fstChar); double v = '0' - fstChar; // the maxima length of number stirng won't exceed 22 for (int i = 0; i < 22; i++) { int c = getChar(); switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': v = v * 10 - (c - '0'); out.append(c); continue; case '.': out.append('.'); return readFrac(out, 22 - i); case 'e': case 'E': out.append(c); return readExp(out, 22 - i); default: if (c != -1) backup(1); if (!isNeg) return v; else return -v } } return 0; }
}
The Scala code:
class ReadNum { private def readNumber(fstChar:Char, isNeg:Boolean) :Double = { val out = new StringBuilder(22) out.append(fstChar) val v:Double = '0' - fstChar def loop(c:Char, v:Double, i:Int) :Double = c match { // the maxima length of number stirng won't exceed 22 case _ if i > 21 => 0 case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => out.append(c) val v1 = v * 10 - (c - '0') loop(getChar, v1, i + 1) case '.' => out.append('.') readFrac(out, 22 - i) case 'e' | 'E' => out.append(c) readExp(out, 22 - i) case _ => if (c != -1) backup(1) if (isNeg) v else -v }; loop(getChar, v, 1) }
}
As you can see in line 25, the loop call is put at the position immediately after the “loop” definition, following “}; “, I don’t put it to another new line, it makes me aware of the “loop” function is just used for this call.
And yes, I named all these embedded looping function as “loop”, every where.
Categories: Blogs Caoyuan Blog
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» JafarAL (Jafar ALi ALatas): Wu ching—->RT @ariipul: Pat kay >>>> RT @JafarAL: Ktemu dewa erlang sm sun go kong RT JulianCAL: Ke langit ke-7 RT @JafarAL
» ariipul (Saiful Bahri): Pat kay >>>> RT @JafarAL: Ktemu dewa erlang sm sun go kong RT JulianCAL: Ke langit ke-7 RT @JafarAL: Kmana malam ini ? Yg gak macet..
» JafarAL (Jafar ALi ALatas): Ktemu dewa erlang sm sun go kong RT @JulianCAL: Ke langit ke-7 RT @JafarAL: Kmana malam ini ? Yg gak macet..
» VaiguntaSarathy (Vaigunta Sarathy): FS#29929: [erlang] Simplify PKGBUILD http://t.co/rDJ85DMb
» vadson27 (vadson ferreira): FS#29929: [erlang] Simplify PKGBUILD http://t.co/6Oox4Ehf
» vaibhavsingh544 (Vabhav Singh): FS#29929: [erlang] Simplify PKGBUILD http://t.co/Sjhjc2aM
» vaccumakeh (Vladimir Rostov): FS#29929: [erlang] Simplify PKGBUILD http://t.co/S86CjIjg
» ITJobs_EU_UK (ITJobs_EU_UK): #JB Ruby Developer ( Ruby / RoR Erlang LAMP ): Job Description : Ruby Developer / Software Engineer Location: Lo… http://t.co/74omWQ9m
» udzura (Uchio KONDO): 文字列操作が弱い、は今のErlangではfalseであると
» udzura (Uchio KONDO): Erlang , R14 あたりからutf-8の文字列の扱いに強くなったとのこと #shinjukuex
Statistics
Number of aggregated posts: 10498
Number of comments: 2115
Most recent article: May 15, 2012
Latest comments
» cheap soccer jerseys on Memory Models in Erlang vs Java: Nice discussion here,you are doing a great job. i was looking for this information. i found it on your page…
» mandesejohn on Couchbase Meetup at new HQ: Thanks for sharing experience. It should be really a great post. It should be knowledgeable and informative. Keep it up. flower delivery columbus ohio
» vermaseo on Scale means Skills: I’m surprised people are still commenting about this. George has been moved on to bigger and better things with the president for awhile now.ledikanten