-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.txt
98 lines (92 loc) · 1.73 KB
/
repl.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
1 + 1
:type 1
1.0 / 2
:type 1.0
"a" + "b"
:type "a"
"ab" * 3
"a".+("b")
"ab".*(3)
1 to 10
1 until 10
1 to 10 by 3
6 to 1 by -2
1.to(10)
BigInt("12345679") * 9
Seq(1, 1, 2, 2, 3, 3)
Set(1, 1, 2, 2, 3, 3)
Seq(1, 1, 2, 2).toSet
IndexedSeq(1, 2, 3)
IndexedSeq(1, 2, 3) :+ 4
0 +: IndexedSeq(1, 2, 3)
List(1, 2, 3)
1::(2::(3::Nil))
1::2::3::Nil
Nil.::(3).::(2).::(1)
val a = 1
var b = 2
b = 3
a = 4
val c = 1 + 2; c + 3
val pair = ("ans", 42)
val (label, value) = pair
pair._1
pair._2
val List(a, b, c) = List(1, 2, 3)
val Array(a, b, c) = "1 2 3" split ' '
def size = 2
5 * size
def square(x: Int) = x * x
square(2)
square(square(2))
def sumOfSquare(x: Int, y: Int) = square(x) + square(y)
def abs(x: Int) = if (x >=0) x else -x
val (xs, ys) = (List(1, 1, 2, 2, 3, 3), IndexedSeq(1, 2, 3))
xs.head
xs.tail
ys.last
ys.init
xs take 4
xs drop 4
ys(1)
xs ++ ys
xs.reverse
ys updated(1, 10)
ys
xs indexOf 3
xs contains 3
xs contains 4
xs zip ys
ys zip xs
xs.sum
xs.product
xs.max
xs.min
xs filter (x => x%2 == 0)
xs filterNot (x => x%2 == 0)
xs filter (_%2 == 0)
xs partition (_%2 == 0)
xs takeWhile (_%3 > 0)
xs dropWhile (_%3 > 0)
xs span (_%3 > 0)
xs map (x => x * 2)
xs map square
xs groupBy (_ % 2)
xs.reduceLeft((acc, x) => acc + x)
xs.foldLeft(10)((acc, x) => acc + x)
xs.foldLeft(10)(_ + _)
(10 /: xs)(_ + _)
def something() = { println("calling something"); 1}
something
def callByValue(x: Int) = { println("x1=" + x); println("x2=" + x) }
def callByName(x: =>Int) = { println("x1=" + x); println("x2=" + x) }
callByValue(something())
callByName(something())
def sum(f: Int => Int)(a: Int, b: Int) = (a to b map f).sum
sum(x => x * x)(1, 10)
sum(_ * 2)(1, 10)
def f(a: Int, b: Int): Int = a * b
def g(a: Int)(b: Int): Int = a * b
f(2, 3)
g(2)(3)
sum(g(2))(1, 10)