Skip to content

Instantly share code, notes, and snippets.

@bishabosha
bishabosha / FlagMirror.scala
Created June 1, 2022 09:28
A Bitfield implementation intended for use with type class derivation on Scala enums
package tastyquery.util
import compiletime.constValue
import compiletime.error
import compiletime.summonAll
sealed abstract class FlagMirror[A <: reflect.Enum]():
outer =>
opaque type FlagSet = Long
@bishabosha
bishabosha / BashScriptTests.out
Created May 19, 2022 18:03
output from on macOS dotty.tools.scripting.BashScriptTests
sbt:scala3> dist/pack
... truncated
sbt:scala3> scala3-bootstrapped/testOnly dotty.tools.scripting.BashScriptsTests
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for scala3-interfaces / Test / testOnly
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for scala3-sbt-bridge / Test / testOnly
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for scala3-library-bootstrapped / Test / testOnly
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
@bishabosha
bishabosha / derived-plugins.scala
Created May 9, 2022 20:41
Showing how the type class derivation mechanism can be used to add methods to the companion object with low boilerplate from the client side
trait Plugin:
type Companion
val exports: Companion
object Plugin:
transparent inline def exports[P <: Plugin](using plugin: P): plugin.exports.type = plugin.exports
trait Hashable[-T] extends Plugin:
@bishabosha
bishabosha / example.scala
Last active February 18, 2022 12:45
ISO Continents and Countries, with lookup
@main def addContinents(path: String): Unit =
val lines = scala.io.Source.fromFile(path).getLines
for case s"$country, $visitors" <- lines.drop(1) do
continentFromCountry(country) match
case Some(continent) =>
println(s"${continent.name}, $country, $visitors")
case None =>
Console.err.println(
"[error] no continent found for country: " + country
)
@bishabosha
bishabosha / ordinalOf.scala
Created January 28, 2022 09:51
Statically get the ordinal of any child of a sum-type in Scala
import scala.compiletime.ops.int.S
import scala.compiletime.constValue
import scala.deriving.Mirror
object Tuples {
type IndexOf[Q, T <: Tuple, I <: Int] <: Int = T match {
case EmptyTuple => -1
case Q *: ts => I
case _ *: ts => IndexOf[Q, ts, S[I]]
}
@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 =
@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 / 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 / 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 / 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" : {