arguments

A Scala shell script to read HTML H1 tag attributes

I’m putting this Scala shell script out here as a “source code snippet” so I can find it again if I need it. This file reads an input file that contains a series of HTML <h1> tags. I use this as part of a process of publishing an Amazon Kindle ebook from an HTML file, and in one of the steps of the creation process, I use this script to help create the Table of Contents (TOC) for the book.

Here’s the source code:

Applying arguments to functions (currying)

I wish I had known more about Haskell and its influence on Scala before I wrote the Scala Cookbook. I don’t think that knowledge would have helped immensely, but it may have made the Cookbook a little better, maybe two to three percent better, something like that.

One area where more knowledge of Haskell would have helped is in regards to applying arguments to functions, i.e, currying. This page from the book Real World Haskell shows a nice example of how applying arguments to functions in Haskell works.

How to use Scala varargs fields from Java (varargs annotation)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 17.5, “How to annotate varargs methods in Scala.”

Problem

You’ve created a Scala method with a varargs field, and would like to be able to call that method from Java code.

Solution

When a Scala method has a field that takes a variable number of arguments, mark it with the @varargs annotation.

How to access command-line arguments in Scala shell scripts

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.11, “How to access command-line arguments in a Scala shell script.”

Problem

You want to access the command-line arguments from your Scala shell script.

Solution

Use the same script syntax as shown in Recipe 14.8, “Generating Documentation with scaladoc”, and then access the command-line arguments using args, which is a List[String]` that is implicitly made available:

How to declare constructor parameters when extending a Scala class

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.10, “How to handle constructor parameters when extending a Scala class.”

Problem

You want to extend a base Scala class, and need to work with the constructor parameters declared in the base class, as well as new parameters in the subclass.

How to provide default values for Scala constructor parameters

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.5, “How to provide default values for Scala constructor parameters.”

Problem

You want to provide a default value for a Scala constructor parameter, which gives other classes the option of specifying that parameter when calling the constructor, or not.

How to control the visibility of Scala constructor fields

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.2, “How to control the visibility of Scala class constructor fields.”

Problem

You want to control the visibility of fields that are used as constructor parameters in a Scala class.