A ScalikeJdbc SQL SELECT “service” example
Here’s another ScalikeJdbc SQL SELECT query example. In this example I use the concept of a “service,” which I probably originally got from the ScalikeJdbc website:
Here’s another ScalikeJdbc SQL SELECT query example. In this example I use the concept of a “service,” which I probably originally got from the ScalikeJdbc website:
I’m not sure why this is called the Scala 2019 developer survey, since the results were released in February or March, 2020, but just follow that link to see more charts like the one shown in this image.
I just found this tweet from Grzegorz Kossakowski, and agree with it all the way. For Scala, the fusion of OOP+FP is a founding principle — and it’s well-explained in the book Programming in Scala — so lets see where it takes us.
If you’ve never heard of the term DICEE, it was coined by Guy Kawasaki. Mr. Kawasaki was a developer evangelist for the original Macintosh team in the 1980s, and used the term in at least one subsequent book to refer to great products.
“DICEE” is an acronym that stands for Deep, Indulgent, Complete, Elegant, and Emotive:
The funny thing about writing the Scala Cookbook is that it started as a whim. I was just about to leave for a vacation at the beach, and right before I turned off the computer a thought flashed in my mind, “I should contact the people at O’Reilly about writing a cookbook for Scala.” I then had a doubt that they would actually do it, but I applied the “What the heck” rule — i.e., “What the heck, what do I have to lose?” — and sent the email.
I dug around the internet for a few minutes, found the correct O’Reilly email address, sent them a message, turned off the computer, and drove to the beach. While I was at the beach the publisher wrote and said, “Love it, send me a full proposal!”
So if you’re thinking about doing something, but are afraid or uncertain about doing it ... apply the “What the heck” rule, and give it a shot. :)
As a little mini-project I wanted to count the number of lines of source code in the Second Edition of the Scala Cookbook as compared to the First Edition. To do this I wrote the following Scala program/script to count the lines between the ----
and ....
sections in the AsciiDoc files that represent the old and new versions of the book:
This image comes from this article. It shows a nice way to use map
instead of match/case when processing an Option
in Scala, and also shows a fold
approach.
For many more ways to work with an Option and higher-order functions, see my tutorial, How to use higher-order functions with Option (instead of using match expressions).
contributors.scala-lang.org remains my favorite website of late. This pre-SIP discussion looking for better ways to define a `main` method is a fun read that demonstrates a collaborative process.
Without much explanation today, here are different ways to create a sorted set from a Set
in Scala:
val set = Set(1, 6, 2, 12, 7, 3, 11) val ss = collection.immutable.SortedSet[Int]() ++ set val ss = collection.immutable.TreeSet[Int]() ++ set val ss = collection.mutable.SortedSet(set.toList: _*)
Here’s what this looks like in the REPL:
scala> val set = Set(1, 6, 2, 12, 7, 3, 11) set: scala.collection.immutable.Set[Int] = Set(1, 6, 2, 12, 7, 3, 11) scala> val ss = collection.immutable.SortedSet[Int]() ++ set ss: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12) scala> val ss = collection.immutable.TreeSet[Int]() ++ set ss: scala.collection.immutable.TreeSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12) scala> val ss = collection.mutable.SortedSet(set.toList: _*) ss: scala.collection.mutable.SortedSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)
I haven’t done any performance comparisons on these approaches, I just thought I’d share them here today.
Note that if you want to convert a Scala Set
to a sorted sequence, you can also use this approach:
val sortedSeq = set.toSeq.sorted
It’s super-early in the process, but the Second Edition of the Scala Cookbook is slowly coming to life. I’m currently updating all of the content for Scala 2.13, and when the book is finished it will be updated for Scala 3.
This morning (January 28, 2020) the folks at O’Reilly released the first two chapters of the new, updated book on the O’Reilly Learning Platform. If you have an O’Reilly account you can start reading the new chapters here. If not, you can view the catalog page here.
To set Java/JVM command line arguments when running a Scala application with the scala
command, set the JAVA_OPTS
environment variable before running your application, something like this:
$ JAVA_OPTS="-Xmx256m -Xms64m -Xss16M" $ scala my_scala_app.jar
I found this information on this scala man page document, where they also state this about the JAVA_OPTS
variable:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.15, “How to Flatten a List of Lists in Scala with flatten
”
You have a list of lists (a sequence of sequences) and want to create one list (sequence) from them.
Use the flatten
method to convert a list of lists into a single list. To demonstrate this, first create a list of lists:
Update: This article was written for Scala 2.9. Things changed a little bit in Scala 2.10, so see this new article, Creating implicit methods in Scala 2.10, for correct examples for 2.10 and newer versions of Scala.
A cool thing about implicit conversions in Scala is that they let you add new methods to existing classes, including existing Java and Scala classes such as String, File, and so on.
UPDATE: This example shows how to create an implicit method in Scala 2.9 or older. You can use a slightly simpler approach with Scala 2.10 and newer, which I've documented in this Scala 2.10 implicit class example.
I'm not going to do much writing here today, but instead I'll just demonstrate how an implicit method argument works with implicit fields in Scala. Without any further ado, here's some code:
Again not much time for a discussion today, but if you’re looking for an example of how to declare, set, and use Option
fields in Scala, I hope this source code is helpful:
Today I’m sharing some examples of the Scala Option
/Some
/None
syntax. These examples will show how to use an Option
for the var
fields in a Scala class. Then I’ll show how to set those Option
fields, and then get the values from the Option
fields.
To get started, we’ll need a little case class to represent an Address
:
Scala FAQ: How do I convert from a Scala BigDecimal instance to a Java BigDecimal?
Call the bigDecimal
method on your Scala BigDecimal
instance, as shown in this example in the REPL:
scala> val sb = scala.math.BigDecimal(12345) sb: scala.math.BigDecimal = 12345 scala> val jb = sb.bigDecimal jb: java.math.BigDecimal = 12345
As you can see, invoking sb.bigDecimal
returns a java.math.BigDecimal.
Scala/Java integration problem: You need to share a Scala map with a Java method, or access a Java map in Scala code.
To use a Java map in Scala code, import the mapAsScalaMap
method from Scala’s JavaConversions
package, and perform the conversion.
Simply trying to use a Java HashMap
with something like a Scala for
loop won’t work. To demonstrate this, first create a simple Java HashMap
:
I was working on some new code for my functional programming in Scala book today. At one point I thought everything looked okay, so I decided to generate some Scaladoc to see what certain things looked like. Admittedly I’m a bit tired today, but when I saw that Scaladoc I thought, “Good grief, Al, what sort of ugly API have you created?”
For some reason, seeing the Scaladoc helped me easily see the errors of my way. I’m not sure that I’ll ever be promoting a “Scaladoc-driven API design” process, but seeing the Scaladoc generated from my code sure helped today.
~ a note from August 30, 2017