Last active
August 29, 2015 14:20
-
-
Save msbaek/e830f66f5165fbbb844e to your computer and use it in GitHub Desktop.
scala symbols
This file contains hidden or 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
| /** ********************************** | |
| * -> "in" generator | |
| * ***********************************/ | |
| for (arg <- List("one", "two", "three")) | |
| println(arg) | |
| /** ********************************** | |
| * ::: 리스트를 붙일 때 | |
| * **********************************/ | |
| List(1, 2) ::: List(3, 4, 5) | |
| /** ********************************** | |
| * :: cons. 새원소를 기존 리스트 앞에 추가한 새 리스트 반환 | |
| * ***********************************/ | |
| 1 :: List(2, 3) | |
| /** ********************************** | |
| * => | |
| * **********************************/ | |
| /** 1. 함수 literal 선언 */ | |
| // thrill.exists(s => s == "until") | |
| /** 2. call by name */ | |
| def callByName(x: => Int) = { | |
| println("x1=" + x) | |
| println("x2=" + x) | |
| } | |
| callByName(2 * 10) | |
| //callByName(someHeavyCalculation()) | |
| /** ********************************** | |
| * _ | |
| * **********************************/ | |
| /** 1. 튜블의 원소에 접근 */ | |
| val pair = (99, "one") | |
| pair._1 | |
| /** 2. unary operator */ | |
| (2.0).unary_- | |
| /** 3. 디폴트값 활당 */ | |
| //var celsius: Float = _ | |
| /** 4. default case. "case _ => xxx" */ | |
| /** 5. 위치 표시자 */ | |
| // 함수 리터럴을 좀 더 간결하게 표현하기 위해 "_"를 하나 이상의 파라미터에 대한 '위치표시자'로 사용 가능 | |
| // 단 함수 리터럴에서 각 인자는 한번씩만 나와야함 | |
| // ------------------------------ | |
| /** 6. partial application */ | |
| def adder(m: Int, n: Int) = m + n | |
| val add2 = adder(2, _: Int) | |
| add2(3) | |
| /** ********************************** | |
| * """ Raw String | |
| * **********************************/ | |
| println( | |
| """Welcome | |
| |Type "Help"... | |
| """.stripMargin) | |
| /** ********************************** | |
| * ' : symbol literal | |
| * **********************************/ | |
| 'cymbal | |
| // def updateRecordByName(r:Symbol, value:Any) | |
| // updateRecordByName('favoriteAlbum, "ok") | |
| // ------------------------------ | |
| /** ********************************** | |
| * require: 전제조건 | |
| * **********************************/ | |
| require(0 == 1) // IllegalArgumentException | |
| // ------------------------------ | |
| /** ********************************** | |
| * *: 반복 파라미터 | |
| * **********************************/ | |
| // args: String* | |
| // ------------------------------ | |
| /** ********************************** | |
| * _* | |
| * **********************************/ | |
| /** 1. 배열을 반복 파라미터가 요구되는 곳에 보냄 */ | |
| // echo(arr: _*) | |
| // | | +-- 배열 전체를 하나의 인자로 다루지 않고, 배열의 각 원소를 echo의 각 인자로 전달 | |
| // | +-- 배열 | |
| // +-- 반복 파라미터를 요구하는 함수 | |
| /** 2. XML 노드의 "임의의 시퀀스"를 위한 패턴 */ | |
| // ------------------------------ | |
| /** ********************************** | |
| * "_root_" | |
| * 숨겨진 package 이름에 접근하기 | |
| * 사용자가 작성한 모든 package 외부에 존재하는 _root_ package | |
| * Listing 13.6 Accessing hidden package names. | |
| * **********************************/ | |
| /** | |
| * "===" 테스트 실패시 더 많은 정보 제공하기 | |
| */ | |
| // ------------------------------ | |
| /** | |
| * "/:" : foldLeft | |
| */ | |
| // ------------------------------ | |
| /** | |
| * +=: | |
| * ListBuffer의 앞에 원소 추가 | |
| */ | |
| // ------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment