Skip to content

Instantly share code, notes, and snippets.

@sshark
sshark / insertion-sort.scala
Last active March 6, 2019 15:28
Insertion sort
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)
}
@sshark
sshark / general-andthen.scala
Last active December 28, 2018 14:03
A general andThen rather than Function1.andThen
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"))
@sshark
sshark / intersect.sc
Created October 23, 2018 16:12
Fast intersection for sorted list only
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)
}
@sshark
sshark / EightQueens.sc
Last active October 7, 2018 14:06
Implementation of eight queens puzzle
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
@sshark
sshark / Geometry.scala
Created October 3, 2018 12:47
Golang interface and Scala typeclass equivalent
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] {
@sshark
sshark / low-priority-implicits.sc
Created September 9, 2018 09:11
Low priority implicits implementation
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 {
@sshark
sshark / fpmax.scala
Created August 22, 2018 17:14 — forked from jdegoes/fpmax.scala
FP to the Max — Code Examples
package fpmax
import scala.util.Try
import scala.io.StdIn.readLine
object App0 {
def main: Unit = {
println("What is your name?")
val name = readLine()
@sshark
sshark / ExprProb3.scala
Last active April 9, 2025 13:44
An example of using typeclass to solve expression problem. Forked from https://gist.github.com/calincru/cea751f050883581730093e93eaf2723
// 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
@sshark
sshark / typeclass-demo.sc
Last active August 19, 2018 16:04
An Introduction to Typeclass
// 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 {
// 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]