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
| 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 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
| /* 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 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 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 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
| //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 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
| // 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 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
| // generic trait addable | |
| trait Addable[T] { | |
| def add(a: T, b: T): T | |
| } |
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
| // 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 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
| // add implementation for int type | |
| implicit val addInt = new Addable[Int] { | |
| override def add(a: Int, b: Int): Int = a + b | |
| } | |
| // add implementation for money type | |
| implicit val addMoney = new Addable[Money] { | |
| override def add(a: Money, b: Money): Money = { | |
| Money(a.dollars + b.dollars + ((a.cents + b.cents) / 100), | |
| (a.cents + b.cents) % 100) |
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
| object Example1Semigroup extends App with Data { | |
| trait Addable[T] { | |
| def add(a: T, b: T): T | |
| } | |
| implicit val addInt = new Addable[Int] { | |
| override def add(a: Int, b: Int): Int = 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
| object Example4Semigroup extends App with Data { | |
| implicit val moneySemigroup = new Semigroup[Money] { | |
| override def combine(x: Money, y: Money): Money = { | |
| Money(x.dollars + y.dollars + ((x.cents + y.cents) / 100), | |
| (x.cents + y.cents) % 100) | |
| } | |
| } | |
| import cats.instances.int._ |