example

An example Scala ‘App’ for the Cats “IO Monad” article alvin June 11, 2017 - 7:38pm

As a quick note, if you’re interested in using the IO monad described in this IO Monad for Cats article, here’s the source code for a complete Scala App based on that article:

How to format source code blocks in Scaladoc comments alvin June 10, 2017 - 4:38pm

As a quick note, if you want to embed a Scala source code example in your Scaladoc comments, just put the source code block in between {{{ and }}} characters in your comments, as shown in this example:

An Akka actors remote example

While doing some crazy things with SARAH, I realized that the best way to solve a particular problem was to use remote Akka actors. I haven’t had the opportunity to work with Akka much since finishing the Scala Cookbook, so I dug around trying to find a simple Akka remote “Hello, world” example. Unable to find a good one, I read some stuff, and created it myself.

“Alexa written with Akka” = Aleka alvin September 4, 2016 - 7:05pm
Table of Contents1 - Background2 - Assumptions3 - 1) Create an SBT project4 - 2) Create a Scala app5 - 3) Messages6 - 4) The brain7 - 5) The mouth8 - 6) The project directory structure9 - 7) Running the application10 - The source code11 - What’s next

As a way of demonstrating how to write code with Akka, Scala, and functional programming (FP), I started creating a new project this weekend. I named it Aleka, because it may eventually be like Amazon’s Echo/Alexa, written with Akka (and Scala).

(I suppose a better name might be “Ekko,” after Echo, but I have a niece named Aleka, so unless she objects, this works for me.)

A Perl script to delete binary files alvin June 28, 2016 - 10:24am

As a quick note and a little bit of source code sharing, I wrote the following Perl script to delete all of the binary files it finds in a list of files it’s given. I named this script deleteBinaryFiles.pl, and it should be called like this:

deleteBinaryFiles.pl listOfFilesToLookAt

where listOfFilesToLookAt is a file that contains a list of filenames, with one filename per line.

Given that brief introduction, here’s the source code:

When you want to store static text in an Android file

As a note to self, when you’re writing an Android application and you think you want to store some static text in an external file, a better approach can be to create a resource file under res/values.

For example, I’m currently adding some help text to an Android app, and to do that I created a file named strings_help.xml under the res/values directory. That file contains HTML wrapped in an XML CDATA tag, like this:

How to create a method that returns a function in Scala alvin June 14, 2015 - 6:58pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.7, “How to create a method that returns a function in Scala.”

Problem

You want to return a function (algorithm) from a function or method.

Solution

Define a function that returns an algorithm (an anonymous function), assign that to a new function, and then call that new function.

The following code declares an anonymous function that takes a String argument and returns a String:

How to create inner classes in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.16, “How to create inner classes in Scala.”

Problem

You want to create a Scala class as an inner class to help keep the class out of your public API, or to otherwise encapsulate your code.

Solution

Declare one class inside another class. In the following example, a case class named Thing is declared inside of a class named PandorasBox: