* @param func Function applied to {@link value}
* @returns Value returned by function
*
- * @see flatMap
+ * @see flat_map
+ * @see light_map
*
* Note: As time taken by function does not contribute to
* {@link wait_until} and the implementation is allowed to compute
return new MapFuture<A, G> (this, func);
}
+ public delegate unowned A LightMapFunc<A, G> (G value);
+
+ /**
+ * Maps a future value to another value by a function and returns the
+ * another value in future.
+ *
+ * @param func Function applied to {@link value}
+ * @returns Value returned by function
+ *
+ * @see flat_map
+ * @see map
+ * @since 0.11.4
+ *
+ * Note: The function may be reevaluated at any time and it might
+ * be called lazily. Therefore it is recommended for it to be
+ * idempotent. If the function needs to be called eagerly or have
+ * side-effects it is recommended to use {@link map}.
+ *
+ * Note: As time taken by function does not contribute to
+ * {@link wait_until} and the implementation is allowed to compute
+ * value eagerly by {@link when_done} it is recommended to use
+ * {@link task} and {@link flat_map} for longer computation.
+ */
+ public virtual Future<A> light_map<A> (LightMapFunc<A, G> func) {
+ return new LightMapFuture<A, G> (this, func);
+ }
+
[CCode (scope = "async")]
public delegate Gee.Future<A> FlatMapFunc<A, G>(G value);
--- /dev/null
+/* lightmapfuture.vala
+ *
+ * Copyright (C) 2013 Maciej Piechotka
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Author:
+ * Maciej Piechotka <uzytkownik2@gmail.com>
+ */
+
+internal class Gee.LightMapFuture<A, G> : Object, Future<A> {
+ public LightMapFuture (Future<G> base_future, Future.LightMapFunc<A, G> func) {
+ _base = base_future;
+ _func = func;
+ }
+
+ public bool ready {
+ get {
+ return _base.ready;
+ }
+ }
+
+ public unowned A wait () {
+ return _func (_base.wait ());
+ }
+
+ public bool wait_until (int64 end_time, out unowned G? value = null) {
+ unowned A arg;
+ bool result;
+ if ((result = _base.wait_until (end_time, out arg))) {
+ value = _func (arg);
+ }
+ return result;
+ }
+
+ public async unowned G wait_async () {
+ unowned A arg = yield _base.wait_async ();
+ return _func (arg);
+ }
+
+ public void when_done (Future.WhenDoneFunc<G> func) {
+ _base.when_done ((a) => {_func (a);});
+ }
+
+ private Future<G> _base;
+ private Future.LightMapFunc<A, G> _func;
+}
+