2014/12/19

scala study memo

・Predef is a great place to start to understand the Scala Library
==>Predefに定義された関数は、すべてのScalaのソースにおいて自動でimportされている
printやprintlnはPredefで定義されている。JavaのようにSystem.out.printlnという長い記述をしなくても、printlnのみで標準出力ができるのは、これのお陰。

・Array===>scala.collection.mutable.ArrayLike

scala myfirstScript.scala my first script
==>args...引数のこと  args.foreach(println);


追加:
    var oldList=List(1,2)
    val newList=3 :: oldList
    ==>newList 3:2:1
    var theList= newList : +3

the :: method. The job of the :: method is to create a new List with all the existing
elements plus the new element added at the front of the List. To add at the end of
the List, invoke the :+ method:


val afterDelete = newList.filterNot(_ == 3)
==>filterNot(_ == 3)


_  An underscore has a special meaning in Scala, and in this context it’s a placeholder
for a parameter; in your case, use two underscores
==> evenNumbers.foldLeft(0) { _ + _ }

val hasUpperCase = name.exists(_.isUpper)

==>temporary value,variablesだよね。。。



・def breakable(op: => Unit) { ... }
What’s this op: => Unit? The special right arrow (=>) lets Scala know that the
breakable function expects a function as a parameter
The right side of the => defines the return type of the function

def foldLeft(initialValue: Int, operator: (Int, Int) => Int)= { ... }
==>the funuction paramater with two its own paramters!



val files = new java.io.File(".").listFiles
   for(file <- files) {
   val filename = file.getName
   if(fileName.endsWith(".scala")) println(file)
}
 <- In Scala this is called a generator, and the job of a generator is to iterate
through a collection.


for { a <- aList; b <- bList } println(a + b)
for { a <- aList; b <- bList } yield(a + b)
==>yield 新たなListが作成された。。。


mkString(",")
==>java splitだよね。。。


def ordinal(number:Int) = number match{
  case 1 => println("1st")
  case 2 => println("2st")
  case 3 => println("3st")
  case 4 => println("4st")
  case 5 => println("5st")
  case _ =>
}


java.util.Arrays.asList(scalaList.toArray:_*)
====》
:_*
tells the Scala compiler to send the result of toArray as a variable argument to the
Arrays.asList method;


class MongoClient(val host:String, val port:Int)
val client = new MongoClient("127.0.0.1", 123)
==>Scala also uses the new keyword for creating instances of a class. But
wait a minute?where’s the body of the MongoClient class? In Scala that’s optional

private
==>so that the Scale will not create get ,set methord automiclly



class MyScript(host:String) {
    require(host != null, "Have to provide host name")
    if(host == "127.0.0.1") println("host = localhost")
    else println("host = " + host)
}
==>require!


・package is a special object to grouping classes
package com {
  package mongo {
    import ....
    class
  }
}


import java.sql.{Date => SqlDate}
val sqlDate = new SqlDate(now.getTime)

import java.sql.{Date => _ }
==>The Date class from the java.sql package is no longer visible for use.


:load nnnn.scala
:l nnn.scala


object RichConsole {
def p(x: Any) = println(x)
}
Here RichConsole is a singleton object. The object declaration is similar to a class declaration
except instead of class you’re using the object keyword. To invoke the new
p method,


object DB {
def apply(underlying: MongDB) = new DB(underlying)
}
====>factory in scala

Class UpdatableCollection
extends DBCollection(collection(name)) with Updatable
==>with .... trait



class Outer {
    class Inner {
        private[Outer] def f() = "This is f"
        private[innerpkg] def g() = "This is g"
        private[outerpkg] def h() = "This is h"
}
}
==>Outer,innerpkg,outerpkg



final」はクラスやメンバーに対して設定できる修飾詞です。
   finalが設定されたクラスは継承できません。
   finalが設定されたメンバーはオーバーライドできません。
sealed」はクラスに設定できる修飾詞です。
   sealedとされたクラスは、同一ファイル内のクラスからは継承できますが、別ファイル内で定義されたクラスでは継承できません。
   ただし、sealedクラスを継承したクラスは、別ファイルのクラスからも継承できます。



・class Stack[+A] {
class Stack[-A] {
=>Scalaのコレクションクラス(List, Seqなど)は、List[+A]と型名の前に+を付けてcovariant(共変)な型を許すように定義されています。covariantとは、Aのクラスを拡張したクラスBがあれば、List[B]はList[A]として代入できることを意味します
関数側で型パラメーターに縛りが必要ない場合は_を使えばOKだった。
共変はサブクラスを許容する場合に使う


・値クラス (value class) は実行時のオブジェクトの割り当てを回避するための Scala の新しい機構だ
は新たに定義付けされる AnyVal のサブクラスによって実現される
=>    class Wrapper(val underlying: Int) extends AnyVal


・case class VS class
パターンマッチを使う場合にはcase class、
それ以外は普通のclass、と使い分けています
==>case classはcase classを継承することができない



hierarchy

.git
==>index file
The index records
and retains those changes, keeping them safe until you are ready to commit them.


=>content-addressable storage system
location-addressed disk storage with built-in search capability. The search logic was incorporated into the disk controller
First, Git’s object store is based on the hashed computation of the contents of its objects,
not on the file or directory names from the user’s original file layout.
If two separate files have exactly the same content, whether in the same or different
directories, Git stores a single copy of that content as a blob within the object store.

★It’s important to see Git as something more than a VCS: Git is a content tracking system.


★What if you only add, say,one line to a file, doesn’t Git store the complete content of both versions?
Luckily, the answer is “No, not really!”
Instead, Git uses a more efficient storage mechanism called a pack file.

Instead, Git uses a more efficient storage mechanism called a pack file. To create a
packed file, Git first locates files whose content is very similar and stores the complete
content for one of them. It then computes the differences, or deltas, between similar
files and stores just the differences. For example, if you were to just change or add one
line to a file, Git might store the complete, newer version and then take note of the one
line change as a delta and store that in the pack too.



★blod <- tree <- commit <- tag(branch)