Skip to content

Commit

Permalink
day2
Browse files Browse the repository at this point in the history
  • Loading branch information
mushtaq committed Jul 25, 2014
1 parent f63e366 commit e7ea1fc
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 10 deletions.
23 changes: 23 additions & 0 deletions salestax.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Items: Books/medicines
Compiute the receipt
Taxes
SalesTax: round down
ImportDuty: only for imported items, round up

SalesTax ImportDuty
Books 10.8 9.4
Medicines 2.7 10.1

Billing
Book1: 120.1
Book2: 1004, imported
Book3: 87.8
Medicine1: 503.3
Medicine2: 19.3

Hints:
Use transform method
Use ceil and floor on double
use sum on double sequences

File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions src/main/scala/ws/3.sc → src/main/scala/ws/day1/3.sc
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ def map3(xs: Seq[Int], f: Int => Int): Seq[Int] = {
}
}

lazy val loop1: (Seq[Int], Seq[Int]) => Seq[Int] = { (remaining, acc) =>
if(remaining.isEmpty)
acc
else {
val newAcc = acc :+ f(remaining.head)
loop1(remaining.tail, newAcc)
}
}

loop1(xs, Seq.empty)
// lazy val loop1: (Seq[Int], Seq[Int]) => Seq[Int] = { (remaining, acc) =>
// if(remaining.isEmpty)
// acc
// else {
// val newAcc = acc :+ f(remaining.head)
// loop1(remaining.tail, newAcc)
// }
// }

loop(xs, Seq.empty)
}

def square(a: Int) = a * a
Expand Down
15 changes: 15 additions & 0 deletions src/main/scala/ws/day1/4.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

val x = 10

def While(cond: => Boolean)(body: () => Unit): Unit =
if(cond) {
body
While(cond)(body)
}

var y = 0

While(y < 10){
println(y)
y = y + 1
}
34 changes: 34 additions & 0 deletions src/main/scala/ws/day1/5.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


def add(a: Int, b: Int) = a + b

def adder2(b: Int) = add(2, b)
def adder3(b: Int) = add(3, b)

adder2(10)
adder3(10)

def add1(a: Int)(b: Int) = a + b
val curr = add1 _

val cAdder2 = curr(2)
val cAdder3 = add1(3) _

def applier(x: Int, adder: Int => Int) =
adder(x)

cAdder2(10)
cAdder3(10)

applier(10, add1(100))
applier(10, add1(101))

applier(10, x => add(100, x))
applier(10, x => add(101, x))
applier(10, x => add(101, x))

def dd(x: Int) = add(101, x)

applier(10, dd)


34 changes: 34 additions & 0 deletions src/main/scala/ws/day2/10.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//abstract class CanDrink1(name: String) {
// def apply(age: Int): Boolean
//
// def isWise(age: Int) = apply(age) && age > 43
//}
//
//class Person1(name: String) extends CanDrink1(name + "asasd") {
// def apply(age: Int) = true
//}
//new Person1("blah")

trait CanDrink {

def x: Int

protected def name: String

def apply(age: Int): Boolean

def isWise(age: Int) = apply(age) && age > 43

}

class Person1(val name: String) extends CanDrink {
val x: Int = 101
def apply(age: Int) = true
}

new Person1("blah").name


class Nitin(name: String) {
private val age = name.length
}
24 changes: 24 additions & 0 deletions src/main/scala/ws/day2/11.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

trait A {
def canDrink(age: Int): Boolean = true
}

trait CanDrink extends A{
// val x = 100
override def canDrink(age: Int) =
age > 19 && super.canDrink(age)
}
trait CanRun extends A{
override def canDrink(age: Int) = age > 19
// val y = 200
def canRun(age: Int) = age < 90
}
class Person extends CanDrink with CanRun
class Person1 extends CanRun with CanDrink

val p = new Person
val p1 = new CanDrink with CanRun

//p.x
//p.y
1
22 changes: 22 additions & 0 deletions src/main/scala/ws/day2/12.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

trait A {
def m: Int
def n = m.toString
}

trait B {
def n: String
def m = 10
}



class C extends A with B

val ab: A with B = new A with B
val ba: A with B = new B with A

val c = new C

c.m
c.n
21 changes: 21 additions & 0 deletions src/main/scala/ws/day2/13.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
trait A { self: B =>
def x = 10
def b = a + 2
}

trait B { self: A =>
def y = x + 100
def a = 1
}

new A with B
new A
new B

class C extends A with B

val c = new C
c.a
c.b
c.x
c.y
22 changes: 22 additions & 0 deletions src/main/scala/ws/day2/6.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

object A {
class Person private (var age: Int, val isFemale: Boolean) {
// def this(age: Int) = this(age, false)
def m = Person.b + 2
private def n = "n"
}
object Person {
private val b = 10
def make(age: Int) = {
val p = new Person(age, false)
p.n
p
}
}
val p3 = Person.make(333)
p3: p3.type

val p = new Person(33, true)

}

13 changes: 13 additions & 0 deletions src/main/scala/ws/day2/7.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

case class Address(line1: String, pin: Int)
case class Person(age: Int, isFemale: Boolean, add: Address) {
def incrementedPin = copy(add = add.copy(pin = add.pin + 1))
}

val p = Person(23, true, Address("baner", 21))

def increaseAge(p: Person) =
p.copy(age = p.age +1)
def increasePin(p: Person) = p.incrementedPin

def compare(p1: Person, p2: Person) = p1 == p2
18 changes: 18 additions & 0 deletions src/main/scala/ws/day2/8.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

trait CanDrink {
def apply(age: Int): Boolean

final def isWise(age: Int) = apply(age) && age > 43
}

class X extends CanDrink {
def apply(age: Int) = true

override def isWise(age: Int) = {
if(age > 100) true else super.isWise(age)
}
}

new CanDrink {
def apply(age: Int) = false
}
11 changes: 11 additions & 0 deletions src/main/scala/ws/day2/9.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


case class Person(name: String, age: Int)

val p1 = new Person("mushtaq", 101)
val p2 = new Person("ashish", 10)
val p3 = new Person("ashish", 10)

p1 == p2

p2 == p3

0 comments on commit e7ea1fc

Please sign in to comment.