multiple

Unix/Linux: Find all files that contain multiple strings/patterns

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.)

How to read from two databases at the same time with ScalikeJdbc alvin May 8, 2018 - 5:26pm

This example shows how to connect-to and read-from multiple databases with ScalikeJdbc (a Scala JDBC library). I assume you already know how to use ScalikeJdbc with one database, so I’m only going to show the code and configuration file. (I’m not going to explain the details.)

The ScalikeJdbc configuration file

My ScalikeJdbc code is in an SBT project, so the ScalikeJdbc configuration file is at src/main/resources/application.conf:

Scala: How to append and prepend items to Vector and Seq

Table of Contents1 - Solution2 - Example data3 - Append a single item4 - Append multiple elements5 - Prepend a single item6 - Prepend multiple elements7 - Seq works just like Vector8 - How to remember the method names9 - A possible problem

Scala FAQ: How do I append or prepend one or more elements to a Vector or Seq class?

Back to top

Solution

To append or prepend one or more elements to a Vector or Seq, use these methods:

How to use multiple Futures in a Scala for-comprehension

If you want to create multiple Scala Futures and merge their results together to get a result in a for comprehension, the correct approach is to (a) first create the futures, (b) merge their results in a for comprehension, then (c) extract the result using onComplete or a similar technique.

A Unix find and move command (find in subdirectories)

This is a dangerous Unix command, but if you want to move a bunch of files from their subdirectories into your current directory, this find and mv command works:

find . -type f -exec mv {} . \;

That command finds all files beneath the current directory, and moves them into the current directory. I just moved a bunch of files from their (iTunes) subdirectories into my current working directory, and that find and move command did the trick. (But again, it’s a dangerous command, be careful out there.)

How to use multiple generators in Scala ‘for’ expressions (loops)

A cool thing about Scala for loops — what I’ll more-accurately call for expressions in this article — is that you can have multiple generators. What’s also very cool about them is how they work.

For example, imagine that you have these two values:

val nums = Seq(1,2,3)
val letters = Seq('a', 'b', 'c')

An interesting question then becomes, “What is the type of res in this expression?”:

How to define Scala methods that return multiple items (tuples)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.5, “How to define Scala methods that returns multiple items (tuples).”

Problem

You want to return multiple values from a method, but don’t want to wrap those values in a makeshift class.

Solution

Although you can return objects from methods just as in other OOP languages, Scala also lets you return multiple values from a method using tuples. First, define a method that returns a tuple:

Scala: How to match multiple conditions (patterns) with one case statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.8, “How to match multiple conditions with one case statement.”

Problem

You have a situation where several match conditions require that the same business logic be executed, and rather than repeating your business logic for each case, you’d like to use one copy of the business logic for the matching conditions.

How to use Scala ‘for’ loops (expressions) with multiple counters (multi-dimensional arrays) alvin June 7, 2015 - 3:07pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.2, “How to use Scala for loops with multiple counters.”

Problem

You want to create a loop with multiple counters, such as when iterating over a multi‐dimensional array.

Solution

You can create a for loop with two counters like this: