Example of Future.transform?

490 views
Skip to first unread message

Freewind

unread,
Jun 1, 2012, 1:11:26 AM6/1/12
to General Dart Discussion
Now I know how to use "Future.chain" and "Future.then", but I don't
understand "Future.transform".

Is there an example of its usage?

Freewind

unread,
Jun 1, 2012, 2:12:24 AM6/1/12
to General Dart Discussion
I know now.

afuture.chain((v) {
return another_future; // must return
}).transform((v) {
// do something, no need to return, but it's still a future
}).then((v) {
// finally
});

Sam McCall

unread,
Jun 1, 2012, 6:53:12 AM6/1/12
to Freewind, General Dart Discussion
On Fri, Jun 1, 2012 at 8:12 AM, Freewind <nowi...@gmail.com> wrote:
I know now.

afuture.chain((v) {
 return another_future; // must return
}).transform((v) {
 // do something, no need to return, but it's still a future
}).then((v) {
 // finally
});
That might work for some cases, but it's not exactly the intended use.
transform() applies some transformation to the result of a future, and is best illustrated by example.

Future<bool> exists(String filename); // async function checking for existence of a file

// a future delivering true or false
Future<bool> e = exists("/foo.txt");

// a future delivering "File exists" or "File does not exist"
Future<String> message = exists("/foo.txt").transform((x) {
  return x ? "File exists" : "File does not exist";
});

It's very similar to chain(). The difference is that chain() applies an asynchronous transformation (so the transformation function returns a Future), while transform() applies a synchronous transformation (so the transformation function returns a value).

Hope this helps,
Sam

Bob Nystrom

unread,
Jun 1, 2012, 12:56:42 PM6/1/12
to Sam McCall, Freewind, General Dart Discussion
On Fri, Jun 1, 2012 at 3:53 AM, Sam McCall <samm...@google.com> wrote:

It's very similar to chain(). The difference is that chain() applies an asynchronous transformation (so the transformation function returns a Future), while transform() applies a synchronous transformation (so the transformation function returns a value).

This is an excellent summary. A cruder way to explain it is: if your transformation function returns a Future, use chain(). If it returns a value that you want to automatically be wrapped in a Future, use transform().

Or, another way: when your first async operation completes if you want to start another new async operation, use chain(). If when it completes you just need to do some processing and then you're done, use transform().

- bob

John Evans

unread,
Jun 1, 2012, 3:33:32 PM6/1/12
to mi...@dartlang.org, Sam McCall, Freewind
I really wish Futures would also work on sequences of data (i.e. support a next() instead of just complete()), then it wouldn't be necessary to write projects like this:  https://github.com/prujohn/Reactive-Dart ;)

On Friday, June 1, 2012 11:56:42 AM UTC-5, Bob Nystrom wrote:

Bob Nystrom

unread,
Jun 1, 2012, 5:58:04 PM6/1/12
to John Evans, mi...@dartlang.org, Sam McCall, Freewind
Me too. I had a conversation last week with someone on the team about how a Future is just an Event that happens to only fire once. I really want to unify this stuff.

- bob

John Evans

unread,
Jun 1, 2012, 6:26:35 PM6/1/12
to mi...@dartlang.org, John Evans, Sam McCall, Freewind
Worth a shot: http://www.dartbug.com/3360  :)


On Friday, June 1, 2012 4:58:04 PM UTC-5, Bob Nystrom wrote:
Me too. I had a conversation last week with someone on the team about how a Future is just an Event that happens to only fire once. I really want to unify this stuff.

- bob

Dirk Detering

unread,
Jun 2, 2012, 4:17:15 AM6/2/12
to Bob Nystrom, Freewind, General Dart Discussion, Sam McCall

Or -correct me if I'm wrong- for people willing to learn the functional way:

transform is the Functor's  map function: 
Future<A> -> (A->B) -> Future<B>

Given a future of type A, transform takes a function of type A to B, and returns a new future of type B.

chain is the Monad's  bind function:
Future<A> -> (A->Future<B>) -> Future<B>

Given a future of type A, chain takes a function of type A to a future of B, and returns said future of B.

If so, and if collections would now support map and flatMap, why not unify and generalize this with comprehensions to bring full monadic support into Dart?
(Or is it there and I missed it?)

--
Det

Allan MacDonald

unread,
Jun 2, 2012, 11:24:48 PM6/2/12
to Sam McCall, Freewind, General Dart Discussion
I thought I would share an example of where I needed to use a transform to accomplish some recursive behaviour in my application. I made the parameter names verbose to illustrate what is getting passed around.

Hope it helps to add some clarity to transform.

Allan
Reply all
Reply to author
Forward
0 new messages