Skip to content

Instantly share code, notes, and snippets.

@bishabosha
bishabosha / prettyPrint.scala
Last active April 16, 2019 23:49
Dotty tail recursive object pretty print - naive indentation
type StackT = List[String]
type StatT = StackT => StackT
type ProgT = List[StatT]
def prettyPrint(
a: Any,
indentSize: Int = 2,
maxElementWidth: Int = 60,
depth: Int = 0): String = {
@bishabosha
bishabosha / migrating-package-objects-with-extends.md
Created January 23, 2020 13:40
Migrating package objects with extends clauses

Here is a trait in package p

package p
trait Aliases { type Env = A with B }

In Scala 2, you could do this:

package object p extends Aliases
@bishabosha
bishabosha / OpaqueTypeEntities.worksheet.sc
Created March 10, 2021 14:40
Demonstrate using Opaque Type Aliases as evidence parameters to abstract over different storage
object Notarisation:
private case object Proven
opaque type Proof <: Singleton = Proven.type
given Proof = Proven
end Notarisation
import Notarisation.Proof
@bishabosha
bishabosha / summon-semigroup-Maybe.scala
Created November 15, 2021 15:20
Summon a Semigroup for Maybe, defined as an enum
// using scala 3.1.0
// using lib org.typelevel::cats-core:2.6.1
package good
import cats.Semigroup
import cats.syntax.all.*
enum Maybe[+A]:
case Just(a: A)
@bishabosha
bishabosha / summon-semigroup-maybe-oldway.scala
Created November 15, 2021 16:00
demonstration that the cats style of type class definition and syntax does not play well with type inference
// using scala 3.1.0
package mycats:
trait Semigroup[A]:
def combine(a1: A, a2: A): A
class SemigroupOps[A](a1: A)(implicit ev: Semigroup[A]):
def combine(a2: A): A = ev.combine(a1, a2)
@bishabosha
bishabosha / bishabosha-setup-scala-channel.json
Last active May 5, 2022 19:46
Coursier Channel for Setting up Scala
{
"coursier": {
"repositories": [
"central"
],
"dependencies": [
"io.get-coursier::coursier-cli:2.1.0-M2"
]
},
"scala-cli" : {
@bishabosha
bishabosha / aoc-2021-day22.scala
Last active July 12, 2022 10:49
Advent of Code 2021 Day 22 Neal Wu solution
// using scala 3.0.2
package day22
import scala.util.Using
import scala.io.Source
import scala.collection.mutable
@bishabosha
bishabosha / aoc-2021-day22-opt.scala
Created December 27, 2021 12:17
Scala Native Optimised Advent of Code 2022 Neal Wu Solution
// using scala 3.0.2
package day22Wu
import scala.util.Using
import scala.io.Source
import scala.collection.mutable
@bishabosha
bishabosha / tastyUtils.scala
Last active October 19, 2024 13:25
get version of any TASTy file
//> using scala "3.5.2"
//> using dep "com.lihaoyi::os-lib:0.11.2"
//> using dep "com.lihaoyi::sourcecode:0.4.2"
//> using dep "io.get-coursier:coursier_2.13:2.1.14"
//> using buildInfo
// setup with `scala --power setup-ide -with-compiler .`
import scala.sys.process.*
import java.nio.file.{Files, Paths, Path}
@bishabosha
bishabosha / tastyOfCompiler.scala
Last active February 3, 2022 08:14
get tasty for any compiler version, run with scala-cli 0.1.0
//> using lib "io.get-coursier:coursier_2.13:2.0.16"
import scala.sys.process.*
import java.nio.file.{Files, Paths, Path}
import java.io.File.pathSeparator as cpSep
import scala.annotation.threadUnsafe as tu
import coursier.*
/** Usage example: `scala-cli tastyOfCompiler.scala -- 3.1.1` */
@main def printTastyVersion(compiler: String): Unit =