Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start on scala [closed]

Tags:

I am .NET developer and I'd like to broaden my horizons a bit and after checking out modern tendencies decided to try Scala. Can you please advise a good strategy to start on it? Should I learn Java first? What source or handbook should I read? Is there any OS projects to practice Scala and grow on them?

Thanks, Dominique

like image 664
make_sense Avatar asked Mar 23 '11 13:03

make_sense


People also ask

What is a free variable in Scala?

A free variable of an expression is a variable that's used inside the expression but not defined inside the expression. For instance, in the function literal expression (x: Int) => (x, y) , both variables x and y are used, but only y is a free variable, because it is not defined inside the expression.

What is Scala closure?

A closure is a function, whose return value depends on the value of one or more variables declared outside this function. The following piece of code with anonymous function. val multiplier = (i:Int) => i * 10.

What is the point of closures?

Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object's properties) with one or more methods.

What is spark closure?

Summing up, closure is those variables and methods which must be visible for the executor to perform its computations on the RDD. This closure is serialized and sent to each executor.


2 Answers

You might gain a first impression by visiting Simply Scala where you have an online interpreter available.

An absolute classic is Scala for Java Refugees which was originally written for people coming from Java, but will be quite helpful for you, considering how similar the basics of C#/Java are.

You don't need to learn Java first , but you need to have the Java runtime/development kit installed and working.

Then go to http://www.scala-lang.org/downloads and download the appropriate package for your operating system (I always prefer the nightly builds of Scala, they have more bug-fixes than the latest stable one).

After that, run the Scala REPL which is basically "Simply Scala offline" (Simply Scala uses the Scala REPL behind the covers, too). Even many Java programmers use the Scala REPL to prototype things first.

If you prefer books to learn I can recommend Programming in Scala (2nd edition) by Martin Odersky (if you start from a language design point of view and want the "reference book"). There are others like "Programming Scala" which are more targeted at beginners so to speak, but personally I found "Programming in Scala" excellent and have learned Scala with just that book.

A nice way to start Scala is working with the collection classes. .NET has added something similar lately with LINQ and extension methods, so it will be easy to pick up for you.

A small example to get you started:

//Define a class with some properties case class Person(name: String, var age: Int, spokenLanguages: String*)  //Create some persons val joe = Person("Joe", 42, "English","French","Danish") val doe = Person("Doe", 23, "English","German") val don = Person("Don", 11, "Italian","French","Polish") val bob = Person("Bob", 17, "German")  //Access a property joe.name  //Don had his 12th birthday! don.age = 12  //Put the persons into a list val persons = List(joe, doe, don, bob)  //Divide the list into minors and adults val (minors, adults) = persons.partition(_.age < 18) //Get the total age of all persons val personsTotalAge  = persons.map(_.age).sum //Return a list with only those speaking English val englishSpeakers  = persons.filter(_.spokenLanguages.contains("English")) //Same as the example above. val englishSpeakers2 =    for{ person   <- persons        language <- person.spokenLanguages         if language == "English"   } yield person 

I'm not that fluent in C#, but I believe many things might look similar to you.

Some examples of Scala's XML support:

//The shoppingCart for breakfast val shoppingCart = <list>                      <item><name>Tomatoes</name><price>0.30</price><amount>4</amount></item>                      <item><name>Eggs</name><price>0.15</price><amount>10</amount></item>                      <item><name>Bread</name><price>2.20</price><amount>1</amount></item>                    </list>  //How much does it cost? val total = (shoppingCart \ "item").map(i => (i \ "price").text.toDouble * (i \ "amount").text.toDouble).sum  //This is a Symbol val sym = 'SomeSymbol  //I'm too lazy to use Strings for XML! (Example for implicits) implicit def symbol2string(symbol: Symbol) = symbol.name  //Now I can use Symbols too! val total = (shoppingCart \ 'item).map(i => (i \ 'price).text.toDouble * (i \ 'amount).text.toDouble).sum 
like image 161
soc Avatar answered Feb 28 '23 05:02

soc


You don't need to learn Java first. Are you familiar with functional programming? If you are, you should be able to jump in quite fast. Anyway, here are some thoughts on how you can learn Scala:

  • Get a good reference book. I recommend Programming In Scala by Odersky, Spoon, and Venners. I find it as one of the most comprehensive Scala books.

  • As with learning any new language, try writing several small application using Scala. If you're not a functional programmer, you might program it in a different paradigm, but that's okay for now. Try writing your program without using "var, (use val instead)" not using loops, and minimizing state change overall.

  • Use sbt to build your program. I'm kinda hesitant to recommend this since you have to learn a new tool to write your program. But I find it a great too to write Scala apps with. And many Scala projects use sbt it seems like.

  • Also check out this comment and that thread overall to help you transition to Scala. Struggle against habits formed by Java when migrating to Scala

like image 27
JWC Avatar answered Feb 28 '23 03:02

JWC