Skip to content

Commit

Permalink
Finished episode 1 notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean committed Dec 21, 2009
1 parent 6f44cfe commit 3ac5660
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
89 changes: 87 additions & 2 deletions src/episode_001/README.markdown
Expand Up @@ -2,6 +2,10 @@

Episode 001

This uses code from the following repository:

http://github.com/francoisdevlin/devlinsf-clojure-utils

In this episode we'll be discussing two new features in Clojure 1.1, futures & promises. Futures
are tools for executing expressions in a different thread, and reading a result later. A promise
is a tool for storing data, and delaying program flow until the value is set.
Expand All @@ -20,10 +24,91 @@ shows a dialog box.

(do
(future
(Thread/sleep 5000)
(Thread/sleep 3000)
(sounds/play-file test-wav))
(messages/plain-message "From the present."))
What happened was that when the future object was created, the code immediately began execution in another
thread. This is why the dialog box showed up immediately.
thread. This is why the dialog box showed up immediately, and the sound had a delay.

Sometimes you need to get at the result of a future object. Let's try that now. First, we'll define our
future object

(def a-future
(future
(Thread/sleep 10000)
"The future is complete")))

Next, we'll dereference our future object at the REPL.

episode-001=>@a-future

Notice how the REPL is hung. That is because the thread that dereferences a future will be blocked
until the future object is complete.

Note: Wait for dialog

The result of a future is cached. This means that if we call it a second time, the result is available instantly.

episode-001=>@a-future

There are a few utility functions available for interacting with futures. The first is future-done?, which determines
if the future is completed execution.

episode-001=>(future-done? a-future)

There are two other interesting functions, `future-cancel` and `future-canlled?`. Let's see them in action.

Note: Restart a-future

episode-001=>(future-cancel a-future)

I just called `future-cancel` on our future object. This stops execution of the thread. We can use the `future-cancelled?`
function to display that the future was cancelled.

episode-001=>(future-cancelled? a-future)

If we deref `a-future` after it is cancelled, an exception is thrown.

episode-001=>@a-future

If a future is cancelled, you still have to take responsibility for cleaning up your own side effects. As such, future-cancel
should be used wisely.

#Promises

Now we will move on to a second feature, promises. A promise object is created by the `promise` function.

episode-001=>(def a-promise (promise))

In order to set the value of a-promise, use the deliver functions

episode-001=>(deliver a-promise :fred)

And if you want to read a promise simply dereference it.

episode-001=>@a-promise

The interesting thing is about promise is that it will block the thread that dereferences it if it hasn't been set
yet. Let's redefine the promise

episode-001=>(def a-promise (promise))

Now, I'm going to deliver a value to the promise using a future and deference it immediately.

episode-001=>(do
(future (Thread/sleep 5000) (deliver a-promise :fred))
@a-promise)

Notice the the REPL is hung. It will stay this way until the promise is delivered.

Note: Wait fo finish

And there it is. One more thing to note about promises is that the can be set only once.

episode-001=>(deliver a-promise :lucy)

episode-001=>@a-promise

Well, that wraps up this episode. I hope this helps you under these two new features in Clojure 1.1. I'm Sean Devlin, and
this is Full Disclojure.
8 changes: 6 additions & 2 deletions src/episode_001/episode_001.clj
Expand Up @@ -6,7 +6,7 @@

(do
(future
(Thread/sleep 5000)
(Thread/sleep 3000)
(sound/play-file test-wav))
(messages/plain-message "From the Present"))

Expand All @@ -15,6 +15,10 @@
(Thread/sleep 10000)
"The future is complete"))

(messages/plain-message @a-future)
(do
(future (Thread/sleep 5000)
(deliver a-promise :fred))
@a-promise)



0 comments on commit 3ac5660

Please sign in to comment.