Skip to content

Instantly share code, notes, and snippets.

View rohinp's full-sized avatar

Rohin rohinp

  • Amsterdam, The Netherlands
View GitHub Profile
@rohinp
rohinp / DummyFile.scala
Last active December 17, 2022 15:32
Gist for the blog FunctionDay0
object DummyFile:
case class File( `type`:File.Type, content:String)
object File:
enum Type:
case TEXT, PRESENTATION, AUDIO, VIDEO, UNKNOWN
end File
end DummyFile
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]
@rohinp
rohinp / DS.scala
Last active July 25, 2020 16:02
FP data structures
/*
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
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]){
@rohinp
rohinp / RecursionSchemes.scala
Last active January 31, 2020 07:32
Trying out recursive schemes with matryoshka scala library
//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
//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
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
}
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
}
// 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..."))
//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