1. Vincent Fiack
  2. blocks

Commits

Vincent Fiack  committed 736cc80

added functional style apis (Map & Filter)

  • Participants
  • Parent commits f5fcf5c
  • Branches default

Comments (0)

Files changed (4)

File blocks-api/src/main/java/functional/Filter.java

View file
  • Ignore whitespace
+/*
+ * Blocks - a graphical programming language
+ * Copyright (C) 2007  Vincent Fiack <vfiack@blocks-language.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package functional;
+
+import org.blocks.runtime.blocks.JavaBlock;
+import org.blocks.runtime.blocks.Block;
+import org.blocks.runtime.Slot;
+import org.blocks.runtime.Types;
+import org.blocks.runtime.ExecutionContext;
+import org.blocks.exceptions.BlocksValidationException;
+import org.blocks.exceptions.BlocksRuntimeException;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class Filter extends JavaBlock {
+	public Filter() throws BlocksValidationException {
+        setDescription("Filter a list list");
+
+        addSlot(new Slot("list", Types.OTHER));
+        addSlot(new Slot("block", Types.BLOCK));
+	}
+
+	@Override
+    @SuppressWarnings("unchecked")
+    public Object run(ExecutionContext context)
+	throws BlocksRuntimeException {
+		List list = (List)context.lookup("list");
+        Block block = context.lookupBlock("block");
+
+        List newList = new ArrayList();
+
+        for(Object elem: list) {
+            context.setVariable("elem", elem);
+            Object result = block.execute(context);
+            if(Boolean.TRUE.equals(result))
+                newList.add(elem);
+        }
+
+        return newList;
+	}
+}

File blocks-api/src/main/java/functional/Map.java

View file
  • Ignore whitespace
+/*
+ * Blocks - a graphical programming language
+ * Copyright (C) 2007  Vincent Fiack <vfiack@blocks-language.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package functional;
+
+import org.blocks.runtime.blocks.JavaBlock;
+import org.blocks.runtime.blocks.Block;
+import org.blocks.runtime.Slot;
+import org.blocks.runtime.Types;
+import org.blocks.runtime.ExecutionContext;
+import org.blocks.exceptions.BlocksValidationException;
+import org.blocks.exceptions.BlocksRuntimeException;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class Map extends JavaBlock {
+	public Map() throws BlocksValidationException {
+        setDescription("Map a block to a list");
+
+        addSlot(new Slot("list", Types.OTHER));
+        addSlot(new Slot("block", Types.BLOCK));
+	}
+
+	@Override
+    @SuppressWarnings("unchecked")
+    public Object run(ExecutionContext context)
+	throws BlocksRuntimeException {
+		List list = (List)context.lookup("list");
+        Block block = context.lookupBlock("block");
+
+        List newList = new ArrayList();
+
+        for(Object elem: list) {
+            context.setVariable("elem", elem);
+            Object value = block.execute(context);
+            newList.add(value);
+        }
+
+        return newList;
+	}
+}

File blocks-tests/src/test/blocks/functional.blocks

View file
  • Ignore whitespace
+<blocks>
+  <code>
+    <call blockId="collections.list.DeclareList">
+      <param value="myList" name="varname" type="text"/>
+      <param value="1" name="*" type="integer"/>
+      <param value="3" name="*" type="integer"/>
+      <param value="5" name="*" type="integer"/>
+      <param value="7" name="*" type="integer"/>
+      <param value="9" name="*" type="integer"/>
+    </call>
+    <call blockId="core.Declare">
+      <param variable="" value="afterMap" name="varname" type="text"/>
+      <callparam name="value" type="other">
+        <call blockId="functional.Map">
+          <param variable="myList" name="list" type="other"/>
+          <blockparam name="block" type="block">
+            <block>
+              <code>
+                <call blockId="integer.Multiply">
+                  <param variable="elem" name="value" type="integer"/>
+                  <param value="2" name="times" type="integer"/>
+                </call>
+              </code>
+              <slots/>
+            </block>
+          </blockparam>
+        </call>
+      </callparam>
+    </call>
+    <call blockId="core.Declare">
+      <param variable="" value="filtered" name="varname" type="text"/>
+      <callparam name="value" type="other">
+        <call blockId="functional.Map">
+          <param variable="afterMap" value="" name="list" type="other"/>
+          <blockparam name="block" type="block">
+            <block>
+              <code>
+                <call blockId="integer.IsGreaterThan">
+                  <param variable="elem" name="left" type="integer"/>
+                  <param variable="" value="10" name="right" type="integer"/>
+                </call>
+              </code>
+              <slots/>
+            </block>
+          </blockparam>
+        </call>
+      </callparam>
+    </call>
+    <call blockId="core.PrintLine">
+      <callparam name="message" type="text">
+        <call blockId="text.ApplyTemplate">
+          <param value="${myList} => (x2) ${afterMap} => (&gt;10) ${filtered}" name="template" type="text"/>
+        </call>
+      </callparam>
+    </call>
+  </code>
+</blocks>

File blocks-tests/src/test/java/org/blocks/tests/BlocksTest.java

View file
  • Ignore whitespace
 		
 		test = new BlocksTestCase(new File(directory, "map.blocks"));
 		test.expectedPrintln("Map size: 3");		
-		test.expectedPrintln("translation for coffee: caf");		
+		test.expectedPrintln("translation for coffee: café");		
         test.expectedPrintln("Map size: 2");		
 		suite.addTest(test);
 
         test.expectedPrintln("${oh, Bonjour}");
         suite.addTest(test);
 
+        test = new BlocksTestCase(new File(directory, "functional.blocks"));
+        test.expectedPrintln("[1, 3, 5, 7, 9] => (x2) [2, 6, 10, 14, 18] => (>10) [false, false, false, true, true]");
+
+        suite.addTest(test);
         
         return suite;
 	}