Skip to content

Instantly share code, notes, and snippets.

View diesalbla's full-sized avatar

Diego Alonso diesalbla

  • Standard Chartered
  • London, United Kingdom
  • LinkedIn in/diesalbla
View GitHub Profile
@diesalbla
diesalbla / BlockingLauncher.scala
Created June 14, 2023 12:25
Scala SIP Coroutines - A simple example of blocking launcher.
class Blocking extends Unwrapped[A], StarterLib:
val ec: ExecutionContextExecutorService =
ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())
sys.addShutdownHook(this.close)
def close: Unit = ec.close()
def apply(block: Continuation[A] ?=> A): A =
val boundary = new Boundary[A] { var result = null }
val latch = new CountDownLatch(1)
val baseContinuation = BuildContinuation(
ec,
@diesalbla
diesalbla / CoroutinesImpl.scala
Created June 14, 2023 12:05
Scala Coroutines - Basic definitions
abstract class Starter[A]:
def invoke(completion: Continuation[A]): A | Any | Null
trait ContinuationLib:
extension [A](continuation: Continuation[A])
def intercepted(ec: ExecutionContext): Continuation[A] =
continuation match
case x: ContinuationImpl =>
x.intercepted(ec).asInstanceOf[Continuation[A]]
case _ => continuation
@diesalbla
diesalbla / GcdRunNextLeg.scala
Created June 14, 2023 10:26
Scala SIP - Coroutine Proposal - Generated runNextLeg
def runNextLeg(a: Int, b: Int)(completion: Continuation[Int]): Int | Suspended =
var a$1:Int = a
var b$2:Int = b
var z$3:Boolean = null
var m$4:Int = null
var g$5:Int = null
val frame = frame match {
case x$0 @ _:GcdFrame if x$0.state & Int.MinValue != 0 =>
x$0.state = x$0.state - Int.MinValue
x$0
@diesalbla
diesalbla / recscheme.scala
Last active November 11, 2019 16:06
A lightweight Introduction to Recursion Schemes in Scala
/**
* Code Snippets from the Blog Post "Basic Recursion Schemes in Scala",
* https://www.47deg.com/blog/basic-recursion-schemes-in-scala/
*
* Written here to accompany the article. This file should compile directly if inserted into ammonte.
*/
object IListBase {
sealed trait IList
case object INil extends IList
case class ICons(head: Int, tail: IList) extends IList
@diesalbla
diesalbla / merge_patches.sh
Created September 18, 2019 17:49
Scala-Steward-Merger: Merge Pull-Requests from scala-steward that carry patch library upgrades
#!/usr/bin/bash
export OWNER=$1
export REPOSITORY=$2
export LOGIN="<GITHUB_LOGIN>"
export TOKEN="<GITHUB_TOKEN>"
processPullRequest() {
NUMPR=$1
COMMIT=$2
@diesalbla
diesalbla / LazyFold.scala
Last active August 1, 2020 21:56
Lazy Fold Right
type Lazy[A] = () => A
def foldRight[A, B](xs: List[A], z: B)(f: (A, Lazy[B]) => B): B =
xs match {
case Nil => z
case h :: t => f(h, () => foldRight(t, z)(f))
}
def find[A](xs: List[A])(p: A => Boolean): Option[A] = {
val init = Option.empty[A]
@diesalbla
diesalbla / OrderedFairChunks.hs
Created September 13, 2018 17:35
Haskell: chunk a list of key-value pairs on N-sized chunks, in fair order.
module OrderedFairChunks where
import Data.List
import Data.Maybe
import Data.Function(on)
-- Useful type aliases
type Cons a = (a, [a])
type Slice k v = (k, [v])