This file contains 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
// method abstraction basis on type | |
def add[A: Addable](a: A, b: A)(implicit addable: Addable[A]): A = addable.add(a, b) |
This file contains 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
// generic trait addable | |
trait Addable[T] { | |
def add(a: T, b: T): T | |
} |
This file contains 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
// add any two integer value | |
def addInt(a: Int, b: Int): Int = a + b | |
// add employe salary to their account balances | |
def addMoneyMap(balances: Map[String, Money], salary: Map[String, Money]): Map[String, Money] = { | |
balances.foldLeft(salaries){ | |
case (acc, (name, money)) => | |
acc + (name -> acc.get(name).map(addMoney(_ , money)).getOrElse(money)) | |
} | |
} |
This file contains 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
//add money function | |
def addMoney(money1: Money, money2: Money): Money = { | |
Money(money1.dollars + money2.dollars + ((money1.cents + money2.cents) / 100), | |
(money1.cents + money2.cents) % 100) | |
} |
This file contains 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 Money(dollars: Int, cents: Int) | |
trait Data { | |
val balance = Money(102, 44) | |
val salary = Money(320, 0) | |
val balances: Map[String, Money] = Map( | |
"James" -> Money(212, 98), | |
"Jimmy" -> Money(43, 44) | |
) |
This file contains 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
/* Custom Collection */ | |
import scala.collection.mutable.ArrayBuffer | |
case class CustomList[A](elements: A*) { | |
val elems = new ArrayBuffer[A] | |
elems ++= elements | |
def foreach(c: A => Unit): Unit = { | |
elems.foreach(c) |
This file contains 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
scala> import scala.collection.mutable.ArrayBuffer | |
import scala.collection.mutable.ArrayBuffer | |
scala> case class CustomList[A](elements: A*) { | |
| | |
| val elems = new ArrayBuffer[A] | |
| elems ++= elements | |
| } | |
defined class CustomList |
This file contains 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
def method1(arg: Int): String = { | |
s"The method1 passed value is $arg" | |
} | |
def method2(arg1: Int, arg2: Int): String = { | |
s"The method2 passed value sum is ${arg1 + arg2}" | |
} | |
def method3(arg1: Int, arg2: Int, arg3: Int): String = { | |
s"The method3 passed value sum is ${arg1 + arg2 + arg3}" |
This file contains 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
/* | |
Translation 2: | |
def funcd(a: Int): Int = a.+(1); | |
private[this] val inc: Int => Int = { | |
((a: Int) => Test.this.funcd(a)) | |
}; | |
scala.collection.immutable.List.apply[Int](1, 2, 3, 4, 5).map[Int, List[Int]](Test.this.func)(immutable.this.List.canBuildFrom[Int]); | |
scala.collection.immutable.List.apply[Int](1, 2, 3, 4, 5).map[Int, List[Int]]({ |
This file contains 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
object Test extends App { | |
val func = (a: Int) => a + 1 | |
def funcd(a: Int) = a + 1 | |
val inc = funcd _ | |
List(1, 2, 3, 4, 5).map(func) | |
List(1, 2, 3, 4, 5).map(funcd) | |
} |