char

How to create a range of characters as a Scala Array

I just noticed this quirk when trying to create an array of characters with the Scala Array.range method:

# works as expected
('a' to 'e').toArray              // Array[Char] = Array(a, b, c, d, e)

# surprise: Array.range always returns Array[Int]
val a = Array.range('a', 'e')     // Array[Int] = Array(97, 98, 99, 100)

I was surprised to see that the Scaladoc for the Array object states that the second example is expected behavior; Array.range always returns an Array[Int]. I suspect this has something to do with a Scala Array being backed by a Java array, but I didn’t dig into the source code to confirm this.

For much more information about arrays, see my Scala Array class examples tutorial.

Scala type aliases (syntax, examples)

In Scala you can declare a type alias. Typically you do this to create a simple alias for a more complex type.

Using a type alias to simplify a complex type

For example, on this page I note a good example where someone on StackOverflow first defined a type alias named Row, and then created a second type alias named Matrix as a list of rows:

Scala: Finding the difference, intersection, and distinct characters in a String

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 1.11, “String Differences, Intersections, and Distinct Characters.”

Problem

In Scala, you need to perform advanced string operations, such as finding the difference between two strings, the common characters between two strings, or the unique characters in a string.

How to process every character in a text file in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.4, “How to process every character in a text file in Scala.”

Problem

You want to open a text file and process every character in the file.

Solution

If performance isn’t a concern, write your code in a straightforward, obvious way:

How to process a Scala String one character at a time (with map, for, and foreach)

Scala FAQ: How can I iterate through each character in a Scala String, performing an operation on each character as I traverse the string?

Solution

Depending on your needs and preferences, you can use the Scala map or foreach methods, a for loop, or other approaches.

The map method

Here’s a simple example of how to create an uppercase string from an input string, using the map method that’s available on all Scala sequential collections:

Scala String differences, intersections, and distinct characters

Scala String problem: You need to find the difference between two strings, the common characters between two strings, or the unique characters in a string.

Solution

You can perform all of these operations with built-in methods. Use the diff method to find the differences between two strings:

Scala data types: bit sizes, ranges, and docs

Scala FAQ: What are the Scala data types? How many bits do they use to store their data, and what is the range of those data types?

Courtesy of the excellent book, Programming in Scala, here is a list and description of the Scala data types, including bit sizes and data ranges:

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.