Category: ‘Scala’

Playframework 事始め

2012年12月24日 Posted by PURGE

先日のScala事始めに引き続き、ScalaもしくはJavaのフレームワークである Playframework に興味を持ってみた。

理由としては、私自身、元々Java専門。しかしJavaでWeb開発するのは、かなり苦痛。
そこでRuby on Railsに暫く乗り換えていたのですが、Rubyはやっぱりすごく楽ちん。

でも、やはり業務での利用となると、MySQLだけでなく、業務専用のDBとの連携が必要となる。確かにRailsでも業務専用DBとの連携は可能なのだが、私自身が、にわかエンジニアなので、どこか違和感と不安感を感じていた。

そこで、Railsの便利さと、Javaの安心感を兼ねそろえていると噂の Scala と、その代表的なフレームワークである Play2 に興味を持った経緯である。

Playframework Home

早速zipダウンロード。

オンラインマニュアルには、権限の無いディレクトリにインストールをすることを非推奨としているようであるが、私の場合は、下記ディレクトリにインストール。

/usr/local/play-2.0.4

私の開発環境は、Macなので、ユーザディレクトリにある .bashrc に下記を追記。

$ vi .bashrc
export PATH=${PATH}:/usr/local/play-2.0.4

環境変数を有効にして、初めての playコマンド。

$ source .bashrc

$ play help
Getting net.java.dev.jna jna 3.2.3 ...
:: retrieving :: org.scala-sbt#boot-app
	confs: [default]
	1 artifacts copied, 0 already retrieved (838kB/176ms)
Getting Scala 2.9.1 (for console)...
:: retrieving :: org.scala-sbt#boot-scala
	confs: [default]
	4 artifacts copied, 0 already retrieved (19939kB/1169ms)
Getting play console_2.9.1 2.0.4 ...
:: retrieving :: org.scala-sbt#boot-app
	confs: [default]
	5 artifacts copied, 0 already retrieved (3667kB/208ms)
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0.4, http://www.playframework.org

Welcome to Play 2.0!

These commands are available:
-----------------------------
license            Display licensing informations.
new [directory]    Create a new Play application in the specified directory.

You can also browse the complete documentation at http://www.playframework.org.

うまく行っているようだ。
Playプロジェクトを作成するディレクトリに移動して、早速新規プロジェクトを作成。プロジェクト名は、PlayTest。

$ cd /Works
$ play new PlayTest
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0.4, http://www.playframework.org

The new application will be created in /Works/PlayTest

What is the application name? 
> PlayTest

Which template do you want to use for this new application? 

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

> 1

OK, application PlayTest is created.

Have fun!

プロジェクトの雛形が作成された。
今日はこれまで。

Scala事始め

2012年12月14日 Posted by PURGE

Scalaに興味を持って、というかRubyでも良かったのだが・・・。
Java版のRails のようなお気軽言語を探していたらこれだった。

まずは練習です。

object Main {
  
  def main(args:Array[String]):Unit = {
    var myVarName:String = "バー"
    myVarName = "2" + 2

    val myValName = "バル"
    val fValue = 2.345 
      
    println("Hello " + myVarName + " 。")
    println("Hello " + myValName + " 。")
    println(fValue + 7.655)
    println("-------")
    
    //配列
    val books:Array[String] = Array("辞書","参考書","問題集","新書","図鑑","小説")
    println(books.length + "冊")
    for(book <- books){
      println(book)
    }
    println("-------")
    
    //リスト
    val cars:List[String] = List("カローラ","プリウス","マーチ","フィット","ムーブ")
    println(cars.length + "台")
    for(car <- cars){
      println(car)
    }
    println("-------")
    
    //マップ
    val citys:Map[String, String]= Map("1"->"東京", "2"->"大阪", "3"->"福岡")
    println(citys("1"))
    println(citys("2"))
    
    println(citys.keys)
    println(citys.values)
    
    //関数
    println(add(5, 7))
    println(reduce(5, 7))
    println(hello)
    bye
    
  }
  
  def add(x: Int, y: Int):Int = {
    x + y
  }
  
  def reduce(x: Int, y: Int):Int = {
    x - y
  }

  def hello:String = {
    "早く帰りたいよ~!!!"
  }
  
  def bye:Unit = {
    println("さよなら")
  }
}

実際やってみると、癖あるな~。