Skip to content

Instantly share code, notes, and snippets.

@rohinp
Last active July 25, 2020 16:02
Show Gist options
  • Save rohinp/6ceda2d6f3accb28e3470cd4b50bcfd2 to your computer and use it in GitHub Desktop.
Save rohinp/6ceda2d6f3accb28e3470cd4b50bcfd2 to your computer and use it in GitHub Desktop.
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
* */
object LiveCoding1 extends App {
println("_" * 50)
//ADT
// sum type, product type
/*
* trait, class, case class, object, case object, tuple
* */
//Sum type
sealed trait MyBool // type constructor
//data constructor 2
case object True extends MyBool
case object False extends MyBool
def someFunc(int:Int):MyBool =
if(int <= 100) True else False
def another(myBool: MyBool):Int =
myBool match {
case True => 12
case False => -1
}
sealed trait Human // 3
case object Male extends Human
case object Female extends Human
case object Trans extends Human
// 2 * 3 product types
case class FamousHumans(human: Human, isFamous:MyBool)
def isFamous(famousHumans: FamousHumans):MyBool = famousHumans match {
case FamousHumans(_ , True) => True
case _ => False
}
/*
* val t = List(1,2,3,4)
* t.head = 1
* t.tail = List(2,3,4)
*
* trait Fruit
* object Apple extends Fruit
* object orange extends Fruit
*
*
* */
trait Fruit
case class Apple(colour:String) extends Fruit
case class Orange(colour:String) extends Fruit
val apple:Fruit = Apple("Green")
val orange:Fruit = Apple("Mandarin")
case class Box[+F](fruit: F)
val appleBox:Box[Fruit] = Box[Apple](Apple("Green"))
sealed trait MyList[+A] //type constructor
//data constructor
case object Empty extends MyList[Nothing]
case class NotEmpty[A](head:A, tail: MyList[A]) extends MyList[A]
val intList:MyList[Int] = NotEmpty(1, NotEmpty(2, NotEmpty(3, Empty)))
println("_" * 50)
println("_" * 50)
//ADT for list
sealed trait MyList[+A] //type constructor
//data constructor
case object Empty extends MyList[Nothing]
case class NotEmpty[A](head:A, tail: MyList[A]) extends MyList[A]
def head[A](list: MyList[A]):Option[A] = list match {
case Empty => None
case NotEmpty(head, _) => Some(head)
}
def tail[A](list: MyList[A]):Option[MyList[A]] = list match {
case Empty => None
case NotEmpty(_, tail) => Some(tail)
}
val list = NotEmpty(1 ,NotEmpty(2,NotEmpty(3,Empty)))
println(tail(Empty))
println(tail(NotEmpty(1,Empty)))
println(tail(list))
println("_" * 50)
//ADT for list
sealed trait MyList[+A] //type constructor
//data constructor
case object Empty extends MyList[Nothing]
case class NotEmpty[A](head: () => A, tail: () => MyList[A]) extends MyList[A]
def head[A](list: MyList[A]):Option[A] = list match {
case Empty => None
case NotEmpty(head, _) => Some(head())
}
def tail[A](list: MyList[A]):Option[MyList[A]] = list match {
case Empty => None
case NotEmpty(_, tail) => Some(tail())
}
def length[A](list: MyList[A]):Int = {
@scala.annotation.tailrec
def loop(remainingList: => MyList[A], l:Int ):Int = remainingList match {
case Empty => l
case NotEmpty(_, tail) => loop(tail(), l + 1)
}
loop(list,0)
}
val list = NotEmpty(() => 1 ,() => NotEmpty(() => 2, () => NotEmpty(() => 3, () =>Empty)))
//List(???, ???, ???)
println(length(NotEmpty(() => ???, () => NotEmpty(() => ???,() => ???))))
println("_" * 50)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment