「Programming in Scala」の読書、一章
積読のProgramming in Scala消化中...
Chapter 1 A Scalable Language
1.1 A language that grows on you
- sample1
var capital = Map("US" -> "Washigton", "France" -> "Paris") capital += ("Japan" -> "Tokyo") capital("France") //=>Paris
- sample1.1
scala.collection.jcl*1のHashMapを利用してみる
import scala.collection.jcl._ var capital = new HashMap[String,String]() capital += ("Japan" -> "Tokyo")
- sample1.2
scala.collection.immutalbeのHashMapを利用してみる*2
import scala.collection.immutable._ //var capital = HashMap[String,String]("US" -> "Washigton", "France" -> "Paris") var capital = HashMap("US" -> "Washigton", "France" -> "Paris") capital += ("Japan" -> "Tokyo")
Growing new types
- sample2
def factorial(x: BigInt): BitInt = if(x == 0) 1 else x * factorial(x-1) factorial(30) //=>BigInt = 265252859812191058636308480000000
factorialの引数をIntで動作してみる。
- sample2.1
def factorial(x: Int): Int = if(x == 0) 1 else x * factorial(x-1) factorial(30) //=>Int = 1409286144
はい、駄目でしたw
import java.math.BigInteger def factorial(x:BigInteger): BigInteger = { if(x == BigInteger.ZERO) BigInteger.ONE else x.multiply(factorial(x.subtract(BigInteger.ONE))) } factorial(new BigInteger("30")) //=>java.math.BigInteger = 265252859812191058636308480000000
javaのBigInteger使いづらいw;
Growing new control constructs
Actorの紹介、キタァーーーー。
けど、サンプル片で動くコードではないかω;
Scala is object-oriented
Scala is functional
- tow main ideas.
- functions are first-calss values
- the operations of a program should map input values to output values
Stringの扱いではRubyは配列のmutabuleでJavaはストリームのimmutableだからJavaが関数的らしいw
Scala is concise
コードの短さはパワーだ>A<
Scala is high-level
文字列中に大文字があるかどうかのJavaとScalaのコード比較。
boolean nameHasUpperCase = false; for(int i = 0; i < name.length(); ++i){ if(Character.isUpperCase(name.charAt(i))){ nameHasUpperCase = true; break; } }
val nameHasUpperCase = name.exists(_.isUpperCase)
で、動かしてみる。
var name:String = "scala is high-level." val nameHasUpperCase = name.exists(_.isUpperCase) //=>false
var name:String = "Scala is high-level." val nameHasUpperCase = name.exists(_.isUpperCase) //=>true
Scala is statically typed
- nested class types
- parameterize types with generics
- combine types intersectins
- hide details of types using abstract types
- combine types intersections と hide details of types using abstract types って何だろう?
静的言語の型の冗長な記述は、
- type inference*3
- pattern matching
- 新しい記法
- compose types
で、回避するのだ>w<
1.4 Scala's roots
いろんな言語から一杯影響^^;
1.5 Conclusion
Scalaは銀の銃弾では有りません。
Programming in Scala: A Comprehensive Step-by-step Guideposted with amazlet at 09.04.30Martin Odersky Lex Spoon Bill Venners
Artima Inc
売り上げランキング: 349