{
"title": "funcTest01",
"color": "teal",
"dependencies": {
"core/g": "dev"
},
"assets": {},
"functions": [
{
"name": "main",
"type": "network",
"renderedNode": "combine1",
"nodes": [
{
"name": "colorize1",
"x": 395,
"y": 315,
"fn": "g.colorize",
"values": {
"fill": "rgba(0, 0, 0, 0.08888888888888889)"
}
},
{
"name": "combine1",
"x": 395,
"y": 485,
"fn": "g.combine"
},
{
"name": "compoundAll1",
"x": 555,
"y": 315,
"fn": "funcTest01.compoundAll",
"values": {}
},
{
"name": "ellipse1",
"x": 395,
"y": 25,
"fn": "g.ellipse",
"values": {
"width": 200,
"height": 200
}
},
{
"name": "resampleByAmount1",
"x": 395,
"y": 75,
"fn": "g.resampleByAmount",
"values": {
"amount": 8
}
},
{
"name": "toPoints1",
"x": 395,
"y": 125,
"fn": "g.toPoints"
},
{
"name": "ellipse2",
"x": 395,
"y": 195,
"fn": "g.ellipse",
"values": {
"width": 300,
"height": 300
}
}
],
"connections": [
{
"output": "colorize1",
"input": "combine1",
"parameter": "list1"
},
{
"output": "compoundAll1",
"input": "combine1",
"parameter": "list2"
},
{
"output": "ellipse1",
"input": "resampleByAmount1",
"parameter": "shape"
},
{
"output": "resampleByAmount1",
"input": "toPoints1",
"parameter": "shape"
},
{
"output": "toPoints1",
"input": "ellipse2",
"parameter": "position"
},
{
"output": "ellipse2",
"input": "colorize1",
"parameter": "shape"
},
{
"output": "ellipse2",
"input": "compoundAll1",
"parameter": "shapes"
}
],
"parameters": [],
"returnsList": true,
"ref": "More functional programming powers such as `reduce`.\n\nThis way we can turn binary functions such as `compound` into functions that take a list of items."
},
{
"name": "reduce",
"type": "code",
"outputType": "",
"parameters": [
{
"name": "items",
"type": "",
"value": "",
"takesList": true
},
{
"name": "fn",
"type": "string",
"value": "g.add"
},
{
"name": "args",
"type": "",
"value": "",
"takesList": true
}
],
"source": "funcTest01.reduce = function (items, fn, args) {\n\n if (!items) return;\n\n // get the function\n var parts = fn.split('.');\n var func = window;\n parts.forEach(part => func = func[part]);\n if(!func) return;\n\n // reduce the items, passing in additional arguments\n return items.reduce((x, y) => func(x, y, ... args));\n\n};",
"ref": "Apply a binary function iteratively to all elements of the list.\n\nSee also: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce",
"category": "",
"returnsList": true,
"async": false
},
{
"name": "compoundAll",
"type": "network",
"renderedNode": "reduce1",
"nodes": [
{
"name": "reduce1",
"x": 35,
"y": 235,
"fn": "funcTest01.reduce",
"values": {
"fn": "g.compound"
}
},
{
"name": "string1",
"x": 135,
"y": 135,
"fn": "g.string",
"values": {
"s": "xor"
}
}
],
"connections": [
{
"inlet": "shapes",
"input": "reduce1",
"parameter": "items"
},
{
"inlet": "method",
"input": "string1",
"parameter": "s"
},
{
"output": "string1",
"input": "reduce1",
"parameter": "args"
}
],
"parameters": [
{
"name": "shapes",
"type": "shape",
"value": "",
"takesList": true
},
{
"name": "method",
"type": "string",
"value": "xor",
"choices": [
[
"union",
"Union"
],
[
"difference",
"Difference"
],
[
"intersection",
"Intersection"
],
[
"xor",
"Xor"
]
],
"takesList": true
}
],
"returnsList": true,
"outputType": "shape",
"ref": "Add, subtract or intersect multiple geometries."
}
],
"id": "funcTest01"
}
We had a little workshop, where we used the compound function quite a lot :)
The participants asked for a function similar to combine that would allow them to create a compound of several shapes at once.
I think a
compoundAllfunction that takes a list of shapes to create a compound would be quite useful. Here's a demo of how it might work ...Screenshot
Project
{ "title": "funcTest01", "color": "teal", "dependencies": { "core/g": "dev" }, "assets": {}, "functions": [ { "name": "main", "type": "network", "renderedNode": "combine1", "nodes": [ { "name": "colorize1", "x": 395, "y": 315, "fn": "g.colorize", "values": { "fill": "rgba(0, 0, 0, 0.08888888888888889)" } }, { "name": "combine1", "x": 395, "y": 485, "fn": "g.combine" }, { "name": "compoundAll1", "x": 555, "y": 315, "fn": "funcTest01.compoundAll", "values": {} }, { "name": "ellipse1", "x": 395, "y": 25, "fn": "g.ellipse", "values": { "width": 200, "height": 200 } }, { "name": "resampleByAmount1", "x": 395, "y": 75, "fn": "g.resampleByAmount", "values": { "amount": 8 } }, { "name": "toPoints1", "x": 395, "y": 125, "fn": "g.toPoints" }, { "name": "ellipse2", "x": 395, "y": 195, "fn": "g.ellipse", "values": { "width": 300, "height": 300 } } ], "connections": [ { "output": "colorize1", "input": "combine1", "parameter": "list1" }, { "output": "compoundAll1", "input": "combine1", "parameter": "list2" }, { "output": "ellipse1", "input": "resampleByAmount1", "parameter": "shape" }, { "output": "resampleByAmount1", "input": "toPoints1", "parameter": "shape" }, { "output": "toPoints1", "input": "ellipse2", "parameter": "position" }, { "output": "ellipse2", "input": "colorize1", "parameter": "shape" }, { "output": "ellipse2", "input": "compoundAll1", "parameter": "shapes" } ], "parameters": [], "returnsList": true, "ref": "More functional programming powers such as `reduce`.\n\nThis way we can turn binary functions such as `compound` into functions that take a list of items." }, { "name": "reduce", "type": "code", "outputType": "", "parameters": [ { "name": "items", "type": "", "value": "", "takesList": true }, { "name": "fn", "type": "string", "value": "g.add" }, { "name": "args", "type": "", "value": "", "takesList": true } ], "source": "funcTest01.reduce = function (items, fn, args) {\n\n if (!items) return;\n\n // get the function\n var parts = fn.split('.');\n var func = window;\n parts.forEach(part => func = func[part]);\n if(!func) return;\n\n // reduce the items, passing in additional arguments\n return items.reduce((x, y) => func(x, y, ... args));\n\n};", "ref": "Apply a binary function iteratively to all elements of the list.\n\nSee also: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce", "category": "", "returnsList": true, "async": false }, { "name": "compoundAll", "type": "network", "renderedNode": "reduce1", "nodes": [ { "name": "reduce1", "x": 35, "y": 235, "fn": "funcTest01.reduce", "values": { "fn": "g.compound" } }, { "name": "string1", "x": 135, "y": 135, "fn": "g.string", "values": { "s": "xor" } } ], "connections": [ { "inlet": "shapes", "input": "reduce1", "parameter": "items" }, { "inlet": "method", "input": "string1", "parameter": "s" }, { "output": "string1", "input": "reduce1", "parameter": "args" } ], "parameters": [ { "name": "shapes", "type": "shape", "value": "", "takesList": true }, { "name": "method", "type": "string", "value": "xor", "choices": [ [ "union", "Union" ], [ "difference", "Difference" ], [ "intersection", "Intersection" ], [ "xor", "Xor" ] ], "takesList": true } ], "returnsList": true, "outputType": "shape", "ref": "Add, subtract or intersect multiple geometries." } ], "id": "funcTest01" }