8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Clojure × Scala (さいきょうタッグの連携技)

8
Last updated at Posted at 2014-10-04

なんと Clojure から Scala で書いたコードも使えるようで、試してみました。

使用するもの

ディレクトリ構成

スクリーンショット 2014-10-04 13.19.44.png

試してみる

下準備

  • dependencies に scala のライブラリを指定します。
  • plugins を書きます。
  • scala-source-path を書きます。
    (今回はscalaというディレクトリに.scalaをおいているので scala を指定してやります。)
  • scalaのバージョンを指定します。
    ライブラリに2.10.3を指定しているので、ここも2.10.3を指定します。
  • pre-tasks を設定します。
    (こうすることでいちいち lein scalac と叩かなくて済みます。)
project.clj
(defproject scala-clj "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [org.scala-lang/scala-library "2.10.3"]]
  :plugins [[io.tomw/lein-scalac "0.1.2"]]
  :scala-source-path "scala"
  :scala-version "2.10.3"
  :prep-tasks ["scalac"]
  :main scala-clj.core)

Scala のコードを記述

みんなscala書けるでしょ?
clojureに比べれば俄然理解は簡単なので、説明は省きます。
(説明するまでもないくらい簡単なコードですしお寿司)

Main.scala
object Main {
 def clojure(txt: String) = {
   println("hello Scala World> txt is " + txt)
 }
}

Clojure から Scala の呼び出し

core.clj
(ns scala-clj.core)

(defn -main []
  (Main/clojure "clojure"))

実行結果

$ lein run
Element '/Users/秘密♥/秘密♥/scala-clj/dev-resources' does not exist.
hello Scala World> txt is clojure

なんか/dev-resourcesとか言われてますが、無視。

終わりに

実に簡単です。

「Clojureで書きました!」とか言って、表面だけClojureで書いてあとは全部Scalaで書いても嘘じゃない!

Clojureなんて書かんくてよかったんや!!!黙ってみんなScala使ったらええんや!!!(違う

8
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?