Scala jeff

Download Scala jeff

Post on 25-Jan-2015

2.894 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> 1. learn in half an hourhttp://jeffkit.info</li></ul> <p> 2. Scalable language (actor)JVM ,also .netDSL 3. Scala 4. tiobe 2010/1 5. Scala! 6. twitter scala 7. foursquare rogueA Lift/MongoDB Query DSLVenue where (_.mayor eqs 1234)and (_.categories contains thai)orderAsc (_.popularity)fetch (10) 8. &amp;Scala 9. Scala 10. Code Time 11. println (hello Scala) 12. 1 package info.jeffkitobject HelloScala {2 def main(args : Array[String]) : Unit = { 3 println("hello world") 4}}12object -&gt; main34 13. techparty say helloDSL 14. package info.jeffkit1 class Group(val name:String){ def say(msg:String):Unit = { println ("The Group " + this.name + " say : " + msg) }}object HelloScala {def main(args : Array[String]) : Unit = {println("hello scala") 2 val techparty = new Group("techparty")techparty.say("hello scala") 3techparty say "im the magic of DSL" 4}} 1 2 3 4 DSL 15. hello 16. 1class Group private (val name:String){ def say(msg:String):Unit = { println ("The Group " + this.name + " say : " + msg) }}object Group{ 2 private val groups = Map( 3 "techparty" -&gt; new Group("techparty"), "barcamp" -&gt; new Group("barcamp"), "openparty" -&gt; new Group("openparty") ) def getGroup(name:String) = {4 if (groups.contains(name)) groups(name) else null }}123 Map4 17. 1def play(game:String,members:Array[Member],perform:Member =&gt; Unit) = { println ("The group is playing a game : " + game ) members.foreach{ member =&gt; perform(member) 3 } }class Member(val name:String,val topic:String){ def this(name:String){ this(name,null)2 }}val members = Array( new Member("laiyonghao","male",true,"2010,My choices"), 4 new Member("jeff","male",true,"learn scala in haft an hour"), new Member("iceberg") )123Listforeach4List 18. //lambda 1 techparty.play("introduce your self", members, member =&gt; println ("My name is " +member.name))//2 def present(member:Member) = { if (member.topic != null) println (member.name + " is presenting.")}techparty.play("present", members, present)3 // ,val potluck = techparty.play("potluck", members, _:Member =&gt; Unit)potluck(_.name + " is eating")// ============ curry===============4 techparty.play_curry("dismiss", members){ member =&gt; member.name + " is leaving and going home"}1 lambda234 curry 19. python fans mixin 20. 1 trait Fan{ def like() = "techparty"}trait JavaFan extends Fan{ override def like() = "java " + super.like()}2trait PythonFan extends Fan{ override def like() = "python " + super.like()}3 val member = new Member("jeff","learn scala in haft an hour")with JavaFan with PythonFan4 member.like()1,trait 2traitclass 3 mixinwith) 4 mixin 21. 1def search(condition:Any):String = { 2 condition match { case "sleepy" =&gt; println("you look sleepy,sleep 1 second") Thread.sleep(1000) 3 case q:String =&gt; println("search by a keyword :" + q) case ("name",name:String) =&gt; println("search by name : " + name) case (year:Int,month:Int) =&gt; println("search by year " + year +" and " + month) 4 case _ =&gt; println("cant handle your search");return "fail" } "success" } 1 2 3 4 22. val receiver = self 1 actor {receiver ! techparty.search("jeff",2010) 2 } 3 val at = actor {receiver ! (self,techparty.search("sleepy")) } println("waiting for actor to finish") 4 for(i println("hey,at is finish his job,and thestatus is: " + status)case "success" =&gt; println("search finish")case "fail" =&gt; println("search fail")} }1actor 2 3 actor 4 for 4 case 23. Thank you! </p>