helpful scala primitives for async computations

36 views
Skip to first unread message

Matthew Pocock

unread,
May 31, 2012, 2:25:04 PM5/31/12
to scal...@googlegroups.com
Hi,

I'm in the middle of developing a fairly complex (as in lots of async and events) GWT application in java. At the time I started out, I made the judgement that scalagwt wasn't ready to use in a production app. Since then, I seem to be spending inordinate amounts of time writing boilerplate to deal with asynchronous computations. As soon as you use any service, you end up with all down-stream logic getting bundled up into continuations, and as far as I can see, GWT doesn't provide primitives for combining async operations - there's no primitive for taking a list of pending asyncs and getting back an async over the list of results, for example. To me, it looks like akka's actors and futures abstractions would map fairly cleanly onto this space. Has anyone looked at this, or is there some other data-flow/futures/continuations approach that has been successful? Something like this would be a big win for me, and could tempt me away from Java for GWT.

Cheers,

Matthew

--
Dr Matthew Pocock
Integrative Bioinformatics Group, School of Computing Science, Newcastle University
skype: matthew.pocock
tel: (0191) 2566550

Aaron Novstrup

unread,
May 31, 2012, 3:07:55 PM5/31/12
to scal...@googlegroups.com
Matthew,

I did some preliminary work on a continuation-based API for handling
sequences of dependent asynchronous operations (see [1]), which might
provide a starting point. It allows you to write code like:

async {
display("waiting for first result")
val result1 = callAsync(function1)
display("result1: " + result1)

val result2 = callAsync(function2(result1))
display("result2: " + result2)

val result3 = callAsync(function3(result2))
display("result3: " + result3)
}

It sounds like you want something that will allow you to send off
independent calls simultaneously and then combine their results, which
is not (yet) supported in this approach. I'd be interested in any
progress in that direction, though.

Here's an brief outline of my thoughts on how one could move this API
in that direction. Currently, callAsync makes a blocking call (from
the perspective of the code in the async block). That method could be
renamed 'blockingCall' or something, and a 'nonblockingCall' method
could be added that would return something like a future. Future
objects could be combined, and they would provide a 'waitFor' method
that would block (i.e., put the rest of the computation into a
continuation). So you could write code like:

async {
val future1 = nonblockingCall(fun1)
val future2 = nonblockingCall(fun2)
val future3 = nonblockingCall(fun3)

val results = (future1 ++ future2 ++ future3).waitFor
results match {
case (result1, result2, result3) =>
display("total:" + (result1 + result2 + result3))
}
}


~Aaron

[1] https://github.com/anovstrup/scalagwt-sample/blob/progress-gwtdlx-hosted/src/com/google/gwt/sample/gwtdlx/client/Async.scala

Nate Bauernfeind

unread,
May 31, 2012, 3:46:34 PM5/31/12
to scal...@googlegroups.com
This is pretty awesome!
Reply all
Reply to author
Forward
0 new messages