0% found this document useful (0 votes)
663 views2 pages

Scala Cheat Sheet Amresh

1. Scala is a functional programming language that emphasizes immutable data and avoids side effects. 2. Core concepts include functions as first-class values, immutable variables, classes and objects, traits for inheritance, and control structures like loops and conditionals. 3. Common data types include collections like Lists, Maps, Sets, and Arrays as well as tuples, case classes, and singleton objects.

Uploaded by

BitCoin Mining
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
663 views2 pages

Scala Cheat Sheet Amresh

1. Scala is a functional programming language that emphasizes immutable data and avoids side effects. 2. Core concepts include functions as first-class values, immutable variables, classes and objects, traits for inheritance, and control structures like loops and conditionals. 3. Common data types include collections like Lists, Maps, Sets, and Arrays as well as tuples, case classes, and singleton objects.

Uploaded by

BitCoin Mining
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

scala cheat sheet

functional programming functions try, catch, finally traits

def hello(s: String): Unit = { try { traits like class except:


1. functions are first-class values println(Hello + s) something 1 - no class params
2. immutable data, no side effects } } catch { 2 - super dynamically bound
def hello: Unit = println(Hello) case ex: IOException => // handle
def hello = println(Hello) case ex: FileNotFoundException => trait Talks {
variables // not recommended // handle def speak() {
def hello { println(Hello) } } finally { doStuff } println(Yada yada yada...)
var x = 1 // mutable }
val x = 1 // immutable // variable length args }
val s = foo // implicit type def foo(args: String*) = {
classes and objects
val i:Int = 1 // explicit type args.foreach(...) class A { ... }
trait T1 { ... }
package foo.bar
// named args trait T2 { ... }
loops point(x=10, y=20) class B extends T1 { ... }
import java.io.File
class C extends A with T1 with T2
import java.io._
while (i < foo) { // can call without a dot or parens if
import java.io.{Foo, File => Bar}
println(i) // it takes only one param
} var x = f.add(10) scripts
class A { ... }
var x = f add 10
class A (s: String) { ... }
do { class A (val s: String) { ... } println(args(0))
// stuff // function literal syntax println(args.toList)
class A (private val s: String) { ... }
} while (condition) (x: Int, y:Int) => x + y args.foreach(println)
class A (var s: String) { ... }
scala foo.scala
for (arg <- args) println(arg) - functions return last value computed
class Person (s: String) {
for (i <- 0 to 5) println(i) by method #!/bin/sh
require(name != Joe)
for (i <- 0 until 10 by 2) - if name ends in :, invoke on right exec scala "$0" "$@"
val name: String = s
println(i) operand !#
private val a = foo
- + is a method on Int, String object Hello {
}
for ( - apply(), update() def main(args: Array[String]) {
class Bird extends Animal with Wings {
file <- files override val foo = true args.foreach(println)
if file.isFile } }
if file.getName.endsWith(.a) data types }
) doSomething(file) // singleton objects
val names = Array(Al, Bob) // Application trait
object Foo {
args.foreach(arg => println(arg)) val names = new Array[String](5) object Hello extends Application {
def main(args: Array[String]) = {
args.foreach(println(_)) val names = Map(99676 -> AK) args.foreach(println)
args.foreach(println)
args.foreach(println) } }
val names = List(Al, Bob)
}
val names2 = Joe :: names
object Bob extends Person { ... }
control structures underscore
// tuples
// abstract class
val things = (100, Foo) think of _ as a blank that needs
if (a == b) foo() abstract class Person {
println(things._1) to be filled in
if (a == b) foo else bar // method with no implementation
println(things._2)
if (a == b) { def walk: Unit
foo } strings.map(_.toUpperCase())
} else if (a == c) { (1 to 10).map(_*2)
collections
bar class Employee(name: String)
} else { extends Person { args.foreach(println(_))
Sequence, List, Array, ListBuffer, args.foreach(println)
baz ...
ArrayBuffer, StringOps, Set, Map,
} }
TreeSet, Stream, Vector, Stack, numbers.filter(_ < 10)
Queue, Range, BitSet, ListMap, more
== is not reference equality // sealed class - no new subclasses
// unless defined in current file // for each element in the array
- Tuples can hold different objects println(array: _*)
foo match { sealed sbstract class Foo { ... }
- Mutable and immutable collections
case a => doA() case class Bar(s: String) extends Foo
- traits: Traversable, Iterable, Seq,
case b => doB() IndexedSeq, LinearSeq, Buffer
case _ => doDefault
}

1 devdaily.com
scala cheat sheet

case classes actors


scala.Any
abstract class Expr import scala.actors._
case class Var(name: String) extends Expr object Foo extends Actor {
case class Num(num: Double) extends Expr def act() {
// your logic here
scala adds syntactic conveniences: } scala.AnyRef
} scala.AnyVal
(java.lang.Object)
1) adds a factory method with the
name of your class import scala.actors.Actor._
2) all args in param list implicitly get val hiActor = actor {
a val, and become fields while(true) {
3) add implementations of toString, receive {
hashCode, and equals case msg => scala.Double
4) adds a copy method // your logic scala.Float
} scala.Int
examples: } scala.Long java classes ...
} scala.ScalaObject
scala.Short
1) val v = Var(x) scala.Byte
2) v.name object Foo extends Actor { scala.Char
3) println(v) (shows toString), def act() { scala.Boolean
== works react { scala.Unit
4) v.copy case ...
} scala.Seq
see http://www.scala-lang.org/node/107 } scala.List
} scala.Option

// send message (other scala


case, match classes ...)
hiActor ! hello
selector match { choices }
_ is the wildcard pattern notes:

def f(x: Int): String = x match { 1. share-nothing, message-passing


case 1|2|3 => 1-2-3 model
// default 2. receive, receiveWithin
scala.Null
case _ => huh? 3. react is more efficient
} 4. dont block when processing
messages (helper actor)
def f(x: Any): String = x match { 5. prefer immutable messages
case 1 => one 6. make messages self-contained scala.Nothing
case 2 => two
// typed pattern
case i:Int => got an int http://www.scala-lang.org/node/71
much more
case s:String => got a string
case _ => // do nothing
// type alias
}
type D = Double
more information
pattern matching:
// anonymous function
(x:D) => x + x http://scala-lang.org
1. constant pattern (a, 10)
2. variable pattern (x)
// lisp cons http://www.scala-lang.org/docu/files/collections-api/collections.html
3. wildcard pattern (_)
var x = 1 :: List(2,3)
4. constructor pattern
Mailing lists: http://www.scala-lang.org/node/199
( Foo(-, e) )
var(a,b,c) = (1,2,3)
5. typed pattern (see above)
val x = List.range(0,20) Lift framework: http://liftweb.net
isInstanceOf and asInstanceOf are
discouraged

2 devdaily.com

You might also like