SlideShare une entreprise Scribd logo
1  sur  212
Télécharger pour lire hors ligne
Scala
Scala
Scala JVM
JVM
OS
VM OS
VM
1
Scala
Scala
Scala
// 1
/*
*
*/
println("Hello, world!") //
42 // Int
3.14 // Double
"hogehoge" // String
true // Boolean
() // Unit
Array(1, 2, 3) //
List(1, 2, 3) //
Map("a" -> 1, "b" -> 2) //
Set(1, 2, 3) //
1 :: 2 :: 3 :: Nil
// :: Nil
// List(1, 2, 3)
(42, "string", 3.14) //
s"$value ${value2}" // $ ${}
val i = 42 // val
// i = 99 // val
val j: Int = 100 //
// k: String = 100 //
var x = 42 //
x = 99 //
Scala if for match while
// if (cond) cond Boolean
if (cond) {
...
} else {
...
}
for (i <- seq) {
...
}
// Scala if for
val ret = if (false) {
"aaa"
} else if (true) {
"bbb"
} else {
"ccc"
}
ret // => "bbb"
val list = 1 :: 2 :: 3 :: Nil
val ret = for (i <- list) { i * 2 }
ret //=> Unit
// for
// list.foreach { i => ... }
val list = for (i <- list) yield { i * 2 }
list //=> List(2, 4, 6)
// yield yield
// list.map { i => ... }
val num = 42
// match
num match {
case n if n > 40 => println("over 40.")
case _ => println("less than or equal to 40.") // _
}
val tuple = (42, "answer")
val (i, s) = tuple
i // 42
s // tuple
// match
tuple match {
case (40, s) => println(s"40 is $s")
case (i, "universe") => println(s"$i is universe")
case (i, s) => println(s"$i is $s")
}
val tuple = (42, "hogefuga", 3.14)
tuple match {
case (40, "foobar", 2.718) => //
case (Num, Str, Point) => //
case (`num`, `str`, `p`) => //
case (n: Int, s: String, p: Int) => //
case tup @ (n, s, p) => //
}
val list = 1 :: 2 :: 3 :: Nil
// head tail
val head :: tail = list
head // 1
tail // List(2, 3)
list match {
case 42 :: tail => // 42
case head :: Nil => // Nil 1
case e1 :: e2 :: _ => // 2
case Nil => //
}
//
val tuple = (42, "answer", 1 :: 2 :: 3 :: Nil)
tuple match {
case (40, str, head :: Nil) =>
case (i, "answer", x :: xs) =>
case (_, _, list @ (head :: tail)) => // list List
case _ => //
}
Scala
def
def func(x: Int, y: Int): Int = {
x + y
}
//
// 1
def func2(x: Int, y: Int) = x + y
Scala
//
val fun = (x: Int, y: Int) => x + y
//
fun(1, 2) //=> 3
=>
// x y Int
val fun: (Int, Int) => Int = (x, y) => x + y
=>
match =>
=>
// =>
val fun: Int => String = (i: Int) => i match {
case 42 => "correct"
}
val fun = (x: Int, y: Int) = x + y
//
def fortytwo(num: Int, f: (Int, Int) => Int) = f(num, 42)
//
fortytwo(99, fun) // 141
//
fortytwo(2, (x, y) => x * y) // 84
// fortytwo x y
map
map
map
Scala List map
val list = 1 :: 2 :: 3 :: Nil
// 2
val fun = i: Int => i * 2
// map
list.map(fun) //=> List(2, 4, 6)
// List fun
//
list.map(i => i + 2) // List(3, 4, 5)
CLI
$ scala cli.scala 74 76 81 89 92 87 79 85
mean: 82
object Main {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
cli.scala
Hello, world!
$ scala cli.scala
Scala
sbt
Scala sbt
sbt
$ sbt
compile
run main
sbt
sbt target
1-1
Scala main
object Main {
def main(args: Array[String]): Unit = {
for (arg <- args) {
println(arg)
}
}
}
args.scala
$ scala args.scala hoge fuga
hoge
fuga
$ scala args.scala 42 99
42
99
2-1
Scala 2.11 scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
val input = scala.io.StdIn.readLine
println(input)
}
}
stdin.scala
$ scala stdin.scala
$ scala stdin.scala
Hello, world!
Hello, world!
split
object Main {
def main(args: Array[String]): Unit = {
val input = scala.io.StdIn.readLine.split(" ")
for (word <- input) println(word)
}
}
$ scala stdin.scala
hoge fuga piyo
hoge
fuga
piyo
2-2
Scala
……
42 + "99" // toString "4299"
Ruby JavaScript
Ruby
irb(main):001:0> 42 + "99"
TypeError: String can't be coerced into Fixnum
from (irb):1:in `+'
from (irb):1
JavaScript
> 42 + '99'
'4299'
> '7' * 9
63
> 42 * 'hoge'
NaN
Scala
NaN
Scala
implicit
toInt
toDouble
42 + "99".toInt // 141
3 * 3 * "3.14".toDouble // 28.26
2
object Main {
def main(args: Array[String]): Unit = {
for (arg <- args) {
println(arg.toInt * 2)
}
}
}
number.scala
$ scala number.scala 42 99 1
84
198
2
3-1
"hoge" toInt
toDouble
Array[String] ->
List[Double]
Array
Array List
toDouble
toList
Array toList List
args.toList
map
map
map
Double
args.map(_.toDouble)
Array[String] List[Double]
args.map(_.toDouble).toList
4-1
Array[String] List[Double]
Array List
val arr = Array("1", "2", "3")
val list = List(1.0, 2.0, 3.0)
CLI
/
sum
sum
Scala sum
sum
0
Scala Nil
Nil sum(Nil) 0
1 2
3 ……
n
n
n n n-1
n = 1 n-1
n n
1..n
1..n 1..n-1 n
1 + 2 + ... + n - 2 + n - 1 + n = (1 +
2 + ... n - 2 + n - 1) + n
3 :: 2 :: 7 :: 5 :: Nil
1 4 17 1 3
12 4 5
17
n
1
n - 1 = 0 0
n - 1 > 0
0
0
sum
0
n n 1..n-1
Scala
sum( 3 :: 2 :: 7 :: 5 :: Nil )
3 + sum( 2 :: 7 :: 5 :: Nil )
3 + 2 + sum( 7 :: 5 :: Nil)
3 + 2 + 7 + sum( 5 :: Nil)
3 + 2 + 7 + 5 + sum( Nil )
3 + 2 + 7 + 5 + 0
3 + 2 + 7 + 5
3 + 2 + 12
3 + 14
17
Scala
Scala
def sum(list: List[Double]): Double = ???
List[Double]
Double
if
match
def sum(list: List[Double]): Double =
list match {
case Nil => ???
case head :: tail => ???
}
0
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => ???
}
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
5-1
1 :: 2 :: 3 :: Nil match {
case a :: b :: rest =>
println(a) // 1
println(b) // 2
println(rest) // List(3)
}
1
length
sum
length
sum 0
length
sum
0
n
n n-1
1 1
n-1 1 n
n
n-1
sum 3 :: 2 :: 7
:: 5 :: Nil
length( 3 :: 2 :: 7 :: 5 :: Nil )
1 + length( 2 :: 7 :: 5 :: Nil )
1 + 1 + length( 7 :: 5 :: Nil )
1 + 1 + 1 + length( 5 :: Nil )
1 + 1 + 1 + 1 + length( Nil )
1 + 1 + 1 + 1 + 0
1 + 1 + 1 + 1
1 + 1 + 2
1 + 3
4
Scala
def length(list: List[Double]): Double =
list match {
case Nil => 0
case _ :: tail => 1 + length(tail)
}
_ Scala
_
6-1
_
_
mean
/
def mean(list: List[Double]): Double = sum(list) / length(list)
CLI
CLI
object Main {
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
def length(list: List[Double]): Double =
list match {
case Nil => 0
case _ :: tail => 1 + length(tail)
}
def mean(list: List[Double]): Double = sum(list) / length(list)
def main(args: Array[String]): Unit = {
$ scala cli.scala
mean: 82.875
2
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
def length(list: List[Double]): Double =
list match {
case Nil => 0
case _ :: tail => 1 + length(tail)
}
def common(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => ??? + common(tail)
}
???
def common(list: List[Double], f: Double => Double): Double =
list match {
case Nil => 0
case head :: tail => f(head) + common(tail, f)
}
1
sum length
def sum(list: List[Double]): Double = common(list, head => head)
def length(list: List[Double]): Double = common(list, _ => 1)
head => head _ => 1
head => head 1
_ => 1 1 1
7-1
_ => 1 head => 1
_
match _
7-2
def test(fun: Double => Double) = fun(3.14)
test(_ => 1) // 1.0
1
val fun = _: Double => 1
test(fun) //
:
7-3
identity
fold
fold
common
fold
fold sum length
map filter
def foldr[A, B](f: (A, B) => B, ini: B, list: List[A]): B =
list match {
case Nil => ini
case head :: tail => f(head, foldr(f, ini, tail))
}
Double
length sum foldr
def length(list: List[Double]): Double =
foldr[Double, Double]((_, i) => i + 1, 0, list)
def sum(list: List[Double]): Double =
foldr[Double, Double]((a, i) => a + i, 0, list)
7-4
foldr
max
min
reverse
filter
map

Contenu connexe

Tendances

PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 

Tendances (18)

Galois Tech Talk / Vinyl: Records in Haskell and Type Theory
Galois Tech Talk / Vinyl: Records in Haskell and Type TheoryGalois Tech Talk / Vinyl: Records in Haskell and Type Theory
Galois Tech Talk / Vinyl: Records in Haskell and Type Theory
 
Scala 101
Scala 101Scala 101
Scala 101
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
P3 2017 python_regexes
P3 2017 python_regexesP3 2017 python_regexes
P3 2017 python_regexes
 
Scala collection
Scala collectionScala collection
Scala collection
 
Why The Free Monad isn't Free
Why The Free Monad isn't FreeWhy The Free Monad isn't Free
Why The Free Monad isn't Free
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
20170509 rand db_lesugent
20170509 rand db_lesugent20170509 rand db_lesugent
20170509 rand db_lesugent
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Scala Parallel Collections
Scala Parallel CollectionsScala Parallel Collections
Scala Parallel Collections
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collections
 
ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 

Similaire à 学生向けScalaハンズオンテキスト

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 

Similaire à 学生向けScalaハンズオンテキスト (20)

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Tuples All the Way Down
Tuples All the Way DownTuples All the Way Down
Tuples All the Way Down
 
FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 
Scala Paradigms
Scala ParadigmsScala Paradigms
Scala Paradigms
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Scalding - the not-so-basics @ ScalaDays 2014
Scalding - the not-so-basics @ ScalaDays 2014Scalding - the not-so-basics @ ScalaDays 2014
Scalding - the not-so-basics @ ScalaDays 2014
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 
Scala for ruby programmers
Scala for ruby programmersScala for ruby programmers
Scala for ruby programmers
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
Erlang for data ops
Erlang for data opsErlang for data ops
Erlang for data ops
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 

Dernier

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Dernier (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

学生向けScalaハンズオンテキスト