write

A Scala method to write a list of strings to a file

As a brief note today, here’s a Scala method that writes the strings in a list — more accurately, a Seq[String] — to a file:

def writeFile(filename: String, lines: Seq[String]): Unit = {
    val file = new File(filename)
    val bw = new BufferedWriter(new FileWriter(file))
    for (line <- lines) {
        bw.write(line)
    }
    bw.close()
}

Don’t objectify me (on writing and writing style)

Sometimes people write to tell me that they like my writing style, that I’m good at explaining things. Other people write and say that if they wrote a book, they would have written it just like mine.

The truth is, when I first started working with Scala I fell in love with the language, so wanting to write about it was easy. After that, I’m not that smart, so I have to break complex things down so I can understand them myself. So I think that by breaking things down and looking for meaningful examples, people seem to appreciate what I’ve written (or I hope they do).

After I wrote the Scala Cookbook and people sent me notes like that, I struggled with writing for a little while. Then I decided to just try to write for a younger version of myself and ignore what other people were saying. I just ask myself, “Would this have helped Al 2+ years ago?” Since then I’ve been fine.

Some Java file utilities

As a bit of warning, this is some old Java code, but if you want to create your own Java file utilities (utility methods), this code might help you get started:

Create, let go, surrender to more

When you worry about where your words land or how others digest or perceive them, you are clinging (and not allowing space for more to come through the channel). Continually create, let go, surrender to more. Create, let go, surrender to more. It is a divine dance. Respect your own story. Remain inside the rhythm.

~ Victoria Erickson

You process things a little better when you put pen to paper.

“You process things a little better when you put pen to paper.”

~ Trevor Siemian, Denver Broncos QB on something he learned from Peyton Manning (and something a professor told me in college many years ago)

How I came to write the Scala Cookbook

The funny thing about writing the Scala Cookbook is that it started as a whim. I was just about to leave for a vacation at the beach, and right before I turned off the computer, a thought flashed in my mind, “I should contact the people at O’Reilly about writing a cookbook for Scala.” I then had a doubt that they would actually do it, but I applied the “What the heck” rule — i.e., “What the heck, what do I have to lose?” — and sent the email.

I dug around the internet for a few minutes, found the correct O’Reilly email address, sent them a message, turned off the computer, and drove to the beach. While I was at the beach the publisher wrote and said, “Love it, send me a full proposal!”

So if you’re thinking about doing something, but are afraid or uncertain about doing it ... apply the “What the heck” rule, and give it a shot. :)

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: