scala

Scala ラムダ 超入門

ryoji@ubuntu:~$ scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def inc(x: Int) = x + 1
inc: (x: Int)Int

scala> inc(2)
res0: Int = 3

scala> val inc2: Int => Int = x => x + 1
inc2: Int => Int = <function1>

scala> inc2(2)
res1: Int = 3

scala> val add : (Int, Int) => Int = (x, y) => x + y
add: (Int, Int) => Int = <function2>

scala> add(2,3)
res2: Int = 5

scala> ((x: Int, y: Int) => x + y)(2, 3)
res3: Int = 5

scala> 

scala> 

scala> val combine = (a: Any) => (b: Any) => (c: Any) => a +: b +: List(c)
combine: Any => (Any => (Any => List[Any])) = <function1>

scala> 

scala> val a = combine(1)
a: Any => (Any => List[Any]) = <function1>

scala> val b = a(2)
b: Any => List[Any] = <function1>

scala> val c = b(3)
c: List[Any] = List(1, 2, 3)

scala> println(c)
List(1, 2, 3)

scala> println(combine('a')('b')('c'))
List(a, b, c)

scala> println(List(1, 2, 3, 4, 5).map(x => x * 2))
List(2, 4, 6, 8, 10)

scala> println(for (x <- List(1, 2, 3, 4, 5)) yield x * 2)
List(2, 4, 6, 8, 10)

scala> println(List(1,2,3,4,5,6,7,8,9).filter(_ < 5))
List(1, 2, 3, 4)

scala> println(for (x <- List(1, 2, 3, 4, 5) if x < 5) yield x)
List(1, 2, 3, 4)

14/03/2017 London Scala Community

sbt

echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt

http://www.scala-sbt.org/0.13/docs/ja/Installing-sbt-on-Linux.html

sbt.png

ryoji@ubuntu:~$  sbt new sbt/scala-seed.g8
Getting org.scala-sbt sbt 0.13.15  (this may take some time)...
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt/0.13.15/jars/sbt.jar ...
	[SUCCESSFUL ] org.scala-sbt#sbt;0.13.15!sbt.jar (1552ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/main/0.13.15/jars/main.jar ...
	[SUCCESSFUL ] org.scala-sbt#main;0.13.15!main.jar (2734ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/compiler-interface/0.13.15/jars/compiler-interface.jar ...
	[SUCCESSFUL ] org.scala-sbt#compiler-interface;0.13.15!compiler-interface.jar (2210ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/actions/0.13.15/jars/actions.jar ...
	[SUCCESSFUL ] org.scala-sbt#actions;0.13.15!actions.jar (2356ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/main-settings/0.13.15/jars/main-settings.jar ...
	[SUCCESSFUL ] org.scala-sbt#main-settings;0.13.15!main-settings.jar (2555ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/interface/0.13.15/jars/interface.jar ...
	[SUCCESSFUL ] org.scala-sbt#interface;0.13.15!interface.jar (1650ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/io/0.13.15/jars/io.jar ...
	[SUCCESSFUL ] org.scala-sbt#io;0.13.15!io.jar (1690ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/ivy/0.13.15/jars/ivy.jar ...
	[SUCCESSFUL ] org.scala-sbt#ivy;0.13.15!ivy.jar (2252ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/logging/0.13.15/jars/logging.jar ...
	[SUCCESSFUL ] org.scala-sbt#logging;0.13.15!logging.jar (1634ms)
downloading https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/logic/0.13.15/jars/logic.jar ...
...

Minimum Scala build. 

name [My Something Project]: miniscala

Template applied in ./miniscala

miniscala.png

ryoji@ubuntu:~$ sbt new playframework/play-scala-seed.g8
[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from /home/ryoji/project
[info] Set current project to ryoji (in build file:/home/ryoji/)

This template generates a Play Scala project 

name [play-scala-seed]: miniplay
organization [com.example]: 
scala_version [2.11.11]: 
play_version [2.5.14]: 
scalatestplusplay_version [2.0.0]: 

Template applied in ./miniplay
ryoji@ubuntu:~$ cd miniplay/
ryoji@ubuntu:~/miniplay$ ls
app           build.sbt  gradle   gradlew.bat  public
build.gradle  conf       gradlew  project      test
ryoji@ubuntu:~/miniplay$ sbt run
[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from /home/ryoji/miniplay/project
[info] Updating {file:/home/ryoji/miniplay/project/}miniplay-build...
[info] Resolving org.fusesource#fusesource-pom;1.9 ...

...

[info] downloading https://repo1.maven.org/maven2/org/ow2/asm/asm-util/4.1/asm-util-4.1.jar ...
[info] 	[SUCCESSFUL ] org.ow2.asm#asm-util;4.1!asm-util.jar (61ms)
[info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.11.11/scala-compiler-2.11.11.jar ...
[info] 	[SUCCESSFUL ] org.scala-lang#scala-compiler;2.11.11!scala-compiler.jar (10586ms)
[info] Done updating.

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

welcome_to_play.png

Starter examples

https://www.playframework.com/download

ryoji@ubuntu:~$ git clone https://github.com/playframework/play-scala-starter-example.git
Cloning into 'play-scala-starter-example'...
remote: Counting objects: 209, done.
remote: Total 209 (delta 0), reused 0 (delta 0), pack-reused 209
Receiving objects: 100% (209/209), 57.29 KiB | 0 bytes/s, done.
Resolving deltas: 100% (92/92), done.
Checking connectivity... done.
ryoji@ubuntu:~$ cd play-scala-starter-example/
ryoji@ubuntu:~/play-scala-starter-example$ sbt run

[info] Done updating.

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

play_starter.png

Warning: node.js detection failed, sbt will use the Rhino based Trireme JavaScript engine in..
sudo apt-get install nodejs

InteliJ

/file/scala/intelij_play_scala.png

Scala IntelliJ Hello

/file/scala/scala_intelij_hello.png
Import > External Model > Modules > Root
/file/scala/external_model.png

Activator

Create New project > Activator > example
/file/scala/run_hello.png
Menu > Run > Hello

SBT

Create New project > SBT > 2.8.0
/file/scala/2.8.png

Scala - Java version mismatch

fail.png

ryoji@ubuntu:~/untitled1$ sbt compile
[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from /home/ryoji/untitled1/project
[info] Set current project to untitled1 (in build file:/home/ryoji/untitled1/)
[info] Compiling 1 Scala source to /home/ryoji/untitled1/target/scala-2.8.0/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.8.0.final. Compiling...
error: error while loading CharSequence, class file '/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar(java/lang/CharSequence.class)' is broken
(bad constant pool tag 18 at byte 10)
error: error while loading AnnotatedElement, class file '/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar(java/lang/reflect/AnnotatedElement.class)' is broken
(bad constant pool tag 18 at byte 76)
error: error while loading Arrays, class file '/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar(java/util/Arrays.class)' is broken
(bad constant pool tag 18 at byte 765)
error: error while loading Comparator, class file '/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar(java/util/Comparator.class)' is broken
(bad constant pool tag 18 at byte 20)
/tmp/sbt_e764eca8/xsbt/ExtractAPI.scala:549: error: java.util.Comparator does not take type parameters
  private[this] val sortClasses = new Comparator[Symbol] {
                                      ^
5 errors found
[info] Resolving org.scala-sbt#interface;0.13.15 ...
[error] (compile:compileIncremental) Error compiling sbt component 'compiler-interface'
[error] Total time: 6 s, completed 13-May-2017 02:27:34

old_scala.png

[info] 'compiler-interface' not yet compiled for Scala 2.11.4. Compiling...
[info]   Compilation completed in 12.169 s
[success] Total time: 13 s, completed 13-May-2017 02:39:54

Scala 2.12.

/file/scala/scala_212_deb.png

ryoji@ubuntu:~$ scala -version
Scala code runner version 2.12.2 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc.

20170513 HackTheTower

http://stackoverflow.com/questions/40497654/sbt-project-refresh-failed-intellij-scala-sbt

    last modified: 13 May 2017 [ View wiki source Close ]
     
SEARCH DIFFERENCE ATTACHMENT RECENT POST