[libgee] Add the light maps



commit 2ea00d0cd33539405eff8e72f24937731eee6246
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sun Jul 7 11:48:09 2013 +0200

    Add the light maps

 gee/Makefile.am         |    1 +
 gee/future.vala         |   30 ++++++++++++++++++++++-
 gee/lightmapfuture.vala |   60 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+), 1 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index 1f9a2b8..20bed89 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -44,6 +44,7 @@ libgee_0_8_la_SOURCES = \
        iterator.vala \
        lazy.vala \
        linkedlist.vala \
+       lightmapfuture.vala \
        list.vala \
        listiterator.vala \
        map.vala \
diff --git a/gee/future.vala b/gee/future.vala
index a08450a..5cb2130 100644
--- a/gee/future.vala
+++ b/gee/future.vala
@@ -131,7 +131,8 @@ public interface Gee.Future<G> : Object {
         * @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
@@ -142,6 +143,33 @@ public interface Gee.Future<G> : Object {
                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);
 
diff --git a/gee/lightmapfuture.vala b/gee/lightmapfuture.vala
new file mode 100644
index 0000000..8b51821
--- /dev/null
+++ b/gee/lightmapfuture.vala
@@ -0,0 +1,60 @@
+/* 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;
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]