#808 eachrWhile?

Yuri Strot Fri 6 Nov 2009

Hi folks,

Not a first time I need to combine sys::List.eachr and sys::List.eachWhile... Is it possible to add useful eachrWhile method to the sys::List?

Thanks, Yuri

brian Sat 7 Nov 2009

done! changeset

Yuri Strot Tue 10 Nov 2009

Thanks Brian, it works perfect!

However implementation missed for JavaScript. I guess it should be following:

diff --git a/src/sys/js/fan/List.js b/src/sys/js/fan/List.js
--- a/src/sys/js/fan/List.js
+++ b/src/sys/js/fan/List.js
@@ -292,6 +297,27 @@
   return null;
 }
 
+fan.sys.List.eachrWhile = function(self, f)
+{
+  if (f.length == 1)
+  {
+    for (var i=self.length-1; i>=0; i--)
+    {
+      var r = f(self[i]);
+      if (r != null) return r;
+    }
+  }
+  else
+  {
+    for (var i=self.length-1; i>=0; i--)
+    {
+      var r = f(self[i], i);
+      if (r != null) return r;
+    }
+  }
+  return null;
+}
+
 fan.sys.List.find = function(self, f)
 {
   if (f.length == 1)

Also found there is no sys::List.size in JS:

diff --git a/src/sys/js/fan/List.js b/src/sys/js/fan/List.js
--- a/src/sys/js/fan/List.js
+++ b/src/sys/js/fan/List.js
@@ -67,6 +67,11 @@
   return self.length == 0;
 }
 
+fan.sys.List.size = function(self)
+{
+  return self.length;
+}
+
 fan.sys.List.add = function(self, item)
 {
   self.push(item);

Best regards, Yuri

andy Wed 11 Nov 2009

Thanks Yuri - pushed a fix

The compiler optimizes the List.size call to Array.length directly.

Yuri Strot Fri 13 Nov 2009

Thanks Andy.

My fault: I didn't find size implementation, but fail to take into account that I call List.size many times and it works anyway :-)

Login or Signup to reply.