My Adventures in Coding

December 30, 2011

Scala – How to combine two lists of different types so they can be sorted by a common field

Filed under: Scala — Brian @ 1:48 pm
Tags: ,

Recently I was pairing with a co-worker on a new view for our application. The purpose of this view was just to show a timeline of all activities (regardless of type) occurring in the system. We were confident there would be a way to accomplish this task easily in Scala!

For this example lets say we are writing a program for a car dealership. The manager of the dealership would like a page showing test drives and sales throughout the day. The point of this page is not to connect test drives to sales, but instead a view used to give a timeline of the activity taking place on the car lot.

In the example below, even though we have two lists of different types testDrives=List[TestDrives] and purchases=List[Purchases], they both have a common field which is “date“. To do the sort we first combine the two lists with (testDrives ::: purchases), creating a new list of type List[{def date: Date}]. The objects in the new list all have “date“. So now sorting the list is easy!

import scala.collection.JavaConversions._
import java.util.Date

case class TestDrive(car: String, priceQuoted: Int, date: Date)
case class Purchase(car: String, priceSold: Int, date: Date)

val date = new Date
val dateFormat = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss")

val testDrives = List(TestDrive("Charger", 22000, new Date(date.getTime - 1000000)), TestDrive("Camero", 250000, new Date(date.getTime - 4000000)), TestDrive("Pony", 2500, new Date(date.getTime - 10000000)))

val purchases = List(Purchase("Infiniti G35x", 19500, new Date(date.getTime - 500000)), Purchase("Infiniti G37x", 28500, new Date(date.getTime - 6000000)), Purchase("Altima", 35000, new Date(date.getTime - 9000000)))

// Combine the TestDrives and Purchases lists and sort descending by date
for (something <- (testDrives ::: purchases).asInstanceOf[List[{def date: Date}]].sortBy( {
	// Now if we want to show fields other than "date", we can easily check the type of each object, and cast it appropriately
	if (something.isInstanceOf[TestDrive]) {
		val testDrive = something.asInstanceOf[TestDrive]
		println("Test Drive: %s\tQuoted price: %d \t Date: %s".format(, testDrive.priceQuoted, dateFormat.format(
	else {
		val purchase = something.asInstanceOf[Purchase]
		println("Purchase: %s\tSold price: %d \t Date: %s".format(, purchase.priceSold, dateFormat.format(

The program creates the following output, all test drives and purchases combined into a single timeline sorted descending by date.

>scala example.scala
Purchase: Infiniti G35x Sold price: 19500        Date: 2011/12/23 14:48:41
Test Drive: Charger     Quoted price: 22000      Date: 2011/12/23 14:40:21
Test Drive: Camero      Quoted price: 250000     Date: 2011/12/23 13:50:21
Purchase: Infiniti G37x Sold price: 28500        Date: 2011/12/23 13:17:01
Purchase: Altima        Sold price: 35000        Date: 2011/12/23 12:27:01
Test Drive: Pony        Quoted price: 2500       Date: 2011/12/23 12:10:21

That is all!

December 22, 2011

Linux – How to ssh between two linux computers without needing a password

Filed under: Linux — Brian @ 10:44 pm
Tags: ,

Having to constantly type in your password on a linux server that you ssh to often can get to be an annoyance. Luckily this is an easy problem to solve. Since I always end up forgetting how to do this setup, I thought I would finally write this down, even if just for my own reference :).

1. ssh to server1

Connect to server1 and generate a public/private key pair.

ssh myusername@server1
ssh-keygen -t rsa

When you run this command you will be prompted to answer several questions. Just hit enter each time until you are returned to a prompt.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/local/myusername/.ssh/id_rsa): 
Created directory '/home/local/myusername/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/local/myusername/.ssh/id_rsa.
Your public key has been saved in /home/local/myusername/.ssh/
The key fingerprint is:
15:68:47:67:0d:40:e1:7c:9a:1c:25:18:be:ab:f1:3a myusername@server1
The key's randomart image is:
+--[ RSA 2048]----+
|        .*Bo=o   |
|       .+o.*  .  |
|       ...= .    |
|         + =     |
|        S +      |
|         .       |
|      . .        |
|      E+         |
|      oo.        |

Now you will need to copy the public key you just generated and save it somewhere, you will need it later. Also ensure when you copy the key that the text is all on one line, if there are line breaks in the text, it will cause problems later when you try and use the key.

cd .ssh
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyFS7YkakcjdyCDOKpE4RrBecRUWShgmwWnxhbVNHmDtJtK
/rUnu0F9nBJFlo7Rvc1cMuSUiul/wvJ8tzlOhU8FUlHvHqoUUw== myusername@server1

2. ssh to server2

Now we will copy the public key from server1 to server2.

ssh myusername@server2
mkdir .ssh
cd .ssh
vi authorized_keys
# paste the public key
chmod 600 authorized_keys

3. Test that your setup is working

ssh myusername@server1
ssh myusername@server2
# you should not be prompted for a password!

That is all! (Thanks Dave!)

Blog at