SlideShare a Scribd company logo
2019/12/23
Scala Hands On!!
1996
WingArc1st MotionBoard
1.
2.
3.
4.
5. Java
6. ( 1 2)
7. ( 1 2 )
1
val add = calc(2,4,(a,b) => a+b)
val sub = calc(2,4,(a,b) => a-b)
val mul = calc(2,4,(a,b) => a*b)
•
def calc(a: Int, b: Int, f: (Int,Int) => Int ):Int = f(a,b)
f:(Int,Int) Int
1
def addFunc(a: Int, b: Int) = a+b
val add = calc(2,4,addFunc)
•
def calc(a: Int, b: Int, f: (Int,Int) => Int ):Int = f(a,b)
f:(Int,Int) Int
1 0 0 1 0 1 0 0 1 1 1
val list = Seq(9,-4,-2,6,-2,5,0,-7,5,3,10)
list.map(e => if(e>0) 1 else 0).foreach(e => print(e+" "))
1 0 map
1
2
val str = Random.nextInt(10) match {
case 0 | 1 => "0 1"
case i if(i%2 == 0) => " "
case i if(i%3 == 0) => "3 "
case _ => " "
}
• |
• case _
2
•
• case Nil
• l: List[Int]
val list = Seq(1,2,3)
val str1 = list match {
case List(1,a,b) => s"1 and a+b=${a+b}"
case List(x,y) => x+y
case Nil => " "
case l: List[Int] => " :"+l.toString()
case _ => " "
}
3
Option[A] Option[A] Some[A] None
Option
val a: String = null
val option: Option[String] = Option(a)
if(option.isDefined){
println(option.get)
}else{
println("Nothing!!")
}
3
option match {
case Some(x) => f(x)
case None => ()
}
option match {
case Some(x) => Some(f(x))
case None => None
}
option match {
case Some(x) if p(x) => Some(x)
case _ => None
}
option.foreach(println)
option.map(_+"¥n")
option.filter(!_.equals(""))
Option
3
Either
def fileSize(file: File): Either[String,Long] = {
if(file.exists) Right(file.length) else Left("Not exists!!")
}
val f = new File("a/b.txt")
val size: Either[String,Long] = fileSize(f)
if(size.isRight){
println("file size :"+size.right.get)
}else{
println("error :"+size.left.get)
}
Either[A,B] Left[A] Right[B]
3
fileSize(f) match {
case Right(size) => println("file size :"+size)
case Left(str) => println("error :"+str)
}
Either
Left Right
3
Try Option/Either
def div(x: Int,y: Int):Try[Int] = Try(x/y)
div(14,0) match {
case Success(value) => println(value)
case Failure(exception) => exception.printStackTrace()
}
Try(x/y) Failure[Trowable]
Success(x/y)
java.lang.ArithmeticException Failure
4
scala.collection.immutable scala.collection.mutable
immutable
4
Seq C A B...C … A
B
A
DC A
key value
B
C
1
3
2Set Map
4
Seq //
val seq = Seq("A","B","C","B","G")
//
val b = seq(1) //
val head = seq.head //
val last = seq.last //
//
val newSeq1 = seq :+ "F" //
val newSeq2 = seq +: "F" //
val newSeq3 = seq ++ Seq("1","2","3")
4
//
val set = Set("A","B","C","B","G")
//
val contains:Boolean = set("C")
//
val newSet1 = set + "F"
val newSet2 = set ++ Set("F","G","5")
Set
4
Map //
val map = Map("Apple" -> 140, "Orange" -> 90)
// key value
val apple = map("Apple")
val lemon = map.get(“Lemon”) // Option
//
val newMap1 = map + ("Banana"->110)
val newMap2 = map ++ Map("Banana"->110,"Lemon"->70)
4
• flatten
•
val words = Iterable(
Seq("H","e","l","l","o"),
Seq(" ","S","c","a","l","a"),
Seq("!","!"))
println(words.flatten)
List(H, e, l, l, o, , S, c, a, l, a, !, !)
Hello!!
val s = Seq("H","e","l","l","o","!","!")
val hello = s.foldLeft("")((accumurator,element) =>
accumurator + element
)
println(hello)
4
5 Java
import java.util.{Date, Locale}
import java.text.DateFormat._
val now = new Date
val df = getDateInstance(LONG,Locale.ENGLISH)
println(df.format(now))
Java
•
• _ Java *
5 Java
val file:Path = Paths.get("file.txt")
val lines:List[String] = Files.readAllLines(file)
lines.asScala.foreach(println)
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
import java.util.List
import scala.collection.JavaConverters._
.asScala .asJava
Java
Scala
6
( )
input: 10 ss 20
mean: 15.0
input: ewqg 3 4 f500 3 5 8 4 5 6 0 w 5
mean: 4.3
1
6
• ( )
val in:Array[String] = StdIn.readLine().split(" ")
val str:String = "14"
val i:Int = str.toInt
• String Int
java.lang.NumberFormatException
•
val list = Iterable(10,44,23,47)
val size: Int = list.size
6
2
6
7
1 2
1
import scala.io.StdIn
import scala.util.Try
import scala.util.Success
import scala.util.Failure
1
def parseInt(str: String): Option[Int] = {
Try(str.toInt) match {
case Success(value) => Some(value)
case Failure(exception) => None
}
}
def inputNums():Iterable[Int]= {
val in:Array[String] = StdIn.readLine().split(" ")
val nums = in
.map(parseInt(_)) // Option
.filter(_.isDefined) // None
.map(_.get) //
nums
}
Int
1
def mean(sample: Iterable[Int]): Double = {
val sum = sample.foldLeft(0)((a,b)=>a+b) //
sum/sample.size.toDouble //
}
print("input: ")
val sample = inputNums()
val res = mean(sample)
println(" mean: "+res)
main
2
import scala.io.Source
import scala.util.Try
import scala.util.Success
import scala.util.Failure
import scala.collection.mutable
2
def getLinesFromfile(path: String):Iterable[String] = {
Try(Source.fromFile(path,"UTF-8")) match {
case Success(value) => value.getLines().toIterable
case Failure(e) => e.printStackTrace();Nil
}
}
I hava a dream.
I have a dream today. I hava a dream. I hava a dream today.
2
1 1
def toWords(lines: Iterable[String]): Iterable[String] = {
val remove = (ward: String,keys: Set[Char]) => ward.filter(!keys(_))
lines.map(_.split(" ")).flatten
.map(remove(_,Set('¥n','.',',','“','”','¥'','’','–')))
}
I hava a dream. I hava a dream today.
I have a dream I have a dream today
2
main
val path = "a/texts/ihaveadream.txt"
//
val wards: Iterable[String] = toWords(getLinesFromfile(path))
//
val set: Set[String] = wards.toSet
//
val keyMap: mutable.Map[String,Int] = mutable.Map.empty ++ set.map(_ -> 0).toMap
//
wards.foreach(keyMap(_)+=1)
//
keyMap.toSeq.sortBy(_._2).foreach(println)
mutable.Map …
Scala Hands On!!

More Related Content

What's hot

ScalaMeter 2014
ScalaMeter 2014ScalaMeter 2014
ScalaMeter 2014
Aleksandar Prokopec
 
Fp java8
Fp java8Fp java8
Fp java8
Yanai Franchi
 
Делаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе ShapelessДелаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе Shapeless
Вадим Челышов
 
ScalaBlitz
ScalaBlitzScalaBlitz
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門
Hiromi Ishii
 
学生向けScalaハンズオンテキスト part2
学生向けScalaハンズオンテキスト part2学生向けScalaハンズオンテキスト part2
学生向けScalaハンズオンテキスト part2
Opt Technologies
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Aleksandar Prokopec
 
Template Haskell とか
Template Haskell とかTemplate Haskell とか
Template Haskell とか
Hiromi Ishii
 
Julia meetup bangalore
Julia meetup bangaloreJulia meetup bangalore
Julia meetup bangalore
Krishna Kalyan
 
Scala Parallel Collections
Scala Parallel CollectionsScala Parallel Collections
Scala Parallel Collections
Aleksandar Prokopec
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
Opt Technologies
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
Kousuke Ruichi
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
Proposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondProposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
Haskell
HaskellHaskell
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
Mahmoud Samir Fayed
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
Brian Lonsdorf
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Sander Kieft
 
Python for R users
Python for R usersPython for R users
Python for R users
Satyarth Praveen
 
Ozma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyOzma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrency
BeScala
 

What's hot (20)

ScalaMeter 2014
ScalaMeter 2014ScalaMeter 2014
ScalaMeter 2014
 
Fp java8
Fp java8Fp java8
Fp java8
 
Делаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе ShapelessДелаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе Shapeless
 
ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門
 
学生向けScalaハンズオンテキスト part2
学生向けScalaハンズオンテキスト part2学生向けScalaハンズオンテキスト part2
学生向けScalaハンズオンテキスト part2
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Template Haskell とか
Template Haskell とかTemplate Haskell とか
Template Haskell とか
 
Julia meetup bangalore
Julia meetup bangaloreJulia meetup bangalore
Julia meetup bangalore
 
Scala Parallel Collections
Scala Parallel CollectionsScala Parallel Collections
Scala Parallel Collections
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
Proposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondProposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyond
 
Haskell
HaskellHaskell
Haskell
 
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
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Python for R users
Python for R usersPython for R users
Python for R users
 
Ozma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyOzma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrency
 

Similar to Scala Hands On!!

(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
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
Werner Hofstra
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Meet scala
Meet scalaMeet scala
Meet scala
Wojciech Pituła
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
league
 
Scala
ScalaScala
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
Vladimir Parfinenko
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
decoupled
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
Aleksandar Veselinovic
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
Istanbul Tech Talks
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
Lukasz Dynowski
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
Knoldus Inc.
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
SeniorDevOnly
 

Similar to Scala Hands On!! (20)

(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: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Scala
ScalaScala
Scala
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Monadologie
MonadologieMonadologie
Monadologie
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 

More from Yoshifumi Takeshima

Agile Testing Night #4 LT
Agile Testing Night #4 LTAgile Testing Night #4 LT
Agile Testing Night #4 LT
Yoshifumi Takeshima
 
ゲーム紹介:GeoGusser
ゲーム紹介:GeoGusserゲーム紹介:GeoGusser
ゲーム紹介:GeoGusser
Yoshifumi Takeshima
 
LTのすゝめっていうLT
LTのすゝめっていうLTLTのすゝめっていうLT
LTのすゝめっていうLT
Yoshifumi Takeshima
 
確率変数とは
確率変数とは確率変数とは
確率変数とは
Yoshifumi Takeshima
 
Scala入門
Scala入門Scala入門
Scala入門
Yoshifumi Takeshima
 
統計:最尤原理の基礎
統計:最尤原理の基礎統計:最尤原理の基礎
統計:最尤原理の基礎
Yoshifumi Takeshima
 
サッカー:2-0は危険なスコアなのか?
サッカー:2-0は危険なスコアなのか?サッカー:2-0は危険なスコアなのか?
サッカー:2-0は危険なスコアなのか?
Yoshifumi Takeshima
 

More from Yoshifumi Takeshima (7)

Agile Testing Night #4 LT
Agile Testing Night #4 LTAgile Testing Night #4 LT
Agile Testing Night #4 LT
 
ゲーム紹介:GeoGusser
ゲーム紹介:GeoGusserゲーム紹介:GeoGusser
ゲーム紹介:GeoGusser
 
LTのすゝめっていうLT
LTのすゝめっていうLTLTのすゝめっていうLT
LTのすゝめっていうLT
 
確率変数とは
確率変数とは確率変数とは
確率変数とは
 
Scala入門
Scala入門Scala入門
Scala入門
 
統計:最尤原理の基礎
統計:最尤原理の基礎統計:最尤原理の基礎
統計:最尤原理の基礎
 
サッカー:2-0は危険なスコアなのか?
サッカー:2-0は危険なスコアなのか?サッカー:2-0は危険なスコアなのか?
サッカー:2-0は危険なスコアなのか?
 

Recently uploaded

National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 

Recently uploaded (20)

National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 

Scala Hands On!!

  • 3. 1. 2. 3. 4. 5. Java 6. ( 1 2) 7. ( 1 2 )
  • 4. 1 val add = calc(2,4,(a,b) => a+b) val sub = calc(2,4,(a,b) => a-b) val mul = calc(2,4,(a,b) => a*b) • def calc(a: Int, b: Int, f: (Int,Int) => Int ):Int = f(a,b) f:(Int,Int) Int
  • 5. 1 def addFunc(a: Int, b: Int) = a+b val add = calc(2,4,addFunc) • def calc(a: Int, b: Int, f: (Int,Int) => Int ):Int = f(a,b) f:(Int,Int) Int
  • 6. 1 0 0 1 0 1 0 0 1 1 1 val list = Seq(9,-4,-2,6,-2,5,0,-7,5,3,10) list.map(e => if(e>0) 1 else 0).foreach(e => print(e+" ")) 1 0 map 1
  • 7. 2 val str = Random.nextInt(10) match { case 0 | 1 => "0 1" case i if(i%2 == 0) => " " case i if(i%3 == 0) => "3 " case _ => " " } • | • case _
  • 8. 2 • • case Nil • l: List[Int] val list = Seq(1,2,3) val str1 = list match { case List(1,a,b) => s"1 and a+b=${a+b}" case List(x,y) => x+y case Nil => " " case l: List[Int] => " :"+l.toString() case _ => " " }
  • 9. 3 Option[A] Option[A] Some[A] None Option val a: String = null val option: Option[String] = Option(a) if(option.isDefined){ println(option.get) }else{ println("Nothing!!") }
  • 10. 3 option match { case Some(x) => f(x) case None => () } option match { case Some(x) => Some(f(x)) case None => None } option match { case Some(x) if p(x) => Some(x) case _ => None } option.foreach(println) option.map(_+"¥n") option.filter(!_.equals("")) Option
  • 11. 3 Either def fileSize(file: File): Either[String,Long] = { if(file.exists) Right(file.length) else Left("Not exists!!") } val f = new File("a/b.txt") val size: Either[String,Long] = fileSize(f) if(size.isRight){ println("file size :"+size.right.get) }else{ println("error :"+size.left.get) } Either[A,B] Left[A] Right[B]
  • 12. 3 fileSize(f) match { case Right(size) => println("file size :"+size) case Left(str) => println("error :"+str) } Either Left Right
  • 13. 3 Try Option/Either def div(x: Int,y: Int):Try[Int] = Try(x/y) div(14,0) match { case Success(value) => println(value) case Failure(exception) => exception.printStackTrace() } Try(x/y) Failure[Trowable] Success(x/y) java.lang.ArithmeticException Failure
  • 15. 4 Seq C A B...C … A B A DC A key value B C 1 3 2Set Map
  • 16. 4 Seq // val seq = Seq("A","B","C","B","G") // val b = seq(1) // val head = seq.head // val last = seq.last // // val newSeq1 = seq :+ "F" // val newSeq2 = seq +: "F" // val newSeq3 = seq ++ Seq("1","2","3")
  • 17. 4 // val set = Set("A","B","C","B","G") // val contains:Boolean = set("C") // val newSet1 = set + "F" val newSet2 = set ++ Set("F","G","5") Set
  • 18. 4 Map // val map = Map("Apple" -> 140, "Orange" -> 90) // key value val apple = map("Apple") val lemon = map.get(“Lemon”) // Option // val newMap1 = map + ("Banana"->110) val newMap2 = map ++ Map("Banana"->110,"Lemon"->70)
  • 19. 4 • flatten • val words = Iterable( Seq("H","e","l","l","o"), Seq(" ","S","c","a","l","a"), Seq("!","!")) println(words.flatten) List(H, e, l, l, o, , S, c, a, l, a, !, !) Hello!! val s = Seq("H","e","l","l","o","!","!") val hello = s.foldLeft("")((accumurator,element) => accumurator + element ) println(hello)
  • 20. 4
  • 21. 5 Java import java.util.{Date, Locale} import java.text.DateFormat._ val now = new Date val df = getDateInstance(LONG,Locale.ENGLISH) println(df.format(now)) Java • • _ Java *
  • 22. 5 Java val file:Path = Paths.get("file.txt") val lines:List[String] = Files.readAllLines(file) lines.asScala.foreach(println) import java.nio.file.Path import java.nio.file.Paths import java.nio.file.Files import java.util.List import scala.collection.JavaConverters._ .asScala .asJava Java Scala
  • 23. 6 ( ) input: 10 ss 20 mean: 15.0 input: ewqg 3 4 f500 3 5 8 4 5 6 0 w 5 mean: 4.3 1
  • 24. 6 • ( ) val in:Array[String] = StdIn.readLine().split(" ") val str:String = "14" val i:Int = str.toInt • String Int java.lang.NumberFormatException • val list = Iterable(10,44,23,47) val size: Int = list.size
  • 25. 6 2
  • 26. 6
  • 27. 7 1 2
  • 28. 1 import scala.io.StdIn import scala.util.Try import scala.util.Success import scala.util.Failure
  • 29. 1 def parseInt(str: String): Option[Int] = { Try(str.toInt) match { case Success(value) => Some(value) case Failure(exception) => None } } def inputNums():Iterable[Int]= { val in:Array[String] = StdIn.readLine().split(" ") val nums = in .map(parseInt(_)) // Option .filter(_.isDefined) // None .map(_.get) // nums } Int
  • 30. 1 def mean(sample: Iterable[Int]): Double = { val sum = sample.foldLeft(0)((a,b)=>a+b) // sum/sample.size.toDouble // } print("input: ") val sample = inputNums() val res = mean(sample) println(" mean: "+res) main
  • 31. 2 import scala.io.Source import scala.util.Try import scala.util.Success import scala.util.Failure import scala.collection.mutable
  • 32. 2 def getLinesFromfile(path: String):Iterable[String] = { Try(Source.fromFile(path,"UTF-8")) match { case Success(value) => value.getLines().toIterable case Failure(e) => e.printStackTrace();Nil } } I hava a dream. I have a dream today. I hava a dream. I hava a dream today.
  • 33. 2 1 1 def toWords(lines: Iterable[String]): Iterable[String] = { val remove = (ward: String,keys: Set[Char]) => ward.filter(!keys(_)) lines.map(_.split(" ")).flatten .map(remove(_,Set('¥n','.',',','“','”','¥'','’','–'))) } I hava a dream. I hava a dream today. I have a dream I have a dream today
  • 34. 2 main val path = "a/texts/ihaveadream.txt" // val wards: Iterable[String] = toWords(getLinesFromfile(path)) // val set: Set[String] = wards.toSet // val keyMap: mutable.Map[String,Int] = mutable.Map.empty ++ set.map(_ -> 0).toMap // wards.foreach(keyMap(_)+=1) // keyMap.toSeq.sortBy(_._2).foreach(println) mutable.Map …