future

Humans as transitional beings

“In a book I read recently, the author talked about humans as transitional beings — beings who are neither fully caught nor fully free, but are in the process of awakening. I find it helpful to think of myself this way. I’m in the process of becoming, in the process of evolving. I’m neither doomed nor completely free, but I’m creating my future with every word, every action, every thought.”

~ Pema Chödrön

Simple concurrency with Scala Futures (Futures tutorial)

Table of Contents1 - Problem2 - Solution3 - Run one task, but block4 - Run one thing, but don’t block, use callback5 - The onSuccess and onFailure callback methods6 - Creating a method to return a Future[T]7 - How to use multiple Futures in a for loop8 - Discussion9 - A future and ExecutionContext10 - Callback methods11 - For-comprehensions (combinators: map, flatMap, filter, foreach, recoverWith, fallbackTo, andThen)12 - See Also13 - The Scala Cookbook

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.9, “Simple concurrency with Scala Futures.”

Back to top

Problem

You want a simple way to run one or more tasks concurrently in a Scala application, including a way to handle their results when the tasks finish. For instance, you may want to make several web service calls in parallel, and then work with their results after they all return.

Back to top

Solution

A Future gives you a simple way to run an algorithm concurrently. A future starts running concurrently when you create it and returns a result at some point, well, in the future. In Scala, it’s said that a future returns “eventually.”

Futureboard, a Flipboard-like Scala Futures demo

I’ll write more about this shortly, but yesterday I created a little video of a demo application I call Futureboard. It’s a Scala/Swing application, but it works like Flipboard in that it updates a number of panels — in this case Java JInternalFrames — simultaneously every time you ask it to update.

The “update” process works by creating Scala futures, one for each internal frame. When you select File>Update, a Future is created for each news source, and then simultaneous calls are made to each news source, and their frames are updated when the data returns. (Remember that Futures are good for one-shot, “handle this relatively slow and potentially long-running computation, and call me back with a result when you’re done” uses.)

Here’s the two-minute demo video:

What’s Important Now (WIN)

I know, it’s a little corny, but instead of writing out a “To-Do List” — which implies hard labor and/or something I force myself to do — these days I write “WIN” on the top of my index cards. WIN stands for, “What’s Important Now”, and I find that this change in wording changes my attitude towards the things that need to be done. Rather than thinking, “Ugh, okay, what do I have to do next,” I now think of these tasks as important to me, my future, and my success.

He just reset our karmic destiny

“I have no idea who that guy was, but I know that he just reset our karmic destiny.”

(A quote from the book, Love Everyone, that makes me wonder how many times our karmic destiny is reset and we don’t notice it.)

“Life is like a dream”

When you hear mindfulness people say something like, “Life is like a dream,” one thing they mean is that more than 99.99% of the stuff going on in our minds are thoughts about the past and the future. (Past happiness or regrets, and future hopes and concerns.) Because the only thing that’s real in the present moment is what’s actually happening in *only this moment*, anything that’s outside of this moment is in a strict sense no longer real.

Along this line of thinking I like Eckhart Tolle’s two quotes, “The present moment is all you ever have” — you know that to be true for sure if you’ve ever lost consciousness, not knowing if you’d ever open your eyes again — and, “The whole essence of Zen consists in walking along the razor’s edge of Now.”