Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created April 25, 2024 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/985bbba473d238043f458c00228336bf to your computer and use it in GitHub Desktop.
Save fancellu/985bbba473d238043f458c00228336bf to your computer and use it in GitHub Desktop.
Scala3 boundary/break/label demo (uses Scala 3.4.1)
import scala.annotation.targetName
import scala.util.boundary
import scala.util.boundary.{Label, break}
// Works in Scala 3.4.1
def firstIndex[T](xs: List[T], p: T): Int = {
boundary:
for (x, i) <- xs.zipWithIndex do if (x == p) break(i)
-1
}
object optional:
inline def apply[T](inline body: Label[None.type] ?=> T): Option[T] = boundary(Some(body))
extension [T](r: Option[T])
@targetName("?")
inline def ?(using label: Label[None.type]): T = r.getOrElse(break(None))
def firstColumn[T](xss: List[List[T]]): Option[List[T]] =
optional:
xss.map(_.headOption.?)
@main
def main(): Unit = {
val li = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
println(firstIndex(li, 5))
val listOfList = List(li, List(99, 100))
println(firstColumn(listOfList))
val listOfList2 = List(li, Nil)
println(firstColumn(listOfList2))
}
@fancellu
Copy link
Author

Outputs

4
Some(List(1, 99))
None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment