string

Unix/Linux: Find all files that contain multiple strings/patterns alvin May 9, 2019 - 5:47pm

When using Unix or Linux, if you ever need to find all files that contain multiple strings/patterns, — such as finding all Scala files that contain 'try', 'catch', and 'finally' — this find/awk command seems to do the trick:

find . -type f -name *scala -exec awk 'BEGIN {RS=""; FS="\n"} /try/ && /catch/ && /finally/ {print FILENAME}' {} \;

As shown in the image, all of the matching filenames are printed out. As Monk says, you’ll thank me later. :)

(I should mention that I got part of the solution from this gnu.org page.)

An example of Scala’s `f` string interpolator

With Scala it’s common to embed variables in strings like this with the s string interpolator:

val name = "Fred"
println(s"My name is $name.")

That’s cool, but when you need to format your string, Scala gives you an even more powerful tool: the f string interpolator. Here’s an example of how I just did this in my LittleLogger logging library:

How to left-justify a multiline Scala string

If you ever need to left-justify a multiline Scala string, the solution is to use the stripMargin method, as shown in this example:

def getWordFormatted(s: String): String = {
    s"""
      |
      |Word of the Day
      |---------------
      |Word: $s
    """.stripMargin
}

The | character has a special meaning when used in conjunction with stripMargin, so that method results in a string like this being output:

Scala: How to use regex pattern matching in a match expression alvin February 17, 2019 - 12:58pm

Scala FAQ: How can I use regular expression (regex) pattern matching in a match expression (a Scala match/case expression)?

As I wrote in my Scala sed class post earlier today, Jon Pretty’s Kaleidoscope project lets you use string pattern-matching code in Scala match expressions. This enables regex pattern-matching code like this:

I put my Scala String Utilities library on Github alvin February 12, 2019 - 10:56am

I put my Scala String Utilities library on Github a few days ago. It includes my Q String Interpolator, and several other string utility functions. It also demonstrates how to write ScalaTest and ScalaCheck tests with an SBT project.

Kaleidoscope, a Scala pattern-matching library alvin February 9, 2019 - 4:52pm

Kaleidoscope is a Scala pattern-matching library created in a string interpolator style.

ScalaCheck custom generator examples

Table of Contents1 - Custom generators2 - Built-in ScalaCheck generators3 - How to use ScalaCheck generators4 - More ScalaCheck generators

Writing custom generators for ScalaCheck can be one of the more difficult and/or time-consuming parts of using it. As a result I thought I’d start putting together a list of generators that I have written or seen elsewhere. Unfortunately I can’t credit all the ones I’ve seen in other places because I google’d and copied them many moons ago, but I’ll give credit/attribution to all the ones I can.

Back to top

Custom generators

This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:

Scala: Convert a String with newline characters to a sequence/list of strings alvin February 2, 2019 - 1:23am

If you ever need a Scala method/function to convert a string with newline characters in it to a sequence of strings (Seq[String]), here you go:

def convertStringWithNewlinesToSeq(s: String): Seq[String] =
    s.split("\n").toVector

You can convert the final result to a Vector, Seq, List, ArrayBuffer, Array, etc., but I prefer Vector. The Scala REPL demonstrates how it works:

scala> convertStringWithNewlinesToSeq("")
res0: Seq[String] = Vector("")

scala> convertStringWithNewlinesToSeq("foo")
res1: Seq[String] = Vector(foo)

scala> convertStringWithNewlinesToSeq("foo\nbar\nbaz")
res2: Seq[String] = Vector(foo, bar, baz)

scala> convertStringWithNewlinesToSeq("foo\nbar\nbaz\n\n")
res3: Seq[String] = Vector(foo, bar, baz)