90 likes | 107 Views
6/13/2014 Redo Week 1 First Scala Course/Demo Scalatestest Spray Can (in JobServer)/sbt. Scala/Spark Review. Recursion. 3 exercises on Recursion Point is: to use matching as replacement for loops Very few if/then statements in proper Scala code Example: Factorial w/wo if then.
E N D
6/13/2014 Redo Week 1 First Scala Course/Demo Scalatestest Spray Can (in JobServer)/sbt Scala/Spark Review
Recursion • 3 exercises on Recursion • Point is: • to use matching as replacement for loops • Very few if/then statements in proper Scala code • Example: • Factorial w/wo if then
Factorial factorial(n:Int):Int={ if(n==0) 1 else n*factorial(n-1) }
Factorial with match def factorial(n:Int):Int = n match{ case 0 => 1 case _ => n*factorial(n-1) }
codingbat examples • http://codingbat.com/java/Recursion-1 ,recursion 2 • Doesn't cover backtracking • Doesn't exists for scala, copy and paste questions and code up solns in unit test. Add maven artifact/scalatest
Maven/Scalatest • Disable surefire tests. Have to add the Java unit tests separately. HW uses @RunWith(classOf[JUnitRunner]) <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7</version> <configuration> <skipTests>true</skipTests> </configuration>
Demo • > cd TestScalatest ; mvn test • Some strange looking patterns: • Either do nested case statement or mixed if/case statements. Can't put conditionals in case () • case _ => if (n<10 && n==7) 1 else if(n<10 && n!=7) 0 else if ((n%10)==7) 1+count7(n/10) else count7(n/10)
SprayCan • Scala server. • Akka-io config, jetty config (servlets) • Used in spark-job-server from Ooyala • Sbt; • build.sbt or scala code. Build.sbt specify k/v pairs for config map • Job-server doesn't use build.sbt. Replace with scala code/sbt api • No blank lines in sbt.build at top; uses for delimiters. Opposite of xml
Spray Resources • SprayExamples from github • Add build.sbt, plugins.sbt • Doesn't exist in SprayExamples • Match akka with spray-can versions java.lang.NoSuchMethodError: akka.actor.Props$.apply(Lscala/Function0;Lscala/reflect/ClassTag;)Lakka/actor/Props; at spray.can.HttpExt.<init>(Http.scala:153)