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 DummyFile: | |
case class File( `type`:File.Type, content:String) | |
object File: | |
enum Type: | |
case TEXT, PRESENTATION, AUDIO, VIDEO, UNKNOWN | |
end File | |
end DummyFile |
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 OddNumScala2 { | |
sealed trait Num | |
object Num { | |
case object Zero extends Num | |
case class Succ[N <: Num](num: N) extends Num | |
} | |
@implicitNotFound("Odd number type not satisfied.") | |
trait OddNum[N <: Num] |
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
/* | |
1. Algebraic data type (ADT) | |
2. Recursive types | |
3. Parametric Polymorphism in ADT | |
4. Function and Curring | |
5. All about making things lazy | |
6. Typeclasses to add features on our data structure | |
7. Extending with syntax and operators | |
8. Optimization and analysis of implementation |
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]){ |
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
//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
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
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
// 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
//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 |
NewerOlder