8000 scala recap · coder-RT/spark-optimization@9e5f127 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e5f127

Browse files
scala recap
1 parent f826a42 commit 9e5f127

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package part1recap
2+
3+
import scala.concurrent.Future
4+
import scala.util.{Failure, Success}
5+
6+
object ScalaRecap extends App {
7+
8+
// values and variables
9+
val aBoolean: Boolean = false
10+
11+
// expressions
12+
val anIfExpression = if(2 > 3) "bigger" else "smaller"
13+
14+
// instructions vs expressions
15+
val theUnit = println("Hello, Scala") // Unit = "no meaningful value" = void in other languages
16+
17+
// functions
18+
def myFunction(x: Int) = 42
19+
20+
// OOP
21+
class Animal
22+
class Cat extends Animal
23+
trait Carnivore {
24+
def eat(animal: Animal): Unit
25+
}
26+
27+
class Crocodile extends Animal with Carnivore {
28+
override def eat(animal: Animal): Unit = println("Crunch!")
29+
}
30+
31+
// singleton pattern
32+
object MySingleton
33+
34+
// companions
35+
object Carnivore
36+
37+
// generics
38+
trait MyList[A]
39+
40+
// method notation
41+
val x = 1 + 2
42+
val y = 1.+(2)
43+
44+
// Functional Programming
45+
val incrementer: Int => Int = x => x + 1
46+
val incremented = incrementer(42)
47+
48+
// map, flatMap, filter
49+
val processedList = List(1,2,3).map(incrementer)
50+
51+
// Pattern Matching
52+
val unknown: Any = 45
53+
val ordinal = unknown match {
54+
case 1 => "first"
55+
case 2 => "second"
56+
case _ => "unknown"
57+
}
58+
59+
// try-catch
60+
try {
61+
throw new NullPointerException
62+
} catch {
63+
case _: NullPointerException => "some returned value"
64+
case _: Throwable => "something else"
65+
}
66+
67+
// Future
68+
import scala.concurrent.ExecutionContext.Implicits.global
69+
val aFuture = Future {
70+
// some expensive computation, runs on another thread
71+
42
72+
}
73+
74+
aFuture.onComplete {
75+
case Success(meaningOfLife) => println(s"I've found $meaningOfLife")
76+
case Failure(ex) => println(s"I have failed: $ex")
77+
}
78+
79+
// Partial functions
80+
val aPartialFunction: PartialFunction[Int, Int] = {
81+
case 1 => 43
82+
case 8 => 56
83+
case _ => 999
84+
}
85+
86+
// Implicits
87+
88+
// auto-injection by the compiler
89+
def methodWithImplicitArgument(implicit x: Int) = x + 43
90+
implicit val implicitInt = 67
91+
val implicitCall = methodWithImplicitArgument
92+
93+
// implicit conversions - implicit defs
94+
case class Person(name: String) {
95+
def greet = println(s"Hi, my name is $name")
96+
}
97+
98+
implicit def fromStringToPerson(name: String) = Person(name)
99+
"Bob".greet // fromStringToPerson("Bob").greet
100+
101+
// implicit conversion - implicit classes
102+
implicit class Dog(name: String) {
103+
def bark = println("Bark!")
104+
}
105+
"Lassie".bark
106+
107+
/*
108+
- local scope
109+
- imported scope
110+
- companion objects of the types involved in the method call
111+
*/
112+
113+
}

0 commit comments

Comments
 (0)
0