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
});
Future<bool> exists(String filename); // async function checking for existence of a file// a future delivering true or falseFuture<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).
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
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