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
// objectキーワードはシングルトンオブジェクト | |
object Hello { | |
// メイン・エントリ・ポイント | |
def main(args: Array[String]) { | |
println("hello, world!") | |
} | |
} |
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
// フィズバズ | |
object FizzBuzz{ | |
def fizzBuzz(max: Int) = { | |
require(max > 0) // assertion | |
(1 to max) // 1~(max-1)のシーケンス生成。1 to maxの箇所は、オブジェクトの1引数メソッドの糖衣構文で、1.to(max)と同じ意味 | |
.map( // お馴染みmap関数はLINQ風にメソッド呼び出し | |
(x: Int) => // lambda式もC#風と思うとわかりやすいです | |
(x % 3, x % 5) match { // タプル生成→パターンマッチで分岐する形はF#(ML)っぽく書けます | |
case (0, 0) => "FizzBuzz!" | |
case (0, _) => "Fizz!" |
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
// クロージャー | |
object Closures { | |
// lambda式はclosureを作ります | |
def makeCounter = { | |
var x = 0 // varは再代入可能な変数の定義 | |
val f = () => { // valは束縛、() => {}の部分はラムダ式 | |
x += 1 | |
x // コードブロック中で最後の式が評価結果になります | |
} | |
f |
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
// カリー化など | |
object Currying { | |
def add(x: Int, y: Int, z: Int) = x + y + z // 式でコードブロック{ ~ }を置き換え可能 | |
def curryTest = { | |
// defで定義したメソッドだとカリー化できないので、スペース+アンダースコアをつけて関数オブジェクトを取得します | |
val addFunc = add _ | |
// add _ と書くのは、 add(_, _, _) と書くのと同じっぽい気がします…… | |
// この辺参考になりそうかも: [_ と _ の違い - kmizuの日記](http://kmizu.hatenablog.com/entry/20120917/1347892258) |
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
// 主要なコレクションクラスの確認と、各種の糖衣構文 | |
object Collections { | |
// cons cell list | |
def listTest = { | |
println("--listTest--") | |
// create | |
val list = List("a", "b", "c", "d", "e") | |
// ↑["a", "b", "c", "d", "e"] | |
// 対象オブジェクト(引数リスト)の記法は、applyメソッドの糖衣構文です。 | |
// 型名(引数リスト)の記法になっているのは、Listクラスに同名のシングルトンオブジェクト(コンパニオンオブジェクト)が定義されていて |
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
package object DataTypes { | |
// クラス定義は1ファイルに複数書き並べることもできます | |
// クラスのインナークラスとしても書けます(Javaと同様、外部クラスへの参照を保持します)。 | |
// package objectは、普通のシングルトンオブジェクトのように書けて、利用側でのimport方法がパッケージの形式になります。 | |
// --------------------------------------------------------------------------------- | |
// immutable data | |
class Person(val name: String, val birthYear: Int) {// コンストラクタで同時に読み取り専用プロパティを定義 | |
// その他コンストラクタ | |
def this() = { |
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
// オブジェクトの比較 | |
object Comparing { | |
// --------------------------------------------------------------------------------- | |
// 適当なcase class | |
case class SomeCase(key1:Int, key2:String) | |
// 適当なclass | |
class SomeClass(val key1:Int, val key2:String) | |
def equalsTest = { | |
println("equalsTest") |
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
// 評価戦略など | |
object Evaluations { | |
def isEven(x: Int) = x % 2 == 0 | |
// 偶数の時だけmessageをprintします。messageは名前呼び出しな引数 | |
def printWhenEven(x: Int, message: => String) = { | |
if(isEven(x)) println(message) | |
else println("...") | |
} |
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
// もなもなしたアレ関係 | |
package object Monads { | |
// Option関係 | |
def optionTest = { | |
println("--optionTest--") | |
// Option(Maybe)型は標準搭載 | |
val o1 = Some("string!!") | |
val o2 = None |
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
;; 整数値からfizzbuzz結果の値に変換します。 | |
(define (int->fizzbuzz x) | |
(let ((is-fizz (= (modulo x 3) 0)); 3で割り切れたらFizz | |
(is-buzz (= (modulo x 5) 0))); 5で割り切れたらBuzz | |
(let ((is-fizzbuzz (and is-fizz is-buzz))); FizzかつBuzzならFizzBuzz | |
(cond | |
(is-fizzbuzz "FizzBuzz") | |
(is-fizz "Fizz") | |
(is-buzz "Buzz") | |
(else x))))) |
OlderNewer