[ 
https://issues.apache.org/jira/browse/TINKERPOP-1849?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16280601#comment-16280601
 ] 

Marko A. Rodriguez commented on TINKERPOP-1849:
-----------------------------------------------

So {{fold()}} does support a seed and a binary operator. For instance:

{code}
sum() <=> fold(0, a,b -> a + b)
{code}

Thus, true {{fold()}} is

{code}
fold([], a,b -> {a.add(b); a})
{code}

However, the binary operator is not necessary as we have {{Operator.addAll}}. 
Thus, the above is:

{code}
fold([], addAll)
{code}

...such that:

{code}
gremlin> g.V().fold([], addAll)
==>[v[1],v[2],v[3],v[4],v[5],v[6]]
{code}

If you were to generalize that to support indices, you would do something like 
this:

{code}
c = 0
fold([], a,b -> a.add([b,c++]); a) 
{code}

...such that, in Gremlin-Groovy:

{code}
gremlin> c = 0
==>0
gremlin> g.V().fold([], {a,b -> a.add([b,c++]); a})
==>[[v[1],0],[v[2],1],[v[3],2],[v[4],3],[v[5],4],[v[6],5]]
{code}

The problem here is that we have a {{c}} defined outside the closure and thus, 
outside the scope of the traversal. Well, let us rewrite the above with {{c}} 
scoped accordingly. That is, make sure that the binary operator can handle the 
index increment.

{code}
gremlin> g.V().fold([], {a, b ->
......1>   if(a.isEmpty())
......2>     a.add([b,0]);
......3>   else
......4>     a.add([b,a.get(a.size()-1)[1]+1]);
......5>   a;
......6> })
==>[[v[1],0],[v[2],1],[v[3],2],[v[4],3],[v[5],4],[v[6],5]]
{code}

Tada! Thus, given the already existing {{fold()}} constructs, it is possible to 
simply create a new {{Operator}}. I would call this operator 
{{addAllWithIndex}} and it would be defined as above. In use it would look like 
this:

{code}
g.V().fold([], addAllWithIndex)
{code}

...sorta don't like {{addAllWithIndex}}... a mouthful. I wonder why we didn't 
simply call it {{Operator.add}}. Perhaps you just do {{Operator.addWithIndex}} 
???....

Anywho, that is the theory.






> Provide a way to fold() with an index
> -------------------------------------
>
>                 Key: TINKERPOP-1849
>                 URL: https://issues.apache.org/jira/browse/TINKERPOP-1849
>             Project: TinkerPop
>          Issue Type: Improvement
>          Components: process
>    Affects Versions: 3.3.0
>            Reporter: stephen mallette
>
> In Groovy you can call {{withIndex()}} to generate output like this:
> {code}
> gremlin> g.V().fold().next().withIndex()
> ==>[v[1],0]
> ==>[v[2],1]
> ==>[v[3],2]
> ==>[v[4],3]
> ==>[v[5],4]
> ==>[v[6],5]
> {code}
> We can currently simulate this with Gremlin via:
> {code}
> gremlin> 
> g.V().project("a","b").by().by(select("x").count(local)).store("x").map(union(select('a'),
>  select('b')).fold()).fold().next()
> ==>[v[1],0]
> ==>[v[2],1]
> ==>[v[3],2]
> ==>[v[4],3]
> ==>[v[5],4]
> ==>[v[6],5]
> {code}
> but it's not easy to follow, efficient, or potentially foolproof. Perhaps we 
> can add an option to {{fold()}} that would take an enum of "fold operators".



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to