ããŒã2.ãã¹ãŠã«ã€ããŠããããŠäœã«ã€ããŠã
ä»æ¥ã¯ãèšäºã®æåã®éšåã«åãŸããªãã£ãå€ãã®å²©ã®ãããªã€ãã£ãªã ã«ã€ããŠèª¬æããŸãã èšèªãšJavaãšã®çžäºäœçšããããŠæãéèŠãªããšãšããŠãScalaã®ãªããžã§ã¯ãæåæ©èœã®èª€çšã«ã€ããŠèŠãŠãããŸãã
ãµã€ã¯ã«æ§é
åŒã®é·ã
Scalaã§ã¯ãã»ãšãã©ãã¹ãŠãåŒã§ãããäœããUnit
è¿ãå Žåã§ããåžžã«()
åºåãååŸã§ããŸãã ã¹ããŒãã¡ã³ããæ®åããŠããèšèªã§ã®é·æããã°ã©ãã³ã°ã®åŸãç§ãã¡ïŒãããŠç§ãäŸå€ã§ã¯ãªãïŒã®å€ãã¯ããã¹ãŠã®èšç®ã1ã€ã®åŒã«æŒã蟌ã¿ãé·æçãªãã¬ãŒãã³ã°ã«ããããšæãã§ããŸãã 次ã®äŸã¯ãEffective Scalaãã倧èã«ãã©ãã°ããŸããã ã¿ãã«ã®ã·ãŒã±ã³ã¹ããããšããŸãããïŒ
val votes = Seq(("scala", 1), ("java", 4), ("scala", 10), ("scala", 1), ("python", 10))
1ã€ã®åŒã§æåã«åŠçã§ããŸãïŒã°ã«ãŒãã«åå²ãã°ã«ãŒãå
ã§èŠçŽãéé ã§äžŠã¹æ¿ãïŒïŒ
val orderedVotes = votes .groupBy(_._1) .map { case (which, counts) => (which, counts.foldLeft(0)(_ + _._2)) }.toSeq .sortBy(_._2) .reverse
ãã®ã³ãŒãã¯ã·ã³ãã«ã§ããããããããè¡šçŸåè±ãã§ããïŒ ãããããç¬ãé£ã¹ã岩ã ããã®ç¬ã®ããã«ã ãã ããåŒãååä»ãã³ã³ããŒãã³ãã«åå²ãããšã誰ã«ãšã£ãŠãç°¡åã«ãªããŸãã
val votesByLang = votes groupBy { case (lang, _) => lang } val sumByLang = votesByLang map { case (lang, counts) => val countsOnly = counts map { case (_, count) => count } (lang, countsOnly.sum) } val orderedVotes = sumByLang.toSeq .sortBy { case (_, count) => count } .reverse
ããããããã®äŸã¯ååã«æ確ã§ã¯ãããŸãã-ãªããç§ã¯ãããèªåã§æãä»ãã«ã¯ããŸãã«ãé¢åã§ããã ããããç§ãä¿¡ããŠãã ãããèè
ãããã€ãã®ã©ã€ã³ã«ç§»ãããšããæ°ã«ãããªãéåžžã«é·ããã¶ã€ã³ã«åºããããŸããã
å€ãã®å ŽåãSparkã䜿çšããŠScalaã«ã¢ã¯ã»ã¹ãã人ãå€ããSparkã䜿çšããå Žåã§ããå€æã®ããã¬ãŒã©ãŒããé·ãè¡šçŸåè±ããªãåè»ãã«åŒã£æããããšæã£ãŠããŸãã ãã®ãããªè¡šçŸãèªãããšã¯å°é£ã§ããããã¬ãŒã·ã§ã³ã®ã¹ã¬ããã¯ããã«å€±ãããŸãã
äœåãªé·ãåŒãšæŒç®åè¡šèš
Scalaã®2 + 2
ãåŒ2.+(2)
æ§æç³è¡£ã§ããããšãçãããç¥ã£ãŠããããšãé¡ã£ãŠããŸãã ãã®ã¿ã€ãã®ã¬ã³ãŒãã¯ãæŒç®åè¡šèšæ³ãšåŒã°ããŸãã ãã®ãããã§ãèšèªèªäœã«ã¯æŒç®åã¯ãããŸããããæå以å€ã®ååã§ã¯ãããŸããã¡ãœããã®ã¿ããããè¡šçŸåè±ããªDSLãäœæã§ãã匷åãªããŒã«ã§ãïŒå®éããã®ããã«èšå·è¡šèšãèšèªã«è¿œå ãããŸããïŒã ããããšæ¬åŒ§ãªãã§ãå¿
èŠãªéãã¡ãœããåŒã³åºããèšé²ã§ããŸãïŒ object fun arg fun1 arg1
èªã¿åãå¯èœãªDSLãäœæããå Žåãããã¯éåžžã«ã¯ãŒã«ã§ãã
myList should have length 10
ããããã»ãšãã©ã®å Žåãé·ãåŒãšçµã¿åãããæŒç®åè¡šèšã¯é£ç¶çãªäžäŸ¿ãããããããŸããã¯ããæ¬åŒ§ãªãã®ã³ã¬ã¯ã·ã§ã³ã®æäœã¯èŠæ ããè¯ããååä»ãã³ã³ããŒãã³ãã«åå²ãããŠããå Žåã«ã®ã¿ç解ã§ããŸãã
åè»ãšåŸçœ®èšæ³
åŸçœ®æŒç®åã¯ãç¹å®ã®æ¡ä»¶äžã§ãäžå¹žãªããŒãµãŒã®é ãåãå¯èœæ§ããããããæè¿ã®ããŒãžã§ã³ã®Scalaã§ã¯ããããã®åŒãæ瀺çã«ã€ã³ããŒãããå¿
èŠããããŸãã
import language.postfixOps
ãã®èšèªæ©èœã䜿çšããªãããã«ããŠããŠãŒã¶ãŒã䜿çšããå¿
èŠããªãããã«DSLãèšèšããŠãã ããã ããã¯éåžžã«ç°¡åã§ãã
åæåãããŠããªãå€
Scalaã¯åæåãããŠããªãå€ããµããŒãããŸãã ããšãã°ãããã¯Beanãäœæãããšãã«åœ¹ç«ã¡ãŸãã 次ã®Javaã¯ã©ã¹ãèŠãŠã¿ãŸãããã
class MyClass {
Scalaããåãåäœãå®çŸã§ããŸãã
class MyClass {
ããã軜çã«è¡ããªãã§ãã ããã å¯èœãªéãå€ãåæåããŸãã ãã®èšèªæ§ââé ã¯ã䜿çšããŠãããã¬ãŒã ã¯ãŒã¯ãŸãã¯ã©ã€ãã©ãªãããã匷ã䞻匵ããŠããå Žåã«ã®ã¿äœ¿çšããŠãã ããã äžæ³šæã«äœ¿çšãããšã倧éã®NullPointerException
ãNullPointerException
ããå¯èœæ§ããããŸãã ãã ãããã®æ©èœã«ã€ããŠç¥ã£ãŠããå¿
èŠããããŸãããã®ãããªç¥èãããã°æéãç¯çŽã§ããŸãã åæåãé
ããããå Žåã¯ã lazy
ããŒã¯ãŒãã䜿çšããŸãã
nullã䜿çšããªã
- åžžã«å€ãåæåããŸãã
Option
å€éšããNullable
ã§ããNullable
ã©ããããŸããnull
è¿ããªãã§ãã ããïŒ Option
ã Either
ã Try
ãªã©ã䜿çšããŠTry
- nullã®å€èŠ³ã®åææ¡ä»¶ã衚瀺ãããŸããååãNPEãåŠçããããã«ç¹å¥ã«èšèšãããèšèªããããžã§ã¯ãã«åãã§æã¡èŸŒããŸã§ãnullãä¿®æ£ããŠãã ããã
ãã«å€ãã¢ãã«ã®äžéšã§ããå ŽåããããŸãã ããããããã®ç¶æ³ã¯ãããŒã ã«åå ãããã£ãšåãããããã«Scalaã®å°å
¥ããããã£ãšåã«çºçããã®ã§ãããã sayingã«ãããããã«ã飲é
ãé²ãããšãã§ããªãå Žåã¯ã飲é
ããã¹ãã§ãã Null ObjectãšåŒã°ãããã¿ãŒã³ãããã«åœ¹ç«ã¡ãŸãã å€ãã®å Žåãããã¯ADTã®å¥ã®ã±ãŒã¹ã¯ã©ã¹ã§ãã
sealed trait User case class Admin extends User case class SuperUser extends User case class NullUser extends User
äœãåŸãããŸããïŒ ãã«ããŠãŒã¶ãŒããã³ã¿ã€ãã®å®å
šæ§ã
æ··éã«ã€ããŠ
æ¹æ³
Scalaã«ã¯ãã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒããªãŒããŒããŒãããæ©èœããããŸãã ãããŠãããã¯åé¡ã解決ããæè¯ã®æ¹æ³ã§ã¯ãããŸããã ãã£ãšèšããŸãããããã¯åé¡ã解決ããæ
£çšçãªæ¹æ³ã§ã¯ãããŸãã ã å®è·µãšããã°ããã®é¢æ°ã¯ãJavaã®ãªãã¬ã¯ã·ã§ã³ã䜿çšããŠãããScalaã³ãŒããJavaããåŒã³åºãããå ŽåããŸãã¯ãã®åäœãå¿
èŠãªå Žåã«åœ¹ç«ã¡ãŸãïŒãã®å ŽåBuilderã䜿çšããªãçç±ïŒã ãã®ä»ã®å Žåãæè¯ã®æŠç¥ã¯ãã³ã³ãããªã³ãªããžã§ã¯ããäœæãããã®äžã«ããã€ãã®apply
ã¡ãœãããå®çŸ©ããããšã§ãã
æã泚ç®ãã¹ãã±ãŒã¹ã¯ãããã©ã«ãã®ãã©ã¡ãŒã¿ãŒã«é¢ããç¡ç¥ã«ããã³ã³ã¹ãã©ã¯ã¿ãŒã®ãªãŒããŒããŒãã§ãã
æè¿ãç§ã¯æ¬¡ã®äžåèªãç®æããŸããïŒ
// ! case class Monster (pos: Position, health: Int, weapon: Weapon) { def this(pos: Position) = this(pos, 100, new Claws) def this(pos: Position, weapon: Weapon) = this(pos, 100, weapon) }
ãç°¡åã«éããŸãïŒ
case class Monster( pos: Position, health: Short = 100, weapon: Weapon = new Claws )
ã¢ã³ã¹ã¿ãŒã«ããºãŒã«ã§å ±é
¬ãäžãããã§ããïŒ ã¯ããåé¡ãããŸããïŒ
val aMonster = Monster(Position(300, 300, 20), weapon = new Bazooka)
ç§ãã¡ã¯äžçãããè¯ãããã¢ã³ã¹ã¿ãŒãããå¹³åã«ãããšåæã«ãåããã®ãã¹ãŠã«éè² è·ããããããšãæ¢ããŸããã ãã£ãšå¹³åã§ããïŒ ééããªãã çµå±ã®ãšãããããºãŒã«ã¯æ¥œåšã§ããããŸãïŒãŠã£ãããã£ã¢ã¯ããã«ã€ããŠé»ã£ãŠããŸããïŒã
ããã¯èšèšè
ã ãã«åœãŠã¯ãŸãããã§ã¯ãããŸããã人ã
ã¯ãã°ãã°åŸæ¥ã®æ¹æ³ããªãŒããŒããŒãããŸãïŒããã¯åé¿ã§ããã¯ãã§ãïŒã
ãªãã¬ãŒã¿ãŒã®éè² è·
éåžžã«ç©è°ãéžãæ©èœScalaãšèŠãªãããŸãã ç§ãèšèªã«æ²¡é ãããšããæŒç®åã®ãªãŒããŒããŒãã¯ã©ãã§ãã誰ã§ããã©ãã§ããå¯èœãªéã䜿çšãããŸããã çŸåšããã®æ©èœã¯ããŸã人æ°ããããŸããã æåã«ãParboiledãakka-httpã®ã«ãŒãã£ã³ã°ã®ããã«ãDSLãäœæã§ããããã«ããããã«ããŸããªãã¬ãŒã¿ãŒã®ãªãŒããŒããŒããè¡ãããŸããã
æŒç®åãäžå¿
èŠã«ãªãŒããŒããŒãããªãã§ãã ãããå¿
èŠããããšæãããå Žåã¯ããšã«ãããªãŒããŒããŒãããªãã§ãã ããã
ãŸãããªãŒããŒããŒãããå ŽåïŒDSLãå¿
èŠãªå ŽåããŸãã¯ã©ã€ãã©ãªã§æ°åŠçãªïŒãŸãã¯èšèã§è¡šçŸããã®ãé£ããïŒå ŽåïŒãéåžžã®ååã®é¢æ°ã§æŒç®åãè€è£œããŠãã ããã ãããŠããã®çµæã«ã€ããŠèããŠãã ããã ããããšããscalazãªãã¬ãŒã¿ãŒ|@|
ïŒ Applicative Builder ïŒã¯Maculay Culkinãšåä»ããããŸããã ãããŠãããããç¯äººãã®åçã§ãã
ãã¡ããããã¶ã€ããŒãäœåºŠããªãŒããŒããŒãããåŸã¯ãç»åãå®æãããããã«ã²ãã¿ãŒãšã»ãã¿ãŒã貌ãä»ããå¿
èŠããããŸãã
ã²ãã¿ãŒãšã»ãã¿ãŒã«ã€ããŠ
Scalaã¯Javaãšã®åªããçžäºäœçšãæäŸããŸãã 圌女ã¯ãŸããããããè±ãèšèšãããšãã«ããªãã®äººçã楜ã«ããããšãã§ããŸãã JavaãŸãã¯Beansã®æŠå¿µã«æ
£ããŠããªãå Žåã¯ãJavaã«æ
£ããŠãã ãã ã
Project Lombokã«ã€ããŠèããããšããããŸããïŒ Scalaæšæºã©ã€ãã©ãªã«ãåæ§ã®ã¡ã«ããºã ããããŸãã BeanProperty
ãšåŒã°ãBeanProperty
ã å¿
èŠãªã®ã¯ãBeanãäœæããã²ãã¿ãŒãŸãã¯ã»ãã¿ãŒãäœæããåãã£ãŒã«ãã«BeanProperty
ã¢ãããŒã·ã§ã³ãè¿œå ããããšã ãã§ãã
ããŒã«åå€æ°ã®ãã¥ãŒåisProperty
ãååŸããã«ã¯ã scala.beans.BooleanBeanProperty
ãã¹ã³ãŒãã«è¿œå ããŸãã
@BeanProperty
ã¯ãã¯ã©ã¹ãã£ãŒã«ãã«ã䜿çšã§ããŸãã
import scala.beans.{BooleanBeanProperty, BeanProperty} class MotherInLaw { // , : @BeanProperty var name = "Megaera" // . @BeanProperty var numberOfCatsSheHas = 0 // . @BooleanBeanProperty val jealous = true }
ã±ãŒã¹ã¯ã©ã¹ã®å Žåãæ©èœããŸãã
import scala.beans.BeanProperty case class Dino(@BeanProperty name: String, @BeanProperty var age: Int)
æç«ã§éãŒãïŒ
// , val barney = Dino("Barney", 29) barney.setAge(30) barney.getAge // res4: Int = 30 barney.getName // res14: String = Barney
ååãå€æ°ã«ããªãã£ããããã»ãã¿ãŒã䜿çšããããšãããšã次ã®ããã«ãªããŸãã
barney.setName <console>:15: error: value setName is not a member of Dino barney.setName
ã±ãŒã¹ã¯ã©ã¹ãšããã°
ã±ãŒã¹ã¯ã©ã¹ã®åºçŸã¯ãJVMãã©ãããã©ãŒã ã®ãã¬ãŒã¯ã¹ã«ãŒã§ãã 圌ãã®äž»ãªå©ç¹ã¯äœã§ããïŒ ãã®äžå€æ§ãããã³æ¢è£œã®equals
ã toString
ãããã³hashCode
ã®å¯çšæ§ã«ãããŠããã§ãã ãã ããå€ãã®å Žåã次ã®ãã®ãèŠã€ããããšãã§ããŸãã
// var. case class Person(var name: String, var age: Int)
å Žåã«ãã£ãŠã¯ãã±ãŒã¹ã¯ã©ã¹ãå€æŽå¯èœã«ããå¿
èŠããããŸããããšãã°ãäžèšã®äŸã®ããã«ãBeanãæš¡å£ããå Žåã
ããããããã¯ãã°ãã°ãæ·±ãåŸèŒ©ãå
ç«ãšã¯äœããç解ããŠããªããšãã«èµ·ãããŸãã éçºè
ã«ãšã£ãŠãã¬ãã«ãé«ãããšã¯ã圌ããäœãããŠããã®ããããç¥ã£ãŠããã®ã§ãããã»ã©èå³æ·±ããã®ã§ã¯ãããŸããã
case class Person (name: String, age: Int) { def updatedAge(newAge: Int) = Person(name, newAge) def updatedName(newName: String) = Person(newName, age) }
ãã ãã誰ããcopy
æ¹æ³ãç¥ã£ãŠããããã§ã¯ãããŸããã ãããæšæºã§ãã ç§ã¯ãã®ãããªããšãäœåºŠãèŠãŸããããããã¯ãã§ã«ããã«ããããã€ãŠç§ã¯ãšãŠãããŒãªã¬ã³ã§ããã copy
ã¯ãã¿ãã«çšã«å®çŸ©ãããŠããååãšåãããã«æ©èœããŸãã
ã±ãŒã¹ã¯ã©ã¹ãµã€ãºã«ã€ããŠ
å Žåã«ãã£ãŠã¯ãã±ãŒã¹ã¯ã©ã¹ã15ã20ã®ãã£ãŒã«ãã«è¥å€§åããåŸåããããŸãã Scala 2.11ããåã§ã¯ããã®ããã»ã¹ã¯22èŠçŽ ã«å¶éãããŠããŸããã ãããä»ãããªãã®æã¯è§£ãããŠããŸãïŒ
case class AlphabetStat ( a: Int, b: Int, c: Int, d: Int, e: Int, f: Int, g: Int, h: Int, i: Int, j: Int, k: Int, l: Int, m: Int, n: Int, o: Int, p: Int, q: Int, r: Int, s: Int, t: Int, u: Int, v: Int, w: Int, x: Int, y: Int, z: Int )
ãŸããç§ã¯ããªãã«åãã€ããŸããïŒãã¡ãããæã¯ããèªç±ã«ãªããŸããããã ããJVMã®å¶éããã£ã³ã»ã«ããŸããã§ããã
倧èŠæš¡ãªã±ãŒã¹ã¯ã©ã¹ã¯äžé©åã§ãã ããã¯éåžžã«æªãã§ãã ãã®ããã®èšãèš³ãããå ŽåããããŸããããªããåããŠãããµããžã§ã¯ããšãªã¢ã§ã¯éçŽãèš±å¯ãããŠããããæ§é ãå¹³ãã«èŠããŸãã 匷åãªç²Ÿç¥å®å®å€ã«åº§ã£ãŠããæ·±ã銬鹿ã«ãã£ãŠèšèšãããAPIã䜿çšããŸãã
ãããŠããåç¥ã®ããã«ãã»ãšãã©ã®å Žåã2çªç®ã®ãªãã·ã§ã³ã«å¯ŸåŠããå¿
èŠããããŸãã ã±ãŒã¹ã¯ã©ã¹ãAPIã«ç°¡åãã€èªç¶ã«ãã£ããããããã«ããŸãã ãããããªããç§ã¯ããªããç解ããŠããŸãã
ããããç§ã¯ããªãã®ã±ãŒã¹ã¯ã©ã¹ã®æ¥µæªãã®è¯ãèšãèš³ã ãããªã¹ãããŸããã æãæçœãªãã®ããããŸãããã¹ããããã¯ã©ã¹ã®å¥¥æ·±ãã«é ããããã£ãŒã«ããæŽæ°ããã«ã¯ãå€ãã®èŠåŽãå¿
èŠã§ãã åã±ãŒã¹ã¯ã©ã¹ãæ
éã«ãœãŒãããå€ã«çœ®ãæããŠçµã¿ç«ãŠãå¿
èŠããããŸãã ãããŠããã®ç
æ°ã«ã¯æ²»çæ³ããããŸããã¬ã³ãºã䜿çšã§ããŸãã
ã¬ã³ãºãã¬ã³ãºãšåŒã°ããã®ã¯ãªãã§ããïŒ åœŒãã¯äž»ãªããšã«éäžã§ããããã§ãã æ§é ã®ç¹å®ã®éšåã«ã¬ã³ãºã®çŠç¹ãåããããããååŸãããšãšãã«ããããæŽæ°ããæ©èœïŒæ§é ïŒãååŸããŸãã ãŸããã±ãŒã¹ã¯ã©ã¹ã宣èšããŸãã
case class Address(street: String, city: String, postcode: String) case class Person(name: String, age: Int, address: Address)
次ã«ãããŒã¿ãå
¥åããŸãã
val person = Person("Joe Grey", 37, Address("Southover Street", "Brighton", "BN2 9UA"))
éãã®ã¬ã³ãºãäœæããŸãïŒãã£ã©ã¯ã¿ãŒãåããããšä»®å®ããŸãïŒïŒ
import shapeless._ val streetLens = lens[Person].address.street
ãã£ãŒã«ããèªã¿åããŸãïŒæååã¿ã€ããèªåçã«è¡šç€ºãããããšã«æ³šæããŠãã ããïŒïŒ
val street = streetLens.get(person)
ãã£ãŒã«ãã®å€ãæŽæ°ããŸãã
val person1 = streetLens.set(person)("Montpelier Road")
äŸã¯ãã ããããã倧èã«çãŸããŸãã
ã¢ãã¬ã¹ã«å¯ŸããŠåæ§ã®æäœãå®è¡ã§ããŸãã ã芧ã®ãšãããããã¯éåžžã«ç°¡åã§ãã æ®å¿µãªããããããã幞ããªããšã«ãScalaã«ã¯ã¬ã³ãºãçµã¿èŸŒãŸããŠããŸããã ãããã£ãŠããµãŒãããŒãã£ã®ã©ã€ãã©ãªã䜿çšããå¿
èŠããããŸãã shapeless
ã䜿çšããããšããå§ãããŸãã å®éãäžèšã®äŸã¯ãã®ã©ã€ãã©ãªã®å©ããåããŠæžããããã®ã§ãåå¿è
ã«ãšã£ãŠéåžžã«ã¢ã¯ã»ã¹ããããã§ãã
ä»ã«ãå€ãã®ã¬ã³ãºã®å®è£
ããããŸããå¿
èŠã«å¿ã㊠ã scalaz ã monocleã䜿çšã§ããŸãã åŸè
ã¯ãå
åŠã䜿çšããããã®ããé«åºŠãªã¡ã«ããºã ãæäŸããŸããããã«äœ¿çšããããšããå§ãããŸãã
æ®å¿µãªãããã¬ã³ãºã®äœçšã¡ã«ããºã ã説æããã«ã¯ãå¥ã®èšäºãå¿
èŠãªå ŽåããããŸãããããã£ãŠãå
åŠã·ã¹ãã ã®ç¬èªã®ç 究ãå§ããã«ã¯äžèšã®æ
å ±ã§ååã ãšæããŸãã
åæåã®åçºæ
çµéšè±å¯ãªJavaéçºè
ãåŒãåããScalaã§ã®äœæã匷å¶ããŸãã 圌ã¯å¿
æ»ã«åæåãæ¢ãå§ããã®ã§ãæ°æ¥ã¯éããŸããã 圌ã¯ããããèŠã€ãããåæºããŠããŸããScalaã«ã¯ããŒã¯ãŒãenum
ãŸãã¯å°ãªããšãenumeration
ã¯ãããŸããã ãã®åŸãã€ãã³ãã«ã¯2ã€ã®ããŒãžã§ã³ããããŸããã€ãã£ãªã ãœãªã¥ãŒã·ã§ã³ãã°ãŒã°ã«ã§æ€çŽ¢ããããç¬èªã®åæãçºæãå§ããŸãã å€ãã®å Žåãæ ãåã¡ãçµæãšããŠæ¬¡ã®ããã«ãªããŸãã
object Weekdays { val MONDAY = 0
ããããäœïŒ ãããŠãããã«äœããããŸãïŒ
if (weekday == Weekdays.Friday) { stop(wearing, Tie) }
äœãæªãã®ïŒ Scalaã«ã¯ããã·ã¢ã®ä»£æ°ããŒã¿åã§ADTïŒä»£æ°ããŒã¿åïŒãšåŒã°ããåæãäœæããæ
£çšçãªæ¹æ³ããããŸãã ããšãã°ãHaskellã§äœ¿çšãããŸãã ããã¯æ¬¡ã®ãããªãã®ã§ãã
sealed trait TrafficLight case object Green extends TrafficLight case object Yellow extends TrafficLight case object Red extends TrafficLight case object Broken extends TrafficLight
ãã¡ããã詳现ãªãèªäœã®ãªã¹ãã¯ããçããã®ã§ããã ãªããããªã«æžãã®ïŒ 次ã®é¢æ°ã宣èšããŸãããïŒ
def tellWhatTheLightIs(tl: TrafficLight): Unit = tl match { case Red => println("No cars go!") case Green => println("Don't stop me now!") case Yellow => println("Ooohhh you better stop!") }
ãããŠãæã
ã¯åŸãïŒ
warning: match may not be exhaustive. It would fail on the following input: Broken def tellWhatTheLightIs(tl: TrafficLight): Unit = tl match { ^ tellWhatTheLightIs: (tl: TrafficLight)Unit
å®æ°ãžã®ãã€ã³ãã®ãªãåæãšããµã³ãã«ãšã®æ¯èŒã®å®å
šæ§ã®ãã§ãã¯ãååŸããŸãã ãããŠãã¯ããç§ã®æåãªååã®1人ãã貧ãã人ã
ã®ããã®ãšãã ããšåŒãã å Žåããã¿ãŒã³ãããã³ã°ã䜿çšããŸãã ãããæãæ
£çšçãªæ¹æ³ã§ãã 泚ç®ã«å€ãããããã¯Scalaã®ããã°ã©ãã³ã°ã®æ¬ã®åé ã§èšåãããŠããŸãã ãã¹ãŠã®é³¥ããããšãã«ã®çãäžã«å°éããããã§ã¯ãªãããã¹ãŠã®ããã¯ãã³ããã°ãã ãªãŒãã¹ãèªãããã§ããããŸããã
å¥åŠãªããšã«ã代æ°çããŒã¿åã¯ãŠã£ãããã£ã¢ã§èª¬æãããŠããŸãã Scalaã«é¢ããŠã¯ãèå³æ·±ããšæããããããªãããªãã¢ã¯ã»ã¹ããããæçš¿ãšãã¬ãŒã³ããŒã·ã§ã³ããããŸãã
é¢æ°ã·ã°ããã£ã®ããŒã«åŒæ°ãé¿ããŸã
確ãã«ãããŒã«å€ãåŒæ°ãšããŠåãã¡ãœãããäœæããŸãããïŒ Javaã®å Žåãç¶æ³ã¯äžè¬ã«æ²æšã§ãã
PrettyPrinter.print(text, 1, true)
1ã¯ã©ãããæå³ã§ããïŒ ç§ãã¡ã¯çŽæãä¿¡ããŠããããã³ããŒã®æ°ã§ãããšä»®å®ããŸãã ãããŠã true
責任ã¯äœã§ããïŒ ããã¯äœã§ãããŸããŸããã ããŠããããããŠããœãŒã¹ã«è¡ãããããäœã§ããããèŠãŠãã ããã
Scalaã§ã¯ãADTã䜿çšã§ããŸãã
def print(text: String, copies: Int, wrapWords: WordWrap)
è«çåŒæ°ãå¿
èŠãšããã³ãŒããç¶æ¿ããå Žåã§ããããã©ã«ãã®ãªãã·ã§ã³ã䜿çšã§ããŸãã
å埩ã«ã€ããŠ
ååž°ãåªããŠãã
æ«å°Ÿååž°ã¯ãã»ãšãã©ã®ã«ãŒããããé«éã§ãã ãã¡ããããããå°Ÿã®å Žåã 確èªããã«ã¯ã @tailrec
ã¢ãããŒã·ã§ã³ã䜿çšããŸãã ç¶æ³ã¯ç°ãªããŸãããåžžã«ååž°çãªè§£æ±ºçãç°¡åã«ã¢ã¯ã»ã¹ã§ããŠç解ã§ãããšã¯éããªãã®ã§ã while
ã䜿çšwhile
ãŸãã ããã«ã¯äœã®åé¡ããããŸããã ããã«ãã³ã¬ã¯ã·ã§ã³ã©ã€ãã©ãªå
šäœã¯ãåææ¡ä»¶ãæã€åçŽãªã«ãŒãã§èšè¿°ãããŠããŸãã
å埩ã§ã¯ãªãå
å
è¡šèšã®å ŽåïŒã€ã³ããã¯ã¹ã«ããïŒ
ãªã¹ããžã§ãã¬ãŒã¿ãŒããŸã㯠"å
å
è¡šèš"ãšãåŒã°ãããªã¹ããžã§ãã¬ãŒã¿ãŒã«ã€ããŠç¥ã£ãŠããã¹ãäž»ãªããšã¯ããã®äž»ãªç®çãã«ãŒããå®è£
ããªãããšã§ãã
ããã«ããã®æ§æã䜿çšããŠã€ã³ããã¯ã¹ãå埩åŠçããããšã¯ãéåžžã«è²»çšã®ãããæé ã«ãªããŸãã while
ãŸãã¯æ«å°Ÿååž°ã®äœ¿çšã¯ãã¯ããã«å®äŸ¡ã§ãã ããæ確ã«ã
ç解ã®ããã«ã map
ã flatMap
ããã³withFilter
æ§æç³è¡£ãããmap
ã yield
ããŒã¯ãŒãã¯ãçµæã®æ§é ã®å€ã®åŸç¶ã®éçŽã«äœ¿çšãããŸãã ãç解ã®ããã«ãã䜿çšãããšãå®éã«ã¯ãåãã³ã³ãããŒã¿ãããŒã«ã«å
ãŸãã圢ã§äœ¿çšããŸãã ããããçŽæ¥äœ¿çšããŸãã
// 1 to 10 foreach println // for (i <- 1 to 10) println(i)
åãã³ãŒããåŒã³åºãããšããäºå®ã«å ããŠãç¹å®ã®å€æ°i
ãè¿œå ããŸããããããã¯çµ¶å¯Ÿã«ããã«ã¯å±ããŸããã é床ãå¿
èŠãªå Žåã¯ã while
䜿çšãwhile
ã
å€æ°åã«ã€ããŠ
質ååç圢åŒã®å€æ°åã®èå³æ·±ãæŽå²ïŒ
質å ïŒ i
ã j
ã k
ã¯ãµã€ã¯ã«ãã©ã¡ãŒã¿ãšããŠã©ãããæ¥ãã®ã§ããïŒ
åç ïŒæ°åŠããã ãããŠãFortranã®ãããã§ããã°ã©ãã³ã°ã«åå ããŸãã.Fortranã§ã¯ãå€æ°ã®åã¯ååã§æ±ºå®ãããŸãïŒååã®æåã®æåãIãJãKãLãMããŸãã¯Nã§å§ãŸãå Žåãããã¯å€æ°ãæŽæ°åã«å±ããããšãèªåçã«æå³ããŸãã ãã以å€ã®å Žåãå€æ°ã¯å®æ°ãšèŠãªãããŸãïŒ IMPLICIT
ãã£ã¬ã¯ãã£ãã䜿çšããŠããã©ã«ãã®åãå€æŽã§ããŸãïŒã
ãããŠããã®æªå€¢ã¯ç§ãã¡ãšã»ãŒ60幎éçããŠããŸããã è¡åãä¹ç®ããªãå ŽåãJavaã§ãi
ã j
ãããã³k
ã䜿çšããèšãèš³ã¯ãããŸããã index
ã row
ã column
䜿çšãindex
ã ãã ããScalaã§èšè¿°ããå Žåã¯ã for
å
ã®å€æ°ã®å埩ãé¿ããŠãã ããã lukvogoããããã
ãã®ã»ã¯ã·ã§ã³ã«è¿œå ãããã®ã¯ããªã¹ããžã§ãã¬ãŒã¿ãŒã«ã€ããŠç¥ãããããšãã¹ãŠã詳ãã説æãããããªã§ãã
åŒã«ã€ããŠ
ãªã¿ãŒã³ã䜿çšããªãã§ãã ãã
Scalaã§ã¯ãã»ãšãã©ãã¹ãŠãè¡šçŸã§ãã äŸå€ã¯return
ã§ããã©ã®ãããªç¶æ³ã§ã䜿çšããªãã§ãã ããã å€ãã®äººãèããããã«ãããã¯ãªãã·ã§ã³ã®åèªã§ã¯ãããŸããã ããã¯ãããã°ã©ã ã®ã»ãã³ãã£ã¯ã¹ãå€æŽããèšèšã§ãã 詳现ã«ã€ããŠã¯ã ãã¡ããã芧ãã ããã
ã¿ã°ã䜿çšããªãã§ãã ãã
Scalaã«ã¿ã°ãããããšãæ³åããŠãã ããã ãããŠããªãããã«è¿œå ãããã®ãããããŸããã Scala 2.8ããåã¯ã continue
ã©ãã«ã¯ãã®ã¢ãã¬ã¹ã«ãããåŸã§åé€ãããŸããã
幞ããªããšã«ãã©ãã«ã¯èšèªã®äžéšã§ã¯ãããŸããããäŸå€ãã¹ããŒããŠãã£ããããããšã§å®è£
ãããŸãïŒãã®èšäºã®åŸåã§äŸå€ã®åŠçæ¹æ³ã«ã€ããŠèª¬æããŸãïŒã
ç§ã®å®è·µã§ã¯ããã®ãããªè¡åãå°ãªããšãæ£åœåã§ããåäžã®ã±ãŒã¹ã«ã¯ãŸã åºäŒã£ãŠããŸããã ç§ããããã§èŠã€ããäŸã®ã»ãšãã©ã¯ãæã«è² ãããç§ã®æããåžãåºãããŠããŸãã ããããããèŠãŸãïŒ
ãã®äŸã¯ããããåãããŸã ïŒ
breakable { for (i <- 1 to 10) { println(i) if (i > 4) break
äŸå€çãªç¶æ³ã«ã€ããŠ
ãã®äŸå€çãªãããã¯ã¯ã倧ããå¥ã®èšäºã«å€ããŸãã ããããäžé£ã®èšäºã§ããã ããã«ã€ããŠã¯é·ãé話ãããšãã§ããŸãã ãŸããScalaã¯äŸå€åŠçã«å¯Ÿããããã€ãã®æ ¹æ¬çã«ç°ãªãã¢ãããŒãããµããŒãããŠããããã§ãã ç¶æ³ã«å¿ããŠãæé©ãªãã®ãéžæã§ããŸãã
Scalaã«ã¯ãã§ãã¯æžã¿ã®äŸå€ã¯ãããŸããã - â . , . , . , . â . , C++ Java, Scala . - . goto
. . , flow. , , , â Scala .
, , Validation
scalaz, scala.util.Try
, Either
. Option
, , . - , .
. , , , . -, , , â . .
ïŒ extends App
:
object Main extends App { Console.println("Hello World: " + (args mkString ", ")) }
« ». , , . . DelayedInit
. . App
, , DelayedInit
. App
.
It should be noted that this trait is implemented using the DelayedInit functionality, which means that fields of the object will not have been initialized before the main method has been executed.
-:
, DelayedInit, , main.
:
Future versions of this trait will no longer extend DelayedInit.
App
? . «Hello world»? . main
.
ã³ã¬ã¯ã·ã§ã³
Scala. :
tvs.filter(tv => tv.displaySize == 50.inches).headOption
, :
tvs.find(tv => tv.displaySize == 50.inches)
«» :
list.size = 0 // list.isEmpty // ok !list.empty // list.nonEmpty // ok tvs.filter(tv => !tv.supportsSkype) // tvs.filterNot(tv => tv.supportsSkype) // ok
, IntelliJ IDEA, . Scala IDE, , .
Scala . Scala collections Tips and Tricks , . , . .
, .
æ¬
, Scala-. , , .
Scala
èšäº
æ å
è¬èŸ
- â ,
- (@firegurafiku) â ,
typedef
, - (@primetalk) â ,
- (@senia) â .
EDU- DataArt , , Scala, . , , , ( ) .