This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//recursive | |
def sum(f: Int => Int, a: Int, b: Int): Int = { | |
if(a > b) 0 else f(a) + sum(f, a + 1, b) | |
} | |
//non recursive | |
def sum(f: Int => Int, a: Int, b: Int): Int = { | |
var res = 0 | |
for(ele <- a to b by 1) | |
res = res + f(ele) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//recursive | |
def sum(f: Int => Int, a: Int, b: Int): Int = { | |
if(a > b) 0 else f(a) + sum(f, a + 1, b) | |
} | |
//non recursive | |
def sum(f: Int => Int, a: Int, b: Int): Int = { | |
var res = 0 | |
for(ele <- a to b by 1) | |
res = res + f(ele) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os.map(x => x.orderDate.replace("-", "").toInt).foreach(println) | |
os.filter(x => x.orderStatus == "COMPLETE") | |
(1 to 100).filter(_ % 2 == 0).reduce((a, b) => a + b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os.sortBy(x => x.orderCustomerId) | |
os.sortWith((a, b) => { | |
if(a.orderCustomerId > b.orderCustomerId) | |
false | |
else if(a.orderCustomerId < b.orderCustomerId) | |
true | |
else { | |
if(a.orderId > b.orderId) | |
false | |
else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os.groupBy(x => x.orderStatus) | |
//To get count by order status | |
os.groupBy(x => x.orderStatus).map(x => (x._1, x._2.size)).foreach(println) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Order( | |
orderId: Int, | |
orderDate: String, | |
orderCustomerId: Int, | |
orderStatus: String | |
) | |
val os = List( | |
Order(1, "2017-01-01", 100, "COMPLETE"), | |
Order(2, "2017-01-01", 20, "CLOSED"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by itversity on 10/05/17. | |
*/ | |
class Fraction(val n: Int, val d: Int) { | |
override def toString = n + "/" + d | |
def result = n/d.toDouble | |
def +(f: Fraction) = { | |
new Fraction(((n*f.d) + (f.n*d)), (d * f.d)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//os is of type immutable set, | |
//hence elements cannot be added to set | |
//+=, ++=, -=, --= will not work | |
val os = Set( | |
Order(1, "2017-01-01", 100, "COMPLETE"), | |
Order(2, "2017-01-01", 20, "CLOSED") | |
) | |
//Creating new set by adding element | |
os + Order(3, "2017-01-01", 301, "PENDING") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val os1 = Set( | |
Order(1, "2017-01-01", 100, "COMPLETE"), | |
Order(2, "2017-01-01", 20, "CLOSED") | |
) | |
val os2 = Set( | |
Order(2, "2017-01-01", 20, "CLOSED"), | |
Order(3, "2017-01-01", 301, "PENDING"), | |
Order(4, "2017-01-01", 202, "CLOSED"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Order( | |
orderId: Int, | |
orderDate: String, | |
orderCustomerId: Int, | |
orderStatus: String | |
) |