Scala
Scalaã«ã¯ã¯ã©ã¹ãªãã©ã«ã¯ããã¾ãããã代ããã«çµã¿è¾¼ã¿é¢æ°ãclassOf[]ãã使ã£ã¦æå®ããåã®java.lang.Classãåå¾ã§ãã¾ãã println( classOf[String] ) println( classOf[List[Object]] ) println( classOf[List[String]] ) å®è¡çµæã§ãã class jâ¦
ã ãã¶åã«Scalaã§æ¸ããæ§è½è¨æ¸¬ã¦ã¼ãã£ãªãã£ãçºæãã¾ãããScalaãªã®ã§Javaã¯ã©ã¹ã®æ§è½è¨æ¸¬ã§ä½¿ãã¾ãã以ä¸ã¯ãµã³ãã«ã // é¢æ°ã5åå®è¡ãã¦ãå¦çæéã®å¹³åã»æ大ã»æå°ãåºåããã print( Perf.perf( "test", 5 ) { Thread.sleep(500); }.toStâ¦
<ç¶æ¿å ã®å> { <追å ã®å®ç¾©...> }ã§ãç¶æ¿å ã®åã«åè´ããããã«è¿½å ã®å®ç¾©ãæã¤åãä½æ?ã§ãã¾ããä¾ãã° // Catæ´¾çã§ãã¤meowã¡ã½ãããæã¤ä½ããå¼æ°ã¨ãã¦åãä»ããé¢æ° def meow( foo: Cat { def meow:Unit } ) = { foo.meow } ã¿ãããªCompoundâ¦
ã¢ããã¼ã·ã§ã³ã使ã£ã¦ã·ãªã¢ã©ã¤ã¶ãã«ã§ãããã¨ãæ示ã§ãã¾ãã // ã¢ããã¼ã·ã§ã³ã使ã£ã¦ã·ãªã¢ã©ã¤ã¶ãã«ã§ãããã¨ãæ示 @serializable class Foo {} // âã¯âã¨åã class Var extends java.io.Serializable {} // ç¢ºèª println( "Foo: " + new Foâ¦
Function#chainã§è¤æ°ã®é¢æ°ãé£çµãã¦ã1ã¤ã®é¢æ°ã«ã§ãã¾ãã // é£çµããé¢æ°ã®é å var fs = Array( (i:Int)=>i+2, (i:Int)=>i*3, (i:Int)=>i-2) // é£çµããé¢æ°ãå®è¡ã println( Function.chain(fs)(4) ) å®è¡çµæã§ãã 16
existsãç°¡åã val array = Array("a", "b", "c") if ( array.exists( i=> "a".equals(i) ) ) println( "exist" ) else println( "not exist" ) findãã¦ãisEmptyã§è©ä¾¡ããã¨ãã if ( array.find( i=> "a".equals(i) ).isEmpty ) println( "not exist" )â¦
Scala ã§ãã*ãã使ãã¾ã! println( "a"*20 ) å®è¡çµæã§ãã aaaaaaaaaaaaaaaaaaaa
ãããªæãã§æ¸ãã¦ã¿ãã var datas = Array(9,3,5,7) // æ大 println( "max:" + datas.reduceLeft( (max,i) => i max max ) ) // æå° println( "min:" + datas.reduceLeft( (min,i) => i min min ) ) // å¹³å println( "avg:" + datas.reduceLeft( (sum,â¦
ãBy-Name Parametersãã使ãã¨ã foo { println("aaaa") } ã¿ãããªæãã§å¼ã³åºããé¢æ°ãä½æã§ãã¾ããé¢æ°fooã®å®ç¾©ã¯ä»¥ä¸ã®ãããªæãã def foo[R]( proc: => R ):R = { try { println("before...") proc // å¼æ°ã®procãè©ä¾¡ãã¦å®è¡ } finally { pâ¦
NodeSeq#\ãNodeSeq#\\ã§éãã§ã¿ãã // XML ãªãã©ã« var xml:scala.xml.Elem = <kittens type="animal" xmlns="http://kittens.com" xmlns:t="http://tigers.com"> <kitten age="1">mii</kitten> <t:tiger age="2">tora</t:tiger> <kitten age="3">shiro</kitten> <foo> <kitten age="3">kuro</kitten> </foo> </kittens> // ã«ã¼ãç´ä¸ã®kâ¦
XMLãªãã©ã«ã¨ãããã¾ãã // XML ãªãã©ã« var xml:scala.xml.Elem = <kittens xmlns="http://kittens.com"> <kitten>mii</kitten> <kitten>tora</kitten> <kitten>shiro</kitten> </kittens> // kittens/kittenè¦ç´ ãæ¢ãã¦ãã¦ãã®ããã¹ãã表示ããã // -filterã®ã¨ããã¯ãNode.xmlTypeãã使ãã®ããããã // -ãNode.\ãã§ãã£ã¨ã·ã³ãã«ã«ãããããâ¦
ã¯ã©ã¹ãTraitã«ã def unary_<æ¼ç®å(- or + or ! or ~)> = ... ã®ãããªã¡ã½ãããå®ç¾©ãã¦ããã¨ã <æ¼ç®å> <ã¬ã·ã¼ã> ã®å½¢å¼ã§ã¡ã½ãããå¼ã³åºããã¨ãã§ãã¾ãã // ãã¹ãç¨ã¯ã©ã¹ class Foo( value:Int ) { override def toString = value.toStrinâ¦
æè¿æ°ä»ããã®ã ãã©ãscalaã®ä¸çã«ããScala Bazaarsãã¨ããããã±ã¼ã¸ç®¡çã®ä»çµã¿(rubyã®RubyGemsçãªã¢ã)ãããã¾ãããsbazãã¨ãããã¼ã«ãæä¾ããã¦ãã¦ãããã使ã£ã¦ããã±ã¼ã¸ã®è¿½å /åé¤ãå¯è½ãåºæ¬çãªä½¿ãæ¹ã¯æ¬¡ã®éãã§ãã # ã¤ã³ã¹ãâ¦
Specsã¯ãScalaã§ä½¿ããBDDãªãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã§ããJUnit4ã¢ããã¿ãæä¾ããã¦ãããMavenã«çµã¿è¾¼ãã§ä½¿ããã¨ãå¯è½!ã¨ãããã¨ã§ãMavenãªJavaããã¸ã§ã¯ãã®ãã¹ããScala+Specsã§æ¸ãæé ãã¾ã¨ãã¦ã¿ã¾ãã ã¨ãã£ã¦ãåºæ¬çã«ã¯ã pom.xmâ¦
maven-scala-pluginã§ã¯ãscaladocã®ä½æããµãã¼ãããã¦ãã¾ããæé ã¯æ¬¡ã®éãã pom.xmlã®ãreportingãã«maven-scala-pluginã®è¨å®ãæ¸ã ãmvn scala:docã! 1.pom.xmlã«maven-scala-pluginã®è¨å®ãæ¸ãã pom.xmlã®ãproject/reportingãã«ä»¥ä¸ã®è¨å®â¦
"""<æåå>""" ã§æ¹è¡ãå«ãæååãªãã©ã«ã¨ãªãã¾ãã // æ¹è¡ãå«ãæååãªãã©ã«ã var strs = """aaa bbb ccc""" println(strs) å®è¡çµæã§ããã¤ã³ãã³ãããã®ã¾ã¾æ®ãã¾ãã aaa bbb cccã¤ã³ãã³ãããã¼ãã¨ãæã人ã¯ãRichString#stripMarginãâ¦
Mavenã使ã£ã¦Scalaã®ã½ã¼ã¹ã³ã¼ããã³ã³ãã¤ã«ããç°¡åãªãµã³ãã«ã§ããmaven-scala-pluginã使ã£ã¦ããã£ã¨ã³ã³ãã¤ã«ã§ãã¾ãã Scalaã®ã½ã¼ã¹ã³ã¼ãç½®ãå ´ãä½æããã½ã¼ã¹ã³ã¼ããé ç½® pom.xmlã«maven-scalaãã©ã°ã¤ã³ã®åå¾å ã¨ããscalaã§ã³ã³ãã¤ã«â¦
ãªãã¸ã§ã¯ããæå®ã®åã«ãã£ã¹ãããã«ã¯ãAny#asInstanceOfã使ãã¾ããã¾ããæå®ããåã¨äºææ§ããããè©ä¾¡ããã«ã¯Any#isInstanceOfã使ãã¾ãã ã©ã¡ããAnyã®ã¡ã½ããã§ãã åãã©ã¡ã¼ã¿ã¨ãã¦ãå¤æorè©ä¾¡ãããåã渡ãã¾ãã var i = 10 var stâ¦
abstractãªclass or trait ã«ãåæå®ã®ãªãååã ãã®type aliasãæ¸ãã¨ãæ´¾çã¯ã©ã¹ã§ãã®type aliasã®å®ç¾©ãå¼·å¶ãããã¨ãã§ãã¾ãã abstract class Foo( ) { // æ´¾çã¯ã©ã¹ã§æ示ããå¿ è¦ãããtype alias // ãã®ã¯ã©ã¹ã®æ´¾çã¯ã©ã¹ã§ã¯ãXã®type alâ¦
type <åå> = <å> ã§ãåã«å¥åãã¤ãããã¨ãã§ãã¾ãã å¥åãã¤ããåã¯ãå¥åã§ãã©ã¡ã¼ã¿ã®åæå®ãªã©ã«ä½¿ãã¾ãã è¤éãªåãã©ã¡ã¼ã¿ãæã¤åãè¤æ°å使ããããªå ´åã«ä¾¿å©ã // ãArray[Int]ãã®å¥åã¨ãã¦Xãä½ãã type X = Array[Int] // Xåã®â¦
MailBoxã¯ãè¤æ°ã¹ã¬ããéã§ã®åæå¶å¾¡ã¨ã¡ãã»ã¼ã¸ã®éåä¿¡ãããã£ã¨ã§ããããã«ããã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã§ãã è¦ã¯ãJavaã«ãããjava.util.concurrent.BlockingQueueã§ããã¡ãã»ã¼ã¸éä»å´ã¯ãããã¯ããªããããªã®ã§ã容éå¶éã®ãªãjava.util.conâ¦
synchronizedã§è¤æ°ã¹ã¬ããã®åæå®è¡å¶å¾¡ãã§ãã¾ãã æ©è½ã¯Javaã®synchronizedã¨åãã§ãæå®ãããå¦çãã1ã¹ã¬ããããã®ã¿å®è¡ãããããã«ãªãã¾ããåæã«è¤æ°ã¹ã¬ããããå®è¡ããããã¨ã¯ããã¾ããã Javaã¨éã£ã¦ããã¹ã¦ã®åç §åã®åºåºã¯ã©ã¹â¦
scala.concurrent.opsã§å®ç¾©ããã¦ããspawnã¡ã½ããã使ã£ã¦ãä»»æã®å¦çãå¥ã¹ã¬ããã§ä¸¦åã«å®è¡ããããã¨ãã§ãã¾ãã å¦çã¯å¼æ°ã§æ¸¡ãã¾ãã å é¨çã«ã¯ãå¦çãå®è¡ããã ãã®å°ç¨ã®ã¹ã¬ããã1ã¤çæãããããã§ããã¹ã¬ããã¯å¦çã®å®è¡å®äºå¾ã«â¦
ä»æ´ã§ãããä¸çã®ããã¢ãã« Scala ã§ææ¦ãã¦ã¿ã¾ããã Scalaã ã¨ã <ã¬ã·ã¼ã> <ã¡ã½ãã> <å¼æ°>ã§ã¡ã½ãããå¼ã³åºãããã§ããã®ã§ãã.ãã¨ãã()ãã¨ããæé¤ã§ãã¦ãã°ããã¨ã«ãªããããã!ã¨æã£ã¦å§ããã®ã ãã©ã0ã«ããã®ã¯ç¡çã§ããã impâ¦
Regexã使ãã®ããããã§ãã 使ãæ¹ã¯APIããã¥ã¡ã³ããè¦ãã°ã ãããããããã¨ã ãªããå¤ãScalaã«ã¯å«ã¾ãã¦ããªãã®ã§æ³¨æãscala-2.7.0ã«ã¯å ¥ã£ã¦ããããscala-2.7.1.finalãè½ã¨ãã¦ããã¨å ¥ã£ã¦ã¾ããã 以ä¸ã¯ç°¡åãªãµã³ãã«ã§ãã // æ£è¦è¡¨ç¾ â¦
ãæé»ãã·ãªã¼ãº3ã¤ããåãã©ã¡ã¼ã¿ã®æå®ã§ [<åãã©ã¡ã¼ã¿å> <% <ã¯ã©ã¹>] ä¾) [X <% String] ã¨ãããã¨ã§ããã«æé»çã«å¤æå¯è½ãªãªã«ãããåãä»ããé¢æ°ãä½æã§ãã¾ãã ä¸ã®ä¾ã ã¨ãXã¯ãStringã«å¤æå¯è½ãªä½ããã«ãªãã¾ãã ãªã®ã§ãå®è¡æâ¦
ãæé»ãã·ãªã¼ãºäºã¤ãããimplicitããã¤ããåå¤æç¨é¢æ°ãå®ç¾©ãããã¨ãæé»ã®åå¤æãè¡ãããããã«ãªãã¾ãã åå¤æã¯ãå¿ è¦ãªã¨ãã«èªåçã«å¤æé¢æ°ãå®è¡ããããã¨ã§è¡ããã¾ãã åå¤æç¨ã®é¢æ°ã¯ãimplicitããã¤ãã¦å®ç¾©ãã¦ããå¿ è¦ãããâ¦
é¢æ°ã®ãã©ã¡ã¼ã¿ã«ãimplicitããã¤ãã¦ããã¨ãé¢æ°ã®å¼ã³åºãæã«ãã®å¼æ°ãçç¥ã§ããããã«ãªãã¾ããçç¥ãããå¼æ°ã«ã¯ãã¡ã½ããã®å¼ã³åºãå ã³ã³ããã¹ãã§ä¸çªããã£ã½ãimplicitä»ãã®å¤ãã³ã³ãã¤ã©ãé¸æãã¦æ¿å ¥ãã¦ããã¾ãã ãimplicitãã¯â¦
ãvalãã§ä¸å¤ã®å¤ãå®ç¾©ããéã«ããlazyã修飾åãã¤ããã¨ãå¤ãé 延è©ä¾¡ããã(æåã«ã¢ã¯ã»ã¹ãããã¨ãã«ã®ã¿è©ä¾¡ãããå®ç¾©æã«ã¯è©ä¾¡ãããªã)ããã«ãªãã¾ãã ãlazyãã¯ãvalãã«ã®ã¿è¨å®ã§ãã¾ãããvarãããdefãã«è¨å®ãããã¨ã¯ã§ãã¾ãããâ¦
è¦ç´ ãå¿ è¦ã«ãªã£ãã¨ãã«ä½æãã¦è¿ãä»çµã¿ã¨ãã¦ã¯Streamã®ã»ãã«Iteratorãããã¾ããhasNext(),next()ãæã¤ã¢ã¬ã§ããJava使ãã«ã¯ãã£ã¡ã®æ¹ãããããããããã以ä¸ã¯ãã£ããããæ°ãé ã«è¿ãã¤ãã¬ã¼ã¿ã®ãµã³ãã«ã§ãã Iteratorãç¶æ¿ããç¡åã¯â¦