It seems to be a common idiom to apply a particular operation on
select elements of a table.  Something like:

    for _, i in pairs(t) do
        if (func1(i)) then func2(i) end
    end

So, how about (first patch) :

---
 lib/awful/util.lua.in |    9 +++++++++
[...]
+---
+-- For each element that matches the filtering criteria, call the callback
+-- function, passing it the matched element.  If the callback returns true,
+-- further matching is terminated.
+function table.apply(t, filter, callback)
+    for _, i in pairs(t) do
+        if filter(i) and callback(i) == false then return end
+    end
+end

The functionality to terminate matching by returning false can be
useful if one wants to take an action on only the first matching item,
like raising a client, etc.

Next we define wrappers in awful.client (second patch) and awful.tag:

---
 lib/awful/client.lua.in |    6 ++++++
[...]
+---
+-- Filter clients.
+function apply(filter, callback)
+    util.table.apply(capi.client.get(), filter, callback)
+end

However, most of the times starting to iterate from the currently
focused client helps, so I can iterate through Chrome instances in
order starting from the one focused.  But I am not sure how to best
incorporate that.  Naive approach is to add a starting index to the
apply function but that dirties the API.  Can you implement that using
an appropriate filtering function somehow (using closures or
something)?

Common filters can be included in the distribution or re-used from
awful.widget.tasklist?

If you like the idea I will document the functions, write proper
commit message and re-send the patches.

-- 
Anurag Priyam
From 397d3a41f158d433be5199b828c622f89f1552b0 Mon Sep 17 00:00:00 2001
From: Anurag Priyam <anurag08pri...@gmail.com>
Date: Fri, 17 Feb 2012 02:30:41 +0530
Subject: [PATCH 1/2] add awful.util.table.apply

---
 lib/awful/util.lua.in |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/lib/awful/util.lua.in b/lib/awful/util.lua.in
index 1c719a7..47f99e5 100644
--- a/lib/awful/util.lua.in
+++ b/lib/awful/util.lua.in
@@ -367,4 +367,13 @@ function table.clone(t)
     return c
 end
 
+---
+-- For each element that matches the filtering criteria, call the callback
+-- function, passing it the matched element.  If the callback returns true,
+-- further matching is terminated.
+function table.apply(t, filter, callback)
+    for _, i in pairs(t) do
+        if filter(i) and callback(i) == false then return end
+    end
+end
 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
-- 
1.7.9

From f2a2594f75ea54d05d18af6c6314742259cd8944 Mon Sep 17 00:00:00 2001
From: Anurag Priyam <anurag08pri...@gmail.com>
Date: Fri, 17 Feb 2012 02:32:28 +0530
Subject: [PATCH 2/2] add awful.client.apply

---
 lib/awful/client.lua.in |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/lib/awful/client.lua.in b/lib/awful/client.lua.in
index 2d5ec45..78d426a 100644
--- a/lib/awful/client.lua.in
+++ b/lib/awful/client.lua.in
@@ -862,6 +862,12 @@ function property.set(c, prop, value)
     c:emit_signal("property::" .. prop)
 end
 
+---
+-- Filter clients.
+function apply(filter, callback)
+    util.table.apply(capi.client.get(), filter, callback)
+end
+
 -- Register standards signals
 capi.client.add_signal("property::floating_geometry")
 capi.client.add_signal("property::floating")
-- 
1.7.9

Reply via email to