Author: hlship
Date: Thu Jun  3 17:22:09 2010
New Revision: 951076

URL: http://svn.apache.org/viewvc?rev=951076&view=rev
Log:
Add F.iterate(), which builds a lazy Flow by repeatedly applying a function to 
an initial value

Added:
    
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterate.java
   (with props)
Modified:
    
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
    
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java

Modified: 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java?rev=951076&r1=951075&r2=951076&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
 Thu Jun  3 17:22:09 2010
@@ -271,4 +271,23 @@ public class F
     {
         return lazy(new LazySeries(start, delta));
     }
+
+    /**
+     * Creates a lazy, infinte Flow consisting of the initial value, then the 
result of passing
+     * the initial value through the Mapper, and so forth, which each step 
value passed through the mapper
+     * to form the next step value.
+     */
+    public static <T> Flow<T> iterate(final T initial, final Mapper<T, T> 
mapper)
+    {
+        assert mapper != null;
+
+        return F.lazy(new LazyFunction<T>()
+        {
+
+            public LazyContinuation<T> next()
+            {
+                return new LazyContinuation<T>(initial, new 
LazyIterate<T>(initial, mapper));
+            }
+        });
+    }
 }

Added: 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterate.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterate.java?rev=951076&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterate.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterate.java
 Thu Jun  3 17:22:09 2010
@@ -0,0 +1,38 @@
+// Copyright 2010 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.func;
+
+/**
+ * Continuously re-applies a function, represented as a {...@link Mapper}, to 
a value.
+ */
+class LazyIterate<T> implements LazyFunction<T>
+{
+    private final T previousValue;
+
+    private final Mapper<T, T> mapper;
+
+    public LazyIterate(T previousValue, Mapper<T, T> mapper)
+    {
+        this.previousValue = previousValue;
+        this.mapper = mapper;
+    }
+
+    public LazyContinuation<T> next()
+    {
+        T current = mapper.map(previousValue);
+
+        return new LazyContinuation<T>(current, new LazyIterate<T>(current, 
mapper));
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterate.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java?rev=951076&r1=951075&r2=951076&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
 Thu Jun  3 17:22:09 2010
@@ -52,4 +52,18 @@ public class RangeTests extends BaseFunc
 
         assertFlowValues(series.filter(evenp).take(4), 2, 4, 6, 8);
     }
+
+    @Test
+    public void iterate()
+    {
+        Mapper<Integer, Integer> times2 = new Mapper<Integer, Integer>()
+        {
+            public Integer map(Integer value)
+            {
+                return 2 * value;
+            }
+        };
+
+        assertFlowValues(F.iterate(1, times2).take(5), 1, 2, 4, 8, 16);
+    }
 }


Reply via email to