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
| import math.Ordering | |
| def insertionSort[A: Ordering](ls: List[A]): List[A] = { | |
| import math.Ordering.Implicits._ | |
| def _insert(ys: List[A]): List[A] = ys match { | |
| case z :: Nil => List(z) | |
| case z0 :: z1 :: zs => if (z0 < z1) ys else z1 :: _insert(z0 :: zs) | |
| } |
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
| import cats.implicits._ | |
| import cats.arrow.Compose | |
| import cats.data.Kleisli | |
| val plainOldFuncTriple: Int => Int = _ * 3 | |
| def fooCompose[~>[_, _] : Compose, A, B, C](f: A ~> B, g: B ~> C): A ~> C = | |
| f andThen g | |
| println(fooCompose((x: String) => x.length, plainOldFuncTriple).apply("123")) |
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
| import Ordering._ | |
| def intersect[A](xs: List[A], ys: List[A])(implicit ev: Ordering[A]): List[A] = { | |
| def _intersect(xs: List[A], ys: List[A], acc: List[A]): List[A] = | |
| if (xs.isEmpty || ys.isEmpty) acc else | |
| ev.compare(xs.head, ys.head) match { | |
| case 0 => _intersect(xs.tail, ys.tail, xs.head :: acc) | |
| case a if a > 0 => _intersect(xs, ys.tail, acc) | |
| case a if a < 0 => _intersect(xs.tail, ys, acc) | |
| } |
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
| import scala.annotation.tailrec | |
| val answer: Set[List[Int]] = """1 5 8 6 3 7 2 4 | |
| |1 6 8 3 7 4 2 5 | |
| |1 7 4 6 8 2 5 3 | |
| |1 7 5 8 2 4 6 3 | |
| |2 4 6 8 3 1 7 5 | |
| |2 5 7 1 3 8 6 4 | |
| |2 5 7 4 1 8 6 3 | |
| |2 6 1 7 4 8 3 5 |
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
| trait Geometry[A] { | |
| def area(a: A): Double | |
| def perim(a: A): Double | |
| } | |
| case class Rect(width: Double, height: Double) | |
| case class Circle(radius: Double) | |
| object Geometry { | |
| implicit object RectGeometry extends Geometry[Rect] { |
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 ApplyEither { | |
| def apply[T, F, G](t: T, f: F, g: G)(implicit ev: EApply[T, F, G]): ev.Out = ev.apply(t, f, g) | |
| } | |
| trait EApply[T, F, G] { | |
| type Out | |
| def apply(t: T, f: F, g: G): Out | |
| } | |
| object EApply extends LowPriorityEApply { |
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
| package fpmax | |
| import scala.util.Try | |
| import scala.io.StdIn.readLine | |
| object App0 { | |
| def main: Unit = { | |
| println("What is your name?") | |
| val name = readLine() |
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
| // The expression problem is a new name for an old problem. The goal is to | |
| // define a datatype by cases, where one can add new cases to the datatype and | |
| // new functions over the datatype, without recompiling existing code, and while | |
| // retaining static type safety (e.g., no casts). | |
| // (Philip Wadler) | |
| // For Scala 3 | |
| import scala.language.implicitConversions |
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
| // Typical OO / Java style | |
| case class Employee(name: String, salary: Int) extends Comparable[Employee] { | |
| override def compareTo(a: Employee): Int = salary - a.salary | |
| } | |
| val emps = List(Employee("Alice", 1000), Employee("Bob", 5000), Employee("Cooper", 3000)) | |
| def maxSalary(xs: List[Employee]): Option[Employee] = | |
| if (xs.isEmpty) None | |
| else Some(xs.reduce((a, b) => a.compareTo(b) match { |
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
| // https://www.47deg.com/blog/basic-recursion-schemes-in-scala/ | |
| import scala.language.higherKinds | |
| sealed trait IntList[+A] | |
| case object Empty extends IntList[Nothing] | |
| case class Cons[A](head: Int, tail: A) extends IntList[A] | |
| trait Functor[F[_]] { | |
| def map[A, B](fun: A => B, from: F[A]): F[B] |