syntax

How to create a Scala ArrayBuffer

As a quick note, this is how you create a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

var fruits = ArrayBuffer[String]()
var ints = ArrayBuffer[Int]()

The key there is that the keyword new is not required before the ArrayBuffer.

While I’m in the neighborhood, here are some other ways you can work with ArrayBuffer:

Scala ‘for’ loop examples and syntax

Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of for loop examples.

This page is currently a work in progress, and as of tonight I haven’t tested some of the examples, but ... if you’re looking for some Scala for loop examples — technically called a for comprehension — I hope these examples are helpful.

Markdown comments syntax: Comments that won’t appear in generated output

Markdown FAQ: How do I create comments in Markdown? Especially comments that won’t appear in the generated output.

Part 1 of my answer is that technically there is no way — or at least no standard way — to create comments in Markdown documents, other than to use HTML comments like this:

Explaining Scala’s `val` function syntax

This is an excerpt from my forthcoming book on Scala and Functional Programming. It’s an appendix that “explains and explores” Scala’s function syntax.

How to write a Scala method that takes a simple generic type

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 19.2, “How to write a Scala method that takes a simple generic type.”

Problem

You’re not concerned about type variance, and want to create a Scala method (or function) that takes a generic type, such as a method that accepts a Seq[A] parameter.

How to manage project dependencies with SBT (Simple Build Tool)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.4, “How to manage dependencies with SBT (Simple Build Tool).”

Problem

You want to use one or more external libraries in your Scala/SBT projects.

Solution

You can use both managed and unmanaged dependencies in your SBT projects.

How to create a method that returns a function in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.7, “How to create a method that returns a function in Scala.”

Problem

You want to return a function (algorithm) from a function or method.

Solution

Define a function that returns an algorithm (an anonymous function), assign that to a new function, and then call that new function.

The following code declares an anonymous function that takes a String argument and returns a String:

How to use closures in Scala (closure examples, syntax)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.5, “How to use closures in Scala (closure examples, syntax).”

Back to top

Problem

You want to pass a function around like a variable, and while doing so, you want that function to be able to refer to one or more fields that were in the same scope as the function when it was declared.

Back to top

Solution

You can demonstrate a closure in Scala with the following simple (but complete) example:

Table of Contents

  1. Problem
  2. Solution
Back to top