Using underscores in a Scala for loop (for comprehension)
I was looking through this code on Github, which was shared at the 2014 Lambda Conference in Boulder, Colorado this summer, and ran across this for
comprehension:
I was looking through this code on Github, which was shared at the 2014 Lambda Conference in Boulder, Colorado this summer, and ran across this for
comprehension:
I just ran into one thing I wish I had included in the Scala Cookbook that I didn’t include: How to access a val
or var
field in a Scala object from your Java code.
In short, if you have a field named appName
defined in a Scala object, like this:
Summary: In this article I share an approach I used to make a condensed version of a PDF book by printing only the first two sentences of each paragraph in the book. In the long term I hope this will help me create a “CliffsNotes” version of the book.
If you ever wanted to see how easy it is to create a form using the JGoodies FormLayout
, this code shows a simple example:
If you ever need to use a JGoodies FormLayout and PanelBuilder, and want to fill and grow components both vertically and horizontally, I hope the following code is helpful:
Problem: You want to add logging to an application in a more Scala-specific way than simply using SLF4J.
Grizzled-SLF4J is a thin wrapper around SLF4J that gives you logging in a more Scala-like way.
To get started with Grizzled-SLF4J, create a simple SBT project, then edit your build.sbt file so it has the dependencies you’ll need:
Thinking in Scala: The map
method on the Scala collections classes is used to transform an input collection to an output collection using a transformation algorithm you supply. This is similar to the way an electricity transformer converts voltage from one purpose to another. The following examples come from the Scala Cookbook:
Problem: You want to write XML data to a file in a Scala application, such as saving application data or configuration information to a file.
Use the scala.xml.XML.save
method to write a Scala literal to a file. Given this XML literal:
Problem: You want to create XML variables, and embed XML into your Scala code.
You can assign XML expressions directly to variables, as shown in these examples:
val hello = <p>Hello, world</p> val p = <person><name>Edward</name><age>42</age></person>
In the REPL you can see that these variables are of type scala.xml.Elem
:
Problem: You want to dynamically generate XML from your Scala source code, such as creating output for a SOAP web service.
A great feature of Scala’s XML support is that you can interweave XML and regular Scala source code together. This lets you dynamically generate XML from your Scala code.
To create XML with dynamic, embedded data, just put your Scala code in curly braces inside the XML tags, as shown in the following example:
Problem: When writing a Scala application, you want to search an XML tree for the data you need using XPath expressions.
Use the \
and \\
methods, which are analogous to the XPath /
and //
expressions. The \
method returns all matching elements directly under the current node, and \\
returns all matching elements from all nodes under the current node (all descendant nodes).
To demonstrate this difference, create this XML literal:
I’m currently working on a Play Framework server-side application that handles money/currency. (The application UI uses Sencha ExtJS, but that doesn’t matter for this example.)
From past experience I know that this means I need to use a decimal
field in my MySQL database table. (MySQL also lets you declare this field type as numeric
.) I further know that the MySQL JDBC driver uses a java.math.BigDecimal field to insert and select from this field type.
There are times when you’re debugging a Play Framework controller that you’ll want to print certain information, such as the request content-type, headers, content body, and query string. As a quick example, the code below shows how to print this information from a Play Framework controller method:
The Play Framework session cookie name is PLAY_SESSION
. You can find this in the Play docs here.
Here’s a quote from that page:
Here’s a quick look at how to run Play Framework “model” methods from the Play console. In my case, my model methods are Anorm database access methods, but you may be accessing MongoDB, Cassandra, CouchDB, whatever.
First, move to your Play application directory and start the Play interpreter:
$ play
Then start the Play console:
[MyApp] $ console
Your prompt will now look like this:
Just a quick note that Scala 2.10.0 was released recently, and the following page, Scala 2.10.0 now available, contains a nice summary of what's new in the official Scala 2.10 release. The short list of new features includes:
Summary: A discussion of Scala type annotations and type ascription.
While Scala normally determines the type you want to use automatically, such as this Int:
scala> val x = 1 x: Int = 1
there may be times you want to control the type, such as if you want a Byte, Short, Long, Double, Float, etc. In these cases you can annotate your type when you create it, like this:
UPDATE: I originally posted this article in January, 2013, and it's now mid-February, 2013, and we're no longer looking for reviewers. I've only kept this page here so people won't get 404 errors.
OLD CONTENT:
Interested in being a reviewer for the Scala Cookbook?
Nothing too earth shattering here today, but if you need an example of the Scala Map class syntax (how to create a Scala Map), or just want to copy and paste a map of month names to numbers (or numbers to names), I hope the following code is helpful:
I've been keeping notes about the process of writing the Scala Cookbook for O'Reilly, and I thought I'd share a few of those thoughts here today (early February, 2013).