alvin's blog

Scala - How to declare multiple variables on one line

I thought I’d have a little fun today, and show different ways to declare multiple variables on one line. These aren’t the most useful or common things to do, but they’re interesting, and I know I learned at least one thing while looking into this.

You can define multiple fields on one line, separated by commas, as long as they are all the same type and have the same value:

MySQL syntax examples - create table, primary key, foreign key

Just a quick note here today that if you need some example MySQL database tables, you can use these. I created them for some experiments I ran last night. They show the MySQL create table, primary key, and foreign key syntax:

Scala file reading performance - Line counting algorithms

Out of curiosity about Scala’s file-reading performance, I decided to write a “line count” program in Scala. One obvious approach was to count the newline characters in the file:

// took 101 secs (10M lines)
// work on one character at a time
def countLines1(source: Source): Long = {
  var newlineCount = 0L
  for {
    c <- source
    if c.toByte == NEWLINE
  } newlineCount += 1
  newlineCount
}

As the comment shows, this took 101 seconds to read a file that has 10M lines. (An Apache access log file for this website.)

Scala zip and zipWithIndex examples (with Stream)

I’ve known about using the Scala zipWithIndex method for quite some time. I’ve used it in for loops to replace counters, and it works like this:

scala> List("a", "b", "c").zipWithIndex
res0: List[(String, Int)] = List((a,0), (b,1), (c,2))

I learned about using zip with Stream last night while reading Joshua Suereth’s book, Scala In Depth. It works like this:

Scala - Thinking of functions as transformers

When you write Scala code like this:

val x = List.range(1, 10)
val evens = x.filter((i: Int) => i % 2 == 0)

the following portion of the code is a function literal (also known as an anonymous function):

(i: Int) => i % 2 == 0

When reading this code, it helps to think of this symbol:

How to run a Unix shell script from the Mac Finder

If you ever want to create a Unix shell script that you can give to someone else so they can double-click it and run it through the Mac OS X Finder, all you have to do is (a) name the file with the ".command" extension and (b) make it executable. So, just name your Mac/Unix script like this:

ShowProcesses.command

Then make it executable, like this:

chmod +x ShowProcesses.command

You can also leave out the usual #!/bin/sh part on the first line.

The Mediator Design Pattern in Java

Summary: The Mediator Design Pattern is demonstrated in a Java example (a Java Mediator Pattern example).

Law of Demeter - Java examples

Summary: The Law of Demeter is discussed using Java source code examples.

Whenever you talk to a good, experienced programmer, they will tell you that "loosely coupled" classes are very important to good software design.

The Law of Demeter for functions (or methods, in Java) attempts to minimize coupling between classes in any program. In short, the intent of this "law" is to prevent you from reaching into an object to gain access to a third object's methods. The Law of Demeter is often described this way:

2013, the year that quality forgot

I truly believe (or at least hope) that 2013 will be seen as the year that software companies forgot about quality. Here are just a few examples I noticed last night and this morning:

Twitter: Today I was notified that people “favorited” some of my posts. Cool. Oh, wait, they were favorited eight days ago, but just showed up today.

A Scala XML EntityRef example (unicode character)

This page shows a good Scala XML EntityRef example:

import scala.xml._
val xml = <body>Hello{EntityRef("#8198")}World</body>

That page states that 8198 is the unicode value for a tiny space character.

That code isn't too helpful unless you can see it in the REPL, so here it is:

Syndicate content