source

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:

A ‘printf’ format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

Trying to understand where the universe comes from alvin May 8, 2017 - 9:20am

My method for trying to understand this fundamental essence – the presence of “something bigger” than me – was to examine intellectually all the reasons I could think of for the universe to exist and to try to envision what had “existed” before the universe came into being.

On the one hand, if there was nothing before creation, how could the “something” of the universe come from “nothing”? On the other hand, if there was something before the creation of the world, it must have always existed, without beginning. But how could “something” have no starting point, no first moment?

I was frustrated by these questions, and by not being able to envision the timelessness that went with “no beginning.” As a boy, I was continually preoccupied by such attempts to explain the world rationally. I was unable to recognize or accept the limitation of my logical mind, its inability to understand the nature of life beyond concepts of solid objects and linear time.

(I had these same thoughts back in high school, but these words are from the book, “Zen at Work.”)

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 use a String like it’s a File in Scala (such as in testing) alvin June 19, 2015 - 11:09am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.7, “How to use a String like it's a file in Scala.”

Problem

Typically for the purposes of testing, you want to pretend that a String is a file.

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
Scala: How to build control structures with types and call-by-name parameters alvin March 16, 2014 - 4:13pm

(This article is an excerpt from the Scala Cookbook.)

Scala file reading performance - Line counting algorithms alvin May 7, 2013 - 7:59am

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:

A simple Scala Try, Success, Failure example (reading a file) alvin April 17, 2013 - 6:25pm

Sorry, not much free time these days, so without any discussion, here’s a simple Scala Try/Success/Failure example:

Ruby command line arguments alvin November 19, 2011 - 1:04pm

Ruby FAQ: How do I read command line arguments in a Ruby script (Ruby command line args)?

To read command line args in a Ruby script, use the special Ruby array ARGV to get the information you need. Here are a few examples.

1) Getting the number of command line args

To get the number of command line arguments passed in to your Ruby script, check ARGV.length, like this: