How to get the first element from a Scala Set

I have no idea what I was thinking, but today I learned (or was reminded) that if you want the first element from a Scala Set you should use its head method, or headOption. For some reason I kept thinking that take should do the job, but you can see the results in the Scala REPL, where take(1) returns a Set:

How to define a collection whose element are all of some base type (inheritance)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.6, “How to define a collection whose element are all of some base type.”

Back to top


You want to specify that a class or method takes a type parameter, and that parameter is limited so it can only be a base type, or a subtype of that base type.

Back to top


Define the class or method by specifying the type parameter with an upper bound. To demonstrate this, create a simple type hierarchy:

Table of Contents

  1. Problem
  2. Solution
Back to top

Getting a random element from a list of elements in Scala

In working on projects like SARAH and other apps where I try to make a computer seem more “human,” I’ve been making my software reply to me in random ways. What I’ve found is that this ends up being an easily repeatable pattern, where you have a list of possible replies, and then you randomly return one of those replies.

Scala and XPath - get the first element of an array

Scala/XPath FAQ: How do I get the first element of an array in an XML document using Scala and XPath?

I ran into the problem of needing to get the first array element from an XML document using Scala and XPath recently, and in short, I ended up writing some Scala/XPath code that looked like this:

CakePHP naming conventions

CakePHP naming conventions FAQ: Can you share some examples of the CakePHP naming conventions, specifically the CakePHP model, view, and controller naming conventions?

As I get back into the CakePHP development world, I wanted to make a little CakePHP naming conventions reference page, showing examples of the standard CakePHP naming conventions for CakePHP model, view, and controller elements.

Perl hash - delete example

Perl hash delete FAQ: How do I delete an element (a key/value pair) from a Perl hash? Also, how do I delete multiple Perl hash elements at one time?

A Perl hash delete example - Delete an element by hash key

To delete a Perl hash element, use the Perl "delete" function. The general syntax of the Perl delete function looks like this:

Perl hash add element - How to add an element to a Perl hash

Perl hash "add" FAQ: How do I add a new element to a Perl hash? (Or, How do I push a new element onto a Perl hash?)

The Perl hash is a cool programming construct, and was very unique when I was learning programming languages in the late 1980s. A Perl hash is basically an array, but the keys of the array are strings instead of numbers.

Basic Perl hash "add element" syntax

To add a new element to a Perl hash, you use the following general syntax:

How to print every element in a Perl array

Summary: A Perl for loop (foreach loop) example, showing how to loop over each element in a Perl array.

Many times you'll need to print all the elements of a Perl array, or perhaps the elements of the array that match some filtering criteria. I thought I'd share several examples of how to print the elements of a Perl array here.

Initial setup

In each of the methods shown below, assume that we have an array named @pizzas that has been defined like this:

Perl for loop - How to do something for each element in an array

Perl for loop FAQ: What is the Perl for loop syntax? (Also written as, "How do I perform some type of operation on every element in a Perl array?)

Perl has a nice "for loop" syntax that lets you iterate through each element in an array, and perform some operation on each element.

Here's a simple Perl for loop example where I first create a small array, and then use the for loop to print each element in the array: