Skip to content

Commit d257705

Browse files
author
Vikas Hazrati
committed
Nuggets for Tuples and Strings
1 parent b53452f commit d257705

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.inphina.nuggets
2+
3+
4+
object Strings{
5+
6+
def main(args: Array[String]) {
7+
// 2 ways to represent a string with double quotes or triple double quotes
8+
println("This is one string")
9+
println("""This is one string""")
10+
println("""This is one string
11+
which could be scattered over
12+
a few lines since it
13+
is with triple double quotes
14+
15+
""")
16+
println(""" Allow me to say \"Scala is Great\" """)
17+
println(" Allow me to say \"Scala is Great\" ")
18+
println(""" Allow me to say "Scala is Great" """)
19+
scala.Symbol("vikas")
20+
val s='vikas
21+
print(s)
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.inphina.nuggets.tuples
2+
3+
class Inventory (stock:Int, cost:Long, expiry:String){
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.inphina.nuggets.tuples
2+
3+
class Payment(amount:Int, description:String){
4+
override def toString = "Amount is " + amount+ " and description is " + description
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.inphina.nuggets.tuples
2+
3+
object SimpleTuple{
4+
5+
/*
6+
Main advantage of tuple is to return more than one value
7+
*/
8+
def main(args:Array[String]):Unit = {
9+
val(tuplePosition1,tuplePosition2,tuplePosition3) = tupularFunction();
10+
println(tuplePosition1)
11+
12+
val completeTuple = tupularFunction();
13+
println(completeTuple);
14+
println("The third value of the tuple is " + completeTuple._3)
15+
16+
val stringTuple = anotherFunctionWhichReturnsTwoValues();
17+
println(stringTuple)
18+
19+
println(doSomeHeavyCalculationAndReturnPaymentAndInventoryDetails())
20+
21+
}
22+
23+
def tupularFunction():(Any, Any, Any)={
24+
val temporaryTupleValues = (1,2,3);
25+
return temporaryTupleValues
26+
}
27+
28+
def anotherFunctionWhichReturnsTwoValues(): (String, String) ={
29+
return ("foo", "bar")
30+
}
31+
32+
def doSomeHeavyCalculationAndReturnPaymentAndInventoryDetails():(Payment, Inventory )={
33+
val payment = new Payment(12, "leeway")
34+
val inventory = new Inventory(18, 34,"never")
35+
36+
return (payment, inventory)
37+
38+
}
39+
40+
41+
42+
}

0 commit comments

Comments
 (0)