I just ran into the craziest problem where I couldn't get any third-party apps on my iPad to open. I'd tap them, there'd be a quick blink, then nothing, I was back to the home screen with all my app icons.
Scala, Java, Unix, MacOS tutorials (page 329)
Note: This page is very much a work in progress.
This page shows a sample Scala SBT build.sbt file, including the last line, which handles the SBT 're-run with -deprecation for details' warning message. (If you get the 're-run with -deprecation' message, that last line hands the "-deprecation" option over to the compiler, so you can see the deprecation problems.)
About eight weeks ago -- May 20, 2012, to be exact -- I started a simple exercise and diet program. I was having several health problems, including constant severe headaches, blood pressure spiking to 150/100 for some reason the doctors hadn't figured out, my thyroid was failing (we knew that, and were treating it), and I also knew about a prostate problem. I generally felt like crap, and because of the headaches I was barely able to work 20 hours a week, constantly laying in bed trying to ride out the pain.
In a little bit of accidental Scala REPL fun this afternoon, I was testing a recursive algorithm like the following simple factorial algorithm, and forgot a return statement:
def factorial(n: Int):Int = {
if (n == 1) n // forgot 'return' here
factorial(n - 1)
}
When I pasted that code into the Scala REPL and then tried to test it as shown below, it didn't come back for a while, until it failed with this error message:
While working on a new Scala application recently, the alarms started going off in my head, saying "You need to use the Factory Pattern here." That's when things got interesting.
Just exactly how do you implement the Factory Pattern in Scala?
Here's a simple, quick example.
1) The interface
Sparing you all the experiments I went through, let's assume you want a factory that produces animals like this:
Okay, this is pretty cool. With sbt, you can magically refer to dependencies that are set up as GitHub projects, and those projects don't need to have a jar file, they can just be a source code library. This means you can save your Scala libraries as source code to GitHub (like you normally would), then pull them into your other Scala projects simply by referencing them in your build.sbt file.
Assuming you're comfortable with sbt, here's a quick six-step example that shows how to pull a GitHub library into a simple Scala project:
While I was working on improving how Sarah gets information from Twitter and other sources, I decided to take some of that code and hack together this example. It shows how to make three Twitter web service requests concurrently -- and then wait for ther results before moving on -- with Akka Futures.
Before sharing the entire class, the cool Akka Futures stuff is in these lines of code:
UPDATE: This example shows how to create an implicit method in Scala 2.9 or older. You can use a slightly simpler approach with Scala 2.10 and newer, which I've documented in this Scala 2.10 implicit class example.
I'm not going to do much writing here today, but instead I'll just demonstrate how an implicit method argument works with implicit fields in Scala. Without any further ado, here's some code:
Akka Futures FAQ: Can you share a simple example that shows how to use an Akka Future?
Sure. To fix a problem in my Sarah application I needed to be able to run some operations concurrently. After digging through the Akka Actors documentation I found that you can run simple operations as a Future, almost as easily as this:
Akka actor ask FAQ: Can you share an example that shows how one Akka actor can ask another actor for information?
Sure. Here's a quick example to demonstrate how one Akka actor can ask another Akka actor for some information and wait for a reply. When using this "ask" functionality, you can either use the "ask" method, or the "?" operator, and I've shown both approaches below.
I was trying to write some code last night while I was very tired, and while the code itself isn't difficult in any way, the thought process I went through demonstrates how your code can become more concise as your understanding of Scala evolves, so I thought I'd share the experience here.
While working on my Sarah application, I was rewriting the Brain class, and I started off writing a method that looked like this:
While I was researching the Erlang "error kernel pattern" yesterday, I ran across this great link of Software Engineering Principles. It's a great "all in one page" collection titled "Programming Rules and Conventions".
As a quick example of what you'll find on that page, here is a list of the software engineering principles from the third chapter of that document:
Akka actor FAQ: How do you stop an Akka actor?
I don't have time this morning to write my usual tutorial, so in short, if you want to stop an Akka actor, use code like this from inside your actor's receive method:
context.stop(self)
Or, if you want to shut down the Akka system, use the following code, again from inside the receive method of one of your actors:
Scala FAQ: Does Scala have a String variable substitution syntax like Ruby?
UPDATE: If you're using Scala 2.10 or newer, see my new String interpolation in Scala 2.10 (embedding variables in strings) tutorial. If you're using Scala 2.9.x or older, continue with this article.
Scala Actors FAQ: Can you share a Scala Akka Actors example/tutorial?
Sure. Most of the Scala Akka Actor tutorials I see jump right into the deep end, throwing you into some tough concepts right away. Personally I'm more into the "crawl before you walk approach," and to that end, here are some simple Akka Actor examples, of the "Hello, world" variety.
A simple Akka "Hello, world" example
My first Akka Actor example is about as simple as I can make it:
I haven't had the need for this syntax yet, but I just saw some code that shows how to create a private primary constructor in Scala, and I thought I'd test it and share an example of it.
In the example below, I've made the primary constructor of the Order class private:
I did some manual labor this weekend, and converted the Scala SBT tool documentation into PDF format (a PDF file). I also tried to contact the SBT author, Mark Harrah, to see if he wanted the PDF on the Github website, but I couldn't find any way to email him through Github, so ...
If you want a copy of the Scala SBT documentation in PDF format, current as of June 3, 2012, here it is: