Skip to content

Instantly share code, notes, and snippets.

Fibers

Fibers are an abstraction over sequential computation, similar to threads but at a higher level. There are two ways to think about this model: by example, and abstractly from first principles. We'll start with the example.

(credit here is very much due to Fabio Labella, who's incredible Scala World talk describes these ideas far better than I can)

Callback Sequentialization

Consider the following three functions

@osamaqarem
osamaqarem / nginx_macos.md
Last active March 28, 2024 18:37
Nginx on MacOS

Install nginx (Homebrew)

brew install nginx

Configuration file for nginx will be at /usr/local/etc/nginx/nginx.conf

Web apps can be stored at /usr/local/var/www

Commands

Start:

@jfcherng
jfcherng / st4-changelog.md
Last active April 20, 2024 00:25
Sublime Text 4 changelog just because it's not on the official website yet.
sealed trait Path
case class Field(name: String, child: Option[Path]) extends Path {
override def toString: String = child match {
case None => name
case Some(path) => s"$name.$path"
}
}

Easy Scala Publication

The following describes how you can publish artifacts for any sbt project using the GitHub Package Registry and the sbt-github-packages plugin.

Step 1: Create a GitHub Token

In your GitHub account, go to Settings > Developer settings > Personal access tokens, then click on Generate new token (or click here). Fill in some sort of meaningful name (I chose Dev) and click on the write:packages checkbox:

the new personal access token page with the above steps having been followed

See context.

To configure this script to automatically execute on file changes, save the XML config in ~/Library/LaunchAgents/dot-files.sync.plist then do:

launchctl load -w ~/Library/LaunchAgents/dot-files.sync.plist
final case class ZIO[-R, +E, +A](run: R => Either[E, A]) {
final def map[B](f: A => B): ZIO[R, E, B] =
ZIO(r => run(r).map(f))
final def flatMap[R1 <: R, E1 >: E, B](f: A => ZIO[R1, E1, B]): ZIO[R1, E1, B] =
ZIO(r => run(r).flatMap(a => f(a).run(r)))
final def provide(r: R): ZIO[Any, E, A] =
ZIO(_ => run(r))
@johnynek
johnynek / almosttailrec.scala
Last active February 29, 2020 17:11
A generalization of tail recursion for stack safety in scala
/*
* Consider a simple recursive function like:
* f(x) = if (x > 1) f(x - 1) + x
* else 0
*
* This function isn't tail recursive (it could be, but let's set that aside for a moment).
* How can we mechanically, which is to say without thinking about it, convert this into a stack safe recursion?
* An approach is to model everything that happens after the recursion as a continuation, and build up that
* continuation in a stack safe manner. Here is some example code:
*/
@non
non / sizes.txt
Last active October 16, 2019 10:09
Size of various JVM data structures in bytes (top column is number of elements).
COLLECTION 0 1 5 10 50 100 500 1000
Array[Int] 16 24 40 56 216 416 2016 4016
immutable.BitSet 24 24 24 24 24 32 96 160
immutable.IntMap[Int] 16 40 328 688 3568 7168 35968 71968
immutable.List[Int] 16 56 216 416 2016 4016 20016 40016
immutable.Map[Int,Int] 16 40 304 720 4856 9680 53504 111760
immutable.Queue[Int] 40 80 240 440 2040 4040 20040 40040
immutable.Set[Int] 16 32 264 480 3016 5840 27696 57952
immutable.SortedMap[Int,Int] 40 88 280 520 2440 4840 30008 62008
immutable.SortedSet[Int] 40 104 296 536 2456
@kubukoz
kubukoz / Playground.scala
Created August 30, 2019 01:47
TCP server dumping input to stdout with fs2
import cats.effect._
import java.net.InetSocketAddress
import java.nio.channels.AsynchronousChannelGroup
import java.util.concurrent.Executors
import fs2.Stream
import cats.implicits._