stream

How to read a binary file with Scala (FileInputStream, BufferedInputStream) alvin November 26, 2019 - 3:28pm

As a brief note today, if you need to read a binary file with Scala, here’s an approach I just tested and used. It uses the Java FileInputStream and BufferedInputStream classes:

Do you hear the murmuring sound of the mountain stream? alvin June 29, 2019 - 11:06am

A monk was anxious to learn Zen and said, “I have been newly initiated into the Brotherhood. Will you be gracious enough to show me the Way?”

The Master said, “Do you hear the murmuring sound of the mountain stream?”

The monk said, “Yes, I do.”

The Master said, “Here is the entrance.”

~~~

“Listening intently” is a simple, fun meditation practice. Just sit, relax, and listen to your environment like a dog, cat — or a squirrel in the wild, where your life depends on your listening. At work I used to have fun by listening to as many conversations as I could simultaneously.

How to convert a Java array into a Stream alvin March 1, 2019 - 7:32pm

If you ever need to convert a Java array into a Stream, there are at least two ways to do it.

1) Converting an array to a Stream

First, to convert the entire array to a Stream, use the Stream.of static method like this:

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 Seq class: methods, examples, and syntax alvin May 21, 2018 - 5:47pm

This page contains a large collection of examples of how to use the methods on the Scala Seq class.

Important note about Seq, IndexedSeq, and LinearSeq

As an important note, I use Seq in the following examples to keep things simple, but in your code you should be more precise and use IndexedSeq or LinearSeq where appropriate. As the Seq class Scaladoc states:

How to read and write binary files in Scala

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 use the Scala Stream class, a lazy version of List

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.6, “How to Use the Scala Stream Class, a Lazy Version of a List”

Problem

You want to use a collection that works like a List but invokes its transformer methods (map, filter, etc.) lazily.