[TinkerPop3] Fold and Unfold

387 views
Skip to first unread message

Marko Rodriguez

unread,
Jul 16, 2014, 11:43:42 AM7/16/14
to gremli...@googlegroups.com
Hi,

In Gremlin2 we had scatter() and gather(). I always felt these terms were misleading because that are usually associated with parallel computing. Moreover, in the early days of Gremlin, I was not familiar with the functional programming communities terminology -- one of the reasons why we have it called transform() instead of map() in Gremlin2. Anywho, with respects to scatter()/gather() we now have fold()/unfold(). However, fold() can take an optional BiFunction to generate a single value. See the magic below:

gremlin> g = TinkerFactory.createClassic()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.fold
==>[v[1], v[2], v[3], v[4], v[5], v[6]]
gremlin> g.V.fold.unfold
==>v[1]
==>v[2]
==>v[3]
==>v[4]
==>v[5]
==>v[6]
gremlin> g.V.has('age').fold(0){a,b -> a + b.value('age')}
==>123

In short, 
1. unfold() is identical to scatter(). Any iterable or iterator it receives, it flattens.
2. fold() is analogous to gather(). It is a blocking step that aggregates all the previous step's objects into a list. 
3. fold(seed, bifunction) is a blocking step as well but it will "fold" all the incoming objects onto the seed given the function.

Comments?,
Marko.
Reply all
Reply to author
Forward
0 new messages