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 scala.math.Ordering | |
import scala.math.Ordering.Implicits._ | |
// https://scastie.scala-lang.org/Yaneeve/msPXA2ThRMqbi8fP6ODl2w | |
def quicksort[A: Ordering](xs: List[A]): List[A] = xs match { | |
case Nil => Nil | |
case h :: t => | |
val smallerThanHead = quicksort(t.filter(_ <= h)) | |
val greaterThanHead = quicksort(t.filter(_ > h)) | |
smallerThanHead ::: h :: greaterThanHead |
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 Dollar { // https://en.wikibooks.org/wiki/Haskell/Higher-order_functions#Function_manipulation | |
def $[A, B]: (A => B) => A => B = { f => { a => f(a) } } | |
} | |
Dollar.$[Int, Int]{_ * 2}(3) | |
implicit class DollarOps[A, B](f: A => B) { | |
private def app[O, P]: (O => P, O) => P = Function.uncurried(Dollar.$) | |
def $(b: A) = app(f, b) | |
} | |
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
//https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Passing_continuations | |
val add: Int => Int => Int = {x => { y => x + y}} | |
//add(1)(2) | |
//Function.uncurried(add)(1, 2) | |
def addCps[R]: Int => Int => ((Int => R) => R) = {x => { y => | |
def lambda[K]: (Int => K) => K = {k: (Int => K) => k(add(x)(y))} | |
lambda | |
}} |
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
// https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Passing_continuations | |
def add(x: Int, y:Int): Int = x + y | |
def addCps[R](x: Int, y: Int): ((Int => R) => R) = { | |
def lambda[K]: (Int => K) => K = {k: (Int => K) => k(add(x, y))} | |
lambda | |
} | |
def square(x: Int): Int = x * x |