The document provides an overview of the Scala programming language. It discusses how Scala removes some features from Java like break/continue and static, unifies functional programming and object-oriented programming, and treats functions as first-class objects. Key aspects of Scala covered include treating all operators as methods, higher-order functions, pattern matching with case classes, and functional operations on collections like List.
2. “I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy.“JamesStrachan, Groovy creator
3. “No other language on the JVM seems as capable of being a "replacement for Java" as Scala, and the momentum behind Scala is now unquestionable.“CharlesNutter, JRubycoredeveloper
5. “On my radar, the current best exit strategy for Java is Scala.„BruceEckel,author of many books
6. Scala vs JavaScala removesbreak, continue, gotostaticprimitive typesraw types (every type parameter „generics“ has to be stated)operators (all are methods)override is not more optional (when overriding in Scala one has to declare override)
7. Scala vs Ruby and GroovyScala has two type of fansJava programmers (pros of functional programming)Ruby programmers (pros of language with static typing)Implicits, structural typing and type inference, resembles work with language with dynamic typingScala is compiles a compiled language and all types are checked in compilation timeone can not call a non existing methodno ClassCastException in runtimepossibility to refactoras fast as Java
9. Why to unify FP and OOPBoth approaches are complementaryFunctionalObject-orientedAbstractionFunctionsHigher-order functionsParametric polymorphismExtending, state dependent behaviorInheritancePolymorphismEncapsulationDynamic configuration
19. Example - filteringJavapublic Collection<Ticket> approved(Collection<Ticket> tickets){ Collection<Ticket> result = new ArrayList<Ticket>();for(Ticket ticket : tickets){if(ticket.getState() == State.APPROVED)result.add(icket); }return result;}
20. Example - filteringJavapublicCollection<Ticket> approved(Collection<Ticket> tickets){Collection<Ticket> result = new ArrayList<Ticket>();for(Ticketticket: tickets){ filterif(ticket.getState()== State.APPROVED)result.add(ticket);}returnresult;}With what?What to do?How?
21. Yes, it can be done in JavaInterface for conditon... and the useFilter util methodpublicinterfaceICondition<T> {publicbooleancheck(T toBeChecked);}publicstatic <T> Collection<T> filter(Collection<T> ts, ICondition<T> c){Collection<T> result = new ArrayList<T>();for(T t : ts)if(c.check(t)) result.add(t);returnresult;}CollestionUtils.filter(verzie, new ICondition<Ticket>() { public boolean check(TickettoBeChecked) { return toBeChecked.getState() == State.APPROVED; }});
23. WTF ?What type has the parameter in the function „filter“?List[+A]{deffilter (p : (A) => Boolean) : List[A] }Java : (A) => Booleanpublic interface Function1<O, I> { O apply(I input);}interface ICondition<I> extends IFunction1<Boolean, I> {}
25. Scala ListMost of them is inherited from trait Iterable. So every collection, array has them, because they inherit from Iterable.Similar functions can be written for trees.count (p : (A) => Boolean) :Intexists (p : (A) => Boolean) : Booleanfilter (p : (A) => Boolean) : List[A] find (p : (A) => Boolean) : Option[A] foldLeft [B](z : B)(f : (B, A) => B) : Bforall (p : (A) => Boolean) : Booleanforeach (f : (A) => Unit) : Unitmap [B](f : (A) => B) : List[B] remove (p : (A) => Boolean) : List[A]
26. Function can be assigned to variablevarf : (String => Unit) = (x) => println(x)Function can be passed to another functiontickets filter (t => t.getState() == State.APPROVED)Return value from function can be a function defdeductKind = tax | insurance | retirementFunction is primitive type, basic building block, with the same importance as an integer or a string.If functions takes another function as an argument or if it returns a function, it is called a higher-order function.
28. Type declarationIn Scala types are written after colonexists (p : (A) => Boolean) : Booleanvar age :Int= 24 val age = 33If type is unambiguous, it doesn’t have to be declaredReturn type of function existsArgument p is of type function from A to BooleanType of variableage
29. ValvsVar immutable (v Java it would be final) creates field and gettervalage:Int=22age = 33 //ERROR variable
34. FoldLeftList(1,2,3,4,5).foldLeft(10)(_ * _)(((((10 * 1) * 2) * 3) * 4) * 5)List(1,2,3,4,5).foldLeft(10)( (x, y) => x +y)(((((10 + 1) + 2) + 3) + 4) + 5)Shortcut for arguments of anonymous functions. First underscore means the first argument, second the second…
35. QuestionsHow would we write function “sum” with the help of foldLeft?defdistinct[A](l : List[A]) : List[A] = (l foldLeft List[A]()) {(res, a)=> if (!res.contains(a)) a :: reselseres}defsumInt(l : List[Int]) : Int = l.foldLeft(0)(_ + _)How would we write function “distinct“with the help of foldLeft? (Selects only distinct elements form the list.)
36. Every value is an objectEvery operator a methodfactorial(x - 1)factorial (x.-(1))map.containsKey(‘a’)map containsKey ‘a’There are no operators in Scala. Method names can contain some symbols.Every method can be called on the object without the dot and the brackets.
37. There are no operators in Scala. Method names can contain some symbols.trait Ordered[A] extendsjava.lang.Comparable[A] {def compare(that: A): Intdef < (that: A): Boolean = (this compare that) < 0def > (that: A): Boolean = (this compare that) > 0def <= (that: A): Boolean = (this compare that) <= 0def >= (that: A): Boolean = (this compare that) >= 0defcompareTo(that: A): Int = compare(that)}
38. “While” doesn’t have to be a keyword... But it is...defwhileAwesome(conditional: => Boolean)(f: => Unit) {if (conditional) { fwhileAwesome(conditional)(f) }}var count = 0whileAwesome(count < 5) {println("still awesome") count += 1}>stillawesomestillawesomestillawesomestillawesomestillawesomeConditionalis of type (=> Boolean) , so it is evaluated in the function “whileAwesome”If conditionalwas of typeBoolean, it would be evaluated before calling the function so the loop would never stop writing"still awesome"
39. It has many real life usestx{ valperson = new Person(id=null, name= "Ivanka"); manager.persist(person) }deftx(f: => Unit) = {manager.getTransaction().begin()try {txmanager.getTransaction().commit() } catch {casee: Exception => manager.getTransaction().rollback() }}
40. Functions are objects tootraitFunction1[-S, +T] {defapply(x: S):T}E.g. anonymous function (x: Int ) => x + 1 is translated by the compiler tonew Function1[Int, Int] { def apply(x: Int): Int = x + 1}
41. Functions are objects tooWhile (=>)is a class, it can be inherited from it.So we can specialize the concept of a function.Instead ofa(i) = a(i) + 2we can writea.update(i, a.apply(i) + 2)Array[T] is a function Int => T, so if such function is needed we can pass an Array[T]classArray[T] ( length: Int) extends(Int=> T) {deflength: Int= ...defapply(i: Int): T= ...defupdate(i: Int, x: T): Unit= ...defelements: Iterator[T] = ...defexists(p: T => Boolean):Boolean = ...}
43. CurryingFunction can return a functiondef cat(s1: String)(s2: String) = s1 + s2def cat(s1: String) = (s2: String) => s1 + s2cat("foo")("bar") > java.lang.String = foobarcat("foo") returns a function {(s2 : Int) => “foo” + s2}, to which is passed an argument "bar" Both notations are correct
46. Partial function applicationIt is not needed to pass all parametersUses Curryingval numbers= List(1, 2, 3, 4, 5);println( numbers map ( 8 +))def plus(a : Int)(b : Int) : Int = a + bval plusFive = plus(5) _println( numbers map plusFive )f(A : a)(B : b) : Cf(B : b) : C
47. „There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”C.A.R. Hoare, author of Quicksort
53. StructuraltypingclassEmployee {varmeno : String = null}classFirm {var title : String = nulldef name = "Firm is called " + title}Attribute nameMethod nameEmployee and Firm does not have a common supertype
54. Structural typingdefgetName(x : {val name : String}) : String = x.namedefsetName(x : {var name : String}) : Unit = x.name = "new name"val employee = new Employee()employee.name = “Kate"val firm = new Firm()firm.name = "PosAm"println (getName(employee))println (getName(firm))println (setName(employee))println (setName(firm)) //ERRORtype mismatch; found : firm.type (with underlying type pads.Firm) required: AnyRef{def name: String; defname_=(x$1: String): Unit}
55. DSL - domain specific languageImplicitsHigher order functionsOptional dots, semi-colons, parenthesesOperators like methodsCurrying
56. DSLExternal DSL + own language, freedom - need to write a parser - extensible from insideInternal DSL + extensible from outside, it’s just a library - syntax of a host languageParser library in Scala simplifies writing external DSLsImplicits, higher-order functions, optional dots and brackets, operator methods and currying simplifies writing of internal DSLs
57. External DSL written with the help of library scala.util.parsing.combinator._ /** @returnParser[Money] */defpercentage = toBe ~> doubleNumber <~ "percent" <~ "of" <~ "gross" ^^ {percentage => grossAmount * (percentage / 100.) }defamount = toBe ~> doubleNumber <~ "in" <~ "gross" <~ "currency" ^^ { Money(_) }deftoBe = "is" | "are"defdoubleNumber = floatingPointNumber ^^ { _.toDouble }|, ~> , <~, ^^ are just functionsLibrary pasing.combinator is internal DSL
58. ^^ /** A parser combinator for function application * *<p>`p ^^ f' succeeds if `p' succeeds; it returns `f' applied to the result of `p'.</p> * * @param f a function that will be applied to this parser's result (see `map' in `ParseResult'). * @return a parser that has the same behaviour as the current parser, but whose result is * transformed by `f'. */def ^^ [U](f: T => U): Parser[U] = map(f).named(toString+"^^")
60. TypesObject has only a single instance. One can obtain this instance by writing object’s name. It can be extended by the Traits.Class can inherit from one class but it can be extended by several Traits.Like an interface/abstract class.Can have an implementation.Can not have a constructor.ClassObjectTrait
61. Dependency injectionclassUserRepository{def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User) = println("creating user: " + user) def delete(user: User) = println("deleting user: " + user) } classUserService { def authenticate(username: String, password: String): User = userRepository.authenticate(username, password) def create(username: String, password: String) = userRepository.create(new User(username, password)) def delete(user: User) = All is statically typed. userRepository.delete(user) } UserService is dependent onUserRepository. It can have more implementations as well.UserRepository can have more implementations.
63. Cake PatternobjectComponentRegistryextends UserServiceComponentwith UserRepositoryComponent{valuserRepository = newUserRepositoryvaluserService = newUserService}In the result object we set implementations of all necessary components. Every component uses right implementation on which it is dependent.
66. Pattern matchingMatching by typeval sundries = List(23, "Hello", 8.5, 'q')for (sundry <- sundries) { sundry match { casei: Int => println("got an Integer: " + i) case s: String => println("got a String: " + s) case f: Double => println("got a Double: " + f) case other => println("got something else: " + other) } }
67. Pattern matchingMatching of sequencesvalwillWork = List(1, 3, 23, 90)valwillNotWork = List(4, 18, 52)val empty = List()for (l <- List(willWork, willNotWork, empty)) { l match {case List(_, 3, _, _) => println("4 elements, with the 2nd being '3'.")case List(_*) => println("Any other list with 0 or more elements.") }}
69. Pattern matchingMatching of case classescaseclass Person(name: String, age: Int)valalice = new Person("Alice", 25)val bob = new Person("Bob", 32)valcharlie = new Person("Charlie", 32)for (person <- List(alice, bob, charlie)) { person match {case Person("Alice",25) => println("Hi Alice!")case Person("Bob", 32) => println("Hi Bob!")case Person(name, age) => println(age + " years old person named " + name ) }}
70. RecapScala is a multi-paradigm languageObject-oriented – objects, classes, traits, inheritance, polymorphism, encapsulationFunctional – function is a value, pattern matching, parametric polymorphism (generics), case classes(Dynamic-types) – implicits, traits, structural typing, type inference These features give programmers a feel of language with dynamic types. Although all types are checked in compilation time.
71. And I haven’t mentionImplicit function parametersLazy valuesXML built in syntaxActor ...And it works with Hibernate, Spring, Guice, Maven, Wicket...
72. Want to know more?programming-scala.labs.oreilly.comwww.scala-lang.org