Scala: 6 Silver Bullets Alex Masselot Dev Forum - Vital-IT Tuesday March 9th, 2015.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 10 API Review; Where Next.
Advertisements

Patterns for Slick database applications Jan Christopher Vogt, EPFL Slick Team #scalax.
7-Jun-14 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Microsoft Research March 20, 2000 A Programming Language for Developing Interactive Web Services Claus Brabrand BRICS, University of Aarhus, Denmark.
1. Scala 2. Traffic DSL in Scala 3. Comparison AToM3 4. Conclusion & Future work.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Introduction to PHP Dr. Charles Severance
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Simple Introduction to Clojure with Analysis By: Shannon Birchell.
Thoughts on Programming Language Books by Magnus Madsen.
6/10/2015Martin Odersky, LAMP, EPFL1 Programming Language Abstractions for Semi-Structured Data Martin Odersky Sebastian Maneth Burak Emir EPFL.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Getting Functional. 2 What is Functional Programming (FP)? In FP, Functions are first-class objects. That is, they are values, just like other objects.
CS0007: Introduction to Computer Programming Introduction to Arrays.
16-Aug-15 Actors Concurrency made easy(-er). 2 The actor model Most of the problems with concurrency--from deadlocks to data corruption-- result from.
Scala Actors -Terrance Dsilva.  Thankfully, Scala offers a reasonable, flexible approach to concurrency  Actors aren’t a concept unique to Scala.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
The Trouble with Types Martin Odersky EPFL and Typesafe.
A Feature Comparison Urs vs.
CMSC 330: Organization of Programming Languages Ruby Hash Tables (and other topics)
AIT 616 Fall 2002 PHP. AIT 616 Fall 2002 PHP  Special scripting language used to dynamically generate web documents  Open source – Free!!!  Performs.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
A Brief Intro to Scala Tim Underwood. About Me Tim Underwood Co-Founder of Frugal Mechanic Software Developer Perl, PHP, C, C++, C#, Java, Ruby and now.
The Scala Programming Language presented by Donna Malayeri.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
UMBC CMSC 331 Traits in Scala. 2 Basic Properties Traits are used to define object types by specifying the signature of the supported methods. Unlike.
Scala Technion – Institute of Technology Software Design (236700) Based on slides by: Sagie Davidovich, Assaf Israel Author: Gal Lalouche - Technion 2015.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
Introduction to Scala Lecture 1 CMSC 331 SD Vick.
Multiparadigm Programming in Scala Adapted from presentation by H. C. Cunningham and J. C. Church University of Mississipi.
- Neeraj Chandra.  It’s language written by by Martin Odersky at EPFL  It’s language written by by Martin Odersky at EPFL (École Polytechnique Fédérale.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
RUBY by Ryan Chase.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Getting Functional. Object-Oriented Programming in Scala Scala is object-oriented, and is based on Java’s model An object is a singleton object (there.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
CSE 341 Section 10 Subtyping, Review, and The Future.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2016 © 1.
Programming Languages Dan Grossman 2013 Everything is an Object.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
“Object-Oriented Meets Functional” SARAH BIRNBAUM KATHERINE JOYCE
SCALA CONCEPTS PROGRAMMING USING FUNCTIONAL OBJECTS UMBC CMSC 331
Multiparadigm Programming in Scala
ML: a quasi-functional language with strong typing
Introduction to Scala SD Vick 1.
Concurrency without Actors
Getting Functional.
Introduction to Python
Introduction to Scala Unit 2
Getting Functional.
CSE-321 Programming Languages Introduction to Functional Programming
(more) Python.
The Scala API.
Presentation transcript:

Scala: 6 Silver Bullets Alex Masselot Dev Forum - Vital-IT Tuesday March 9th, 2015

Scala: JVM on Steroids

Since 2001 (at EPFL)

Six Silver Bullets

Bullet #1: OO & FP

Type Inference val x:Int=10 //explicit val x=10 //implicit Int x=11 //Error var x=10 //mutable x=2.5 //Error

Tuples val y=(1, "paf") //Tuple2[Int,String] println(y._2) //paf val (n, s)=y //n=1, s="paf"

Class class Peak(val x: Double, val intens: Double) { def times(n:2)=new Peak(x, intens*n) def sqrtIntens = intens * intens lazy val hundred = { new Peak(x, 100 * intens) } override def toString = s"[$x, $intens]” } val p = new Peak(12.34, 100) println(p) //”[12.34, 100]”

case class case class Peak(x:Double, intens:Double){ … } val p=Peak(12.34, 100)

value class case class RunId(value:Int) extends AnyVal val rid = RunId(42) val x = rid+12 //Error: meaningless

Everything is a method val i = 40 val j = i + 2 val k = i.+(2) //strictly equivalent

Everything is a method case class Mass(value:Double) extends AnyVal{ def +(m:Mass) = Mass(value+m.value) } case class Peak(x:Mass, intens:Double){ def +(m:Mass) = Peak(x+m, intens) }

Hoare’s One Billion Dollar Error null

Option[T] Try[T], Future[T] Some[T]NoneSuccess[T]Failure(exc)

Singleton: object object Peak{ def apply(m:Double) = Peak(m,0); def readListFromFile(f:File):List[Peak]=… }

Trait for Inheritance

Functional Programming Functions are First Class Citizens def strangeSum(x:Double, y:Double, f:(Double) => Double) = f(x) + f(y) def square(x:Double) = x*x strangeSum(3, 4, square) // 25

Bullet #2: Collections

Scala Cookbook – Alvin Alexander

Collection List[T] : to be traversed, with head and last direct access, Vector[T] : with random access on integer index, Map[K, V] : for dictionary or hash map structure.

val l = List(1,2,3) val l2 = l1 :+ 4 // List(1,2,3,4) val l3 = l1 :: List(5,6,7) // List(1,2,3,4,5,6,7) Adding Values

val l = List(1,2,3,4) l(2) // 3 l.head // 1 l.last // 4 l.tail // List(2,3,4) l.take(2) // List(1,2) l.drop(2) // List(3,4) … Accessing

val l = List(1,2,3,4) l.map(_ * 10) // List(10,20,30,40) l.find(_%2 == 0) // Some(2) l.filter(_%2 == 0) // List(2,4) l.partition(_%2==0) // (List(2,4), List(1,3)) l.combinations(2) // iterator (1,2), (1,3)... lang.org/api/current/index.html#scala.collection.immutable.List And (so) much more

val l:List[Peak] =... peaks.groupBy(p=>math.floor(p.x)).map({pl => (pl._1, pl._2.map(_.intens).sum) }).toList.sortBy(-_._2).take(2) And compose…

And for loops

Bullet #3: Pattern Matching

def useless(x:Any) = x match{ case x:String => "hello "+x case i:Int if i "small" case i:Int => "large" case _ => "whatever" } A Simple Situation

With def biRev(l:List[Int]):List[Int] = l match{ case Nil => List() case x::Nil => List(x) case x1::x2::xs => List(x2, x1):::biRev(xs) } biRev(List(1,2,3,4,5)) //List(2,1,4,3,5)

val reMiles="""(\d+)miles""".r val reKm="""(\d+)km""".r def toKm(x:Any):Int = x match{ case x:Int=> x case reMiles(v) => (v.toInt*1.609).toInt case reKm(v) => v.toInt case _ => throw new IllegalArgumentException( s"cannot convert $x") } With Regular Expressions

Bullet #4: Concurrency

val t0 = System.currentTimeMillis() def ten(i: Int) = { Thread.sleep(100) println(System.currentTimeMillis() - t0) i * 10 } (0 to 20).toList.map(ten).sum; (0 to 20).toList.par.map(ten).sum; List Parallelization

Actors

case class Greeting(who: String) class GreetingActor extends{ def receive = { case Greeting(who) ⇒ println(s"Hello $who") } } val system = ActorSystem("MySystem") val greeter = system.actorOf( Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker")

Way way way more on actors

Bullet #4: the Ecosystem

Great IDE Integration

Typesafe stack Play/Slick/Akka/Spark/Activator

And still all Java

REPL (Read-Eval-Print-Loop)

So Many More Bullets

xml parsing: mixing xpath and the list manipulation for a mixed SAX/DOM approach; http queries; file system traversal and files parsing; Json serialization; dynamic classes, to add handle whatever method code; regular expression (ok, Perl is the king, but Scala is not bad); macros; string interpolation and reverse; more Java integration; abstract classes; profiling with yourkit or jProfiler context bound & parametric types streams foldLeft /: for loops structure map getOrElse case class implicit

Try it!

Further links coursera.org functional programming in scala course by Martin Odersky (and reactive, and parallel in Fall 2015); Scala for the Impatient by Carl S. Horstamnn is a good introduction to the language, specially for Java folks; Programming in Scala by Martin Odersky, Lex Spoon and Bill Venners is a pretty comprehensive one; daily scala blog, for inspiration; scala days conference (videos available); Scala meetups (monthly in Lausanne/Genève).

Try it!