Skip to content

Instantly share code, notes, and snippets.

@romanowski
romanowski / a-scala-cli.md
Last active May 31, 2023 13:55
A demo of Scala CLI usage

Scala CLI live demo

  1. Install Scala CLI - most likely with brew install Virtuslab/scala-cli/scala-cli

  2. Create a project with library (greet.scala and app greeter.sc)

greet.scala:

def greet(name: String) = s"Hello, $name!"
val numbers = Seq(1,2,3,5,7,42)
@romanowski
romanowski / scala-cli-sip-channel.json
Last active August 30, 2022 17:09
A coursier channel for scala-cli-sip app
{
"scala-cli-sip": {
"repositories": [
"central"
],
"name": "scala-cli-sip",
"dependencies": [
"org.virtuslab.scala-cli:cli_3:latest.release"
],
"launcherType": "graalvm-native-image",
// using scala 3.0.2
// using lib org.apache.spark:spark-sql_2.13:3.2.0
// using repository https://wip-repos.s3.eu-central-1.amazonaws.com/.release
// using lib io.github.vincenzobaz::spark-scala3:0.1.3-new-spark
import scala3encoders.given
import org.apache.spark.sql.Dataset
import org.apache.spark.sql.SparkSession
@romanowski
romanowski / scala3spark.scala
Last active October 13, 2021 01:10
A demonstration of using Scala 3 within spark context. Run using Scala CLI (https://scala-cli.virtuslab.org/) using following command: `scala-cli https://gist.github.com/romanowski/de96bb08f0efa64be2591cc398631cc5`
// using scala 3
// using repository https://repository.apache.org/content/repositories/staging
// using org.apache.spark:spark-sql_2.13:3.2.0
// using repository https://wip-repos.s3.eu-central-1.amazonaws.com/.release
// using io.github.vincenzobaz::spark-scala3:0.1.3-new-spark
// using org.scala-lang::scala3-tasty-inspector:3.0.2
// using "io.get-coursier:coursier_2.13:2.0.16-169-g194ebc55c"
@romanowski
romanowski / ScalaVersion.scala
Last active October 7, 2021 22:34
Get currently used Scala and Java versions. Works with 2.12, 2.13 and 3.x. Based on https://gist.github.com/ymasory/923640/131ba9b367e4ea111e35ac1d57bf1279596a0439
object ScalaVersion extends App {
def props(url: java.net.URL): java.util.Properties = {
val properties = new java.util.Properties()
val is = url.openStream()
try {
properties.load(is)
properties
} finally is.close()
}
@romanowski
romanowski / tests.md
Created September 29, 2021 15:11
Proposal for defining tests in scala-cli

The mechanism to define tests in scala-cli should cover following usecases:

  1. User should be able to define test file without any configuration
  2. User should be able to define test directory within a global configuration file (e.g. config.scala)
  3. User should be able to mark current file as tests

We've considered many ways how we can achieve that with @using directives however this introduced following problems:

  • @using directictives content applies to the whole compilation unit but in case of tests we want to apply it to the current file. We can workaround this using following syntax @using tests.from thisFile or similar but it feels not natural and ovious.
  • we feel that defining a constrains about build when file should be applied is conecptually different then providing setting that are compliation unit-wide

Our proposal is to create another directive called @target that will define contraints about target that this files applies to. We think that target should be define before any @usin

@romanowski
romanowski / PlayerTraits.scala
Created April 14, 2018 15:02
For Scalasphere presentation
class PlayerTraits(str : Int, dex : Int, wis : Int) {
@CompilerControl(DONT_INLINE) def strCost: Int = str
@CompilerControl(DONT_INLINE) def dexCost: Int = dex
@CompilerControl(DONT_INLINE) def wisCost: Int = wis
}
@romanowski
romanowski / MappingOptions.scala
Created January 7, 2018 16:45
Piece of code to endcode type that is ether Rep[A] or Rep[Option[A]]
trait Mapper[A, B]{
def map(a: Rep[B]): Rep[Option[A]]
}
object Mapper {
implicit def plain[A: BaseTypedType] = new Mapper[A, A] { override def map(a: Rep[A]) = a.?}
implicit def option[A] = new Mapper[A, Option[A]] { override def map(a: Rep[Option[A]]) = a}
}
trait Optionable[A]{
type M[Original] = Mapper[A, Original]
}
package object lib {
// When we make RicherString implicit class Foo won't compile
class RicherString(base: String) {
val head: Char = if(base.isEmpty) ' ' else base.charAt(0)
}
}