Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / aoc-day-6-optimal.scala
Created December 6, 2022 16:30
AOC 2022 day 6 optimal
def part1(input: String): Int =
findIndexOptimal(input, n = 4)
def part2(input: String): Int =
findIndexOptimal(input, n = 14)
class MultiSet:
private val counts = new Array[Int](128)
private var total = 0
def size = total
@bishabosha
bishabosha / loops.scala
Last active December 14, 2024 22:55
Scala break/continue zero-cost loop abstraction
//> using scala "3.3.0"
import scala.util.boundary, boundary.break
object Loops:
type ExitToken = Unit { type Exit }
type ContinueToken = Unit { type Continue }
type Exit = boundary.Label[ExitToken]
@bishabosha
bishabosha / MH.scala
Created July 19, 2023 09:48
MethodHandles with static fields in scala 3
package foo
import scala.annotation.static
import java.lang.invoke.{MethodHandle, MethodHandles, MethodType}
class MH
object MH:
@static private val lookup = MethodHandles.lookup()
@bishabosha
bishabosha / LazyClasspathSuite.test.scala
Last active November 1, 2023 10:42
tasty-query classpath from dotty classpath
//> using toolkit 0.2.1
//> using scala 3.3.1
//> using dep ch.epfl.scala::tasty-query:0.11.0
//> using dep org.scala-lang::scala3-compiler:3.3.1
//> using dep io.get-coursier:coursier_2.13:2.1.7
package tastyquery.jdk.classpaths
import tastyquery.Classpaths.Classpath
import tastyquery.Classpaths.ClasspathEntry
@bishabosha
bishabosha / day17-aoc-2021.scala
Last active November 30, 2023 11:17
Advent of Code Day 17 2021 using named tuples
//> using scala "3.4.0-RC1-namedtuples-bin-SNAPSHOT"
// original solution taken from https://scalacenter.github.io/scala-advent-of-code/puzzles/day17#final-code (by @bishabosha)
import scala.language.experimental.namedTuples
type Target = (xs: Range, ys: Range)
type Point = (x: Int, y: Int)
type Probe = (position: Point, velocity: Point)
val initial = (x = 0, y = 0)