未验证 提交 275ea54b 编写于 作者: G Giuseppe Villani 提交者: GitHub

Fixes #1660: Add running total function for collection #26 (#1792)

上级 c9af387d
package apoc.coll;
import apoc.result.ListResult;
import com.google.common.util.concurrent.AtomicDouble;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.math3.util.Combinations;
import org.neo4j.graphdb.GraphDatabaseService;
......@@ -50,6 +51,18 @@ public class Coll {
@Context public Transaction tx;
@UserFunction
@Description("apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]")
public List<Number> runningTotal(@Name("list") List<Number> list) {
if (list == null || list.isEmpty()) return null;
AtomicDouble sum = new AtomicDouble();
return list.stream().map(i -> {
double value = sum.addAndGet(i.doubleValue());
if (value == sum.longValue()) return sum.longValue();
return value;
}).collect(Collectors.toList());
}
@Procedure
@Description("apoc.coll.zipToRows(list1,list2) - creates pairs like zip but emits one row per pair")
public Stream<ListResult> zipToRows(@Name("list1") List<Object> list1, @Name("list2") List<Object> list2) {
......
......@@ -30,6 +30,12 @@ public class CollTest {
TestUtil.registerProcedure(db, Coll.class, Json.class);
}
@Test
public void testRunningTotal() throws Exception {
testCall(db, "RETURN apoc.coll.runningTotal([1,2,3,4,5.5,1]) as value",
(row) -> assertEquals(asList(1L, 3L, 6L, 10L, 15.5D, 16.5D), row.get("value")));
}
@Test
public void testZip() throws Exception {
testCall(db, "RETURN apoc.coll.zip([1,2,3],[4,5]) as value",
......
......@@ -37,6 +37,7 @@
¦apoc.coll.remove(coll :: LIST? OF ANY?, index :: INTEGER?, length = 1 :: INTEGER?) :: (LIST? OF ANY?)
¦apoc.coll.removeAll(first :: LIST? OF ANY?, second :: LIST? OF ANY?) :: (LIST? OF ANY?)
¦apoc.coll.reverse(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)
¦apoc.coll.runningTotal(list :: LIST? OF NUMBER?) :: (LIST? OF ANY?)
¦apoc.coll.set(coll :: LIST? OF ANY?, index :: INTEGER?, value :: ANY?) :: (LIST? OF ANY?)
¦apoc.coll.shuffle(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)
¦apoc.coll.sort(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)
......
......@@ -37,6 +37,7 @@
¦function¦apoc.coll.remove¦apoc.coll.remove(coll :: LIST? OF ANY?, index :: INTEGER?, length = 1 :: INTEGER?) :: (LIST? OF ANY?)¦apoc.coll.remove(coll, index, [length=1]) | remove range of values from index to length
¦function¦apoc.coll.removeAll¦apoc.coll.removeAll(first :: LIST? OF ANY?, second :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.removeAll(first, second) - returns first list with all elements of second list removed
¦function¦apoc.coll.reverse¦apoc.coll.reverse(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.reverse(coll) - returns reversed list
¦function¦apoc.coll.runningTotal¦apoc.coll.runningTotal(list :: LIST? OF NUMBER?) :: (LIST? OF ANY?)¦apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]
¦function¦apoc.coll.set¦apoc.coll.set(coll :: LIST? OF ANY?, index :: INTEGER?, value :: ANY?) :: (LIST? OF ANY?)¦apoc.coll.set(coll, index, value) | set index to value
¦function¦apoc.coll.shuffle¦apoc.coll.shuffle(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.shuffle(coll) - returns the shuffled list
¦function¦apoc.coll.sort¦apoc.coll.sort(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.sort(coll) sort on Collections
......
¦signature
¦apoc.coll.runningTotal(list :: LIST? OF NUMBER?) :: (LIST? OF ANY?)
¦type¦qualified name¦signature¦description
¦function¦apoc.coll.runningTotal¦apoc.coll.runningTotal(list :: LIST? OF NUMBER?) :: (LIST? OF ANY?)¦apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]
......@@ -334,6 +334,7 @@ for the provided `label` and `uuidProperty`, in case the UUID handler is already
¦function¦apoc.coll.remove¦apoc.coll.remove(coll :: LIST? OF ANY?, index :: INTEGER?, length = 1 :: INTEGER?) :: (LIST? OF ANY?)¦apoc.coll.remove(coll, index, [length=1]) | remove range of values from index to length¦true¦
¦function¦apoc.coll.removeAll¦apoc.coll.removeAll(first :: LIST? OF ANY?, second :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.removeAll(first, second) - returns first list with all elements of second list removed¦true¦
¦function¦apoc.coll.reverse¦apoc.coll.reverse(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.reverse(coll) - returns reversed list¦true¦
¦function¦apoc.coll.runningTotal¦apoc.coll.runningTotal(list :: LIST? OF NUMBER?) :: (LIST? OF ANY?)¦apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]¦true¦
¦function¦apoc.coll.set¦apoc.coll.set(coll :: LIST? OF ANY?, index :: INTEGER?, value :: ANY?) :: (LIST? OF ANY?)¦apoc.coll.set(coll, index, value) | set index to value¦true¦
¦function¦apoc.coll.shuffle¦apoc.coll.shuffle(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.shuffle(coll) - returns the shuffled list¦true¦
¦function¦apoc.coll.sort¦apoc.coll.sort(coll :: LIST? OF ANY?) :: (LIST? OF ANY?)¦apoc.coll.sort(coll) sort on Collections¦true¦
......
////
This file is generated by DocsTest, so don't change it!
////
= apoc.coll.runningTotal
:description: This section contains reference documentation for the apoc.coll.runningTotal function.
label:function[] label:apoc-core[]
[.emphasis]
apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]
== Signature
[source]
----
apoc.coll.runningTotal(list :: LIST? OF NUMBER?) :: (LIST? OF ANY?)
----
== Input parameters
[.procedures, opts=header]
|===
| Name | Type | Default
|list|LIST? OF NUMBER?|null
|===
......@@ -198,6 +198,11 @@ apoc.coll.removeAll(first, second) - returns first list with all elements of sec
apoc.coll.reverse(coll) - returns reversed list
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.coll/apoc.coll.runningTotal.adoc[apoc.coll.runningTotal icon:book[]]
apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.coll/apoc.coll.set.adoc[apoc.coll.set icon:book[]]
apoc.coll.set(coll, index, value) \| set index to value
......
......@@ -422,6 +422,11 @@ apoc.coll.removeAll(first, second) - returns first list with all elements of sec
apoc.coll.reverse(coll) - returns reversed list
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.coll/apoc.coll.runningTotal.adoc[apoc.coll.runningTotal icon:book[]]
apoc.coll.runningTotal(list1) - returns an accumulative array. For example apoc.coll.runningTotal([1,2,3.5]) return [1,3,6.5]
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.coll/apoc.coll.set.adoc[apoc.coll.set icon:book[]]
apoc.coll.set(coll, index, value) \| set index to value
......
......@@ -82,6 +82,7 @@ This file is generated by DocsTest, so don't change it!
*** xref::overview/apoc.coll/apoc.coll.remove.adoc[]
*** xref::overview/apoc.coll/apoc.coll.removeAll.adoc[]
*** xref::overview/apoc.coll/apoc.coll.reverse.adoc[]
*** xref::overview/apoc.coll/apoc.coll.runningTotal.adoc[]
*** xref::overview/apoc.coll/apoc.coll.set.adoc[]
*** xref::overview/apoc.coll/apoc.coll.shuffle.adoc[]
*** xref::overview/apoc.coll/apoc.coll.sort.adoc[]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册