Two of the first things I read about Scala is that "Every value is an object" and "Every function is a value". With this knowledge one can easily conclude that all functions are objects. I also read that Scala is a so called pure object oriented language where everything is an object. While this is easy to remember its harder to understand the implications of such a simple statement. As an application developer I don't think its crucial to understand the true meaning of it, just that it is a important fact in the design of the language.
At the point of writing the current version of the language is 2.10. The first version available version appeared in 2003 and its creator is Martin Odersky. He is still engaged in Scala and teaches an online course in Scala on Coursera (link later in this post). Scala is under development and from time to time new releases are made available. The Scala Improvment Process (SIP) provide insight into pending and completed changes to the Scala language.
One of the challenges in learning something new is finding good material to learn from. When learning to program the resources are often plentiful and this makes it only harder. Just by doing a few searches on Scala I was able to find tutorials, books, videos and who else knows what. For me its a thin balance between collecting too much and too little reference material and its important not to be on the far edge of either side. If I have to much material the task of digesting and extracting what is useful is overwhelming and off-putting. If I have too little references there's a risk that I will shortly again be searching for more information and risk being overloaded that time. To get started I've filtered the resources available online and ended up with this list.
- Official Scala Language Specification (PDF) - Useful to look in from time to time. I wont read it back to back.
- A cheat sheet with small nuggets of Scala code
- The well know site Stack Overflow have a compiled list of questions tagged with Scala
- A selection of video recordings of Scala topics are on Nescala.org. Taught by Martin Odersky
- Scala School tutorial with 14 sections
- Course online course
- Ecplise
- IntelliJ IDEA
- Netbeans
- http://ideone.com/
- http://www.compileonline.com/
compile_scala_online.php - http://www.simplyscala.com/
Some of the central concepts in Scala that a beginner should have no problems understanding I've tried to explain below which will be the end of my first blog post on Scala.
Higher order functions are the opposites to a first order functions and come in three forms:
- A function with one or more functions as parameters, and that returns a value
- A function that takes one or more values as parameters, and that returns a function
- Both of the above, i.e. a function that returns another function and have one or more functions as input parameters
In calculus, good examples are the limit function, which given a function returns a value, and the derivative function, which given a function returns a different function.
An example in Scala looks like this:
def apply(f: Int => String, v: Int) = f(v)
Currying is a process that transforms a function that has more than one parameter into a series of embedded functions, each of which has a single parameter. In other words, when a function is called with fewer number of arguments as prescribed by the function signature a new function will be returned that expects the missing arguments as parameters.
def route (m:Message) = {
(e: Endpoint) => e.send(m)
}
case class Demo( title : String, author : String )
Sequence comprehension, in Scala also called for-comprehension (generally called list comprehension in other constructs), is the process of creating a sequence based on an existing sequence. If its sounds like a for-loop it is because it its very similar. Sequence comprehension has it roots in mathematics in generally composed of an output variable(in the example the output variable is i), input domain or generator (in the example the List.range), the guard(in the example the if-statement), and the output function (in this case also the i variable)
for (i <- List.range(from, to) if i % 2 == 0) yield i
An in-depth explanation of comprehensions can be found here.
Closures are functions, whose return value depends on the value of one or more variables declared outside the function. A closure function is a simple function featuring special characteristics. In the example the function on row three is the closure. If you look at that row in isolation you'll notice that the value first is not declared as an argument to the function, it is a so called free variable. What actually happens at runtime is that the compiler extracts a new function that binds the free variable, which is a characteristic of a closure.
val largerThanFirst
= ( xs : List[Int] ) =>
{
val first = xs(0)
val isLarger = ( y : Int )
=> y > first
for ( x <- xs; for ( isLarger (x) ) yield x
}
No comments:
Post a Comment