How to filter None’s out of List[Option]?
If you want to get rid of the options at the same time, you can use flatten: scala> someList.flatten res0: List[String] = List(Hello, Goodbye)
If you want to get rid of the options at the same time, you can use flatten: scala> someList.flatten res0: List[String] = List(Hello, Goodbye)
What you’re looking for is a stable identifier. In Scala, these must either start with an uppercase letter, or be surrounded by backticks. Both of these would be solutions to your problem: def mMatch(s: String) = { val target: String = “a” s match { case `target` => println(“It was” + target) case _ => … Read more
From sbt version 0.13.5 you can add to your build.sbt cancelable in Global := true It is defined as “Enables (true) or disables (false) the ability to interrupt task execution with CTRL+C.” in the Keys definition If you are using Scala 2.12.7+ you can also cancel the compilation with CTRL+C. Reference https://github.com/scala/scala/pull/6479 There are some … Read more
Funny that no one added lenses, since they were MADE for this kind of stuff. So, here is a CS background paper on it, here is a blog which touch briefly on lenses use in Scala, here is a lenses implementation for Scalaz and here is some code using it, which looks surprisingly like your … Read more
val a: A = _ is a compile error. For example: scala> val a: String = _ <console>:1: error: unbound placeholder parameter val a: String = _ ^ What does work is var a: A = _ (note var instead of val). As Chuck says in his answer, this initialises the variable to a default … Read more
Resources in Scala work exactly as they do in Java. It is best to follow the Java best practices and put all resources in src/main/resources and src/test/resources. Example folder structure: testing_styles/ ├── build.sbt ├── src │ └── main │ ├── resources │ │ └── readme.txt Scala 2.12.x && 2.13.x reading a resource To read resources … Read more
scala> 10 to 1 by -1 res1: scala.collection.immutable.Range = Range(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
More or less any use of member (ie. nested) types can give rise to a need for dependent method types. In particular, I maintain that without dependent method types the classic cake pattern is closer to being an anti-pattern. So what’s the problem? Nested types in Scala are dependent on their enclosing instance. Consequently, in … Read more
You can force SBT to reload changes: Open SBT toolwindow (on the right side of IDE) and press refresh button. If you use auto-import feature you need to save your file to force auto-refresh.
They’re both object oriented languages for the JVM that have lambdas and closures and interoperate with Java. Other than that, they’re extremely different. Groovy is a “dynamic” language in not only the sense that it is dynamically typed but that it supports dynamic meta-programming. Scala is a “static” language in that it is statically typed … Read more