• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
1.2 scala basics
 

1.2 scala basics

on

  • 738 views

 

Statistics

Views

Total Views
738
Views on SlideShare
736
Embed Views
2

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 2

https://twitter.com 2

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    1.2 scala basics 1.2 scala basics Presentation Transcript

    • Scala basics
    • ;
    • Type definitionsScala Javas: String String si: Int int i / Integer i
    • VariablesScala: Java:val s = “Hello World” public final String s = “Hello World”;var i = 1 public int i = 1;private var j = 3 private int j = 3;
    • MethodsScala: Java:def add(x: Int, y: Int): Int = { public int add(int x, int y) { x+y return x + y;} }def add(x: Int, y: Int) = x + ydef doSomething(text: String) { public void doSometing(String text) {} }
    • Methods (2)Scala: Java:myObject.myMethod(1) myObject.myMethod(1);myObject myMethod(1)myObject myMethod 1myObject.myOtherMethod(1, 2) myObject.myOtherMethod(1, 2);myObject myOtherMethod(1, 2)myObject.myMutatingMethod() myObject.myMutatingMethod()myObject.myMutatingMethodmyObject myMutatingMethod
    • Methods (3)Scala: Java:override def toString = ... @Override public String toString() {...}
    • Classes and constructorsScala: Java:class Person(val name: String) public class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
    • Traits (= Interface + Mixin)Scala: Java:trait Shape { interface Shape { def area: Double public double area();} }class Circle extends Object with public class Circle extends Object Shape implements Shape
    • No “static” in ScalaScala: Java:object PersonUtil { public class PersonUtil { val AgeLimit = 18 public static final int AGE_LIMIT = 18; def countPersons(persons: List[Person]) = ... public static int countPersons(List<Person>} persons) { ... } }
    • if-then-elseScala: Java:if (foo) { if (foo) { ... ...} else if (bar) { } else if (bar) { ... ...} else { } else { ... ...} }
    • For-loopsScala: Java:for (i <- 0 to 3) { for (int i = 0; i < 4; i++) { ... ...} }for (s <- args) println(s) for (String s : args) { System.out.println(s); }
    • While-loopsScala: Java:while (true) { while (true) { ... ...} }
    • ExceptionsScala: Java:throw new Exception(“...”) throw new Exception(“...”)try { try {} catch { } catch (IOException e) { case e: IOException => ... ...} finally { } finally {} }
    • Varargsdef foo(values: String*){ } public void foo(String... values){ }foo("bar", "baz") foo("bar", "baz");val arr = Array("bar", "baz") String[] arr = new String[]{"bar", "baz"}foo(arr: _*) foo(arr);
    • (Almost) everything is an expressionval res = if (foo) x else yval res = for (i <- 1 to 10) yield i // List(1, ..., 10)val res = try { x } catch { ...; y } finally { } // x or y
    • Collections – ListScala: Java:val numbers = List(1, 2, 3) List<Integer> numbers = new ArrayList<Integer>();val numbers = 1 :: 2 :: 3 :: Nil numbers.add(1); numbers.add(2); numbers.add(3);numbers(0) numbers.get(0);=> 1 => 1
    • Collections – MapScala: Java:var m = Map(1 -> “apple”) Map<Int, String> m =m += 2 -> “orange” new HashMap<Int, String>(); m.put(1, “apple”); m.put(2, “orange”);m(1) m.get(1);=> “apple” => apple
    • GenericsScala: Java:List[String] List<String>
    • TuplesScala: Java:val tuple: Tuple2[Int, String] = Pair<Integer, String> tuple = new Pair<Integer, String>(1, “apple”) (1, “apple”)val quadruple = ... yeah right... ;-) (2, “orange”, 0.5d, false)
    • PackagesScala: Java:package mypackage package mypackage;... ...
    • ImportsScala: Java:import java.util.{List, ArrayList} import java.util.List import java.util.ArrayListimport java.io._ import java.io.*import java.sql.{Date => SDate} ???
    • Nice to knowScala: Java:Console.println(“Hello”) System.out.println(“Hello”);println(“Hello”)val line = Console.readLine() BufferedReader r = new BufferedReader(newval line = readLine() InputStreamRead(System.in) String line = r.readLine();error(“Bad”) throw new RuntimeException(“Bad”)1+1 new Integer(1).toInt() + new Integer(1).toInt();1 .+(1)1 == new Object new Integer(1).equals(new Object());1 eq new Object new Integer(1) == new Object();"""Asregex""".r java.util.regex.Pattern.compile(“Asregex”);