Scala ならその心配はありません。何の躊躇もなく演算子を書き換えれば独自の世界が展開します。BigInt の扱いも自然です。オペレータ演算子をほぼすべてそのまま使えます。いわゆる中置記法(infix)で書けるので読みやすくデバッグしやすいコードが書けます。
Java: BigInteger で階乗を計算する では Java の BigInteger で階乗を計算するコードを載せました。Scala で書いたのがこれです。これだとなんか特記すべきこともないです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(x:BigInt):BigInt={ | |
if(x<=0){ | |
0 | |
}else{ | |
var counter:BigInt=1 | |
var res:BigInt=1 | |
while(counter <= x){ | |
res *= counter | |
counter+=1 | |
} | |
res | |
} | |
} |
比べてみれば利点は明らかです。言語設定がプログラミングの効率をどこまで上げるのか、現場を顧みない世迷いごとは「プログラミング言語はどれもC言語が基本だから(!)、基本構造が同じなら書けることは同じ」これで論破できますね。