Skip to content

Instantly share code, notes, and snippets.

View Yaneeve's full-sized avatar

Yaneeve Yaneeve

  • Berlin
View GitHub Profile
@Yaneeve
Yaneeve / cps_no_curry.scala
Created December 14, 2018 17:38
Continuation Passing Style - example converted to scala from haskell; perhaps a more idiomatic scala than https://gist.github.com/Yaneeve/19a2615f3e4ab45c87920d7eaf616ef2
// 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
@Yaneeve
Yaneeve / cps_without_dollar_op.scala
Last active December 14, 2018 17:39
Continuation Passing Style - example converted to scala from haskell; this time without the $ op
//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
}}
@Yaneeve
Yaneeve / cps.scala
Created December 14, 2018 15:06
Continuation Passing Style - example converted to scala from haskell
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)
}
@Yaneeve
Yaneeve / quicksort.scala
Last active December 13, 2018 10:43
quicksort à la LYAHFGG
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