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
trait Monoid[T] { | |
def associative(t1:T,t2:T): | |
def zero: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
//We will take a really simple example of summing a list of ints | |
def summ(xs:List[Int]):Int = xs.foldLeft(0)(_ + _) | |
//there is a reason why we have selected foldLeft operation | |
//May be later in the blog it will be clear. |
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
//this wont work as type T is unware of the operation + | |
//value 0 is not a zero value for type T | |
def summ[T](xs:List[T]):T = xs.foldLeft(0)(_ + _) |
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
//we have our monoid | |
trait Monoid[T] { | |
def associative(t1:T,t2:T):T | |
def zero:T | |
} | |
// lets have an implimentaion for int type | |
implicit val IntSumMonoid = new Monoid[Int] { | |
def associative(t1:Int,t2:Int): t1 + t2 | |
def zero:Int = 0 |
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
// one more implementation | |
implicit val StringConcateMonoid = new Monoid[String] { | |
def associative(t1:String,t2:String): t1 + t2 | |
def zero:Int = "" | |
} | |
//Lets pass some strings | |
summ(List("1.","2.","3...")) |
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
trait Monoid[T] { | |
def assco(t1:T,t2:T):T | |
def zero:T | |
} | |
implicit val IntSumMnoid = new Monoid[Int] { | |
override def assco(t1: Int, t2: Int): Int = t1 + t2 | |
override def zero: Int = 0 | |
} |
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 add[A: Num](a: A, b: A): A = implicitly[Num[A]].+(a, b) | |
trait Num[A] { | |
def +(a: A, b: A): A | |
def -(a: A, b: A): A | |
def *(a: A, b: A): A | |
def negate(a: A): A | |
def fromInteger(i: Int): A | |
} |
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
//simple addition operation, nothing to explain in this v1 | |
def add(a: Int, b: Int): Int = a + b | |
//Lets make version v1 polymorphic | |
//below code will fail as the type A may or may not have an + operation | |
def add[A](a: A, b: A): A = a + b | |
//so we need a trait which can define that operation for us | |
trait Num[A] { | |
def +(a: A, b: A): A |
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
//Notes and code from this talk https://www.youtube.com/watch?v=IlvJnkWH6CA | |
object RecursiveSchemes extends App{ | |
object one { | |
sealed trait Exp | |
final case class IntValue(v:Int) extends Exp | |
final case class DecValue(v:Double) extends Exp | |
final case class Sum(exp1:Exp, exp2:Exp) extends Exp | |
final case class Multiply(exp1:Exp, exp2:Exp) extends Exp | |
final case class Divide(exp1:Exp, exp2:Exp) extends Exp |
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
import cats.Monad | |
import cats.implicits._ | |
/* | |
Interesting to note that if the parametic type is F[A] and your function is expecting F[F[A]] | |
Then the implicit function as used in the below function works like a charm | |
It tells scala compiler to use F[F[A]] the implicit is provided by scala itself you don't need to implement | |
*/ | |
implicit class SomeOps[F[_], A](v:F[A]){ |
OlderNewer