read

If you could give one tip for reaching heights in tech today, what would it be? alvin July 19, 2018 - 10:38am

When asked, “If you could give one tip for reaching heights in tech today, what would it be?”, this was the initial response from Jonas Bonér, creator of Akka:

  • Work hard at minimizing your ego & attachment to identity
  • Learn deliberately, seek out weaknesses & work hard at them
  • Eliminate bad habits, replace them with good, one at a time
  • Read a lot, foundational stuff, not just latest hyped thing

How to read from two databases at the same time with ScalikeJdbc

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:

“I have known no wise people who didn’t read all the time” alvin January 11, 2018 - 5:01pm

“In my whole life, I have known no wise people (over a broad subject matter area) who didn’t read all the time — none, zero. You’d be amazed at how much Warren reads — and at how much I read. My children laugh at me. They think I’m a book with a couple of legs sticking out.”

~ Charlie Munger, talking about Warren Buffett and himself

The beginning of a Scala “FileUtils” class alvin November 30, 2017 - 3:31pm

In production code I recommend that you use a good “Files” library like Apache Commons IO, but if you want to create your own Scala FileUtils class, here’s some source code that can help you get started.

First, here’s some code for the FileUtils class (an object, technically):

How to process every line in a file with a Unix/Linux shell script

Unix/Linux shell script FAQ: How do I write a Unix or Linux shell script where I "do something" for every line in a text file?

Solution: An easy way to process every line in a text file is to use a Unix/Linux while loop in combination with the Linux cat command, like this:

Scala: How to read input from one file while writing output to another file alvin June 14, 2017 - 3:53pm

Without much introduction or discussion, here’s a Scala example that shows how to read from one text file while simultaneously writing the uppercase version of the text to a second output file:

Scala code to read a text file to an Array (or Seq) alvin January 17, 2017 - 5:04pm

As a quick note, I use code like this read a text file into an Array, List, or Seq using Scala:

def readFile(filename: String): Seq[String] = {
    val bufferedSource = io.Source.fromFile(filename)
    val lines = (for (line <- bufferedSource.getLines()) yield line).toList
    bufferedSource.close
    lines
}
How to prompt users for input from Scala shell scripts alvin June 20, 2015 - 4:37pm

This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 14.12, “How to prompt users for input from Scala shell scripts.”

Back to top

Problem

You want to prompt a user for input from a Scala shell script and read her responses.

Back to top

Solution

Use the readLine, print, printf, and Console.read* methods to read user input, as demonstrated in the following script. Comments in the script describe each method:

Table of Contents

  1. Problem
  2. Solution
Back to top
How to read and write binary files in Scala alvin June 19, 2015 - 11:01am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.3, “How to read and write binary files in Scala.”

Problem

You want to read data from a binary file or write data to a binary file.

Solution

Scala doesn’t offer any special conveniences for reading or writing binary files, so use the Java FileInputStream and FileOutputStream classes.

How to open and read text files in Scala alvin June 19, 2015 - 10:57am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.1, “How to open and read a text file in Scala.”

Back to top

Problem

You want to open a plain-text file in Scala and process the lines in that file.

Back to top

Solution

There are two primary ways to open and read a text file:

Table of Contents

  1. Problem
  2. Solution
Back to top