Scala, Python quick reference
| syntax | Scala | Python |
|---|---|---|
| immutable variable | val x = 1 |
Starlark:REV = "1.1.0" |
| lazy variable | lazy val x = 1 |
n/a |
| mutable variable | var x = 1 |
in function:x = 1 |
| if expression | if (x > 1) "a" else "b" |
"a" if x > 1 else "b" |
| ———————- | —————————- | —————————- |
| function | def add3(x: Int): Int = x + 3 |
def add3(x): return x + 3 |
| anonymous function | _ * 2 |
not in Starlark:lambda x: x * 2 |
| ———————- | —————————- | —————————- |
| List | val xs = List(1, 2, 3, 4) |
xs = [1, 2, 3, 4] |
| size | xs.size |
len(xs) |
| empty test | xs.isEmpty |
not xs |
| head | xs.head |
xs[0] |
| tail | // List(2, 3, 4)xs.tail |
# [2, 3, 4]xs[1:] |
| take | // List(1, 2)xs.take(2) |
# [1, 2]xs[:2] |
| drop | // List(3, 4)xs.drop(2) |
# [3, 4]xs[2:] |
| drop right | // List(1, 2, 3)xs.dropRight(1) |
# [1, 2, 3]xs[:-1] |
| nth element | xs(2) |
xs[2] |
| map | xs.map(_ * 2)for { x <- xs} yield x * 2 |
map(lambda x: x * 2, xs)[x * 2 for x in xs] |
| filter | xs.filter(_ % 2 == 0)for { x <- xs if x % 2 == 0} yield x |
filter(lambda x: not x % 2, xs)[x for x in xs if not x % 2 ] |
| fold from left | // "a1234"xs.foldLeft("a") { _ + _ } |
from functools import reduce# "a1234"reduce(lambda a,x: a + str(x), xs, "a") |
| membership | xs.contains(3) |
3 in xs |
| ———————- | —————————- | —————————- |
| String | val s = "hello" |
s = "hello" |
| variable interpolation | val count = 3s"$count items" |
not in Starlark:count = 3f"{count} items" |
| split | // Array(1.2.3, M1)"1.2.3-M1".split("-") |
# ['1.2.3', 'M1']"1.2.3-M1".split("-") |
| substring test | s.contains("el") |
"el" in s |
| ———————- | —————————- | —————————- |
| Map | val d = Map("a" -> 1, “b” -> 2) |
d = { "a": 1, "b": 2 } |
- Skylark one-page overview
- Many of the examples were borrowed from Hyperpolyglot: Rust, Swift, Scala
notes
I’m picking up Python and its Bazel dialect Skylark lately. On the other hand, I’m familiar with Scala and its sbt dialect. Often I know exactly what I want to express, and I am fairly certain Python has the equivalent concept as Scala, but just don’t remember the exact incantation. For people starting Scala, maybe they could use this table in reverse.
POM consistency for sbt plugins
There’s a long-standing bug that sbt maintainers have known for a while, which is that when sbt plugin is published to a Maven repository, the POM file sbt generates is not valid. From a mailing list thread titled [0.12] plan for instance, Mark McBride reported it in 2012:
On the maven note, the poms generated for plugins aren’t actually valid. Trying to upload them to artifactory without disabling pom consistency checks fails :/
sbt 1.5.2
I’m happy to announce sbt 1.5.2 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.5.2
How to upgrade
Download the official sbt runner + launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.5.2
This mechanism allows that sbt 1.5.2 is used only for the builds that you want.
Highlights
- Fixes
sbt newleaving behindtargetdirectory #6488 by @eed3si9n - Fixes
ConcurrentModificationExceptionwhile compiling Scala 2.13.4 and Java sources zinc#974 by @lefou - Improved developer guide for new contributors #6469 by @Nirvikalpa108
- Fixes
-clientby making it the same as--client#6500 by @Nirvikalpa108 - Uses
-Duser.homeinstead of$HOMEto download launcher JAR #6483 by @rdesgroppes
For more details please see https://github.com/sbt/sbt/releases/tag/v1.5.2
Bintray to JFrog Artifactory migration status and sbt 1.5.1
I’m happy to announce sbt 1.5.1 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.5.1. This post will also report the Bintray to JFrog Artifactory migration.
Bintray to JFrog Artifactory migration status
First and foremost, I would like to thank JFrog for their continued support of sbt project and the Scala ecosystem.
As sbt was taking off in the number of contributors and plugins, we had a Bintray-shaped problem. We wanted individuals to create Ivy-layout repository, publish sbt plugins, but somehow aggregate the resolution to them. Having Github sbt organization allowed fluid ownership of plugin sources, but distributing the binary files were challenge as sbt version was churning. We adopted Bintray in 2014 and it provided the distribution mechanism during our growth years. In addition, we used Bintray to host Debian and RPM installers for sbt, paid for by Lightbend.
herding cats: day 19
Wrote herding cats: day 19 featuring FunctionK, or Rúnar’s encoding of rank-2 polymorphic function, and Resource datatype, which he envisioned rank-N polymorphism would unlock back in 2010.
sbt 1.5.0
Hi everyone. On behalf of the sbt project, I am happy to announce sbt 1.5.0. This is the fifth feature release of sbt 1.x, a binary compatible release focusing on new features. sbt 1.x is released under Semantic Versioning, and the plugins are expected to work throughout the 1.x series.
The headline features of sbt 1.5.0 are:
- Scala 3 support
- Eviction error
- Deprecation of sbt 0.13 syntax
- Coursier-based launcher
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/tag/v1.5.0. This installer includes the new Coursier-based launcher.
sbt 1.5.0-RC2
Hi everyone. On behalf of the sbt project, I am happy to announce sbt 1.5.0-RC2. This is the fifth feature release of sbt 1.x, a binary compatible release focusing on new features. sbt 1.x is released under Semantic Versioning, and the plugins are expected to work throughout the 1.x series.
- If no serious issues are found by Saturday, April 3rd 2021, 1.5.0-RC2 will become 1.5.0 final.
If no serious issues are found by Saturday, March 27th 2021, 1.5.0-RC1 will become 1.5.0 final.
The headline features of sbt 1.5.0 are:
sbt 1.4.9
I’m happy to announce sbt 1.4.9 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.9
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.9
This mechanism allows that sbt 1.4.9 is used only for the builds that you want.
Highlights
- sbt 1.4.9 fixes JLine 2 fork + JAnsi version to match that of JLine 3.19.0 to fix line reading, which among other things affected IntelliJ import.
- sbt 1.4.9 is a maintenance patch. The most notable thing is that this was that it was released without using Bintray, and a few things were dropped. See below for details.
Changes with compatibility implications
sbt 1.4.9 is published to Sonatype OSS without going through Bintray.
sbt 1.4.8
I’m happy to announce sbt 1.4.8 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.8
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.8
This mechanism allows that sbt 1.4.8 is used only for the builds that you want.
Highlights
- sbt 1.4.8 is a maintenance patch. The most notable thing is that this was the first release that was released without using Bintray, and a few things were dropped. See below for details.
Changes with compatibility implications
sbt 1.4.8 is published to Sonatype OSS without going through Bintray.
2021.03 mixtape
syntactic Scalafix rule for unified slash syntax
In sbt 1.1.0 I implemented unified slash syntax for sbt. Today I sent a pull request to deprecate the old sbt 0.13 shell syntax #6309.
Naturally, the topic of deprecating old syntax for build.sbt also came up.
will you also deprecate `scalacOptions in (Compile, console)` in *.sbt and *.scala files? I hope so
— Seth Tisue (@SethTisue) February 16, 2021
This is because “unified” slash syntax is called so because it unifies the shell syntax and the build syntax together. Thus, it makes sense to deprecate the old build.sbt syntax that uses in like skip in publish or scalacOptions in (Compile, console), if we’re deprecating the old shell syntax.
git bisecting scala/scala
git bisecting is a useful technique to locate the source of a bug. For scala/scala in particular,bisect.sh can save a lot of time by using the pre-build compiler artifacts on the Scala CI Artifactory.
sbt 1.4.7
I’m happy to announce sbt 1.4.7 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.7
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.7
This mechanism allows that sbt 1.4.7 is used only for the builds that you want.
Highlights
- Updates to Coursier 2.0.9, fixing authentication with Sonatype Nexus #6278 / [coursier#1948] by @cchepelov
- Fixes Ctrl-C printing out stack trace #6213 by @eatkins
- GNU Emacs support for
sbtnandsbt --client#6276 by @fommil
Participation
sbt 1.4.7 was brought to you by 5 contributors. Sam Halliday, Eugene Yokota (eed3si9n), Adrien Piquerez, Cyrille Chepelov, Ethan Atkins. Thank you!
sbt 1.4.6
I’m happy to announce sbt 1.4.6 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.6
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.6
This mechanism allows that sbt 1.4.6 is used only for the builds that you want.
Highlights
- Updates to Coursier 2.0.8, which fixes the cache directory setting on Windows (Fix contributed by Frank Thomas)
- Fixes performance regression in shell tab completion #6214 by @eed3si9n
- Fixes match error when using
withDottyCompatlm#352 by @eed3si9n - Fixes thread-safety in AnalysisCallback handler zinc#957 by @dotta
Participation
sbt 1.4.6 was brought to you by 3 contributors. Eugene Yokota (eed3si9n), Mirco Dotta, and Frank Thomas. Thank you!
enforcing Semantic Versioning with sbt-strict-update
I want to tell sbt “this specific version breaks binary compatibility, so don’t resolve it via eviction, fail the build instead.” How do I do this? Complete answers only, I’m done trying to figure it out by following clues.
I wrote a small sbt plugin sbt-strict-update to do this.
Add this to project/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-strict-update" % "0.1.0")
and then add this to build.sbt:
ThisBuild / libraryDependencySchemes += "org.typelevel" %% "cats-effect" % "early-semver"
That’s it.
ThisBuild / scalaVersion := "2.13.3"
ThisBuild / libraryDependencySchemes += "org.typelevel" %% "cats-effect" % "early-semver"
lazy val root = (project in file("."))
.settings(
name := "demo",
libraryDependencies ++= List(
"org.http4s" %% "http4s-blaze-server" % "0.21.11",
"org.typelevel" %% "cats-effect" % "3.0-8096649",
),
)
Now if Rob tries to compile this build, he should get:
sbt 1.4.5
I’m happy to announce sbt 1.4.5 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.5
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.5
This mechanism allows that sbt 1.4.5 is used only for the builds that you want.
Highlights
- sbt 1.4.5 adds support for Apple silicon (AArch64 also called ARM64) #6162/#6169 by @eatkins
- Updates to Coursier 2.0.7 #6120 by @jtjeferreira
- Fixes watch shell option #6166 by @eatkins
- Fixes
onLoadto run with the correctFileTreeRepositoryandCacheStoreFactory#6190 by @mkurz
Participation
sbt 1.4.5 was brought to you by 4 contributors. Ethan Atkins, Matthias Kurz, Eugene Yokota (eed3si9n), João Ferreira. Thank you!
unit of intensive care (2020.12 mixtape)
- Spotify: https://open.spotify.com/playlist/1mQ6JuDwWlYvuqqji53E4N?si=AopTuNoTTyCVBcOraXiZ-w
- YouTube: https://www.youtube.com/playlist?list=PLSUh6oJ5ZotW71OG0WR0XTJl3IlcJGNIZ
4h
auto publish sbt plugin from GitHub Actions
This is a GitHub Actions version of auto publish sbt plugin from Travis CI.
In this post, we’ll try to automate the release of an sbt plugin using Ólaf’s olafurpg/sbt-ci-release. The README of sbt-ci-release covers the use case for a library published to Sonatype OSS. Read it thoroughly since this post will skip over the details that do not change for publishing sbt plugins.
Automated release in general is a best practice, but there’s one benefit specifically for sbt plugin releases. Using this setup allows multiple people to share the authorization to release an sbt plugin without adding them to Bintray sbt organization. This is useful for plugins maintained at work.
scopt 4
This post was first published in December 2018 together with 4.0.0-RC2. It’s updated to reflect the changes made in November 2020 for 4.0.0.
You can skip to the readme, if you’re in a hurry.
To try new scopt 4.0.0:
libraryDependencies += "com.github.scopt" %% "scopt" % "4.0.0"
scopt 4.0.0 is cross published for the following build matrix:
| Scala | JVM | JS (1.x) | JS (0.6.x) | Native (0.4.0-M2) | Native (0.3.x) |
|---|---|---|---|---|---|
| 3.0.0-M2 | ✅ | ✅ | n/a | n/a | n/a |
| 3.0.0-M1 | ✅ | ✅ | n/a | n/a | n/a |
| 2.13.x | ✅ | ✅ | ✅ | n/a | n/a |
| 2.12.x | ✅ | ✅ | ✅ | n/a | n/a |
| 2.11.x | ✅ | ✅ | ✅ | ✅ | ✅ |
scopt is a little command line options parsing library. scopt started its life in 2008 as aaronharnly/scala-options based loosely on Ruby’s OptionParser. scopt 2 added immutable parsing, and scopt 3 cleaned up the number of methods by introducing Read typeclass.
Weehawken-Lang1
about Weehawken-Lang
It’s a strange time we live in. We can’t just meet up and catch up and talk about coding. This also opens an opportunity to think more virtually about the idea of meetups.
I want to start Weehawken-Lang, a virtual meetup group about programming languages and tooling design (compilers, interpreters, build tools etc). It aims to be a casual place where people with different language backgrounds can exchange ideas about programming languages.
sbt 1.4.4
I’m happy to announce sbt 1.4.4 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.4
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.4
This mechanism allows that sbt 1.4.4 is used only for the builds that you want.
Highlights
- Updates SemanticDB to 4.4.0 to support Scala 2.13.4 #6148 by @adpi2
- Fixes sbt plugin cross building fix #6091/#6151 by @xuwei-k and @eatkins
- Fixes scala-compiler not included into the metabuild classpath #6146 by @eatkins
- Fixes UTF-8 handling in shell and console #6106 by @eatkins
- Fixes macro occasionally dropping expressions trying to work around Scala compiler displaying “a pure expression does nothing” when sbt is really doing something #6158 by @eed3si9n
Global / localCacheDirectoryfor remote caching #6155 by @eed3si9n- Adds system property
sbt.build.onchangeforonChangedBuildSource#6099 by @xirc
For more details please see https://github.com/sbt/sbt/releases/tag/v1.4.4
sbt 1.4.3
I’m happy to announce sbt 1.4.3 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.3
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/. This installer includes the sbtn binary.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.3
This mechanism allows that sbt 1.4.3 is used only for the builds that you want.
Highlights
- Updates to Coursier 2.0.6 #6036 by @jtjeferreira
- Fixes IntelliJ import on Windows #6051 by @eatkins
- Fixes the dependency resolution in metabuild #6085 by @eed3si9n
- Removes “duplicate compilations” assertion to work around Play issue zinc#940 by @johnduffell
- Fixes GC monitor warnings #6082 by @nafg
For more details please see https://github.com/sbt/sbt/releases/tag/v1.4.3
sbt 1.4.2
I’m happy to announce sbt 1.4.2 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.2
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://github.com/sbt/sbt/releases/. This installer includes the sbtn binary.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.2
This mechanism allows that sbt 1.4.2 is used only for the builds that you want.
Highlights
- Remote caching is now content-based #6026 by @eed3si9n
installSbtnwizard for installing sbtn and completions #6023 by @eatkins- Fixes memory leak during task evaluation #6001 by @eatkins
- Various read line and character handling fixes by @eatkins
- Various BSP fixes by @adpi2
For more details please see https://github.com/sbt/sbt/releases/tag/v1.4.2
remote caching sbt builds with Bintray
The feature in sbt and Zinc 1.4.x that I spent most amount of time and energy probably is the virtualization of file, and lifting out timestamps. Combined together, we can liberate the Zinc state from machine-specificity and time, and become the foundation we lay towards building incremental remote caching for Scala. I blogged about this in cached compilation for sbt. This is part 2.
Now that sbt 1.4.x is out, there is a growing interest in this feature among people who want to try this out.
sbt 1.4.1
I’m happy to announce sbt 1.4.1 patch release is available. Full release note is here - https://github.com/sbt/sbt/releases/tag/v1.4.1
How to upgrade
Download the official sbt launcher from SDKMAN or download from https://www.scala-sbt.org/download.html. This installer includes the sbtn binary.
In addition, the sbt version used for your build is upgraded by putting the following in project/build.properties:
sbt.version=1.4.1
This mechanism allows that sbt 1.4.1 is used only for the builds that you want.
Highlights
- Various read line and character handling fixes by @eatkins, including
sbt newnot echoing back the characters - Fixes Scala 2.13-3.0 sandwich support for Scala.JS #5984 by @xuwei-k
- Fixes
shellPromptandrelease*keys warning on build linting #5983/#5991 by @xirc and @eed3si9n - Improves
plugincommand output by grouping by subproject #5932 by @aaabramov
For more details please see https://github.com/sbt/sbt/releases/tag/v1.4.1
virtualizing a hackathon at ScalaMatsuri 2020
Here’s a report of running a virtual hackathon at ScalaMatsuri Day 2 Unconference. Someone proposed it for the Unconference, and I volunteered to be a facilitator on the day, so I went in without preparation. I booked the time originally for 4h (noon - 4pm JST, 11pm - 3am EDT) but it was successful so it got extended after some coffee break.
One thing I emphasize is The Law of Two Feet:
Equality in Scala
I gave a talk at ScalaMatsuri on ‘Equality in Scala’
sbt 1.4.0
Hi everyone. On behalf of the sbt project, I am happy to announce sbt 1.4.0. This is the fourth feature release of sbt 1.x, a binary compatible release focusing on new features. sbt 1.x is released under Semantic Versioning, and the plugins are expected to work throughout the 1.x series.
The headline features of sbt 1.4.0 are:
- build server protocol (BSP) support
- sbtn: a native thin client for sbt
- build caching
ThisBuild / versionSchemeto take the guessing out of eviction warning
sbt 1.4.0-RC2
Hi everyone. On behalf of the sbt project, I am happy to announce sbt 1.4.0-RC2. This is the fourth feature release of sbt 1.x, a binary compatible release focusing on new features. sbt 1.x is released under Semantic Versioning, and the plugins are expected to work throughout the 1.x series.
- If no serious issues are found by Saturday, October 3rd 2020, 1.4.0-RC2 will become 1.4.0 final.
If no serious issues are found by Saturday, September 19th 2020, 1.4.0-RC1 will become 1.4.0 final.
The headline features of sbt 1.4.0 are:
- build server protocol (BSP) support
- sbtn: a native thin client for sbt
- build caching
ThisBuild / versionSchemeto take the guessing out of eviction warning